Skip to content

Commit 6eeedc7

Browse files
authored
Merge pull request #93 from datachainlab/improve-proto-compiler
Refactor proto-compiler Signed-off-by: Jun Kimura <[email protected]>
2 parents 6ea8c35 + 3d9a6cd commit 6eeedc7

File tree

7 files changed

+221
-259
lines changed

7 files changed

+221
-259
lines changed

proto-compiler/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto-compiler/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
tempfile = "3"
87
walkdir = "2.3"
98
argh = "0.1.3"
109
tonic-build = "0.8"

proto-compiler/src/cmd/clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
// TODO implement this
1+
// TODO implement this

proto-compiler/src/cmd/compile.rs

Lines changed: 14 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use std::fs::remove_dir_all;
2-
use std::fs::{copy, create_dir_all};
1+
use std::fs::{create_dir_all, remove_dir_all};
32
use std::path::{Path, PathBuf};
43
use std::process;
54

6-
use tempfile::TempDir;
5+
use argh::FromArgs;
76
use walkdir::WalkDir;
87

9-
use argh::FromArgs;
108
#[derive(Debug, FromArgs)]
119
#[argh(subcommand, name = "compile")]
1210
/// Compile
@@ -26,16 +24,14 @@ pub struct CompileCmd {
2624

2725
impl CompileCmd {
2826
pub fn run(&self) {
29-
let tmp_ibc = TempDir::new().unwrap();
30-
Self::compile_ibc_protos(&self.ibc, tmp_ibc.as_ref());
31-
32-
let tmp_lcp = TempDir::new().unwrap();
33-
Self::compile_lcp_protos(&self.ibc, tmp_lcp.as_ref(), &self.descriptor);
34-
35-
Self::copy_generated_files(tmp_lcp.as_ref(), tmp_ibc.as_ref(), &self.out);
27+
Self::compile_protos(&self.ibc, self.out.as_ref(), &self.descriptor);
3628
}
3729

38-
fn compile_lcp_protos(ibc_dir: &Path, out_dir: &Path, descriptor_path: &Path) {
30+
fn compile_protos(ibc_dir: &Path, out_dir: &Path, descriptor_path: &Path) {
31+
// Remove old compiled files
32+
remove_dir_all(&out_dir).unwrap_or_default();
33+
create_dir_all(&out_dir).unwrap();
34+
3935
println!(
4036
"[info ] Compiling LCP .proto files to Rust into '{}'...",
4137
out_dir.display()
@@ -45,90 +41,15 @@ impl CompileCmd {
4541

4642
// Paths
4743
let proto_paths = [
48-
format!(
49-
"{}/../proto/definitions",
50-
root
51-
),
52-
];
53-
54-
let proto_includes_paths = [
5544
format!("{}/../proto/definitions", root),
56-
format!("{}/proto", ibc_dir.display()),
57-
format!("{}/third_party/proto", ibc_dir.display()),
58-
];
59-
60-
// List available proto files
61-
let mut protos: Vec<PathBuf> = vec![];
62-
for proto_path in &proto_paths {
63-
println!("Looking for proto files in {:?}", proto_path);
64-
protos.append(
65-
&mut WalkDir::new(proto_path)
66-
.into_iter()
67-
.filter_map(|e| e.ok())
68-
.filter(|e| {
69-
e.file_type().is_file()
70-
&& e.path().extension().is_some()
71-
&& e.path().extension().unwrap() == "proto"
72-
})
73-
.map(|e| e.into_path())
74-
.collect(),
75-
);
76-
}
77-
78-
println!("Found the following protos:");
79-
// Show which protos will be compiled
80-
for proto in &protos {
81-
println!("\t-> {:?}", proto);
82-
}
83-
println!("[info ] Compiling..");
84-
85-
// List available paths for dependencies
86-
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect();
87-
let attrs_serde = r#"#[derive(::serde::Serialize, ::serde::Deserialize)]"#;
88-
let compilation = tonic_build::configure()
89-
.build_client(true)
90-
.compile_well_known_types(true)
91-
.client_mod_attribute(".", r#"#[cfg(feature = "client")]"#)
92-
.server_mod_attribute(".", r#"#[cfg(feature = "server")]"#)
93-
.build_server(true)
94-
.out_dir(out_dir)
95-
.extern_path(".tendermint", "::tendermint_proto")
96-
.type_attribute(".cosmos.upgrade.v1beta1", attrs_serde)
97-
.type_attribute(".cosmos.base.v1beta1", attrs_serde)
98-
.type_attribute(".cosmos.base.query.v1beta1", attrs_serde)
99-
.type_attribute(".cosmos.bank.v1beta1", attrs_serde)
100-
.type_attribute(".lcp.service.enclave.v1", attrs_serde)
101-
.type_attribute(".lcp.service.elc.v1", attrs_serde)
102-
.file_descriptor_set_path(descriptor_path)
103-
.compile(&protos, &includes);
104-
105-
match compilation {
106-
Ok(_) => {
107-
println!("Successfully compiled proto files");
108-
}
109-
Err(e) => {
110-
println!("Failed to compile:{:?}", e.to_string());
111-
process::exit(1);
112-
}
113-
}
114-
}
115-
116-
fn compile_ibc_protos(ibc_dir: &Path, out_dir: &Path) {
117-
println!(
118-
"[info ] Compiling IBC .proto files to Rust into '{}'...",
119-
out_dir.display()
120-
);
121-
122-
// Paths
123-
let proto_paths = [
124-
// ibc-go proto files
12545
format!(
12646
"{}/proto/ibc/core/client/v1/client.proto",
12747
ibc_dir.display()
12848
),
12949
];
13050

13151
let proto_includes_paths = [
52+
format!("{}/../proto/definitions", root),
13253
format!("{}/proto", ibc_dir.display()),
13354
format!("{}/third_party/proto", ibc_dir.display()),
13455
];
@@ -160,85 +81,28 @@ impl CompileCmd {
16081

16182
// List available paths for dependencies
16283
let includes: Vec<PathBuf> = proto_includes_paths.iter().map(PathBuf::from).collect();
163-
16484
let attrs_serde = r#"#[derive(::serde::Serialize, ::serde::Deserialize)]"#;
16585
let attrs_jsonschema =
16686
r#"#[cfg_attr(feature = "json-schema", derive(::schemars::JsonSchema))]"#;
16787
let attrs_ord = "#[derive(Eq, PartialOrd, Ord)]";
168-
let attrs_eq = "#[derive(Eq)]";
16988
let attrs_serde_default = r#"#[serde(default)]"#;
170-
let attrs_serde_base64 = r#"#[serde(with = "crate::base64")]"#;
171-
let attrs_jsonschema_str =
172-
r#"#[cfg_attr(feature = "json-schema", schemars(with = "String"))]"#;
173-
17489
let compilation = tonic_build::configure()
17590
.build_client(true)
17691
.compile_well_known_types(true)
17792
.client_mod_attribute(".", r#"#[cfg(feature = "client")]"#)
178-
.build_server(false)
93+
.server_mod_attribute(".", r#"#[cfg(feature = "server")]"#)
94+
.build_server(true)
17995
.out_dir(out_dir)
180-
.extern_path(".tendermint", "::tendermint_proto")
18196
.type_attribute(".google.protobuf.Any", attrs_serde)
18297
.type_attribute(".google.protobuf.Timestamp", attrs_serde)
18398
.type_attribute(".google.protobuf.Duration", attrs_serde)
18499
.type_attribute(".ibc.core.client.v1", attrs_serde)
185100
.type_attribute(".ibc.core.client.v1.Height", attrs_ord)
186101
.type_attribute(".ibc.core.client.v1.Height", attrs_jsonschema)
187102
.field_attribute(".ibc.core.client.v1.Height", attrs_serde_default)
188-
.type_attribute(".ibc.core.commitment.v1", attrs_serde)
189-
.type_attribute(".ibc.core.commitment.v1.MerkleRoot", attrs_jsonschema)
190-
.field_attribute(
191-
".ibc.core.commitment.v1.MerkleRoot.hash",
192-
attrs_serde_base64,
193-
)
194-
.field_attribute(
195-
".ibc.core.commitment.v1.MerkleRoot.hash",
196-
attrs_jsonschema_str,
197-
)
198-
.type_attribute(".ibc.core.commitment.v1.MerklePrefix", attrs_jsonschema)
199-
.field_attribute(
200-
".ibc.core.commitment.v1.MerklePrefix.key_prefix",
201-
attrs_serde_base64,
202-
)
203-
.field_attribute(
204-
".ibc.core.commitment.v1.MerklePrefix.key_prefix",
205-
attrs_jsonschema_str,
206-
)
207-
.type_attribute(".ibc.core.channel.v1", attrs_serde)
208-
.type_attribute(".ibc.core.channel.v1.Channel", attrs_jsonschema)
209-
.type_attribute(".ibc.core.channel.v1.Counterparty", attrs_jsonschema)
210-
.type_attribute(".ibc.core.connection.v1", attrs_serde)
211-
.type_attribute(".ibc.core.connection.v1.ConnectionEnd", attrs_jsonschema)
212-
.type_attribute(".ibc.core.connection.v1.Counterparty", attrs_jsonschema)
213-
.type_attribute(".ibc.core.connection.v1.Version", attrs_jsonschema)
214-
.type_attribute(".ibc.core.types.v1", attrs_serde)
215-
.type_attribute(".ibc.applications.transfer.v1", attrs_serde)
216-
.type_attribute(".ibc.applications.transfer.v2", attrs_serde)
217-
.type_attribute(
218-
".ibc.applications.interchain_accounts.controller.v1",
219-
attrs_serde,
220-
)
221-
.type_attribute(".ics23", attrs_serde)
222-
.type_attribute(".ics23.LeafOp", attrs_eq)
223-
.type_attribute(".ics23.LeafOp", attrs_jsonschema)
224-
.field_attribute(".ics23.LeafOp.prehash_key", attrs_serde_default)
225-
.field_attribute(".ics23.LeafOp.prefix", attrs_serde_base64)
226-
.field_attribute(".ics23.LeafOp.prefix", attrs_jsonschema_str)
227-
.type_attribute(".ics23.InnerOp", attrs_jsonschema)
228-
.field_attribute(".ics23.InnerOp.prefix", attrs_serde_base64)
229-
.field_attribute(".ics23.InnerOp.prefix", attrs_jsonschema_str)
230-
.field_attribute(".ics23.InnerOp.suffix", attrs_serde_base64)
231-
.field_attribute(".ics23.InnerOp.suffix", attrs_jsonschema_str)
232-
.type_attribute(".ics23.InnerOp", attrs_eq)
233-
.type_attribute(".ics23.ProofSpec", attrs_eq)
234-
.type_attribute(".ics23.ProofSpec", attrs_jsonschema)
235-
.field_attribute(".ics23.ProofSpec.max_depth", attrs_serde_default)
236-
.field_attribute(".ics23.ProofSpec.min_depth", attrs_serde_default)
237-
.type_attribute(".ics23.InnerSpec", attrs_eq)
238-
.type_attribute(".ics23.InnerSpec", attrs_jsonschema)
239-
.field_attribute(".ics23.InnerSpec.empty_child", attrs_serde_default)
240-
.field_attribute(".ics23.InnerSpec.empty_child", attrs_serde_base64)
241-
.field_attribute(".ics23.InnerSpec.empty_child", attrs_jsonschema_str)
103+
.type_attribute(".lcp.service.enclave.v1", attrs_serde)
104+
.type_attribute(".lcp.service.elc.v1", attrs_serde)
105+
.file_descriptor_set_path(descriptor_path)
242106
.compile(&protos, &includes);
243107

244108
match compilation {
@@ -251,86 +115,4 @@ impl CompileCmd {
251115
}
252116
}
253117
}
254-
255-
fn copy_generated_files(from_dir_lcp: &Path, from_dir_ibc: &Path, to_dir: &Path) {
256-
println!(
257-
"[info ] Copying generated files into '{}'...",
258-
to_dir.display()
259-
);
260-
261-
// Remove old compiled files
262-
remove_dir_all(&to_dir).unwrap_or_default();
263-
create_dir_all(&to_dir).unwrap();
264-
265-
// Copy new compiled files (prost does not use folder structures)
266-
// Copy the SDK files first
267-
let errors_sdk = WalkDir::new(from_dir_lcp)
268-
.into_iter()
269-
.filter_map(|e| e.ok())
270-
.filter(|e| e.file_type().is_file())
271-
.map(|e| {
272-
copy(
273-
e.path(),
274-
format!(
275-
"{}/{}",
276-
to_dir.display(),
277-
&e.file_name().to_os_string().to_str().unwrap()
278-
),
279-
)
280-
})
281-
.filter_map(|e| e.err())
282-
.collect::<Vec<_>>();
283-
284-
if !errors_sdk.is_empty() {
285-
for e in errors_sdk {
286-
println!("[error] Error while copying SDK-compiled file: {}", e);
287-
}
288-
289-
panic!("[error] Aborted.");
290-
}
291-
292-
// Copy the IBC-go files second, double-checking if anything is overwritten
293-
let errors_ibc = WalkDir::new(from_dir_ibc)
294-
.into_iter()
295-
.filter_map(|e| e.ok())
296-
.filter(|e| e.file_type().is_file())
297-
.map(|e| {
298-
let generated_fname = e.file_name().to_owned().into_string().unwrap();
299-
let prefix = &generated_fname[0..6];
300-
301-
let target_fname = format!(
302-
"{}/{}",
303-
to_dir.display(),
304-
generated_fname,
305-
);
306-
307-
// If it's a cosmos-relevant file and it exists, we should not overwrite it.
308-
if Path::new(&target_fname).exists() && prefix.eq("cosmos") {
309-
let original_cosmos_file = std::fs::read(target_fname.clone()).unwrap();
310-
let new_cosmos_file = std::fs::read(e.path()).unwrap();
311-
if original_cosmos_file != new_cosmos_file {
312-
println!(
313-
"[warn ] Cosmos-related file exists already {}! Ignoring the one generated from IBC-go {:?}",
314-
target_fname, e.path()
315-
);
316-
}
317-
Ok(0)
318-
} else {
319-
copy(
320-
e.path(),
321-
target_fname,
322-
)
323-
}
324-
})
325-
.filter_map(|e| e.err())
326-
.collect::<Vec<_>>();
327-
328-
if !errors_ibc.is_empty() {
329-
for e in errors_ibc {
330-
println!("[error] Error while copying IBC-go compiled file: {}", e);
331-
}
332-
333-
panic!("[error] Aborted.");
334-
}
335-
}
336118
}

proto/src/descriptor.bin

4.01 KB
Binary file not shown.

proto/src/prost/cosmos.upgrade.v1beta1.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/// Plan specifies information about a planned upgrade and when it should occur.
2-
#[derive(::serde::Serialize, ::serde::Deserialize)]
32
#[allow(clippy::derive_partial_eq_without_eq)]
43
#[derive(Clone, PartialEq, ::prost::Message)]
54
pub struct Plan {
@@ -27,7 +26,6 @@ pub struct Plan {
2726
}
2827
/// SoftwareUpgradeProposal is a gov Content type for initiating a software
2928
/// upgrade.
30-
#[derive(::serde::Serialize, ::serde::Deserialize)]
3129
#[allow(clippy::derive_partial_eq_without_eq)]
3230
#[derive(Clone, PartialEq, ::prost::Message)]
3331
pub struct SoftwareUpgradeProposal {
@@ -40,7 +38,6 @@ pub struct SoftwareUpgradeProposal {
4038
}
4139
/// CancelSoftwareUpgradeProposal is a gov Content type for cancelling a software
4240
/// upgrade.
43-
#[derive(::serde::Serialize, ::serde::Deserialize)]
4441
#[allow(clippy::derive_partial_eq_without_eq)]
4542
#[derive(Clone, PartialEq, ::prost::Message)]
4643
pub struct CancelSoftwareUpgradeProposal {

0 commit comments

Comments
 (0)