Skip to content

Commit

Permalink
fix(proto-compiler): std (tonic) and no-std generated code conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Apr 8, 2024
1 parent 883d1f5 commit 466285a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 29 deletions.
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
64 changes: 51 additions & 13 deletions proto-compiler/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{
env,
fs::{copy, create_dir_all, read_to_string, remove_dir_all, File},
fs::{copy, create_dir_all, read, read_to_string, remove_dir_all, File},
io::Write,
path::{Path, PathBuf},
};

use prost::Message;
use walkdir::WalkDir;

use crate::constants::DEFAULT_TENDERDASH_COMMITISH;
Expand Down Expand Up @@ -247,6 +248,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 +278,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 +302,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 All @@ -328,10 +329,15 @@ pub(crate) fn tenderdash_commitish() -> String {

/// Save the commitish of last successful download to a file in a state file,
/// located in the `dir` directory and named `download.state`.
pub(crate) fn save_state(dir: &Path, commitish: &str) {
pub(crate) fn save_state(dir: &Path, commitish: &str, module: &str) {
let state_file = PathBuf::from(&dir).join("download.state");

std::fs::write(&state_file, commitish)
let state = StateInfo {
commitish: commitish.to_string(),
module_name: module.to_string(),
};

std::fs::write(&state_file, state.encode_to_vec())
.map_err(|e| {
println!(
"[warn] => Failed to write download.state file {}: {}",
Expand All @@ -342,16 +348,48 @@ pub(crate) fn save_state(dir: &Path, commitish: &str) {
.ok();
}

#[derive(prost::Message)]
struct StateInfo {
#[prost(string, tag = "1")]
commitish: String,
#[prost(string, tag = "2")]
module_name: String,
}

impl PartialEq for StateInfo {
fn eq(&self, other: &Self) -> bool {
self.commitish == other.commitish && self.module_name == other.module_name
}
}

/// Check if the state file contains the same commitish as the one we are trying
/// to download. State file should be located in the `dir` and named
/// `download.state`
pub(crate) fn check_state(dir: &Path, commitish: &str) -> bool {
pub(crate) fn check_state(dir: &Path, commitish: &str, module_name: &str) -> bool {
let state_file = PathBuf::from(&dir).join("download.state");

match read_to_string(state_file) {
Ok(content) => {
println!("[info] => Detected Tenderdash version: {}.", content.trim());
content.eq(commitish)
let expected = StateInfo {
commitish: commitish.to_string(),
module_name: module_name.to_string(),
};

match read(&state_file) {
Ok(content) => match StateInfo::decode(content.as_slice()) {
Ok(state) => {
println!(
"[info] => Detected Tenderdash version: {}.",
state.commitish
);
state.eq(&expected)
},
Err(e) => {
println!(
"[warn] => Failed to decode download.state file {}: {}",
state_file.display(),
e
);
false
},
},
Err(_) => false,
}
Expand Down
27 changes: 16 additions & 11 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 All @@ -52,7 +51,7 @@ pub fn proto_compile() {

// check if this commitish is already downloaded
let download = std::fs::metadata(tenderdash_dir.join("proto")).is_err()
|| !check_state(&prost_out_dir, &commitish);
|| !check_state(&prost_out_dir, &commitish, module_name);

if download {
println!("[info] => Fetching {TENDERDASH_REPO} at {commitish} into {tenderdash_dir:?}.");
Expand Down Expand Up @@ -121,8 +120,14 @@ 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);
save_state(&prost_out_dir, &commitish, module_name);
println!("[info] => Done!");
}
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

0 comments on commit 466285a

Please sign in to comment.