Skip to content

Commit

Permalink
feat(server): gRPC service implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrohba1 authored and gusinacio committed Dec 24, 2024
1 parent 476e449 commit 77041df
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tap_aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
8 changes: 8 additions & 0 deletions tap_aggregator/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
13 changes: 13 additions & 0 deletions tap_aggregator/proto/tap_aggregator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
45 changes: 45 additions & 0 deletions tap_aggregator/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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!(
Expand Down Expand Up @@ -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<ApiVersionsRequest>,
) -> Result<Response<TapGRpcApiVersionsInfo>, Status> {
// Example response
let response = TapGRpcApiVersionsInfo {
versions_deprecated: vec![],
versions_supported: vec![],
};

Ok(Response::new(response))
}

async fn aggregate_receipts(
&self,
request: Request<RavRequest>,
) -> Result<Response<RavResponse>, 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<TapRpcApiVersionsInfo> {
Ok(JsonRpcResponse::ok(tap_rpc_api_versions_info()))
Expand Down Expand Up @@ -208,6 +252,7 @@ impl RpcServer for RpcImpl {
}
}

// TODO: create a gRPC alike run_server
pub async fn run_server(
port: u16,
wallet: PrivateKeySigner,
Expand Down

0 comments on commit 77041df

Please sign in to comment.