Skip to content

Commit

Permalink
eth_call: cache key considers state override setting
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyke-bot committed Feb 20, 2024
1 parent f1e7f65 commit 7d117c2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
10 changes: 5 additions & 5 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 Down Expand Up @@ -56,7 +56,7 @@ async fn rpc_call(
"reason": err.to_string(),
}))),
)
.into();
.into();
}
};

Expand Down Expand Up @@ -91,7 +91,7 @@ async fn rpc_call(
None => {
tracing::warn!(method, "cache is not supported");
push_uncached_request_and_continue!()
},
}
};

let params_key = match cache_entry.handler.extract_cache_key(&params) {
Expand Down Expand Up @@ -433,6 +433,6 @@ impl Serialize for RpcRequest {
self.method.clone(),
self.params.clone(),
)
.serialize(serializer)
.serialize(serializer)
}
}
54 changes: 53 additions & 1 deletion src/rpc_cache_handler/eth_call.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Context;
use anyhow::{bail, Context};
use serde_json::Value;

use crate::rpc_cache_handler::{common, RpcCacheHandler};
Expand Down Expand Up @@ -29,6 +29,16 @@ impl RpcCacheHandler for Handler {

let tx_hash = common::hash_string(tx.as_str());

if params.len() > 2 {
if !params[2].is_object() {
bail!("params[2] not a state override setting object")
}

let state_override = common::hash_string(&serde_json::to_string(&params[2]).unwrap());

return Ok(Some(format!("{block_tag}-{tx_hash}-{state_override}",)));
}

Ok(Some(format!("{block_tag}-{tx_hash}")))
}
}
Expand Down Expand Up @@ -80,4 +90,46 @@ mod test {
let err = HANDLER.extract_cache_key(&params).unwrap_err();
assert_eq!(err.to_string(), "params[1] not a valid block tag");
}

#[test]
fn test_with_state_override() {
let params = json!([
{
"from": null,
"to": "0x6b175474e89094c44da98b954eedeac495271d0f",
"data": "0x70a082310000000000000000000000006E0d01A76C3Cf4288372a29124A26D4353EE51BE"
},
"0x12341324",
{
"0x6b175474e89094c44da98b954eedeac495271d0f": {
"code": "0x12341234"
}
}
]);

let cache_key = HANDLER.extract_cache_key(&params).unwrap().unwrap();
assert_eq!(
cache_key,
"0x12341324-aa734bab822de3d5f3191359094abe1eb49e3563-22884c3a09357b73375ee790393367081571afb7"
);
}

#[test]
fn test_invalid_state_override() {
let params = json!([
{
"from": null,
"to": "0x6b175474e89094c44da98b954eedeac495271d0f",
"data": "0x70a082310000000000000000000000006E0d01A76C3Cf4288372a29124A26D4353EE51BE"
},
"0x12341324",
"ggg"
]);

let err = HANDLER.extract_cache_key(&params).unwrap_err();
assert_eq!(
err.to_string(),
"params[2] not a state override setting object"
);
}
}
1 change: 0 additions & 1 deletion src/rpc_cache_handler/eth_chainid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ mod test {

static HANDLER: Handler = Handler;


#[test]
fn test() {
let params = json!([]);
Expand Down

0 comments on commit 7d117c2

Please sign in to comment.