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: take historical block count from env #574

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Changes from all 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: 18 additions & 2 deletions crates/zksync/core/src/vm/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,12 @@ pub fn encode_create_params(
signature.iter().copied().chain(params).collect()
}

/// Get last 256 block hashes mapped to block numbers. This excludes the current block.
/// Get historical block hashes mapped to block numbers. This excludes the current block.
fn get_historical_block_hashes<DB: Database>(ecx: &mut EvmContext<DB>) -> HashMap<rU256, B256> {
let mut block_hashes = HashMap::default();
for i in 1..=256u32 {
let num_blocks = get_env_historical_block_count();
tracing::debug!("fetching last {num_blocks} block hashes");
for i in 1..=num_blocks {
let (block_number, overflow) =
ecx.env.block.number.overflowing_sub(alloy_primitives::U256::from(i));
if overflow {
Expand All @@ -294,3 +296,17 @@ fn get_historical_block_hashes<DB: Database>(ecx: &mut EvmContext<DB>) -> HashMa

block_hashes
}

/// Get the number of historical blocks to fetch, from the env.
/// Default: `256`.
fn get_env_historical_block_count() -> u32 {
let name = "ZK_DEBUG_HISTORICAL_BLOCK_HASHES";
std::env::var(name)
.map(|value| {
value.parse::<u32>().unwrap_or_else(|err| {
panic!("failed parsing env variable {}={}, {:?}", name, value, err)
})
})
.map(|num| num.min(256))
.unwrap_or(256)
}