Skip to content

Commit

Permalink
fix: ensure prevrandao is set on reset fork (#6488)
Browse files Browse the repository at this point in the history
* fix: ensure prevrandao is set on reset fork

* rustfmt
  • Loading branch information
mattsse authored Dec 1, 2023
1 parent 8f50260 commit f17d00b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,12 @@ impl Backend {
timestamp: fork_block.timestamp.to_alloy(),
gas_limit: fork_block.gas_limit.to_alloy(),
difficulty: fork_block.difficulty.to_alloy(),
prevrandao: fork_block.mix_hash.map(|h| h.to_alloy()),
// ensures prevrandao is set
prevrandao: Some(fork_block.mix_hash.map(|h| h.to_alloy()).unwrap_or_default()),
// Keep previous `coinbase` and `basefee` value
coinbase: env.block.coinbase,
basefee: env.block.basefee,
..Default::default()
..env.block.clone()
};

self.time.reset((env.block.timestamp.to_ethers()).as_u64());
Expand Down
32 changes: 32 additions & 0 deletions crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,3 +944,35 @@ async fn can_override_fork_chain_id() {
let chain_id = provider.get_chainid().await.unwrap();
assert_eq!(chain_id.as_u64(), chain_id_override);
}

// <https://github.com/foundry-rs/foundry/issues/6485>
#[tokio::test(flavor = "multi_thread")]
async fn test_fork_reset_moonbeam() {
crate::init_tracing();
let (api, handle) = spawn(
fork_config()
.with_eth_rpc_url(Some("https://rpc.api.moonbeam.network".to_string()))
.with_fork_block_number(None::<u64>),
)
.await;
let provider = handle.http_provider();

let accounts: Vec<_> = handle.dev_wallets().collect();
let from = accounts[0].address();

let tx = TransactionRequest::new().to(Address::random()).value(1337u64).from(from);
let tx = provider.send_transaction(tx, None).await.unwrap().await.unwrap().unwrap();
assert_eq!(tx.status, Some(1u64.into()));

// reset to check timestamp works after resetting
api.anvil_reset(Some(Forking {
json_rpc_url: Some("https://rpc.api.moonbeam.network".to_string()),
block_number: None,
}))
.await
.unwrap();

let tx = TransactionRequest::new().to(Address::random()).value(1337u64).from(from);
let tx = provider.send_transaction(tx, None).await.unwrap().await.unwrap().unwrap();
assert_eq!(tx.status, Some(1u64.into()));
}

0 comments on commit f17d00b

Please sign in to comment.