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

fix: Blockhash of current block should be zero #483

Merged
merged 6 commits into from
Jul 22, 2024
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
2 changes: 1 addition & 1 deletion crates/zksync/core/src/vm/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub fn encode_create_params(
/// Get last 256 block hashes mapped to block numbers
fn get_historical_block_hashes<DB: Database>(ecx: &mut EvmContext<DB>) -> HashMap<rU256, B256> {
let mut block_hashes = HashMap::default();
for i in 0..256u32 {
for i in 1..=256u32 {
let (block_number, overflow) = ecx.env.block.number.overflowing_sub(rU256::from(i));
if overflow {
break
Expand Down
4 changes: 3 additions & 1 deletion crates/zksync/core/src/vm/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,10 @@ impl<S: Send, H: HistoryMode> DynTracer<S, SimpleMemory<H>> for CheatcodeTracer
self.call_context.block_hashes.get(&block_number.to_ru256())
{
self.farcall_handler.set_immediate_return(block_hash.to_vec());
return;
} else {
self.farcall_handler.set_immediate_return(rU256::ZERO.to_be_bytes_vec());
elfedy marked this conversation as resolved.
Show resolved Hide resolved
}
return;
}
}

Expand Down
13 changes: 10 additions & 3 deletions zk-tests/src/Basic.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,20 @@ contract ZkBasicTest is Test {
be.chainid() == block.chainid,
"propagated block chainid is the same as current"
);

require(
be.ZkBlockhash(block.number) == blockhash(block.number),
"propagated blockhash is the same as current"
"Blockhash of the current block should be zero"
);

require(
be.ZkBlockhash(block.number) == bytes32(0),
"blockhash mismatch"
be.ZkBlockhash(block.number - 1) == blockhash(block.number - 1),
"Blockhash of the previous block should be equal"
);

require(
be.ZkBlockhash(0) == blockhash(0),
"Blockhash of the genesis block should be equal"
);

be = new BlockEnv();
Expand Down