From e4048a4f988fda72f562f4a40eefd245c9f1653f Mon Sep 17 00:00:00 2001 From: Jrigada Date: Fri, 19 Jul 2024 17:00:52 -0300 Subject: [PATCH 1/5] respect same behaviour as L1 --- crates/zksync/core/src/vm/runner.rs | 2 +- crates/zksync/core/src/vm/tracer.rs | 4 +++- zk-tests/src/Basic.t.sol | 14 +++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/zksync/core/src/vm/runner.rs b/crates/zksync/core/src/vm/runner.rs index fdc701361..438c701cb 100644 --- a/crates/zksync/core/src/vm/runner.rs +++ b/crates/zksync/core/src/vm/runner.rs @@ -277,7 +277,7 @@ pub fn encode_create_params( /// Get last 256 block hashes mapped to block numbers fn get_historical_block_hashes(ecx: &mut EvmContext) -> HashMap { 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 diff --git a/crates/zksync/core/src/vm/tracer.rs b/crates/zksync/core/src/vm/tracer.rs index 89422ce1d..4879771a9 100644 --- a/crates/zksync/core/src/vm/tracer.rs +++ b/crates/zksync/core/src/vm/tracer.rs @@ -310,8 +310,10 @@ impl DynTracer> 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()); } + return; } } diff --git a/zk-tests/src/Basic.t.sol b/zk-tests/src/Basic.t.sol index 14bbb7cae..f81c65754 100644 --- a/zk-tests/src/Basic.t.sol +++ b/zk-tests/src/Basic.t.sol @@ -2,6 +2,7 @@ pragma solidity ^0.8.13; import {Test} from "forge-std/Test.sol"; +import {console2} from "forge-std/console2.sol"; contract BlockEnv { uint256 public number; @@ -96,13 +97,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(); From 86aea76d42d51d6818c618b56ba4cdb5ef64b8ae Mon Sep 17 00:00:00 2001 From: Jrigada Date: Fri, 19 Jul 2024 17:02:53 -0300 Subject: [PATCH 2/5] remove console log --- zk-tests/src/Basic.t.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/zk-tests/src/Basic.t.sol b/zk-tests/src/Basic.t.sol index f81c65754..3edddbf01 100644 --- a/zk-tests/src/Basic.t.sol +++ b/zk-tests/src/Basic.t.sol @@ -2,7 +2,6 @@ pragma solidity ^0.8.13; import {Test} from "forge-std/Test.sol"; -import {console2} from "forge-std/console2.sol"; contract BlockEnv { uint256 public number; From 3bd5770380e4f0196987931a3622118fd89c4a28 Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Mon, 22 Jul 2024 12:09:44 +0200 Subject: [PATCH 3/5] simplify block hash override --- crates/zksync/core/src/vm/tracer.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/zksync/core/src/vm/tracer.rs b/crates/zksync/core/src/vm/tracer.rs index 4879771a9..2004c2898 100644 --- a/crates/zksync/core/src/vm/tracer.rs +++ b/crates/zksync/core/src/vm/tracer.rs @@ -306,13 +306,12 @@ impl DynTracer> 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()); - } else { - self.farcall_handler.set_immediate_return(rU256::ZERO.to_be_bytes_vec()); - } + 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; } } From 2824ff4a31ae67aab7d0accd006d73caf898715c Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Mon, 22 Jul 2024 12:09:54 +0200 Subject: [PATCH 4/5] update docs --- crates/zksync/core/src/vm/runner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/zksync/core/src/vm/runner.rs b/crates/zksync/core/src/vm/runner.rs index 438c701cb..17da2ae5d 100644 --- a/crates/zksync/core/src/vm/runner.rs +++ b/crates/zksync/core/src/vm/runner.rs @@ -274,7 +274,7 @@ 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(ecx: &mut EvmContext) -> HashMap { let mut block_hashes = HashMap::default(); for i in 1..=256u32 { From 096c31c1b05d98646012ace76cd2b49ea45a54ca Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Mon, 22 Jul 2024 12:10:14 +0200 Subject: [PATCH 5/5] consistent style --- zk-tests/src/Basic.t.sol | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/zk-tests/src/Basic.t.sol b/zk-tests/src/Basic.t.sol index 3edddbf01..f2a2a1886 100644 --- a/zk-tests/src/Basic.t.sol +++ b/zk-tests/src/Basic.t.sol @@ -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); } } @@ -98,18 +98,19 @@ contract ZkBasicTest is Test { ); require( - be.ZkBlockhash(block.number) == blockhash(block.number), - "Blockhash of the current block should be zero" + 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 - 1) == blockhash(block.number - 1), - "Blockhash of the previous block should be equal" + 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.zkBlockhash(0) == blockhash(0), + "blockhash of the genesis block should be equal" ); be = new BlockEnv(); @@ -164,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" ); }