Skip to content

Commit

Permalink
tendermint-proto: Add a feature flag and generate gRPC server definit…
Browse files Browse the repository at this point in the history
…ions

- `proto-compiler` uses `tonic-build`
- tendermint-proto can be compiled with std for the `grpc` feature
(as required by the generated `tonic` code)
- the primary motivation is the new gRPC-based PrivVal interface
in Tendermint 0.35
(see informalsystems#1134)
  • Loading branch information
tomtau committed May 30, 2022
1 parent cd3e391 commit 1fdd02d
Show file tree
Hide file tree
Showing 8 changed files with 1,109 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/1134-proto-grpc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[tendermint-proto]` Add a feature flag and generate gRPC server definitions
([#1134](https://github.com/informalsystems/tendermint-rs/issues/1134))
5 changes: 5 additions & 0 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ description = """
[package.metadata.docs.rs]
all-features = true

[features]
default = []
grpc = ["tonic"]

[dependencies]
prost = { version = "0.10", default-features = false }
prost-types = { version = "0.10", default-features = false }
Expand All @@ -27,6 +31,7 @@ num-traits = { version = "0.2", default-features = false }
num-derive = { version = "0.3", default-features = false }
time = { version = "0.3.5", default-features = false, features = ["macros", "parsing"] }
flex-error = { version = "0.4.4", default-features = false }
tonic = { version = "0.7", optional = true }

[dev-dependencies]
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
2 changes: 1 addition & 1 deletion proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! tendermint-proto library gives the developer access to the Tendermint proto-defined structs.

#![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 Down
688 changes: 688 additions & 0 deletions proto/src/prost/tendermint.abci.rs

Large diffs are not rendered by default.

224 changes: 224 additions & 0 deletions proto/src/prost/tendermint.privval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,227 @@ pub enum Errors {
ReadTimeout = 4,
WriteTimeout = 5,
}
/// Generated server implementations.
#[cfg(feature = "grpc")]
pub mod priv_validator_api_server {
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
use tonic::codegen::*;
///Generated trait containing gRPC methods that should be implemented for use with PrivValidatorApiServer.
#[async_trait]
pub trait PrivValidatorApi: Send + Sync + 'static {
async fn get_pub_key(
&self,
request: tonic::Request<super::PubKeyRequest>,
) -> Result<tonic::Response<super::PubKeyResponse>, tonic::Status>;
async fn sign_vote(
&self,
request: tonic::Request<super::SignVoteRequest>,
) -> Result<tonic::Response<super::SignedVoteResponse>, tonic::Status>;
async fn sign_proposal(
&self,
request: tonic::Request<super::SignProposalRequest>,
) -> Result<tonic::Response<super::SignedProposalResponse>, tonic::Status>;
}
#[derive(Debug)]
pub struct PrivValidatorApiServer<T: PrivValidatorApi> {
inner: _Inner<T>,
accept_compression_encodings: (),
send_compression_encodings: (),
}
struct _Inner<T>(Arc<T>);
impl<T: PrivValidatorApi> PrivValidatorApiServer<T> {
pub fn new(inner: T) -> Self {
Self::from_arc(Arc::new(inner))
}
pub fn from_arc(inner: Arc<T>) -> Self {
let inner = _Inner(inner);
Self {
inner,
accept_compression_encodings: Default::default(),
send_compression_encodings: Default::default(),
}
}
pub fn with_interceptor<F>(
inner: T,
interceptor: F,
) -> InterceptedService<Self, F>
where
F: tonic::service::Interceptor,
{
InterceptedService::new(Self::new(inner), interceptor)
}
}
impl<T, B> tonic::codegen::Service<http::Request<B>> for PrivValidatorApiServer<T>
where
T: PrivValidatorApi,
B: Body + Send + 'static,
B::Error: Into<StdError> + Send + 'static,
{
type Response = http::Response<tonic::body::BoxBody>;
type Error = std::convert::Infallible;
type Future = BoxFuture<Self::Response, Self::Error>;
fn poll_ready(
&mut self,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: http::Request<B>) -> Self::Future {
let inner = self.inner.clone();
match req.uri().path() {
"/tendermint.privval.PrivValidatorAPI/GetPubKey" => {
#[allow(non_camel_case_types)]
struct GetPubKeySvc<T: PrivValidatorApi>(pub Arc<T>);
impl<
T: PrivValidatorApi,
> tonic::server::UnaryService<super::PubKeyRequest>
for GetPubKeySvc<T> {
type Response = super::PubKeyResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::PubKeyRequest>,
) -> Self::Future {
let inner = self.0.clone();
let fut = async move { (*inner).get_pub_key(request).await };
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = GetPubKeySvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
"/tendermint.privval.PrivValidatorAPI/SignVote" => {
#[allow(non_camel_case_types)]
struct SignVoteSvc<T: PrivValidatorApi>(pub Arc<T>);
impl<
T: PrivValidatorApi,
> tonic::server::UnaryService<super::SignVoteRequest>
for SignVoteSvc<T> {
type Response = super::SignedVoteResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::SignVoteRequest>,
) -> Self::Future {
let inner = self.0.clone();
let fut = async move { (*inner).sign_vote(request).await };
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = SignVoteSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
"/tendermint.privval.PrivValidatorAPI/SignProposal" => {
#[allow(non_camel_case_types)]
struct SignProposalSvc<T: PrivValidatorApi>(pub Arc<T>);
impl<
T: PrivValidatorApi,
> tonic::server::UnaryService<super::SignProposalRequest>
for SignProposalSvc<T> {
type Response = super::SignedProposalResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::SignProposalRequest>,
) -> Self::Future {
let inner = self.0.clone();
let fut = async move {
(*inner).sign_proposal(request).await
};
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = SignProposalSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
_ => {
Box::pin(async move {
Ok(
http::Response::builder()
.status(200)
.header("grpc-status", "12")
.header("content-type", "application/grpc")
.body(empty_body())
.unwrap(),
)
})
}
}
}
}
impl<T: PrivValidatorApi> Clone for PrivValidatorApiServer<T> {
fn clone(&self) -> Self {
let inner = self.inner.clone();
Self {
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
}
}
}
impl<T: PrivValidatorApi> Clone for _Inner<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for _Inner<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl<T: PrivValidatorApi> tonic::transport::NamedService
for PrivValidatorApiServer<T> {
const NAME: &'static str = "tendermint.privval.PrivValidatorAPI";
}
}

0 comments on commit 1fdd02d

Please sign in to comment.