@@ -12,7 +12,7 @@ use jsonrpsee::{
1212 server:: { ServerBuilder , ServerHandle , TowerService } ,
1313} ;
1414use lazy_static:: lazy_static;
15- use log:: info;
15+ use log:: { error , info} ;
1616use prometheus:: { register_counter, register_int_counter, Counter , IntCounter } ;
1717use tap_core:: signed_message:: Eip712SignedMessage ;
1818use tap_graph:: { Receipt , ReceiptAggregateVoucher , SignedReceipt } ;
@@ -94,6 +94,7 @@ struct RpcImpl {
9494 wallet : PrivateKeySigner ,
9595 accepted_addresses : HashSet < Address > ,
9696 domain_separator : Eip712Domain ,
97+ kafka : Option < rdkafka:: producer:: ThreadedProducer < rdkafka:: producer:: DefaultProducerContext > > ,
9798}
9899
99100/// Helper method that checks if the given API version is supported.
@@ -205,6 +206,14 @@ impl v1::tap_aggregator_server::TapAggregator for RpcImpl {
205206 TOTAL_GRT_AGGREGATED . inc_by ( receipts_grt as f64 ) ;
206207 TOTAL_AGGREGATED_RECEIPTS . inc_by ( receipts_count) ;
207208 AGGREGATION_SUCCESS_COUNTER . inc ( ) ;
209+ if let Some ( kafka) = & self . kafka {
210+ produce_kafka_records (
211+ kafka,
212+ & self . wallet . address ( ) ,
213+ & res. message . allocationId ,
214+ res. message . valueAggregate ,
215+ ) ;
216+ }
208217
209218 let response = v1:: RavResponse {
210219 rav : Some ( res. into ( ) ) ,
@@ -253,6 +262,14 @@ impl v2::tap_aggregator_server::TapAggregator for RpcImpl {
253262 TOTAL_GRT_AGGREGATED . inc_by ( receipts_grt as f64 ) ;
254263 TOTAL_AGGREGATED_RECEIPTS . inc_by ( receipts_count) ;
255264 AGGREGATION_SUCCESS_COUNTER . inc ( ) ;
265+ if let Some ( kafka) = & self . kafka {
266+ produce_kafka_records (
267+ kafka,
268+ & res. message . payer ,
269+ & res. message . allocationId ,
270+ res. message . valueAggregate ,
271+ ) ;
272+ }
256273
257274 let response = v2:: RavResponse {
258275 rav : Some ( res. into ( ) ) ,
@@ -294,6 +311,14 @@ impl RpcServer for RpcImpl {
294311 TOTAL_GRT_AGGREGATED . inc_by ( receipts_grt as f64 ) ;
295312 TOTAL_AGGREGATED_RECEIPTS . inc_by ( receipts_count) ;
296313 AGGREGATION_SUCCESS_COUNTER . inc ( ) ;
314+ if let Some ( kafka) = & self . kafka {
315+ produce_kafka_records (
316+ kafka,
317+ & self . wallet . address ( ) ,
318+ & res. data . message . allocationId ,
319+ res. data . message . valueAggregate ,
320+ ) ;
321+ }
297322 Ok ( res)
298323 }
299324 Err ( e) => {
@@ -304,6 +329,7 @@ impl RpcServer for RpcImpl {
304329 }
305330}
306331
332+ #[ allow( clippy:: too_many_arguments) ]
307333pub async fn run_server (
308334 port : u16 ,
309335 wallet : PrivateKeySigner ,
@@ -312,12 +338,14 @@ pub async fn run_server(
312338 max_request_body_size : u32 ,
313339 max_response_body_size : u32 ,
314340 max_concurrent_connections : u32 ,
341+ kafka : Option < rdkafka:: producer:: ThreadedProducer < rdkafka:: producer:: DefaultProducerContext > > ,
315342) -> Result < ( JoinHandle < ( ) > , std:: net:: SocketAddr ) > {
316343 // Setting up the JSON RPC server
317344 let rpc_impl = RpcImpl {
318345 wallet,
319346 accepted_addresses,
320347 domain_separator,
348+ kafka,
321349 } ;
322350 let ( json_rpc_service, _) = create_json_rpc_service (
323351 rpc_impl. clone ( ) ,
@@ -432,6 +460,25 @@ fn create_json_rpc_service(
432460 Ok ( ( handle, server_handle) )
433461}
434462
463+ fn produce_kafka_records (
464+ kafka : & rdkafka:: producer:: ThreadedProducer < rdkafka:: producer:: DefaultProducerContext > ,
465+ sender : & Address ,
466+ allocation : & Address ,
467+ aggregated_value : u128 ,
468+ ) {
469+ let topic = "gateway_ravs" ;
470+ let key = format ! ( "{sender:?}:{allocation:?}" ) ;
471+ let payload = aggregated_value. to_string ( ) ;
472+ let result = kafka. send (
473+ rdkafka:: producer:: BaseRecord :: to ( topic)
474+ . key ( & key)
475+ . payload ( & payload) ,
476+ ) ;
477+ if let Err ( ( err, _) ) = result {
478+ error ! ( "error producing to {topic}: {err}" ) ;
479+ }
480+ }
481+
435482#[ cfg( test) ]
436483#[ allow( clippy:: too_many_arguments) ]
437484mod tests {
@@ -508,6 +555,7 @@ mod tests {
508555 http_request_size_limit,
509556 http_response_size_limit,
510557 http_max_concurrent_connections,
558+ None ,
511559 )
512560 . await
513561 . unwrap ( ) ;
@@ -557,6 +605,7 @@ mod tests {
557605 http_request_size_limit,
558606 http_response_size_limit,
559607 http_max_concurrent_connections,
608+ None ,
560609 )
561610 . await
562611 . unwrap ( ) ;
@@ -637,6 +686,7 @@ mod tests {
637686 http_request_size_limit,
638687 http_response_size_limit,
639688 http_max_concurrent_connections,
689+ None ,
640690 )
641691 . await
642692 . unwrap ( ) ;
@@ -714,6 +764,7 @@ mod tests {
714764 http_request_size_limit,
715765 http_response_size_limit,
716766 http_max_concurrent_connections,
767+ None ,
717768 )
718769 . await
719770 . unwrap ( ) ;
@@ -805,6 +856,7 @@ mod tests {
805856 http_request_size_limit,
806857 http_response_size_limit,
807858 http_max_concurrent_connections,
859+ None ,
808860 )
809861 . await
810862 . unwrap ( ) ;
0 commit comments