Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONRPC request-response logging #1236

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ resolver = "2"
# 3rd-party deps
anyhow = { workspace = true }
backoff = { workspace = true }
futures = { workspace = true }
hyper = { workspace = true }
jsonrpsee = { workspace = true, features = ["http-client", "server"] }
lru = { workspace = true }
tokio = { workspace = true }
tower-http = { workspace = true }
tracing = { workspace = true }

# Sov SDK deps
sov-db = { path = "../sovereign-sdk/full-node/db/sov-db" }
Expand Down
37 changes: 35 additions & 2 deletions crates/common/src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
//! Common RPC crate provides helper methods that are needed in rpc servers
use std::time::Duration;

use futures::future::BoxFuture;
use futures::FutureExt;
use hyper::Method;
use jsonrpsee::core::RegisterMethodError;
use jsonrpsee::server::middleware::http::ProxyGetRequestLayer;
use jsonrpsee::server::middleware::rpc::RpcServiceT;
use jsonrpsee::types::error::{INTERNAL_ERROR_CODE, INTERNAL_ERROR_MSG};
use jsonrpsee::types::ErrorObjectOwned;
use jsonrpsee::RpcModule;
use jsonrpsee::types::{ErrorObjectOwned, Request};
use jsonrpsee::{MethodResponse, RpcModule};
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
use sov_db::schema::types::BatchNumber;
use tower_http::cors::{Any, CorsLayer};
Expand Down Expand Up @@ -77,3 +80,33 @@ pub fn get_cors_layer() -> CorsLayer {
.allow_origin(Any)
.allow_headers(Any)
}

#[derive(Debug, Clone)]
pub struct Logger<S>(pub S);

impl<'a, S> RpcServiceT<'a> for Logger<S>
where
S: RpcServiceT<'a> + Send + Sync + Clone + 'a,
{
type Future = BoxFuture<'a, MethodResponse>;

fn call(&self, req: Request<'a>) -> Self::Future {
let req_id = req.id();
let req_method = req.method_name().to_string();

tracing::debug!(id = ?req_id, method = ?req_method, params = ?req.params().as_str(), "rpc_request");

let service = self.0.clone();
async move {
let resp = service.call(req).await;
if resp.is_success() {
tracing::debug!(id = ?req_id, method = ?req_method, result = ?resp.as_result(), "rpc_success");
} else {
tracing::warn!(id = ?req_id, method = ?req_method, result = ?resp.as_result(), "rpc_error");
}

resp
}
.boxed()
}
}
29 changes: 0 additions & 29 deletions crates/ethereum-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use subscription::{handle_logs_subscription, handle_new_heads_subscription};
use tokio::join;
use tokio::sync::broadcast;
use trace::{debug_trace_by_block_number, handle_debug_trace_chain};
use tracing::info;

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -93,13 +92,10 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
enable_subscriptions: bool,
) -> Result<(), jsonrpsee::core::RegisterMethodError> {
rpc.register_async_method("web3_clientVersion", |_, ethereum, _| async move {
info!("eth module: web3_clientVersion");

Ok::<_, ErrorObjectOwned>(ethereum.web3_client_version.clone())
})?;

rpc.register_blocking_method("web3_sha3", move |params, _, _| {
info!("eth module: web3_sha3");
let data: Bytes = params.one()?;

let hash = B256::from_slice(keccak256(&data).as_slice());
Expand All @@ -108,7 +104,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
})?;

rpc.register_blocking_method("eth_gasPrice", move |_, ethereum, _| {
info!("eth module: eth_gasPrice");
let price = {
let mut working_set = WorkingSet::<C>::new(ethereum.storage.clone());

Expand All @@ -121,7 +116,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
})?;

rpc.register_blocking_method("eth_maxFeePerGas", move |_, ethereum, _| {
info!("eth module: eth_maxFeePerGas");
let max_fee_per_gas = {
let mut working_set = WorkingSet::<C>::new(ethereum.storage.clone());

Expand All @@ -134,7 +128,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
})?;

rpc.register_blocking_method("eth_maxPriorityFeePerGas", move |_, ethereum, _| {
info!("eth module: eth_maxPriorityFeePerGas");
let max_priority_fee = {
let mut working_set = WorkingSet::<C>::new(ethereum.storage.clone());

Expand All @@ -147,7 +140,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
})?;

rpc.register_blocking_method("eth_feeHistory", move |params, ethereum, _| {
info!("eth module: eth_feeHistory");
let mut params = params.sequence();

let block_count: Index = params.next()?;
Expand All @@ -173,15 +165,11 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(

#[cfg(feature = "local")]
rpc.register_async_method("eth_accounts", |_, ethereum, _| async move {
info!("eth module: eth_accounts");

Ok::<_, ErrorObjectOwned>(ethereum.eth_signer.signers())
})?;

// #[cfg(feature = "local")]
// rpc.register_async_method("eth_sendTransaction", |parameters, ethereum| async move {
// info!("eth module: eth_sendTransaction");

// let mut transaction_request: TransactionRequest = parameters.one().unwrap();

// let evm = Evm::<C>::default();
Expand Down Expand Up @@ -405,8 +393,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
rpc.register_blocking_method::<Result<Vec<GethTrace>, ErrorObjectOwned>, _>(
"debug_traceBlockByHash",
move |parameters, ethereum, _| {
info!("eth module: debug_traceBlockByHash");

let mut params = parameters.sequence();

let block_hash: B256 = params.next()?;
Expand All @@ -429,8 +415,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
rpc.register_blocking_method::<Result<Vec<GethTrace>, ErrorObjectOwned>, _>(
"debug_traceBlockByNumber",
move |parameters, ethereum, _| {
info!("eth module: debug_traceBlockByNumber");

let mut params = parameters.sequence();

let block_number: BlockNumberOrTag = params.next()?;
Expand All @@ -457,8 +441,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
// else; calls the debug_trace_transaction_block function in evm
// that function traces the entire block, returns all the traces to here
// then we put them into cache and return the trace of the requested transaction
info!(params = ?parameters, "eth module: debug_traceTransaction");

let mut params = parameters.sequence();

let tx_hash: B256 = params.next()?;
Expand Down Expand Up @@ -493,8 +475,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
)?;

rpc.register_async_method("txpool_content", |_, _, _| async move {
info!("eth module: txpool_content");

// This is a simple mock for serde.
let json = json!({
"pending": {},
Expand All @@ -507,8 +487,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
rpc.register_async_method(
"eth_getUncleByBlockHashAndIndex",
|parameters, _, _| async move {
info!("eth module: eth_getUncleByBlockHashAndIndex");

let mut params = parameters.sequence();

let _block_hash: String = params.next()?;
Expand All @@ -524,7 +502,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
rpc.register_async_method::<Result<B256, ErrorObjectOwned>, _, _>(
"eth_sendRawTransaction",
|parameters, ethereum, _| async move {
info!(params = ?parameters, "Full Node: eth_sendRawTransaction");
// send this directly to the sequencer
let data: Bytes = parameters.one()?;
// sequencer client should send it
Expand All @@ -551,10 +528,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
let mut params = parameters.sequence();
let hash: B256 = params.next()?;
let mempool_only: Result<Option<bool>, ErrorObjectOwned> = params.optional_next();
info!(
"Full Node: eth_getTransactionByHash({}, {:?})",
hash, mempool_only
);

// check if mempool_only parameter was given what was its value
match mempool_only {
Expand Down Expand Up @@ -612,8 +585,6 @@ fn register_rpc_methods<C: sov_modules_api::Context, Da: DaService>(
rpc.register_async_method::<Result<SyncStatus, ErrorObjectOwned>, _, _>(
"citrea_syncStatus",
|_, ethereum, _| async move {
info!("Full Node: citrea_syncStatus");

// sequencer client should send latest l2 height
// da service should send latest finalized l1 block header
let (sequencer_response, da_response) = join!(
Expand Down
Loading
Loading