Skip to content

Commit

Permalink
eth_getLogs: fix filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyke-bot committed Feb 20, 2024
1 parent 7d117c2 commit 1c755ae
Showing 1 changed file with 127 additions and 5 deletions.
132 changes: 127 additions & 5 deletions src/rpc_cache_handler/eth_get_logs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::B256;
use anyhow::Context;
use anyhow::{bail, Context};
use serde_json::Value;
use std::str::FromStr;

Expand All @@ -15,11 +15,15 @@ impl RpcCacheHandler for Handler {
}

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

let filter = params[0]
.as_object()
.context("params[0] not a filter object")?;
println!("params: {:?}", params);

let filter = &params[0];

if !filter.is_object() {
bail!("params[0] not a filter object");
}

let mut block_tag = None;

Expand Down Expand Up @@ -60,3 +64,121 @@ impl RpcCacheHandler for Handler {
Ok(cache_key)
}
}

#[cfg(test)]
mod test {
use super::*;
use serde_json::json;

static HANDLER: Handler = Handler;

#[test]
fn test_block_range() {
let params = json!([
{
"address": [
"0xb59f67a8bff5d8cd03f6ac17265c550ed8f33907"
],
"fromBlock": "0x429d3b",
"toBlock": "0x429d3c",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x00000000000000000000000000b46c2526e227482e2ebb8f4c69e4674d262e75",
"0x00000000000000000000000054a2d42a40f51259dedd1978f6c118a0f0eff078"
]
},
]);

let cache_key = HANDLER.extract_cache_key(&params).unwrap();
assert_eq!(
cache_key,
Some("0x429d3b-0x429d3c-bc57b716eb2996bd7f98537dd51516bb541ca882".to_string())
);
}

#[test]
fn test_block_hash() {
let params = json!([
{
"address": [
"0xb59f67a8bff5d8cd03f6ac17265c550ed8f33907"
],
"blockHash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x00000000000000000000000000b46c2526e227482e2ebb8f4c69e4674d262e75",
"0x00000000000000000000000054a2d42a40f51259dedd1978f6c118a0f0eff078"
]
},
]);

let cache_key = HANDLER.extract_cache_key(&params).unwrap();
assert_eq!(
cache_key,
Some(
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef-\
b27f759a6574516ddaa99bc9534f4cfcae86d386"
.to_string()
)
);
}

#[test]
fn test_invalid_block_number() {
let params = json!([
{
"address": [
"0xb59f67a8bff5d8cd03f6ac17265c550ed8f33907"
],
"fromBlock": "0x12345ggggggg",
"toBlock": "0x12345",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x00000000000000000000000000b46c2526e227482e2ebb8f4c69e4674d262e75",
"0x00000000000000000000000054a2d42a40f51259dedd1978f6c118a0f0eff078"
]
},
]);

let err = HANDLER.extract_cache_key(&params).unwrap_err();
assert_eq!(err.to_string(), "`fromBlock` is not a valid block number");

let params = json!([
{
"address": [
"0xb59f67a8bff5d8cd03f6ac17265c550ed8f33907"
],
"fromBlock": "0x12345",
"toBlock": "0x12345ggggggg",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x00000000000000000000000000b46c2526e227482e2ebb8f4c69e4674d262e75",
"0x00000000000000000000000054a2d42a40f51259dedd1978f6c118a0f0eff078"
]
},
]);

let err = HANDLER.extract_cache_key(&params).unwrap_err();
assert_eq!(err.to_string(), "`toBlock` is not a valid block number");
}

#[test]
fn test_invalid_block_hash() {
let params = json!([
{
"address": [
"0xb59f67a8bff5d8cd03f6ac17265c550ed8f33907"
],
"blockHash": "0x1234567890abcdef1234567890abcdef1234567890abcdef123456789ggggggg",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x00000000000000000000000000b46c2526e227482e2ebb8f4c69e4674d262e75",
"0x00000000000000000000000054a2d42a40f51259dedd1978f6c118a0f0eff078"
]
},
]);

let err = HANDLER.extract_cache_key(&params).unwrap_err();
assert_eq!(err.to_string(), "expect a valid block hash");
}
}

0 comments on commit 1c755ae

Please sign in to comment.