Skip to content

Commit

Permalink
add util method
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec committed Sep 20, 2023
1 parent 093fadc commit 467d466
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
20 changes: 4 additions & 16 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 25 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -245,6 +247,27 @@ pub fn mine_empty_blocks<S: std::fmt::Debug + ForkSource>(
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;
Expand Down

0 comments on commit 467d466

Please sign in to comment.