@@ -47,10 +47,21 @@ impl<T> FullEthApiServer for T where
4747{
4848}
4949
50+ /// Bitfinity lag API
51+ pub const LAG_STATUS_BAD : & str = "LAGGING" ;
52+
53+ /// Bitfinity lag API
54+ pub const LAG_STATUS_OK : & str = "ACCEPTED_LAG" ;
55+
5056/// Eth rpc interface: <https://ethereum.github.io/execution-apis/api-documentation/>
5157#[ cfg_attr( not( feature = "client" ) , rpc( server, namespace = "eth" ) ) ]
5258#[ cfg_attr( feature = "client" , rpc( server, client, namespace = "eth" ) ) ]
5359pub trait EthApi < T : RpcObject , B : RpcObject , R : RpcObject , H : RpcObject > {
60+ /// Bitfinity LB api extenstion
61+ /// Handler for: `eth_lagging`
62+ #[ method( name = "lagging" ) ]
63+ async fn lagging ( & self , accepted_lag : Option < U64 > ) -> RpcResult < String > ;
64+
5465 /// Returns the protocol version encoded as a string.
5566 #[ method( name = "protocolVersion" ) ]
5667 async fn protocol_version ( & self ) -> RpcResult < U64 > ;
@@ -386,6 +397,28 @@ where
386397 T : FullEthApi ,
387398 jsonrpsee_types:: error:: ErrorObject < ' static > : From < T :: Error > ,
388399{
400+ /// Handler for: `eth_lagging`
401+ async fn lagging ( & self , accepted_lag : Option < U64 > ) -> RpcResult < String > {
402+ let accepted_lag = match accepted_lag {
403+ Some ( lag) => U256 :: from ( lag) ,
404+ // Assuming that lag behind for 3 block is ok
405+ None => U256 :: from ( 3 ) ,
406+ } ;
407+
408+ let network_block = BitfinityEvmRpc :: network_block_number ( self ) . await ?;
409+ let node_block = self . block_number ( ) ?;
410+ let lag = network_block. saturating_sub ( node_block) ;
411+
412+ let status =
413+ if lag > accepted_lag { LAG_STATUS_BAD . to_owned ( ) } else { LAG_STATUS_OK . to_owned ( ) } ;
414+
415+ // Better to repond with string that add structure, import serde and stuff
416+ let response =
417+ format ! ( "{}: lag: {} node: {} network: {}" , status, lag, node_block, network_block) ;
418+
419+ Ok ( response)
420+ }
421+
389422 /// Handler for: `eth_protocolVersion`
390423 async fn protocol_version ( & self ) -> RpcResult < U64 > {
391424 trace ! ( target: "rpc::eth" , "Serving eth_protocolVersion" ) ;
0 commit comments