-
Notifications
You must be signed in to change notification settings - Fork 31
/
compile.rs
253 lines (220 loc) · 9.52 KB
/
compile.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use std::path::{Path, PathBuf};
use std::process;
use similar::TextDiff;
use walkdir::WalkDir;
use argh::FromArgs;
#[derive(Debug, FromArgs)]
#[argh(subcommand, name = "compile")]
/// Compile
pub struct CompileCmd {
#[argh(option, short = 'i')]
/// path to the IBC-Go proto files
ibc: PathBuf,
#[argh(option, short = 's')]
/// path to the Cosmos SDK proto files
sdk: PathBuf,
#[argh(option, short = 'c')]
/// path to the Cosmos ICS proto files
ics: PathBuf,
#[argh(option, short = 'o')]
/// path to output the generated Rust sources into
out: PathBuf,
}
impl CompileCmd {
pub fn run(&self) {
Self::compile_ibc_protos(
self.ibc.as_ref(),
self.sdk.as_ref(),
self.ics.as_ref(),
self.out.as_ref(),
)
.unwrap_or_else(|e| {
eprintln!("[error] failed to compile protos: {}", e);
process::exit(1);
});
Self::patch_generated_files(self.out.as_ref()).unwrap_or_else(|e| {
eprintln!("[error] failed to patch generated files: {}", e);
process::exit(1);
});
Self::build_pbjson_impls(self.out.as_ref()).unwrap_or_else(|e| {
eprintln!("[error] failed to build pbjson impls: {}", e);
process::exit(1);
});
println!("[info ] Done!");
}
fn compile_ibc_protos(
ibc_dir: &Path,
sdk_dir: &Path,
ics_dir: &Path,
out_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
println!(
"[info ] Compiling IBC .proto files to Rust into '{}'...",
out_dir.display()
);
let root = env!("CARGO_MANIFEST_DIR");
// Paths
let proto_paths = [
format!("{}/../../definitions/mock", root),
format!("{}/../../definitions/ibc/lightclients/localhost/v1", root),
format!("{}/../../definitions/stride/interchainquery/v1", root),
format!("{}/ibc", ibc_dir.display()),
format!("{}/cosmos/auth", sdk_dir.display()),
format!("{}/cosmos/gov", sdk_dir.display()),
format!("{}/cosmos/tx", sdk_dir.display()),
format!("{}/cosmos/base", sdk_dir.display()),
format!("{}/cosmos/crypto", sdk_dir.display()),
format!("{}/cosmos/bank", sdk_dir.display()),
format!("{}/cosmos/staking", sdk_dir.display()),
format!("{}/cosmos/upgrade", sdk_dir.display()),
format!("{}/interchain_security/ccv/v1", ics_dir.display()),
format!("{}/interchain_security/ccv/provider", ics_dir.display()),
format!("{}/interchain_security/ccv/consumer", ics_dir.display()),
];
let proto_includes_paths = [
format!("{}", sdk_dir.display()),
format!("{}", ibc_dir.display()),
format!("{}", ics_dir.display()),
format!("{}/../../definitions/mock", root),
format!("{}/../../definitions/ibc/lightclients/localhost/v1", root),
format!("{}/../../definitions/stride/interchainquery/v1", root),
];
// List available proto files
let mut protos: Vec<PathBuf> = vec![];
for proto_path in &proto_paths {
println!("Looking for proto files in {:?}", proto_path);
protos.append(
&mut WalkDir::new(proto_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
e.file_type().is_file()
&& e.path().extension().is_some()
&& e.path().extension().unwrap() == "proto"
})
.map(|e| e.into_path())
.collect(),
);
}
println!("Found the following protos:");
// Show which protos will be compiled
for proto in &protos {
println!("\t-> {:?}", proto);
}
println!("[info ] Compiling..");
// List available paths for dependencies
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect();
let attrs_jsonschema = r#"#[cfg_attr(all(feature = "json-schema", feature = "serde"), derive(::schemars::JsonSchema))]"#;
let attrs_jsonschema_str = r#"#[cfg_attr(all(feature = "json-schema", feature = "serde"), schemars(with = "String"))]"#;
let attrs_ord = "#[derive(Eq, PartialOrd, Ord)]";
let attrs_eq = "#[derive(Eq)]";
// Automatically derive a `prost::Name` implementation.
let mut config = prost_build::Config::new();
config.enable_type_names();
tonic_build::configure()
.build_client(true)
.compile_well_known_types(true)
.client_mod_attribute(".", r#"#[cfg(feature = "client")]"#)
.build_server(true)
.server_mod_attribute(".", r#"#[cfg(feature = "server")]"#)
.out_dir(out_dir)
.file_descriptor_set_path(out_dir.join("proto_descriptor.bin"))
.extern_path(".tendermint", "::tendermint_proto")
.extern_path(".ics23", "::ics23")
.type_attribute(".google.protobuf.Any", attrs_eq)
.type_attribute(".google.protobuf.Any", attrs_jsonschema)
.type_attribute(".google.protobuf.Duration", attrs_eq)
.type_attribute(".ibc.core.client.v1.Height", attrs_ord)
.type_attribute(".ibc.core.client.v1.Height", attrs_jsonschema)
.type_attribute(".ibc.core.commitment.v1.MerkleRoot", attrs_jsonschema)
.field_attribute(
".ibc.core.commitment.v1.MerkleRoot.hash",
attrs_jsonschema_str,
)
.type_attribute(".ibc.core.commitment.v1.MerklePrefix", attrs_jsonschema)
.field_attribute(
".ibc.core.commitment.v1.MerklePrefix.key_prefix",
attrs_jsonschema_str,
)
.type_attribute(".ibc.core.channel.v1.Channel", attrs_jsonschema)
.type_attribute(".ibc.core.channel.v1.Counterparty", attrs_jsonschema)
.type_attribute(".ibc.core.connection.v1.ConnectionEnd", attrs_jsonschema)
.type_attribute(".ibc.core.connection.v1.Counterparty", attrs_jsonschema)
.type_attribute(".ibc.core.connection.v1.Version", attrs_jsonschema)
.type_attribute(".ibc.lightclients.wasm.v1.ClientMessage", attrs_jsonschema)
.compile_with_config(config, &protos, &includes)?;
println!("[info ] Protos compiled successfully");
Ok(())
}
fn build_pbjson_impls(out_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
println!("[info] Building pbjson Serialize, Deserialize impls...");
let descriptor_set_path = out_dir.join("proto_descriptor.bin");
let descriptor_set = std::fs::read(descriptor_set_path)?;
pbjson_build::Builder::new()
.register_descriptors(&descriptor_set)?
.out_dir(&out_dir)
.exclude([
// The validator patch is not compatible with protojson builds
".cosmos.staking.v1beta1.StakeAuthorization",
".cosmos.staking.v1beta1.ValidatorUpdates",
// TODO: These have dependencies on tendermint-proto, which does not implement protojson.
// After it's implemented there, we can delete these exclusions.
".cosmos.base.abci.v1beta1",
".cosmos.tx.v1beta1",
".cosmos.base.tendermint.v1beta1",
".interchain_security.ccv.v1",
".interchain_security.ccv.provider.v1",
".interchain_security.ccv.consumer.v1",
".stride.interchainquery.v1",
])
.emit_fields()
.build(&[
".ibc",
".cosmos",
".interchain_security",
".stride",
".google",
])?;
Ok(())
}
fn patch_generated_files(out_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
println!(
"[info ] Patching generated files in '{}'...",
out_dir.display()
);
const PATCHES: &[(&str, &[(&str, &str)])] = &[
(
"cosmos.staking.v1beta1.rs",
&[
("pub struct Validators", "pub struct ValidatorsVec"),
(
"impl ::prost::Name for Validators {",
"impl ::prost::Name for ValidatorsVec {",
),
("AllowList(Validators)", "AllowList(ValidatorsVec)"),
("DenyList(Validators)", "DenyList(ValidatorsVec)"),
],
),
(
"ibc.applications.transfer.v1.rs",
&[(
"The denomination trace (\\[port_id\\]/[channel_id])+/\\[denom\\]",
"The denomination trace `([port_id]/[channel_id])+/[denom]`",
)],
),
];
for (file, patches) in PATCHES {
println!("[info ] Patching {file}...");
let path = out_dir.join(file);
let original = std::fs::read_to_string(&path)?;
let mut patched = original.clone();
for (before, after) in patches.iter() {
patched = patched.replace(before, after);
}
let diff = TextDiff::from_lines(&original, &patched);
println!("{}", diff.unified_diff().context_radius(3));
std::fs::write(&path, patched)?;
}
Ok(())
}
}