From 1c755ae17ae3aca29225f7bfc014fc732143b81e Mon Sep 17 00:00:00 2001 From: Tony Ke Date: Tue, 20 Feb 2024 14:30:32 +0800 Subject: [PATCH] eth_getLogs: fix filters --- src/rpc_cache_handler/eth_get_logs.rs | 132 +++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 5 deletions(-) diff --git a/src/rpc_cache_handler/eth_get_logs.rs b/src/rpc_cache_handler/eth_get_logs.rs index 0a58a0b..785ada8 100644 --- a/src/rpc_cache_handler/eth_get_logs.rs +++ b/src/rpc_cache_handler/eth_get_logs.rs @@ -1,5 +1,5 @@ use alloy_primitives::B256; -use anyhow::Context; +use anyhow::{bail, Context}; use serde_json::Value; use std::str::FromStr; @@ -15,11 +15,15 @@ impl RpcCacheHandler for Handler { } fn extract_cache_key(&self, params: &Value) -> anyhow::Result> { - 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 = ¶ms[0]; + + if !filter.is_object() { + bail!("params[0] not a filter object"); + } let mut block_tag = None; @@ -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(¶ms).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(¶ms).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(¶ms).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(¶ms).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(¶ms).unwrap_err(); + assert_eq!(err.to_string(), "expect a valid block hash"); + } +}