Skip to content

Commit

Permalink
fix(proto-compiler): std and no-std generated code conflict (#61)
Browse files Browse the repository at this point in the history
* fix(proto-compiler): std (tonic) and no-std generated code conflict

* chore: self review

* chore: self review

* chore: self review

* chore: self review

* chore: skip fmt of generated code

* test:bump timeouts
  • Loading branch information
lklimek authored Apr 8, 2024
1 parent 52a0b7c commit 0b6b569
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 25 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 15
env:
# warn when test takes more than 3s, kill after 6s
RUST_TEST_TIME_UNIT: "3000,6000"
RUST_TEST_TIME_INTEGRATION: "3000,6000"
RUST_TEST_TIME_DOCTEST: "3000,6000"
# warn when test takes more than 5s, kill after 10s
RUST_TEST_TIME_UNIT: "5000,10000"
RUST_TEST_TIME_INTEGRATION: "5000,10000"
RUST_TEST_TIME_DOCTEST: "5000,10000"
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/deps
Expand Down
16 changes: 9 additions & 7 deletions proto-compiler/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ pub fn generate_tenderdash_lib(
tenderdash_lib_target: &Path,
abci_ver: &str,
td_ver: &str,
module_name: &str,
) {
let mut file_names = WalkDir::new(prost_dir)
.into_iter()
Expand Down Expand Up @@ -276,11 +277,7 @@ pub fn generate_tenderdash_lib(

let mut tab_count = parts.len();

let mut inner_content = format!(
"{}include!(\"prost/{}\");",
tab.repeat(tab_count),
file_name
);
let mut inner_content = format!("{}include!(\"./{}\");", tab.repeat(tab_count), file_name);

for part in parts {
tab_count -= 1;
Expand All @@ -304,13 +301,16 @@ pub mod meta {{
pub const ABCI_VERSION: &str = \"{}\";
/// Version of Tenderdash server used to generate protobuf configs
pub const TENDERDASH_VERSION: &str = \"{}\";
/// Name of module where generated files are stored; used to distinguish between std and no-std version
pub const TENDERDASH_MODULE_NAME: &str = \"{}\";
}}
",
content,
crate::constants::TENDERDASH_REPO,
tenderdash_commitish(),
abci_ver,
td_ver,
module_name,
);

let mut file =
Expand Down Expand Up @@ -348,10 +348,12 @@ pub(crate) fn save_state(dir: &Path, commitish: &str) {
pub(crate) fn check_state(dir: &Path, commitish: &str) -> bool {
let state_file = PathBuf::from(&dir).join("download.state");

let expected = commitish.to_string();

match read_to_string(state_file) {
Ok(content) => {
println!("[info] => Detected Tenderdash version: {}.", content.trim());
content.eq(commitish)
println!("[info] => Detected Tenderdash version: {}.", content);
content == expected
},
Err(_) => false,
}
Expand Down
23 changes: 14 additions & 9 deletions proto-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ use crate::functions::{check_state, save_state};
/// Checkouts tenderdash repository to ../target/tenderdash and generates
/// Rust protobuf definitions in ../proto/src/prost/ and
/// ../proto/src/tenderdash.rs
pub fn proto_compile() {
///
/// # Arguments
///
/// * `module_name` - name of module to put generated files into
pub fn proto_compile(module_name: &str) {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

let tenderdash_lib_target = root
.join("..")
.join("proto")
.join("src")
.join("tenderdash.rs");

let prost_out_dir = root.join("..").join("proto").join("src").join("prost");
let prost_out_dir = root.join("..").join("proto").join("src").join(module_name);
let tenderdash_lib_target = prost_out_dir.join("mod.rs");

let out_dir = var("OUT_DIR")
.map(PathBuf::from)
Expand Down Expand Up @@ -121,7 +120,13 @@ pub fn proto_compile() {
println!("[info] => Removing old structs and copying new structs.");
copy_files(&out_dir, &prost_out_dir); // This panics if it fails.

generate_tenderdash_lib(&out_dir, &tenderdash_lib_target, &abci_ver, &tenderdash_ver);
generate_tenderdash_lib(
&out_dir,
&tenderdash_lib_target,
&abci_ver,
&tenderdash_ver,
module_name,
);

save_state(&prost_out_dir, &commitish);
println!("[info] => Done!");
Expand Down
5 changes: 4 additions & 1 deletion proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ fn main() {
env::set_var("TENDERDASH_COMMITISH", DEFAULT_VERSION);
}

tenderdash_proto_compiler::proto_compile();
#[cfg(feature = "std")]
tenderdash_proto_compiler::proto_compile("tenderdash_std");
#[cfg(not(feature = "std"))]
tenderdash_proto_compiler::proto_compile("tenderdash_nostd");

println!("cargo:rerun-if-changed=../proto-compiler/src");
println!("cargo:rerun-if-changed=Cargo.toml");
Expand Down
4 changes: 4 additions & 0 deletions proto/src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
tenderdash_nostd/
tenderdash_std/

# prost/ and tenderdash.rs are deprecated and can be removed in the future
prost/
tenderdash.rs
16 changes: 12 additions & 4 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ pub mod google {
}

mod error;
#[allow(warnings)]
mod tenderdash;

#[cfg(not(feature = "std"))]
use core::{
Expand All @@ -34,12 +32,22 @@ use std::fmt::Display;
use bytes::{Buf, BufMut};
pub use error::Error;
use prost::{encoding::encoded_len_varint, Message};
pub use tenderdash::*;
#[cfg(not(feature = "std"))]
#[rustfmt::skip]
pub mod tenderdash_nostd;
#[cfg(not(feature = "std"))]
pub use tenderdash_nostd::*;

#[cfg(feature = "std")]
#[rustfmt::skip]
pub mod tenderdash_std;
#[cfg(feature = "std")]
pub use tenderdash_std::*;

pub mod serializers;

pub use meta::ABCI_VERSION;
use prelude::*;
pub use tenderdash::meta::ABCI_VERSION;
#[cfg(feature = "grpc")]
pub use tonic;

Expand Down

0 comments on commit 0b6b569

Please sign in to comment.