Skip to content

Commit

Permalink
rpc: Add RPC log middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Aug 21, 2024
1 parent 290033b commit 1a085b7
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,35 @@
use crate::ledger::errors::LedgerError;
use crate::{Client, RpcApiWrapper};
use jsonrpsee::server::Server;
use jsonrpsee::server::ServerHandle;
use jsonrpsee::server::middleware::rpc::RpcServiceT;
use jsonrpsee::server::{RpcServiceBuilder, Server};
use jsonrpsee::types::Request;
use std::{io::Error, net::SocketAddr, net::TcpListener};
use traits::RpcServer;

mod adapter;
#[allow(clippy::too_many_arguments)]
mod traits;

pub struct MockRpc {
pub socket_address: SocketAddr,
pub handle: ServerHandle,
/// Logger middleware.
#[derive(Clone)]
pub struct Logger<S>(S);
impl<'a, S> jsonrpsee::server::middleware::rpc::RpcServiceT<'a> for Logger<S>
where
S: RpcServiceT<'a> + Send + Sync,
{
type Future = S::Future;

/// This will get called for every RPC request.
fn call(&self, req: Request<'a>) -> Self::Future {
tracing::info!(
"Received RPC call: {}, with parameters: {:?}",
req.method,
req.params
);

self.0.call(req)
}
}

/// Spawns an RPC server for the mock blockchain.
Expand All @@ -42,7 +59,12 @@ pub async fn spawn_rpc_server(host: Option<&str>, port: Option<u16>) -> Result<S
}

async fn start_server(url: &str) -> Result<SocketAddr, Error> {
let server = Server::builder().build(url).await?;
let rpc_middleware = RpcServiceBuilder::new().layer_fn(Logger);

let server = Server::builder()
.set_rpc_middleware(rpc_middleware)
.build(url)
.await?;

let addr = server.local_addr()?;

Expand Down

0 comments on commit 1a085b7

Please sign in to comment.