From 77041dfc13651212ca48c673393cb6137029a15a Mon Sep 17 00:00:00 2001 From: pedro bufulin Date: Thu, 28 Nov 2024 16:01:01 -0300 Subject: [PATCH] feat(server): gRPC service implementation --- tap_aggregator/Cargo.toml | 8 +++- tap_aggregator/build.rs | 8 ++++ tap_aggregator/proto/tap_aggregator.proto | 13 +++++++ tap_aggregator/src/server.rs | 45 +++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tap_aggregator/build.rs diff --git a/tap_aggregator/Cargo.toml b/tap_aggregator/Cargo.toml index 9a92d9a..691cefb 100644 --- a/tap_aggregator/Cargo.toml +++ b/tap_aggregator/Cargo.toml @@ -35,7 +35,13 @@ axum = { version = "0.7.5", features = [ futures-util = "0.3.28" lazy_static = "1.4.0" ruint = "1.10.1" -tower = { version = "0.4", features = ["util"] } +tower = { version = "0.4", features = ["util", "steer"] } +tonic = { version = "0.12.3", features = ["transport"] } +prost = "0.13.3" + +[build-dependencies] +tonic-build = "0.12.3" + [dev-dependencies] jsonrpsee = { workspace = true, features = ["http-client", "jsonrpsee-core"] } diff --git a/tap_aggregator/build.rs b/tap_aggregator/build.rs new file mode 100644 index 0000000..8e1d93a --- /dev/null +++ b/tap_aggregator/build.rs @@ -0,0 +1,8 @@ +fn main() -> Result<(), Box> { + println!("Running build.rs..."); + let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set by Cargo"); + println!("OUT_DIR: {}", out_dir); // This should print the output directory + + tonic_build::compile_protos("./proto/tap_aggregator.proto")?; + Ok(()) +} diff --git a/tap_aggregator/proto/tap_aggregator.proto b/tap_aggregator/proto/tap_aggregator.proto index bdc3c85..f16fc63 100644 --- a/tap_aggregator/proto/tap_aggregator.proto +++ b/tap_aggregator/proto/tap_aggregator.proto @@ -33,7 +33,20 @@ message RavResponse { SignedRav rav = 1; } +message TapRpcApiVersion { + string version = 1; +} + +message TapRpcApiVersionsInfo { + repeated TapRpcApiVersion versions_supported = 1; + repeated TapRpcApiVersion versions_deprecated = 2; +} + +// Optional request message for ApiVersions (TODO: should we use use google.protobuf.Empty?) +message ApiVersionsRequest {} + service TapAggregator { + rpc ApiVersions(ApiVersionsRequest) returns (TapRpcApiVersionsInfo); rpc AggregateReceipts(RavRequest) returns (RavResponse); } diff --git a/tap_aggregator/src/server.rs b/tap_aggregator/src/server.rs index 0ff03d3..cb10126 100644 --- a/tap_aggregator/src/server.rs +++ b/tap_aggregator/src/server.rs @@ -10,6 +10,7 @@ use anyhow::Result; use jsonrpsee::{proc_macros::rpc, server::ServerBuilder, server::ServerHandle}; use lazy_static::lazy_static; use prometheus::{register_counter, register_int_counter, Counter, IntCounter}; +// use tower::steer::Steer; use crate::aggregator::check_and_aggregate_receipts; use crate::api_versioning::{ @@ -22,6 +23,10 @@ use tap_core::{ rav::ReceiptAggregateVoucher, receipt::Receipt, signed_message::EIP712SignedMessage, }; +pub mod tap_aggregator { + tonic::include_proto!("tap_aggregator.v1"); +} + // Register the metrics into the global metrics registry. lazy_static! { static ref AGGREGATION_SUCCESS_COUNTER: IntCounter = register_int_counter!( @@ -171,6 +176,45 @@ fn aggregate_receipts_( } } +use tap_aggregator::tap_aggregator_server::TapAggregator; +use tap_aggregator::{ + ApiVersionsRequest, RavRequest, RavResponse, SignedRav, + TapRpcApiVersionsInfo as TapGRpcApiVersionsInfo, +}; +use tonic::{Request, Response, Status}; + +#[tonic::async_trait] +impl TapAggregator for RpcImpl { + async fn api_versions( + &self, + _request: Request, + ) -> Result, Status> { + // Example response + let response = TapGRpcApiVersionsInfo { + versions_deprecated: vec![], + versions_supported: vec![], + }; + + Ok(Response::new(response)) + } + + async fn aggregate_receipts( + &self, + request: Request, + ) -> Result, Status> { + let rav_request = request.into_inner(); + + // Example implementation: create a dummy response + let response = RavResponse { + rav: Some(SignedRav { + message: None, // Fill this with your aggregate logic + signature: vec![], + }), + }; + Ok(Response::new(response)) + } +} + impl RpcServer for RpcImpl { fn api_versions(&self) -> JsonRpcResult { Ok(JsonRpcResponse::ok(tap_rpc_api_versions_info())) @@ -208,6 +252,7 @@ impl RpcServer for RpcImpl { } } +// TODO: create a gRPC alike run_server pub async fn run_server( port: u16, wallet: PrivateKeySigner,