Skip to content

Commit

Permalink
fix: force prevrandao on Moonbeam networks (#9489)
Browse files Browse the repository at this point in the history
* chore: force prevrandao

* add test for fix

* fix forge fmt

---------

Co-authored-by: zerosnacks <[email protected]>
Co-authored-by: zerosnacks <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2024
1 parent a4de7e8 commit c161c7c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
11 changes: 9 additions & 2 deletions crates/evm/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use alloy_consensus::BlockHeader;
use alloy_json_abi::{Function, JsonAbi};
use alloy_network::AnyTxEnvelope;
use alloy_primitives::{Address, Selector, TxKind, U256};
use alloy_primitives::{Address, Selector, TxKind, B256, U256};
use alloy_provider::{network::BlockResponse, Network};
use alloy_rpc_types::{Transaction, TransactionRequest};
use foundry_config::NamedChain;
Expand Down Expand Up @@ -34,18 +34,25 @@ pub fn apply_chain_and_block_specific_env_changes<N: Network>(
env: &mut revm::primitives::Env,
block: &N::BlockResponse,
) {
use NamedChain::*;
if let Ok(chain) = NamedChain::try_from(env.cfg.chain_id) {
let block_number = block.header().number();

match chain {
NamedChain::Mainnet => {
Mainnet => {
// after merge difficulty is supplanted with prevrandao EIP-4399
if block_number >= 15_537_351u64 {
env.block.difficulty = env.block.prevrandao.unwrap_or_default().into();
}

return;
}
Moonbeam | Moonbase | Moonriver | MoonbeamDev => {
if env.block.prevrandao.is_none() {
// <https://github.com/foundry-rs/foundry/issues/4232>
env.block.prevrandao = Some(B256::random());
}
}
c if c.is_arbitrum() => {
// on arbitrum `block.number` is the L1 block which is included in the
// `l1BlockNumber` field
Expand Down
3 changes: 3 additions & 0 deletions crates/forge/tests/it/repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ test_repro!(3753);
// https://github.com/foundry-rs/foundry/issues/3792
test_repro!(3792);

// https://github.com/foundry-rs/foundry/issues/4232
test_repro!(4232);

// https://github.com/foundry-rs/foundry/issues/4402
test_repro!(4402);

Expand Down
1 change: 1 addition & 0 deletions crates/forge/tests/it/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ pub fn rpc_endpoints() -> RpcEndpoints {
("arbitrum", RpcEndpoint::Url(next_rpc_endpoint(NamedChain::Arbitrum))),
("polygon", RpcEndpoint::Url(next_rpc_endpoint(NamedChain::Polygon))),
("avaxTestnet", RpcEndpoint::Url("https://api.avax-test.network/ext/bc/C/rpc".into())),
("moonbeam", RpcEndpoint::Url("https://moonbeam-rpc.publicnode.com".into())),
("rpcEnvAlias", RpcEndpoint::Env("${RPC_ENV_ALIAS}".into())),
])
}
Expand Down
31 changes: 31 additions & 0 deletions testdata/default/repros/Issue4232.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;

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

// https://github.com/foundry-rs/foundry/issues/4232
contract Issue4232Test is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testFork() public {
// Smoke test, worked previously as well
vm.createSelectFork("sepolia", 7215400);
vm.assertFalse(block.prevrandao == 0);

// Would previously fail with:
// [FAIL: backend: failed while inspecting; header validation error: `prevrandao` not set; `prevrandao` not set; ] setUp() (gas: 0)
//
// Related fix:
// Moonbeam | Moonbase | Moonriver | MoonbeamDev => {
// if env.block.prevrandao.is_none() {
// // <https://github.com/foundry-rs/foundry/issues/4232>
// env.block.prevrandao = Some(B256::random());
// }
// }
//
// Note: public RPC node used for `moonbeam` discards state quickly so we need to fork against the latest block
vm.createSelectFork("moonbeam");
vm.assertFalse(block.prevrandao == 0);
}
}

0 comments on commit c161c7c

Please sign in to comment.