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: force prevrandao on Moonbeam networks #9489

Merged
merged 5 commits into from
Dec 5, 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
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);
}
}
Loading