Skip to content

Commit

Permalink
feat(proto)!: separate client and server feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Dec 17, 2024
1 parent 36999a4 commit fb9f8a6
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 27 deletions.
5 changes: 0 additions & 5 deletions proto-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
12 changes: 9 additions & 3 deletions proto-compiler/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand All @@ -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(),
}
}
Expand Down
17 changes: 16 additions & 1 deletion proto-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 10 additions & 11 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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 }
Expand Down
8 changes: 6 additions & 2 deletions proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions proto/src/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
23 changes: 18 additions & 5 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions proto/src/time.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit fb9f8a6

Please sign in to comment.