Skip to content

Commit

Permalink
chore: add utility function to convert block numbers (#129)
Browse files Browse the repository at this point in the history
* add utility function to convert block numbers

* add util method

* update other occurrences
  • Loading branch information
nbaztec committed Sep 20, 2023
1 parent ee82bc3 commit 2b645a3
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 96 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
106 changes: 29 additions & 77 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
formatter,
system_contracts::{self, SystemContracts},
utils::{
adjust_l1_gas_price_for_tx, derive_gas_estimation_overhead, to_human_size, IntoBoxedFuture,
self, adjust_l1_gas_price_for_tx, derive_gas_estimation_overhead, to_human_size,
IntoBoxedFuture,
},
};
use clap::Parser;
Expand Down Expand Up @@ -1387,74 +1388,30 @@ impl<S: Send + Sync + 'static + ForkSource + std::fmt::Debug> EthNamespaceT for
Ok(r) => r,
Err(_) => return Err(into_jsrpc_error(Web3Error::InternalError)),
};
match block_number {
zksync_types::api::BlockNumber::Latest
| zksync_types::api::BlockNumber::Pending
| zksync_types::api::BlockNumber::Finalized
| zksync_types::api::BlockNumber::Committed => reader
.block_hashes
.get(&reader.current_miniblock)
.and_then(|hash| reader.blocks.get(hash))
.cloned()
.or_else(|| {
reader
.fork_storage
.inner
.read()
.expect("failed reading fork storage")
.fork
.as_ref()
.and_then(|fork| {
fork.fork_source
.get_block_by_number(block_number, true)
.ok()
.flatten()
})
}),
zksync_types::api::BlockNumber::Number(ask_number) => {
let block = reader
.block_hashes
.get(&ask_number.as_u64())
.and_then(|hash| reader.blocks.get(hash))
.cloned()
.or_else(|| {
reader
.fork_storage
.inner
.read()
.expect("failed reading fork storage")
.fork
.as_ref()
.and_then(|fork| {
fork.fork_source
.get_block_by_number(block_number, true)
.ok()
.flatten()
})
});
block
}
zksync_types::api::BlockNumber::Earliest => reader
.block_hashes
.get(&0)
.and_then(|hash| reader.blocks.get(hash))
.cloned()
.or_else(|| {
reader
.fork_storage
.inner
.read()
.expect("failed reading fork storage")
.fork
.as_ref()
.and_then(|fork| {
fork.fork_source
.get_block_by_number(block_number, true)
.ok()
.flatten()
})
}),
}
let number =
utils::to_real_block_number(block_number, U64::from(reader.current_miniblock))
.as_u64();

reader
.block_hashes
.get(&number)
.and_then(|hash| reader.blocks.get(hash))
.cloned()
.or_else(|| {
reader
.fork_storage
.inner
.read()
.expect("failed reading fork storage")
.fork
.as_ref()
.and_then(|fork| {
fork.fork_source
.get_block_by_number(block_number, true)
.ok()
.flatten()
})
})
};

match maybe_block {
Expand Down Expand Up @@ -2026,14 +1983,9 @@ impl<S: Send + Sync + 'static + ForkSource + std::fmt::Debug> EthNamespaceT for
Ok(r) => r,
Err(_) => return Err(into_jsrpc_error(Web3Error::InternalError)),
};
let number = match block_number {
zksync_types::api::BlockNumber::Latest
| zksync_types::api::BlockNumber::Pending
| zksync_types::api::BlockNumber::Finalized
| zksync_types::api::BlockNumber::Committed => reader.current_miniblock,
zksync_types::api::BlockNumber::Number(ask_number) => ask_number.as_u64(),
zksync_types::api::BlockNumber::Earliest => 0,
};
let number =
utils::to_real_block_number(block_number, U64::from(reader.current_miniblock))
.as_u64();

reader
.block_hashes
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);
}
}

0 comments on commit 2b645a3

Please sign in to comment.