From 1af44bf750e6c3917dcdcaf8f853a44aacb061ad Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 21 Oct 2024 19:25:31 +0530 Subject: [PATCH] fix(`anvil`): arb fork mining (#9153) * fix(`anvil`): use header.number not best_number * test * ignore test_arbitrum_fork_block_number * fix(`anvil`): miner logic for arb-like chains * clippy * test --- crates/anvil/src/eth/backend/mem/mod.rs | 33 ++++++++++++++++--------- crates/anvil/tests/it/fork.rs | 22 ++++++++++++++++- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index 590e940e1dc0..0414b013cbdd 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -1098,8 +1098,17 @@ impl Backend { env.cfg.disable_base_fee = true; } + let block_number = + self.blockchain.storage.read().best_number.saturating_add(U64::from(1)); + // increase block number for this block - env.block.number = env.block.number.saturating_add(U256::from(1)); + if is_arbitrum(env.cfg.chain_id) { + // Temporary set `env.block.number` to `block_number` for Arbitrum chains. + env.block.number = block_number.to(); + } else { + env.block.number = env.block.number.saturating_add(U256::from(1)); + } + env.block.basefee = U256::from(current_base_fee); env.block.blob_excess_gas_and_price = current_excess_blob_gas_and_price; @@ -1149,9 +1158,7 @@ impl Backend { let ExecutedTransactions { block, included, invalid } = executed_tx; let BlockInfo { block, transactions, receipts } = block; - let mut storage = self.blockchain.storage.write(); let header = block.header.clone(); - let block_number = storage.best_number.saturating_add(U64::from(1)); trace!( target: "backend", @@ -1160,7 +1167,7 @@ impl Backend { transactions.len(), transactions.iter().map(|tx| tx.transaction_hash).collect::>() ); - + let mut storage = self.blockchain.storage.write(); // update block metadata storage.best_number = block_number; storage.best_hash = block_hash; @@ -1909,13 +1916,7 @@ impl Backend { let mut block = WithOtherFields::new(block); // If Arbitrum, apply chain specifics to converted block. - if let Ok( - NamedChain::Arbitrum | - NamedChain::ArbitrumGoerli | - NamedChain::ArbitrumNova | - NamedChain::ArbitrumTestnet, - ) = NamedChain::try_from(self.env.read().env.cfg.chain_id) - { + if is_arbitrum(self.env.read().cfg.chain_id) { // Set `l1BlockNumber` field. block.other.insert("l1BlockNumber".to_string(), number.into()); } @@ -2952,3 +2953,13 @@ pub fn prove_storage(storage: &HashMap, keys: &[B256]) -> Vec bool { + matches!( + NamedChain::try_from(chain_id), + Ok(NamedChain::Arbitrum | + NamedChain::ArbitrumTestnet | + NamedChain::ArbitrumGoerli | + NamedChain::ArbitrumNova) + ) +} diff --git a/crates/anvil/tests/it/fork.rs b/crates/anvil/tests/it/fork.rs index 7fd9293f3195..d8df936b1e0a 100644 --- a/crates/anvil/tests/it/fork.rs +++ b/crates/anvil/tests/it/fork.rs @@ -1216,9 +1216,29 @@ async fn test_arbitrum_fork_dev_balance() { } } +// +#[tokio::test(flavor = "multi_thread")] +async fn test_arb_fork_mining() { + let fork_block_number = 266137031u64; + let fork_rpc = next_rpc_endpoint(NamedChain::Arbitrum); + let (api, _handle) = spawn( + fork_config() + .with_fork_block_number(Some(fork_block_number)) + .with_eth_rpc_url(Some(fork_rpc)), + ) + .await; + + let init_blk_num = api.block_number().unwrap().to::(); + + // Mine one + api.mine_one().await; + let mined_blk_num = api.block_number().unwrap().to::(); + + assert_eq!(mined_blk_num, init_blk_num + 1); +} + // #[tokio::test(flavor = "multi_thread")] -#[ignore] async fn test_arbitrum_fork_block_number() { // fork to get initial block for test let (_, handle) = spawn(