From fb9f8a684822ee6acb4347ee9ae24f2d120d80ca Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Tue, 17 Dec 2024 16:51:07 +0100 Subject: [PATCH] feat(proto)!: separate client and server feature --- proto-compiler/Cargo.toml | 5 ----- proto-compiler/src/constants.rs | 12 +++++++++--- proto-compiler/src/lib.rs | 17 ++++++++++++++++- proto/Cargo.toml | 21 ++++++++++----------- proto/build.rs | 8 ++++++-- proto/src/.gitignore | 1 + proto/src/lib.rs | 23 ++++++++++++++++++----- proto/src/time.rs | 2 ++ 8 files changed, 62 insertions(+), 27 deletions(-) diff --git a/proto-compiler/Cargo.toml b/proto-compiler/Cargo.toml index cedec0c..cbd5dca 100644 --- a/proto-compiler/Cargo.toml +++ b/proto-compiler/Cargo.toml @@ -26,9 +26,4 @@ tonic-build = { version = "0.12.3", optional = true } [features] 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/grpc feature. -server = ["grpc"] -# 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 f5f7fec..024a751 100644 --- a/proto-compiler/src/constants.rs +++ b/proto-compiler/src/constants.rs @@ -11,15 +11,20 @@ pub const TENDERDASH_REPO: &str = "https://github.com/dashpay/tenderdash"; pub enum GenerationMode { /// Generate the files using `tonic` and put them into `tenderdash_grpc` /// module. - Grpc, + GrpcServer, + /// Generate minimal gRPC client using tonic, without transport + /// implementation. Put them into `tenderdash_grpc_client` module. + GrpcClient, /// 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::GrpcServer => "tenderdash_grpc".to_string(), + GenerationMode::GrpcClient => "tenderdash_grpc_client".to_string(), GenerationMode::NoStd => "tenderdash_nostd".to_string(), } } @@ -28,7 +33,8 @@ impl GenerationMode { impl ToString for GenerationMode { fn to_string(&self) -> String { match self { - GenerationMode::Grpc => "tonic".to_string(), + GenerationMode::GrpcServer => "grpc-client-server".to_string(), + GenerationMode::GrpcClient => "grpc-client".to_string(), GenerationMode::NoStd => "nostd".to_string(), } } diff --git a/proto-compiler/src/lib.rs b/proto-compiler/src/lib.rs index fa21dfa..1777495 100644 --- a/proto-compiler/src/lib.rs +++ b/proto-compiler/src/lib.rs @@ -124,9 +124,24 @@ pub fn proto_compile(mode: GenerationMode) { println!("[info] => Creating structs."); match mode { - GenerationMode::Grpc => { + GenerationMode::GrpcServer => { #[cfg(feature = "grpc")] tonic_build::configure() + .build_client(true) + .build_server(true) + .build_transport(true) + .generate_default_stubs(true) + .compile_protos_with_config(pb, &protos, &proto_includes_paths) + .unwrap(); + #[cfg(not(feature = "grpc"))] + panic!("grpc feature is required to compile {}", mode.to_string()); + }, + GenerationMode::GrpcClient => { + #[cfg(feature = "grpc")] + tonic_build::configure() + .build_client(true) + .build_server(false) + .build_transport(false) .generate_default_stubs(true) .compile_protos_with_config(pb, &protos, &proto_includes_paths) .unwrap(); diff --git a/proto/Cargo.toml b/proto/Cargo.toml index 074f422..601b7c3 100644 --- a/proto/Cargo.toml +++ b/proto/Cargo.toml @@ -34,17 +34,16 @@ all-features = true # # Sometimes cleaning the build cache with `cargo clean` might be necessary to solve # issues related to outdated generated files. -default = ["grpc"] +default = ["server"] -# Enable standard library support; DEPRECATED - use `grpc` instead -std = ["grpc"] -# Build gRPC server using tonic -grpc = [ - "prost/std", - "tenderdash-proto-compiler/server", - "tenderdash-proto-compiler/client", - "dep:tonic", -] +# Enable standard library support; DEPRECATED - use `client` and/or `server` instead +std = ["client"] +# Enable gRPC support using tonic; internal, consider using `server` or `client` instead +grpc = ["client", "dep:tonic", "tonic/codegen", "tonic/prost"] +# Build gRPC server using tonic. Includes `client` feature. +server = ["client"] +# Build minimal gRPC client using tonic, without transport +client = ["tenderdash-proto-compiler/grpc", "prost/std", "grpc"] serde = ["dep:serde", "bytes/serde"] @@ -53,7 +52,7 @@ bytes = { version = "1.7", default-features = false } prost = { version = "0.13", default-features = false, features = [ "prost-derive", ] } -tonic = { version = "0.12.3", optional = true } +tonic = { version = "0.12.3", optional = true, default-features = false } serde = { version = "1.0.208", default-features = false, features = [ "derive", ], optional = true } diff --git a/proto/build.rs b/proto/build.rs index f0a79ad..edf2a03 100644 --- a/proto/build.rs +++ b/proto/build.rs @@ -13,8 +13,12 @@ fn main() { env::set_var("TENDERDASH_COMMITISH", DEFAULT_VERSION); } - #[cfg(feature = "grpc")] - tenderdash_proto_compiler::proto_compile(GenerationMode::Grpc); + #[cfg(feature = "server")] + // build gRPC server (includes client) + tenderdash_proto_compiler::proto_compile(GenerationMode::GrpcServer); + #[cfg(all(feature = "client", not(feature = "server")))] + // build gRPC client only + tenderdash_proto_compiler::proto_compile(GenerationMode::GrpcClient); // we always build nostd version tenderdash_proto_compiler::proto_compile(GenerationMode::NoStd); diff --git a/proto/src/.gitignore b/proto/src/.gitignore index 40ad867..032989a 100644 --- a/proto/src/.gitignore +++ b/proto/src/.gitignore @@ -1,5 +1,6 @@ tenderdash_nostd/ tenderdash_grpc/ +tenderdash_grpc_client/ # prost/ and tenderdash.rs are deprecated and can be removed in the future prost/ diff --git a/proto/src/lib.rs b/proto/src/lib.rs index 399fa72..be1025d 100644 --- a/proto/src/lib.rs +++ b/proto/src/lib.rs @@ -33,17 +33,30 @@ use bytes::{Buf, BufMut}; pub use error::Error; pub use prost; use prost::{encoding::encoded_len_varint, Message}; + #[rustfmt::skip] pub mod tenderdash_nostd; -#[cfg(not(feature = "grpc"))] -// Re-export the nostd module only if the std one is not available -pub use tenderdash_nostd::*; -#[cfg(feature = "grpc")] + +// Depending on feature, add correct module + +#[cfg(feature = "server")] #[rustfmt::skip] pub mod tenderdash_grpc; -#[cfg(feature = "grpc")] +#[cfg(feature = "client")] +#[rustfmt::skip] +pub mod tenderdash_grpc_client; + +// Now, re-export correct module + +#[cfg(feature = "server")] pub use tenderdash_grpc::*; +#[cfg(all(not(feature = "server"), feature = "client"))] +pub use tenderdash_grpc_client::*; +#[cfg(not(feature = "grpc"))] +// Re-export the nostd module only if the std one is not available +pub use tenderdash_nostd::*; + #[cfg(feature = "serde")] pub mod serializers; mod time; diff --git a/proto/src/time.rs b/proto/src/time.rs index 4ab3c18..d2a5f88 100644 --- a/proto/src/time.rs +++ b/proto/src/time.rs @@ -1,5 +1,7 @@ //! Time conversion traits and functions +#[cfg(not(feature = "std"))] +use crate::format; use crate::{google::protobuf::Timestamp, Error}; pub trait ToMillis { /// Convert protobuf timestamp into milliseconds since epoch