From 2ce48420e71fc58d6406c15ae44bbc7ea987677a Mon Sep 17 00:00:00 2001 From: Tristan <122918260+TAdev0@users.noreply.github.com> Date: Tue, 5 Dec 2023 20:12:13 +0100 Subject: [PATCH] FEAT: add healthCheck to KKRT Net API (#662) * healthCheck * health-check * health-check --- crates/eth-rpc/src/api/net_api.rs | 5 +++++ crates/eth-rpc/src/rpc.rs | 4 ++-- crates/eth-rpc/src/servers/net_rpc.rs | 25 +++++++++++++++++++------ crates/eth-rpc/tests/integration.rs | 8 ++++++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/eth-rpc/src/api/net_api.rs b/crates/eth-rpc/src/api/net_api.rs index a63e918d3..648a204af 100644 --- a/crates/eth-rpc/src/api/net_api.rs +++ b/crates/eth-rpc/src/api/net_api.rs @@ -19,4 +19,9 @@ pub trait NetApi { /// Otherwise false. #[method(name = "listening")] fn listening(&self) -> Result; + + /// Returns true if Kakarot RPC_URL is reachable. + /// Otherwise throw an EthApiError. + #[method(name = "health")] + async fn health(&self) -> Result; } diff --git a/crates/eth-rpc/src/rpc.rs b/crates/eth-rpc/src/rpc.rs index be86e3227..a4b5c7ccf 100644 --- a/crates/eth-rpc/src/rpc.rs +++ b/crates/eth-rpc/src/rpc.rs @@ -33,9 +33,9 @@ pub struct KakarotRpcModuleBuilder { impl KakarotRpcModuleBuilder

{ pub fn new(kakarot_client: Arc>) -> Self { let eth_rpc_module = KakarotEthRpc::new(kakarot_client.clone()).into_rpc(); - let alchemy_rpc_module = AlchemyRpc::new(kakarot_client).into_rpc(); + let alchemy_rpc_module = AlchemyRpc::new(kakarot_client.clone()).into_rpc(); let web3_rpc_module = Web3Rpc::default().into_rpc(); - let net_rpc_module = NetRpc::default().into_rpc(); + let net_rpc_module = NetRpc::new(kakarot_client.clone()).into_rpc(); let mut modules: HashMap = HashMap::new(); diff --git a/crates/eth-rpc/src/servers/net_rpc.rs b/crates/eth-rpc/src/servers/net_rpc.rs index 9202dff6a..28cfe6826 100644 --- a/crates/eth-rpc/src/servers/net_rpc.rs +++ b/crates/eth-rpc/src/servers/net_rpc.rs @@ -1,22 +1,28 @@ use jsonrpsee::core::{async_trait, RpcResult as Result}; use kakarot_rpc_core::client::constants::CHAIN_ID; +use kakarot_rpc_core::client::errors::EthApiError; +use kakarot_rpc_core::client::KakarotClient; use reth_primitives::U64; use reth_rpc_types::PeerCount; +use starknet::providers::Provider; +use std::sync::Arc; + use crate::api::net_api::NetApiServer; /// The RPC module for the implementing Net api -#[derive(Default)] -pub struct NetRpc {} +pub struct NetRpc { + pub kakarot_client: Arc>, +} -impl NetRpc { - pub const fn new() -> Self { - Self {} +impl NetRpc

{ + pub fn new(kakarot_client: Arc>) -> Self { + Self { kakarot_client } } } #[async_trait] -impl NetApiServer for NetRpc { +impl NetApiServer for NetRpc

{ fn version(&self) -> Result { Ok(CHAIN_ID.into()) } @@ -30,4 +36,11 @@ impl NetApiServer for NetRpc { // Kakarot RPC currently does not support peer-to-peer connections Ok(false) } + + async fn health(&self) -> Result { + // Calls starknet block_number method to check if it resolves + self.kakarot_client.starknet_provider().block_number().await.map_err(EthApiError::from)?; + + Ok(true) + } } diff --git a/crates/eth-rpc/tests/integration.rs b/crates/eth-rpc/tests/integration.rs index bf9964f29..6cfcae7b1 100644 --- a/crates/eth-rpc/tests/integration.rs +++ b/crates/eth-rpc/tests/integration.rs @@ -32,6 +32,14 @@ mod integration_tests { async fn test_erc20(#[future] katana: Katana) { let (server_addr, server_handle) = start_kakarot_rpc_server(&katana).await.expect("Error setting up Kakarot RPC server"); + + let reqwest_client = reqwest::Client::new(); + let _res = reqwest_client + .post(format!("http://localhost:{}/net_health", server_addr.port())) + .send() + .await + .expect("net_health: health check failed"); + let wallet: LocalWallet = SigningKey::from_slice(katana.eoa().private_key().as_ref()) .expect("EOA Private Key should be used to init a LocalWallet") .into();