From 277fd57cbf5169cfac4e73afe7382685f0b400f1 Mon Sep 17 00:00:00 2001 From: tomasarrachea Date: Fri, 28 Feb 2025 16:01:55 -0300 Subject: [PATCH] feat: add feature flag for generating rpc client --- bin/faucet/src/client.rs | 2 +- crates/proto/build.rs | 7 +- crates/proto/src/generated/rpc.rs | 391 ++++++++++++++++++++++++++++++ 3 files changed, 398 insertions(+), 2 deletions(-) diff --git a/bin/faucet/src/client.rs b/bin/faucet/src/client.rs index 330321681..cc3fa2618 100644 --- a/bin/faucet/src/client.rs +++ b/bin/faucet/src/client.rs @@ -6,7 +6,7 @@ use miden_node_proto::generated::{ requests::{ GetAccountDetailsRequest, GetBlockHeaderByNumberRequest, SubmitProvenTransactionRequest, }, - api_client::ApiClient, + rpc::api_client::ApiClient, }; use miden_objects::{ account::{Account, AccountFile, AccountId, AuthSecretKey}, diff --git a/crates/proto/build.rs b/crates/proto/build.rs index a9d3f5094..e72f63901 100644 --- a/crates/proto/build.rs +++ b/crates/proto/build.rs @@ -75,8 +75,13 @@ fn main() -> anyhow::Result<()> { prost_config.skip_debug(["AccountId", "Digest"]); // Generate the stub of the user facing server from its proto file + #[cfg(feature = "rpc-client")] + let generate_rpc_server = true; + #[cfg(not(feature = "rpc-client"))] + let generate_rpc_server = false; + tonic_build::configure() - .build_client(false) + .build_client(generate_rpc_server) .file_descriptor_set_path(&file_descriptor_path) .skip_protoc_run() .out_dir(&dst_dir) diff --git a/crates/proto/src/generated/rpc.rs b/crates/proto/src/generated/rpc.rs index 09bef2c86..4f25dfdbb 100644 --- a/crates/proto/src/generated/rpc.rs +++ b/crates/proto/src/generated/rpc.rs @@ -1,4 +1,395 @@ // This file is @generated by prost-build. +/// Generated client implementations. +pub mod api_client { + #![allow( + unused_variables, + dead_code, + missing_docs, + clippy::wildcard_imports, + clippy::let_unit_value, + )] + use tonic::codegen::*; + use tonic::codegen::http::Uri; + #[derive(Debug, Clone)] + pub struct ApiClient { + inner: tonic::client::Grpc, + } + impl ApiClient { + /// Attempt to create a new client by connecting to a given endpoint. + pub async fn connect(dst: D) -> Result + where + D: TryInto, + D::Error: Into, + { + let conn = tonic::transport::Endpoint::new(dst)?.connect().await?; + Ok(Self::new(conn)) + } + } + impl ApiClient + where + T: tonic::client::GrpcService, + T::Error: Into, + T::ResponseBody: Body + std::marker::Send + 'static, + ::Error: Into + std::marker::Send, + { + pub fn new(inner: T) -> Self { + let inner = tonic::client::Grpc::new(inner); + Self { inner } + } + pub fn with_origin(inner: T, origin: Uri) -> Self { + let inner = tonic::client::Grpc::with_origin(inner, origin); + Self { inner } + } + pub fn with_interceptor( + inner: T, + interceptor: F, + ) -> ApiClient> + where + F: tonic::service::Interceptor, + T::ResponseBody: Default, + T: tonic::codegen::Service< + http::Request, + Response = http::Response< + >::ResponseBody, + >, + >, + , + >>::Error: Into + std::marker::Send + std::marker::Sync, + { + ApiClient::new(InterceptedService::new(inner, interceptor)) + } + /// Compress requests with the given encoding. + /// + /// This requires the server to support it otherwise it might respond with an + /// error. + #[must_use] + pub fn send_compressed(mut self, encoding: CompressionEncoding) -> Self { + self.inner = self.inner.send_compressed(encoding); + self + } + /// Enable decompressing responses. + #[must_use] + pub fn accept_compressed(mut self, encoding: CompressionEncoding) -> Self { + self.inner = self.inner.accept_compressed(encoding); + self + } + /// Limits the maximum size of a decoded message. + /// + /// Default: `4MB` + #[must_use] + pub fn max_decoding_message_size(mut self, limit: usize) -> Self { + self.inner = self.inner.max_decoding_message_size(limit); + self + } + /// Limits the maximum size of an encoded message. + /// + /// Default: `usize::MAX` + #[must_use] + pub fn max_encoding_message_size(mut self, limit: usize) -> Self { + self.inner = self.inner.max_encoding_message_size(limit); + self + } + /// Returns a nullifier proof for each of the requested nullifiers. + pub async fn check_nullifiers( + &mut self, + request: impl tonic::IntoRequest< + super::super::requests::CheckNullifiersRequest, + >, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static("/rpc.Api/CheckNullifiers"); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new("rpc.Api", "CheckNullifiers")); + self.inner.unary(req, path, codec).await + } + /// Returns a list of nullifiers that match the specified prefixes and are recorded in the node. + /// + /// Note that only 16-bit prefixes are supported at this time. + pub async fn check_nullifiers_by_prefix( + &mut self, + request: impl tonic::IntoRequest< + super::super::requests::CheckNullifiersByPrefixRequest, + >, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static( + "/rpc.Api/CheckNullifiersByPrefix", + ); + let mut req = request.into_request(); + req.extensions_mut() + .insert(GrpcMethod::new("rpc.Api", "CheckNullifiersByPrefix")); + self.inner.unary(req, path, codec).await + } + /// Returns the latest state of an account with the specified ID. + pub async fn get_account_details( + &mut self, + request: impl tonic::IntoRequest< + super::super::requests::GetAccountDetailsRequest, + >, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static( + "/rpc.Api/GetAccountDetails", + ); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new("rpc.Api", "GetAccountDetails")); + self.inner.unary(req, path, codec).await + } + /// Returns the latest state proofs of the specified accounts. + pub async fn get_account_proofs( + &mut self, + request: impl tonic::IntoRequest< + super::super::requests::GetAccountProofsRequest, + >, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static("/rpc.Api/GetAccountProofs"); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new("rpc.Api", "GetAccountProofs")); + self.inner.unary(req, path, codec).await + } + /// Returns delta of the account states in the range from `from_block_num` (exclusive) to + /// `to_block_num` (inclusive). + pub async fn get_account_state_delta( + &mut self, + request: impl tonic::IntoRequest< + super::super::requests::GetAccountStateDeltaRequest, + >, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static( + "/rpc.Api/GetAccountStateDelta", + ); + let mut req = request.into_request(); + req.extensions_mut() + .insert(GrpcMethod::new("rpc.Api", "GetAccountStateDelta")); + self.inner.unary(req, path, codec).await + } + /// Returns raw block data for the specified block number. + pub async fn get_block_by_number( + &mut self, + request: impl tonic::IntoRequest< + super::super::requests::GetBlockByNumberRequest, + >, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static("/rpc.Api/GetBlockByNumber"); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new("rpc.Api", "GetBlockByNumber")); + self.inner.unary(req, path, codec).await + } + /// Retrieves block header by given block number. Optionally, it also returns the MMR path + /// and current chain length to authenticate the block's inclusion. + pub async fn get_block_header_by_number( + &mut self, + request: impl tonic::IntoRequest< + super::super::requests::GetBlockHeaderByNumberRequest, + >, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static( + "/rpc.Api/GetBlockHeaderByNumber", + ); + let mut req = request.into_request(); + req.extensions_mut() + .insert(GrpcMethod::new("rpc.Api", "GetBlockHeaderByNumber")); + self.inner.unary(req, path, codec).await + } + /// Returns a list of notes matching the provided note IDs. + pub async fn get_notes_by_id( + &mut self, + request: impl tonic::IntoRequest, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static("/rpc.Api/GetNotesById"); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new("rpc.Api", "GetNotesById")); + self.inner.unary(req, path, codec).await + } + /// Submits proven transaction to the Miden network. + pub async fn submit_proven_transaction( + &mut self, + request: impl tonic::IntoRequest< + super::super::requests::SubmitProvenTransactionRequest, + >, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static( + "/rpc.Api/SubmitProvenTransaction", + ); + let mut req = request.into_request(); + req.extensions_mut() + .insert(GrpcMethod::new("rpc.Api", "SubmitProvenTransaction")); + self.inner.unary(req, path, codec).await + } + /// Returns info which can be used by the client to sync up to the tip of chain for the notes they are interested in. + /// + /// Client specifies the `note_tags` they are interested in, and the block height from which to search for new for + /// matching notes for. The request will then return the next block containing any note matching the provided tags. + /// + /// The response includes each note's metadata and inclusion proof. + /// + /// A basic note sync can be implemented by repeatedly requesting the previous response's block until reaching the + /// tip of the chain. + pub async fn sync_notes( + &mut self, + request: impl tonic::IntoRequest, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static("/rpc.Api/SyncNotes"); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new("rpc.Api", "SyncNotes")); + self.inner.unary(req, path, codec).await + } + /// Returns info which can be used by the client to sync up to the latest state of the chain + /// for the objects (accounts and notes) the client is interested in. + /// + /// This request returns the next block containing requested data. It also returns `chain_tip` + /// which is the latest block number in the chain. Client is expected to repeat these requests + /// in a loop until `response.block_header.block_num == response.chain_tip`, at which point + /// the client is fully synchronized with the chain. + /// + /// Each update response also contains info about new notes, accounts etc. created. It also returns + /// Chain MMR delta that can be used to update the state of Chain MMR. This includes both chain + /// MMR peaks and chain MMR nodes. + /// + /// For preserving some degree of privacy, note tags contain only high + /// part of hashes. Thus, returned data contains excessive notes, client can make + /// additional filtering of that data on its side. + pub async fn sync_state( + &mut self, + request: impl tonic::IntoRequest, + ) -> std::result::Result< + tonic::Response, + tonic::Status, + > { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic::codec::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static("/rpc.Api/SyncState"); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new("rpc.Api", "SyncState")); + self.inner.unary(req, path, codec).await + } + } +} /// Generated server implementations. pub mod api_server { #![allow(