diff --git a/src/filters.rs b/src/filters.rs index 331f74e5..48fdb876 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -4,6 +4,8 @@ use zksync_basic_types::{H160, H256, U256, U64}; use zksync_types::api::{BlockNumber, Log}; use zksync_web3_decl::types::FilterChanges; +use crate::utils; + /// Specifies a filter type #[derive(Debug, Clone, PartialEq)] pub enum FilterType { @@ -34,22 +36,8 @@ pub struct LogFilter { impl LogFilter { fn matches(&self, log: &Log, latest_block_number: U64) -> bool { - let from = match self.from_block { - BlockNumber::Finalized - | BlockNumber::Pending - | BlockNumber::Committed - | BlockNumber::Latest => latest_block_number, - BlockNumber::Earliest => U64::zero(), - BlockNumber::Number(n) => n, - }; - let to = match self.to_block { - BlockNumber::Finalized - | BlockNumber::Pending - | BlockNumber::Committed - | BlockNumber::Latest => latest_block_number, - BlockNumber::Earliest => U64::zero(), - BlockNumber::Number(n) => n, - }; + let from = utils::to_real_block_number(self.from_block, latest_block_number); + let to = utils::to_real_block_number(self.to_block, latest_block_number); let n = log.block_number.expect("block number must exist"); if n < from || n > to { diff --git a/src/utils.rs b/src/utils.rs index 67c6bb7f..d4fb301c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -10,11 +10,13 @@ use vm::{ }, HistoryEnabled, OracleTools, }; -use zksync_basic_types::{H256, U256}; +use zksync_basic_types::{H256, U256, U64}; use zksync_state::StorageView; use zksync_state::WriteStorage; use zksync_types::{ - api::Block, zk_evm::zkevm_opcode_defs::system_params::MAX_TX_ERGS_LIMIT, MAX_TXS_IN_BLOCK, + api::{Block, BlockNumber}, + zk_evm::zkevm_opcode_defs::system_params::MAX_TX_ERGS_LIMIT, + MAX_TXS_IN_BLOCK, }; use zksync_utils::{ceil_div_u256, u256_to_h256}; @@ -245,6 +247,27 @@ pub fn mine_empty_blocks( node.current_batch = node.current_batch.saturating_add(1); } +/// Returns the actual [U64] block number from [BlockNumber]. +/// +/// # Arguments +/// +/// * `block_number` - [BlockNumber] for a block. +/// * `latest_block_number` - A [U64] representing the latest block number. +/// +/// # Returns +/// +/// A [U64] representing the input block number. +pub fn to_real_block_number(block_number: BlockNumber, latest_block_number: U64) -> U64 { + match block_number { + BlockNumber::Finalized + | BlockNumber::Pending + | BlockNumber::Committed + | BlockNumber::Latest => latest_block_number, + BlockNumber::Earliest => U64::zero(), + BlockNumber::Number(n) => n, + } +} + #[cfg(test)] mod tests { use zksync_basic_types::U256;