Skip to content

Commit

Permalink
fix chainid rpc and debug_traceTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyke-bot committed Feb 8, 2024
1 parent 0b947bc commit 454bbbe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
23 changes: 15 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ mod utils;

#[actix_web::post("/{chain}")]
async fn rpc_call(
path: web::Path<(String,)>,
path: web::Path<(String, )>,
data: web::Data<AppState>,
body: web::Json<Value>,
) -> Result<HttpResponse, Error> {
let (chain,) = path.into_inner();
let (chain, ) = path.into_inner();
let chain_state = data
.chains
.get(&chain.to_uppercase())
Expand All @@ -48,15 +48,15 @@ async fn rpc_call(
let mut cache_backend = match chain_state.cache_factory.get_instance() {
Ok(v) => v,
Err(err) => {
tracing::error!("fail to get cache backend because: {}", err);
tracing::error!("fail to get cache backend because: {err:#}");
return JsonRpcResponse::from_error(
None,
DefinedError::InternalError(Some(json!({
"error": "fail to get cache backend",
"reason": err.to_string(),
}))),
)
.into();
.into();
}
};

Expand Down Expand Up @@ -88,14 +88,21 @@ async fn rpc_call(

let cache_entry = match chain_state.cache_entries.get(&method) {
Some(cache_entry) => cache_entry,
None => push_uncached_request_and_continue!(),
None => {
tracing::warn!(method, "cache is not supported");
push_uncached_request_and_continue!()
},
};

let params_key = match cache_entry.handler.extract_cache_key(&params) {
Ok(Some(params_key)) => params_key,
Ok(None) => push_uncached_request_and_continue!(),
Err(err) => {
tracing::error!("fail to extract cache key because: {}", err);
tracing::error!(
method,
params = format_args!("{}", params),
"fail to extract cache key: {err:#}",
);
push_uncached_request_and_continue!();
}
};
Expand All @@ -110,7 +117,7 @@ async fn rpc_call(
push_uncached_request_and_continue!(key);
}
Err(err) => {
tracing::error!("fail to read cache because: {}", err);
tracing::error!("fail to read cache because: {err:#}");
push_uncached_request_and_continue!();
}
}
Expand Down Expand Up @@ -426,6 +433,6 @@ impl Serialize for RpcRequest {
self.method.clone(),
self.params.clone(),
)
.serialize(serializer)
.serialize(serializer)
}
}
2 changes: 1 addition & 1 deletion src/rpc_cache_handler/debug_trace_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Handler;

impl RpcCacheHandler for Handler {
fn method_name(&self) -> &'static str {
"debug_traceCall"
"debug_traceTransaction"
}

fn extract_cache_key(&self, params: &Value) -> anyhow::Result<Option<String>> {
Expand Down
15 changes: 2 additions & 13 deletions src/rpc_cache_handler/eth_chainid.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde_json::Value;

use crate::rpc_cache_handler::common::ParamsSpec;
use crate::rpc_cache_handler::{common, RpcCacheHandler};
use crate::rpc_cache_handler::RpcCacheHandler;

#[derive(Default, Clone)]
pub struct Handler;
Expand All @@ -11,9 +10,7 @@ impl RpcCacheHandler for Handler {
"eth_chainId"
}

fn extract_cache_key(&self, params: &Value) -> anyhow::Result<Option<String>> {
common::require_array_params(params, ParamsSpec::Exact(0))?;

fn extract_cache_key(&self, _: &Value) -> anyhow::Result<Option<String>> {
Ok(Some("static".to_string()))
}
}
Expand All @@ -25,14 +22,6 @@ mod test {

static HANDLER: Handler = Handler;

#[test]
fn test_invalid_params_len() {
let params = json!([1]);
assert_eq!(
HANDLER.extract_cache_key(&params).unwrap_err().to_string(),
"expected 0 params, got 1"
);
}

#[test]
fn test() {
Expand Down

0 comments on commit 454bbbe

Please sign in to comment.