Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proto-compiler): std and no-std generated code conflict #61

Merged
merged 7 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions proto-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false

[dependencies]
walkdir = { version = "2.3" }
prost = { version = "0.12" }
prost-build = { version = "0.12" }
tempfile = { version = "3.2.0" }
regex = { "version" = "1.7.1" }
Expand Down
18 changes: 10 additions & 8 deletions proto-compiler/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
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 @@

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 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 check_state(dir: &Path, commitish: &str) -> bool {
let state_file = PathBuf::from(&dir).join("download.state");

match read_to_string(state_file) {
let expected = commitish.to_string();

match read_to_string(&state_file) {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
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"))]
pub mod tenderdash_nostd;
#[allow(warnings)]
#[cfg(not(feature = "std"))]
pub use tenderdash_nostd::*;

#[cfg(feature = "std")]
#[allow(warnings)]
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
Loading