From 2e0e177cad5de92d1aa4a472d130402703712c86 Mon Sep 17 00:00:00 2001 From: lklimek <842586+lklimek@users.noreply.github.com> Date: Sat, 13 Apr 2024 10:13:26 +0200 Subject: [PATCH] fix(proto-compiler): not used no-std proto definitions refer to tonic (#63) * fix(proto-compiler): not used no-std proto definitions refer to tonic * chore: self-review --- abci/Cargo.toml | 6 ++-- proto-compiler/Cargo.toml | 4 +-- proto-compiler/src/constants.rs | 29 ++++++++++++++++ proto-compiler/src/functions.rs | 10 +++--- proto-compiler/src/lib.rs | 33 ++++++++++++------- proto/Cargo.toml | 9 ++--- proto/build.rs | 10 +++--- proto/src/.gitignore | 2 +- proto/src/error.rs | 4 +-- proto/src/lib.rs | 18 +++++----- proto/src/protobuf.rs | 4 +-- proto/src/serializers/from_str.rs | 4 +-- .../src/serializers/part_set_header_total.rs | 4 +-- proto/src/serializers/time_duration.rs | 4 +-- proto/src/serializers/timestamp.rs | 4 +-- proto/tests/unit.rs | 2 +- 16 files changed, 95 insertions(+), 52 deletions(-) diff --git a/abci/Cargo.toml b/abci/Cargo.toml index 1d68b11..6cfef7d 100644 --- a/abci/Cargo.toml +++ b/abci/Cargo.toml @@ -21,7 +21,6 @@ default = [ "unix", "grpc", "tracing-span", - "std", ] # docker-tests includes integration tests that require docker to be available docker-tests = ["server"] @@ -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"] diff --git a/proto-compiler/Cargo.toml b/proto-compiler/Cargo.toml index ba65a6a..24b3002 100644 --- a/proto-compiler/Cargo.toml +++ b/proto-compiler/Cargo.toml @@ -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"] diff --git a/proto-compiler/src/constants.rs b/proto-compiler/src/constants.rs index 0ff9aca..65688b7 100644 --- a/proto-compiler/src/constants.rs +++ b/proto-compiler/src/constants.rs @@ -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 diff --git a/proto-compiler/src/functions.rs b/proto-compiler/src/functions.rs index 59bc118..f8133bf 100644 --- a/proto-compiler/src/functions.rs +++ b/proto-compiler/src/functions.rs @@ -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. /// @@ -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() @@ -301,8 +301,8 @@ 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, @@ -310,7 +310,7 @@ pub mod meta {{ tenderdash_commitish(), abci_ver, td_ver, - module_name, + mode.to_string(), ); let mut file = diff --git a/proto-compiler/src/lib.rs b/proto-compiler/src/lib.rs index 4d02afa..eaf5c2c 100644 --- a/proto-compiler/src/lib.rs +++ b/proto-compiler/src/lib.rs @@ -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}; @@ -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") @@ -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. @@ -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); diff --git a/proto/Cargo.toml b/proto/Cargo.toml index 412cf02..4b13bf5 100644 --- a/proto/Cargo.toml +++ b/proto/Cargo.toml @@ -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", diff --git a/proto/build.rs b/proto/build.rs index 0e6b055..c98d49c 100644 --- a/proto/build.rs +++ b/proto/build.rs @@ -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"; @@ -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"); diff --git a/proto/src/.gitignore b/proto/src/.gitignore index 2a145fb..40ad867 100644 --- a/proto/src/.gitignore +++ b/proto/src/.gitignore @@ -1,5 +1,5 @@ tenderdash_nostd/ -tenderdash_std/ +tenderdash_grpc/ # prost/ and tenderdash.rs are deprecated and can be removed in the future prost/ diff --git a/proto/src/error.rs b/proto/src/error.rs index cfa6eb4..4c1d268 100644 --- a/proto/src/error.rs +++ b/proto/src/error.rs @@ -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}; diff --git a/proto/src/lib.rs b/proto/src/lib.rs index f62ca63..79b11f1 100644 --- a/proto/src/lib.rs +++ b/proto/src/lib.rs @@ -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)] @@ -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; diff --git a/proto/src/protobuf.rs b/proto/src/protobuf.rs index b21e789..04237ac 100644 --- a/proto/src/protobuf.rs +++ b/proto/src/protobuf.rs @@ -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 diff --git a/proto/src/serializers/from_str.rs b/proto/src/serializers/from_str.rs index 0544cc7..c1536ab 100644 --- a/proto/src/serializers/from_str.rs +++ b/proto/src/serializers/from_str.rs @@ -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}; diff --git a/proto/src/serializers/part_set_header_total.rs b/proto/src/serializers/part_set_header_total.rs index 17c9222..f55b977 100644 --- a/proto/src/serializers/part_set_header_total.rs +++ b/proto/src/serializers/part_set_header_total.rs @@ -6,9 +6,9 @@ //! Tendermint Core v0.34.0. This deserializer allows backwards-compatibility by //! deserializing both ways. See also: -#[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::{ diff --git a/proto/src/serializers/time_duration.rs b/proto/src/serializers/time_duration.rs index f30e943..0bdc33a 100644 --- a/proto/src/serializers/time_duration.rs +++ b/proto/src/serializers/time_duration.rs @@ -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}; diff --git a/proto/src/serializers/timestamp.rs b/proto/src/serializers/timestamp.rs index 41bf1f0..10ff120 100644 --- a/proto/src/serializers/timestamp.rs +++ b/proto/src/serializers/timestamp.rs @@ -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}; diff --git a/proto/tests/unit.rs b/proto/tests/unit.rs index e0fdfeb..229c353 100644 --- a/proto/tests/unit.rs +++ b/proto/tests/unit.rs @@ -1,4 +1,4 @@ -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "grpc"))] use core::convert::TryFrom; use tenderdash_proto::{