Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add utility function to convert block numbers #129

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
nbaztec marked this conversation as resolved.
Show resolved Hide resolved
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
65 changes: 62 additions & 3 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,11 +247,32 @@ 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;

use crate::utils::to_human_size;
use super::*;

#[test]
fn test_human_sizes() {
Expand All @@ -260,4 +283,40 @@ mod tests {
assert_eq!("1", to_human_size(U256::from(1)));
assert_eq!("250_000_000", to_human_size(U256::from(250000000u64)));
}

#[test]
fn test_to_real_block_number_finalized() {
let actual = to_real_block_number(BlockNumber::Finalized, U64::from(10));
assert_eq!(U64::from(10), actual);
}

#[test]
fn test_to_real_block_number_pending() {
let actual = to_real_block_number(BlockNumber::Pending, U64::from(10));
assert_eq!(U64::from(10), actual);
}

#[test]
fn test_to_real_block_number_committed() {
let actual = to_real_block_number(BlockNumber::Committed, U64::from(10));
assert_eq!(U64::from(10), actual);
}

#[test]
fn test_to_real_block_number_latest() {
let actual = to_real_block_number(BlockNumber::Latest, U64::from(10));
assert_eq!(U64::from(10), actual);
}

#[test]
fn test_to_real_block_number_earliest() {
let actual = to_real_block_number(BlockNumber::Earliest, U64::from(10));
assert_eq!(U64::zero(), actual);
}

#[test]
fn test_to_real_block_number_number() {
let actual = to_real_block_number(BlockNumber::Number(U64::from(5)), U64::from(10));
assert_eq!(U64::from(5), actual);
}
}