Skip to content

Commit

Permalink
fix(proto-compiler): not used no-std proto definitions refer to tonic (
Browse files Browse the repository at this point in the history
…#63)

* fix(proto-compiler): not used no-std proto definitions refer to tonic

* chore: self-review
  • Loading branch information
lklimek authored Apr 13, 2024
1 parent 17e57f5 commit 2e0e177
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 52 deletions.
6 changes: 3 additions & 3 deletions abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ default = [
"unix",
"grpc",
"tracing-span",
"std",
]
# docker-tests includes integration tests that require docker to be available
docker-tests = ["server"]
Expand All @@ -31,8 +30,9 @@ server = [
"dep:tokio-util",
"dep:futures",
]
std = ["tenderdash-proto/std"]
grpc = ["std", "tenderdash-proto/grpc"]
# std is deprecated, use "grpc" instead
std = ["grpc"]
grpc = ["tenderdash-proto/grpc"]
crypto = ["dep:lhash"]
tcp = ["server"]
unix = ["server"]
Expand Down
4 changes: 2 additions & 2 deletions proto-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ default = []
# Enable gRPC support; needed by server and client features.
# Conflicts with no_std
grpc = ["dep:tonic-build"]
# Build the gRPC server. Requires tenderdash-proto/std feature.
# Build the gRPC server. Requires tenderdash-proto/grpc feature.
server = ["grpc"]
# Build the gRPC client. Requires tenderdash-proto/std feature.
# Build the gRPC client. Requires tenderdash-proto/grpc feature.
client = ["grpc"]
29 changes: 29 additions & 0 deletions proto-compiler/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@
/// Tenderdash repository URL.
pub const TENDERDASH_REPO: &str = "https://github.com/dashpay/tenderdash";

/// How to generate the protobuf files.
pub enum GenerationMode {
/// Generate the files using `tonic` and put them into `tenderdash_grpc`
/// module.
Grpc,
/// Generate the files without `std` and put them into `tenderdash_nostd`
/// module.
NoStd,
}
impl GenerationMode {
pub fn module_name(&self) -> String {
match self {
GenerationMode::Grpc => "tenderdash_grpc".to_string(),
GenerationMode::NoStd => "tenderdash_nostd".to_string(),
}
}
}

impl ToString for GenerationMode {
fn to_string(&self) -> String {
match self {
GenerationMode::Grpc => "tonic".to_string(),
GenerationMode::NoStd => "nostd".to_string(),
}
}
}

// Commitish formats:
// Tag: v0.34.0-rc4
// Branch: master
Expand Down
10 changes: 5 additions & 5 deletions proto-compiler/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use walkdir::WalkDir;

use crate::constants::DEFAULT_TENDERDASH_COMMITISH;
use crate::constants::{GenerationMode, DEFAULT_TENDERDASH_COMMITISH};

/// Check out a specific commitish of the tenderdash repository.
///
Expand Down Expand Up @@ -247,7 +247,7 @@ pub fn generate_tenderdash_lib(
tenderdash_lib_target: &Path,
abci_ver: &str,
td_ver: &str,
module_name: &str,
mode: &GenerationMode,
) {
let mut file_names = WalkDir::new(prost_dir)
.into_iter()
Expand Down Expand Up @@ -301,16 +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 = \"{}\";
/// Module generation mode
pub const TENDERDASH_MODULE_MODE: &str = \"{}\";
}}
",
content,
crate::constants::TENDERDASH_REPO,
tenderdash_commitish(),
abci_ver,
td_ver,
module_name,
mode.to_string(),
);

let mut file =
Expand Down
33 changes: 22 additions & 11 deletions proto-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use functions::{
};

mod constants;
pub use constants::GenerationMode;
use constants::{CUSTOM_FIELD_ATTRIBUTES, CUSTOM_TYPE_ATTRIBUTES, TENDERDASH_REPO};

use crate::functions::{check_state, save_state};
Expand All @@ -22,10 +23,14 @@ use crate::functions::{check_state, save_state};
/// # Arguments
///
/// * `module_name` - name of module to put generated files into
pub fn proto_compile(module_name: &str) {
pub fn proto_compile(mode: GenerationMode) {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

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

let out_dir = var("OUT_DIR")
Expand Down Expand Up @@ -108,14 +113,20 @@ pub fn proto_compile(module_name: &str) {

println!("[info] => Creating structs.");

#[cfg(feature = "grpc")]
tonic_build::configure()
.generate_default_stubs(true)
.compile_with_config(pb, &protos, &proto_includes_paths)
.unwrap();

#[cfg(not(feature = "grpc"))]
pb.compile_protos(&protos, &proto_includes_paths).unwrap();
match mode {
GenerationMode::Grpc => {
#[cfg(feature = "grpc")]
tonic_build::configure()
.generate_default_stubs(true)
.compile_with_config(pb, &protos, &proto_includes_paths)
.unwrap();
#[cfg(not(feature = "grpc"))]
panic!("grpc feature is required to compile {}", mode.to_string());
},
GenerationMode::NoStd => {
pb.compile_protos(&protos, &proto_includes_paths).unwrap();
},
}

println!("[info] => Removing old structs and copying new structs.");
copy_files(&out_dir, &prost_out_dir); // This panics if it fails.
Expand All @@ -125,7 +136,7 @@ pub fn proto_compile(module_name: &str) {
&tenderdash_lib_target,
&abci_ver,
&tenderdash_ver,
module_name,
&mode,
);

save_state(&prost_out_dir, &commitish);
Expand Down
9 changes: 5 additions & 4 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ all-features = true
# issues related to outdated generated files.
default = ["grpc"]

# Enable standard library support
std = ["prost/std", "prost-types/std"]
# Build gRPC server
# Enable standard library support; DEPRECATED - use `grpc` instead
std = ["grpc"]
# Build gRPC server using tonic
grpc = [
"std",
"prost/std",
"prost-types/std",
"tenderdash-proto-compiler/server",
"tenderdash-proto-compiler/client",
"dep:tonic",
Expand Down
10 changes: 6 additions & 4 deletions proto/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::env;

use tenderdash_proto_compiler::GenerationMode;

fn main() {
// default Tenderdash version to use if TENDERDASH_COMMITISH is not set
const DEFAULT_VERSION: &str = "v0.14.0-dev.5";
Expand All @@ -11,10 +13,10 @@ fn main() {
env::set_var("TENDERDASH_COMMITISH", DEFAULT_VERSION);
}

#[cfg(feature = "std")]
tenderdash_proto_compiler::proto_compile("tenderdash_std");
#[cfg(not(feature = "std"))]
tenderdash_proto_compiler::proto_compile("tenderdash_nostd");
#[cfg(feature = "grpc")]
tenderdash_proto_compiler::proto_compile(GenerationMode::Grpc);
// we always build nostd version
tenderdash_proto_compiler::proto_compile(GenerationMode::NoStd);

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

# prost/ and tenderdash.rs are deprecated and can be removed in the future
prost/
Expand Down
4 changes: 2 additions & 2 deletions proto/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This module defines the various errors that be raised during Protobuf
//! conversions.
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
use core::{convert::TryFrom, fmt::Display, num::TryFromIntError};
#[cfg(feature = "std")]
#[cfg(feature = "grpc")]
use std::{fmt::Display, num::TryFromIntError};

use flex_error::{define_error, DisplayOnly};
Expand Down
18 changes: 9 additions & 9 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! tenderdash-proto library gives the developer access to the Tenderdash
//! proto-defined structs.
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "grpc"), no_std)]
#![deny(warnings, trivial_casts, trivial_numeric_casts, unused_import_braces)]
#![allow(clippy::large_enum_variant)]
#![forbid(unsafe_code)]
Expand All @@ -21,28 +21,28 @@ pub mod google {

mod error;

#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
use core::{
convert::{TryFrom, TryInto},
fmt::Display,
};
#[cfg(feature = "std")]
#[cfg(feature = "grpc")]
use std::fmt::Display;

use bytes::{Buf, BufMut};
pub use error::Error;
use prost::{encoding::encoded_len_varint, Message};
#[cfg(not(feature = "std"))]
#[rustfmt::skip]
pub mod tenderdash_nostd;
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
// Re-export the nostd module only if the std one is not available
pub use tenderdash_nostd::*;

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

pub mod serializers;

Expand Down
4 changes: 2 additions & 2 deletions proto/src/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// Prost does not seem to have a way yet to remove documentations defined in
// protobuf files. These structs are defined in gogoproto v1.3.1 at https://github.com/gogo/protobuf/tree/v1.3.1/protobuf/google/protobuf

#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
use core::fmt;
#[cfg(feature = "std")]
#[cfg(feature = "grpc")]
use std::fmt;

/// A Timestamp represents a point in time independent of any time zone or local
Expand Down
4 changes: 2 additions & 2 deletions proto/src/serializers/from_str.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Serialize and deserialize any `T` that implements [[core::str::FromStr]]
//! and [[core::fmt::Display]] from or into string. Note this can be used for
//! all primitive data types.
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
use core::{fmt::Display, str::FromStr};
#[cfg(feature = "std")]
#[cfg(feature = "grpc")]
use std::{fmt::Display, str::FromStr};

use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
Expand Down
4 changes: 2 additions & 2 deletions proto/src/serializers/part_set_header_total.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
//! Tendermint Core v0.34.0. This deserializer allows backwards-compatibility by
//! deserializing both ways. See also: <https://github.com/informalsystems/tendermint-rs/issues/679>
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
use core::{convert::TryFrom, fmt::Formatter};
#[cfg(feature = "std")]
#[cfg(feature = "grpc")]
use std::fmt::Formatter;

use serde::{
Expand Down
4 changes: 2 additions & 2 deletions proto/src/serializers/time_duration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Serialize/deserialize core::time::Duration type from and into string:
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
use core::time::Duration;
#[cfg(feature = "std")]
#[cfg(feature = "grpc")]
use std::time::Duration;

use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
Expand Down
4 changes: 2 additions & 2 deletions proto/src/serializers/timestamp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Serialize/deserialize Timestamp type from and into string:
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
use core::fmt::{self, Debug};
#[cfg(feature = "std")]
#[cfg(feature = "grpc")]
use std::fmt::{self, Debug};

use serde::{de::Error as _, ser::Error, Deserialize, Deserializer, Serialize, Serializer};
Expand Down
2 changes: 1 addition & 1 deletion proto/tests/unit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(not(feature = "std"))]
#[cfg(not(feature = "grpc"))]
use core::convert::TryFrom;

use tenderdash_proto::{
Expand Down

0 comments on commit 2e0e177

Please sign in to comment.