Skip to content

Commit

Permalink
fix: set block.number to l1BlockNumber on arbitrum (foundry-rs#4669)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Mar 30, 2023
1 parent 79ab19d commit abd83de
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
13 changes: 12 additions & 1 deletion evm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub fn apply_chain_and_block_specific_env_changes<T>(env: &mut revm::Env, block:
if let Ok(chain) = Chain::try_from(env.cfg.chain_id) {
let block_number = block.number.unwrap_or_default();

#[allow(clippy::single_match)]
match chain {
Chain::Mainnet => {
// after merge difficulty is supplanted with prevrandao EIP-4399
Expand All @@ -49,6 +48,18 @@ pub fn apply_chain_and_block_specific_env_changes<T>(env: &mut revm::Env, block:

return
}
Chain::Arbitrum |
Chain::ArbitrumGoerli |
Chain::ArbitrumNova |
Chain::ArbitrumTestnet => {
// on arbitrum `block.number` is the L1 block which is included in the
// `l1BlockNumber` field
if let Some(l1_block_number) = block.other.get("l1BlockNumber").cloned() {
if let Ok(l1_block_number) = serde_json::from_value::<U256>(l1_block_number) {
env.block.number = l1_block_number;
}
}
}
_ => {}
}
}
Expand Down
6 changes: 6 additions & 0 deletions forge/tests/it/repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ fn test_issue_2984() {
test_repro!("Issue2984");
}

// <https://github.com/foundry-rs/foundry/issues/4640>
#[test]
fn test_issue_4640() {
test_repro!("Issue4640");
}

// <https://github.com/foundry-rs/foundry/issues/3077>
#[test]
fn test_issue_3077() {
Expand Down
2 changes: 1 addition & 1 deletion forge/tests/it/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub static COMPILED_WITH_LIBS: Lazy<ProjectCompileOutput> = Lazy::new(|| {
pub static EVM_OPTS: Lazy<EvmOpts> = Lazy::new(|| EvmOpts {
env: Env {
gas_limit: 18446744073709551615,
chain_id: Some(foundry_common::DEV_CHAIN_ID),
chain_id: None,
tx_origin: Config::DEFAULT_SENDER,
block_number: 1,
block_timestamp: 1,
Expand Down
17 changes: 17 additions & 0 deletions testdata/repros/Issue4640.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;

import "ds-test/test.sol";
import "../cheats/Cheats.sol";

// https://github.com/foundry-rs/foundry/issues/4640
contract Issue4640Test is DSTest {
Cheats constant vm = Cheats(HEVM_ADDRESS);

function testArbitrumBlockNumber() public {
// <https://arbiscan.io/block/75219831>
vm.createSelectFork("https://rpc.ankr.com/arbitrum", 75219831);
// L1 block number
assertEq(block.number, 16939475);
}
}

0 comments on commit abd83de

Please sign in to comment.