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 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
4 changes: 2 additions & 2 deletions crates/zksync/core/src/vm/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ pub fn encode_create_params(
signature.iter().copied().chain(params).collect()
}

/// Get last 256 block hashes mapped to block numbers
/// Get last 256 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 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
13 changes: 7 additions & 6 deletions crates/zksync/core/src/vm/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,13 @@ impl<S: Send, H: HistoryMode> DynTracer<S, SimpleMemory<H>> for CheatcodeTracer
calldata.starts_with(&SELECTOR_BLOCK_HASH)
{
let block_number = U256::from(&calldata[4..36]);
if let Some(block_hash) =
self.call_context.block_hashes.get(&block_number.to_ru256())
{
self.farcall_handler.set_immediate_return(block_hash.to_vec());
return;
}
let block_hash = self
.call_context
.block_hashes
.get(&block_number.to_ru256())
.unwrap_or_default();
self.farcall_handler.set_immediate_return(block_hash.to_vec());
return;
}
}

Expand Down
22 changes: 15 additions & 7 deletions zk-tests/src/Basic.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract BlockEnv {
chainid = block.chainid;
}

function ZkBlockhash(uint256 _blockNumber) public view returns (bytes32) {
function zkBlockhash(uint256 _blockNumber) public view returns (bytes32) {
return blockhash(_blockNumber);
}
}
Expand Down Expand Up @@ -96,13 +96,21 @@ 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"
be.zkBlockhash(block.number) == blockhash(block.number),
"blockhash of the current block should be zero"
);

// this corresponds to the the genesis block since the test runs in block #1
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 Expand Up @@ -157,11 +165,11 @@ contract ZkBasicTest is Test {
);
}

function testZkBlockhashWithNewerBlocks() public {
function testzkBlockhashWithNewerBlocks() public {
vm.selectFork(latestForkEth);
BlockEnv be = new BlockEnv();
require(
be.ZkBlockhash(block.number) == blockhash(block.number),
be.zkBlockhash(block.number) == blockhash(block.number),
"blockhash mismatch"
);
}
Expand Down