Skip to content

Commit f046a05

Browse files
committed
LB lagging health check
1 parent c4b54ff commit f046a05

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

crates/rpc/rpc-eth-api/src/core.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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"))]
5359
pub 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");

crates/rpc/rpc-eth-api/src/helpers/bitfinity_evm_rpc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ pub trait BitfinityEvmRpc {
1616
/// Returns the `ChainSpec`.
1717
fn chain_spec(&self) -> Arc<ChainSpec>;
1818

19+
/// Returns latest block number at the network/sync source.
20+
fn network_block_number(&self) -> impl Future<Output = RpcResult<U256>> + Send {
21+
let chain_spec = self.chain_spec();
22+
async move {
23+
let (rpc_url, client) = get_client(&chain_spec)?;
24+
25+
let block_number = client.get_block_number().await.map_err(|e| {
26+
internal_rpc_err(format!(
27+
"failed to forward eth_blockNumber request to {}: {}",
28+
rpc_url, e
29+
))
30+
})?;
31+
32+
Ok(U256::from(block_number))
33+
}
34+
}
35+
1936
/// Forwards `eth_gasPrice` calls to the Bitfinity EVM.
2037
fn gas_price(&self) -> impl Future<Output = RpcResult<U256>> + Send {
2138
let chain_spec = self.chain_spec();

0 commit comments

Comments
 (0)