From 51339c999bd32b277814a380ad04e636047dddcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marcos=20Bezerra?= Date: Fri, 2 Aug 2024 14:54:18 -0300 Subject: [PATCH] chore: simplify log filter `to_block` it was an Option, but inconditionally set as Some --- src/eth/primitives/log_filter.rs | 7 ++----- src/eth/primitives/log_filter_input.rs | 5 ++--- src/eth/rpc/rpc_server.rs | 10 +++------- src/eth/storage/inmemory/inmemory_permanent.rs | 5 +---- src/eth/storage/permanent_storage.rs | 2 -- src/eth/storage/redis/redis_permanent.rs | 5 +---- src/eth/storage/rocks/rocks_state.rs | 7 +------ 7 files changed, 10 insertions(+), 31 deletions(-) diff --git a/src/eth/primitives/log_filter.rs b/src/eth/primitives/log_filter.rs index af80144fb..c049d56cd 100644 --- a/src/eth/primitives/log_filter.rs +++ b/src/eth/primitives/log_filter.rs @@ -10,7 +10,7 @@ use crate::ext::not; #[cfg_attr(test, derive(Default))] pub struct LogFilter { pub from_block: BlockNumber, - pub to_block: Option, + pub to_block: BlockNumber, pub addresses: Vec
, /// Original payload received via RPC. @@ -22,10 +22,7 @@ impl LogFilter { /// Checks if a log matches the filter. pub fn matches(&self, log: &LogMined) -> bool { // filter block range - if log.block_number < self.from_block { - return false; - } - if self.to_block.as_ref().is_some_and(|to_block| log.block_number > *to_block) { + if log.block_number < self.from_block || self.to_block < log.block_number { return false; } diff --git a/src/eth/primitives/log_filter_input.rs b/src/eth/primitives/log_filter_input.rs index 67c77ee57..4ac7dec46 100644 --- a/src/eth/primitives/log_filter_input.rs +++ b/src/eth/primitives/log_filter_input.rs @@ -63,9 +63,8 @@ impl LogFilterInput { StoragePointInTime::MinedPast(number) => number, }; let to = match to { - StoragePointInTime::Pending => None, - StoragePointInTime::Mined => None, - StoragePointInTime::MinedPast(number) => Some(number), + StoragePointInTime::MinedPast(number) => number, + StoragePointInTime::Mined | StoragePointInTime::Pending => storage.read_mined_block_number()?, }; Ok(LogFilter { diff --git a/src/eth/rpc/rpc_server.rs b/src/eth/rpc/rpc_server.rs index ea7cc2656..fae68306c 100644 --- a/src/eth/rpc/rpc_server.rs +++ b/src/eth/rpc/rpc_server.rs @@ -673,19 +673,15 @@ fn eth_get_logs(params: Params<'_>, ctx: Arc, ext: Extensions) -> Re // parse params reject_unknown_client(ext.rpc_client())?; let (_, filter_input) = next_rpc_param_or_default::(params.sequence())?; - let mut filter = filter_input.parse(&ctx.storage)?; + let filter = filter_input.parse(&ctx.storage)?; - // for this operation, the filter always need the end block specified to calculate the difference - if filter.to_block.is_none() { - filter.to_block = Some(ctx.storage.read_mined_block_number()?); - } - let blocks_in_range = filter.from_block.count_to(&filter.to_block.unwrap()); + let blocks_in_range = filter.from_block.count_to(&filter.to_block); // track Span::with(|s| { s.rec_str("filter", &to_json_string(&filter)); s.rec_str("filter_from", &filter.from_block); - s.rec_str("filter_to", &filter.to_block.unwrap()); + s.rec_str("filter_to", &filter.to_block); s.rec_str("filter_range", &blocks_in_range); }); tracing::info!(?filter, "reading logs"); diff --git a/src/eth/storage/inmemory/inmemory_permanent.rs b/src/eth/storage/inmemory/inmemory_permanent.rs index f5eb38566..a527e0ce0 100644 --- a/src/eth/storage/inmemory/inmemory_permanent.rs +++ b/src/eth/storage/inmemory/inmemory_permanent.rs @@ -202,10 +202,7 @@ impl PermanentStorage for InMemoryPermanentStorage { .logs .iter() .skip_while(|log| log.block_number < filter.from_block) - .take_while(|log| match filter.to_block { - Some(to_block) => log.block_number <= to_block, - None => true, - }) + .take_while(|log| log.block_number <= filter.to_block) .filter(|log| filter.matches(log)) .cloned() .collect(); diff --git a/src/eth/storage/permanent_storage.rs b/src/eth/storage/permanent_storage.rs index 63b17a673..1b19bec26 100644 --- a/src/eth/storage/permanent_storage.rs +++ b/src/eth/storage/permanent_storage.rs @@ -47,8 +47,6 @@ pub trait PermanentStorage: Send + Sync + 'static { fn read_transaction(&self, hash: &Hash) -> anyhow::Result>; /// Retrieves logs from the storage. - /// - /// TODO: `filter.to_block` is always populated, need to change the type to reflect that. fn read_logs(&self, filter: &LogFilter) -> anyhow::Result>; // ------------------------------------------------------------------------- diff --git a/src/eth/storage/redis/redis_permanent.rs b/src/eth/storage/redis/redis_permanent.rs index b5c7adcad..b138dfe72 100644 --- a/src/eth/storage/redis/redis_permanent.rs +++ b/src/eth/storage/redis/redis_permanent.rs @@ -200,10 +200,7 @@ impl PermanentStorage for RedisPermanentStorage { fn read_logs(&self, filter: &LogFilter) -> anyhow::Result> { // prepare keys let from_block = filter.from_block.as_u64(); - let to_block = match filter.to_block { - Some(number) => number.as_u64(), - None => self.read_mined_block_number()?.as_u64(), - }; + let to_block = filter.to_block.as_u64(); let block_keys = (from_block..=to_block).map(key_block_by_number).collect_vec(); // exit if no keys diff --git a/src/eth/storage/rocks/rocks_state.rs b/src/eth/storage/rocks/rocks_state.rs index f01f53de6..a5b6891c5 100644 --- a/src/eth/storage/rocks/rocks_state.rs +++ b/src/eth/storage/rocks/rocks_state.rs @@ -251,11 +251,6 @@ impl RocksStorageState { pub fn read_logs(&self, filter: &LogFilter) -> Result> { let addresses: HashSet = filter.addresses.iter().map(|&address| AddressRocksdb::from(address)).collect(); - let end_block_range_filter = |number: BlockNumber| match filter.to_block.as_ref() { - Some(&last_block) => number <= last_block, - None => true, - }; - let iter = self .blocks_by_number .iter_from(BlockNumberRocksdb::from(filter.from_block), Direction::Forward)?; @@ -265,7 +260,7 @@ impl RocksStorageState { for next in iter { let (number, block) = next?; - if !end_block_range_filter(number.into()) { + if number > filter.to_block.into() { break; } let transactions_with_matching_addresses = block