From 91c14bd777ecabec834aec936b13e0a4488dcd55 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Thu, 28 Nov 2024 14:32:43 -0500 Subject: [PATCH] add tonic feature to proto crate (#474) --- CHANGELOG.md | 2 ++ yellowstone-grpc-client/Cargo.toml | 2 +- yellowstone-grpc-proto/Cargo.toml | 10 ++++++---- yellowstone-grpc-proto/build.rs | 17 +++++++++++++++-- yellowstone-grpc-proto/src/lib.rs | 20 +++++++++++++++++--- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 329769dd..91275fe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ The minor version will be incremented upon a breaking change and the patch versi ### Features +- proto: add tonic feature ([#474](https://github.com/rpcpool/yellowstone-grpc/pull/474)) + ### Breaking ## 2024-11-28 diff --git a/yellowstone-grpc-client/Cargo.toml b/yellowstone-grpc-client/Cargo.toml index f8ada6fd..168cbaa5 100644 --- a/yellowstone-grpc-client/Cargo.toml +++ b/yellowstone-grpc-client/Cargo.toml @@ -16,7 +16,7 @@ futures = { workspace = true } thiserror ={ workspace = true } tonic = { workspace = true, features = ["tls", "tls-roots"] } tonic-health = { workspace = true } -yellowstone-grpc-proto = { workspace = true } +yellowstone-grpc-proto = { workspace = true, features = ["tonic", "tonic-compression"] } [dev-dependencies] tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } diff --git a/yellowstone-grpc-proto/Cargo.toml b/yellowstone-grpc-proto/Cargo.toml index 1d9a3078..9d7f7745 100644 --- a/yellowstone-grpc-proto/Cargo.toml +++ b/yellowstone-grpc-proto/Cargo.toml @@ -31,7 +31,7 @@ solana-transaction-status = { workspace = true, optional = true } smallvec = { workspace = true, optional = true } spl-token-2022 = { workspace = true, optional = true } thiserror = { workspace = true, optional = true } -tonic = { workspace = true } +tonic = { workspace = true, optional = true } [dev-dependencies] criterion = { workspace = true } @@ -44,7 +44,7 @@ protobuf-src = { workspace = true } tonic-build = { workspace = true } [features] -default = ["convert", "tonic-compression"] +default = ["convert", "tonic", "tonic-compression"] convert = [ "dep:bincode", "dep:solana-account-decoder", @@ -60,10 +60,12 @@ plugin = [ "dep:serde", "dep:smallvec", "dep:spl-token-2022", - "dep:thiserror" + "dep:thiserror", + "dep:tonic" ] plugin-bench = ["plugin", "dep:prost_011", "dep:solana-storage-proto"] -tonic-compression = ["tonic/gzip", "tonic/zstd"] +tonic = ["dep:tonic"] +tonic-compression = ["tonic", "tonic/gzip", "tonic/zstd"] [lints] workspace = true diff --git a/yellowstone-grpc-proto/build.rs b/yellowstone-grpc-proto/build.rs index 9df65193..df89c383 100644 --- a/yellowstone-grpc-proto/build.rs +++ b/yellowstone-grpc-proto/build.rs @@ -1,10 +1,23 @@ -use tonic_build::manual::{Builder, Method, Service}; +use { + std::{env, fs, path::Path}, + tonic_build::manual::{Builder, Method, Service}, +}; fn main() -> anyhow::Result<()> { std::env::set_var("PROTOC", protobuf_src::protoc()); // build protos - tonic_build::compile_protos("proto/geyser.proto")?; + tonic_build::configure().compile_protos(&["proto/geyser.proto"], &["proto"])?; + + // build protos without tonic (wasm) + let out_dir = env::var("OUT_DIR").expect("OUT_DIR not found"); + let out_dir_path = Path::new(&out_dir).join("no-tonic"); + fs::create_dir_all(&out_dir_path).expect("failed to create out no-tonic directory"); + tonic_build::configure() + .build_client(false) + .build_server(false) + .out_dir(out_dir_path) + .compile_protos(&["proto/geyser.proto"], &["proto"])?; // build with accepting our custom struct let geyser_service = Service::builder() diff --git a/yellowstone-grpc-proto/src/lib.rs b/yellowstone-grpc-proto/src/lib.rs index f3bb8cea..80b56af5 100644 --- a/yellowstone-grpc-proto/src/lib.rs +++ b/yellowstone-grpc-proto/src/lib.rs @@ -4,7 +4,10 @@ pub mod geyser { #![allow(clippy::clone_on_ref_ptr)] #![allow(clippy::missing_const_for_fn)] - tonic::include_proto!("geyser"); + #[cfg(feature = "tonic")] + include!(concat!(env!("OUT_DIR"), "/geyser.rs")); + #[cfg(not(feature = "tonic"))] + include!(concat!(env!("OUT_DIR"), "/no-tonic/geyser.rs")); } pub mod solana { @@ -12,7 +15,16 @@ pub mod solana { pub mod storage { pub mod confirmed_block { - tonic::include_proto!("solana.storage.confirmed_block"); + #[cfg(feature = "tonic")] + include!(concat!( + env!("OUT_DIR"), + "/solana.storage.confirmed_block.rs" + )); + #[cfg(not(feature = "tonic"))] + include!(concat!( + env!("OUT_DIR"), + "/no-tonic/solana.storage.confirmed_block.rs" + )); } } } @@ -21,7 +33,9 @@ pub mod prelude { pub use super::{geyser::*, solana::storage::confirmed_block::*}; } -pub use {prost, tonic}; +pub use prost; +#[cfg(feature = "tonic")] +pub use tonic; #[cfg(feature = "plugin")] pub mod plugin;