Skip to content

Commit

Permalink
FEAT: add healthCheck to KKRT Net API (#662)
Browse files Browse the repository at this point in the history
* healthCheck

* health-check

* health-check
  • Loading branch information
TAdev0 authored Dec 5, 2023
1 parent 4266568 commit 2ce4842
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions crates/eth-rpc/src/api/net_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ pub trait NetApi {
/// Otherwise false.
#[method(name = "listening")]
fn listening(&self) -> Result<bool>;

/// Returns true if Kakarot RPC_URL is reachable.
/// Otherwise throw an EthApiError.
#[method(name = "health")]
async fn health(&self) -> Result<bool>;
}
4 changes: 2 additions & 2 deletions crates/eth-rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ pub struct KakarotRpcModuleBuilder<P: Provider + Send + Sync + 'static> {
impl<P: Provider + Send + Sync + 'static> KakarotRpcModuleBuilder<P> {
pub fn new(kakarot_client: Arc<KakarotClient<P>>) -> 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<KakarotRpcModule, Methods> = HashMap::new();

Expand Down
25 changes: 19 additions & 6 deletions crates/eth-rpc/src/servers/net_rpc.rs
Original file line number Diff line number Diff line change
@@ -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<P: Provider + Send + Sync + 'static> {
pub kakarot_client: Arc<KakarotClient<P>>,
}

impl NetRpc {
pub const fn new() -> Self {
Self {}
impl<P: Provider + Send + Sync> NetRpc<P> {
pub fn new(kakarot_client: Arc<KakarotClient<P>>) -> Self {
Self { kakarot_client }
}
}

#[async_trait]
impl NetApiServer for NetRpc {
impl<P: Provider + Send + Sync + 'static> NetApiServer for NetRpc<P> {
fn version(&self) -> Result<U64> {
Ok(CHAIN_ID.into())
}
Expand All @@ -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<bool> {
// Calls starknet block_number method to check if it resolves
self.kakarot_client.starknet_provider().block_number().await.map_err(EthApiError::from)?;

Ok(true)
}
}
8 changes: 8 additions & 0 deletions crates/eth-rpc/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2ce4842

Please sign in to comment.