From c5ae3315aca778e48a3325fcd369e318381f10e8 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:03:26 +0530 Subject: [PATCH 1/6] fix(`anvil`): use header.number not best_number --- crates/anvil/src/eth/backend/mem/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index f24be966c6f9..52afa595ec65 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -1917,7 +1917,7 @@ impl Backend { ) = NamedChain::try_from(self.env.read().env.cfg.chain_id) { // Block number is the best number. - block.header.number = self.best_number(); + block.header.number = number; // Set `l1BlockNumber` field. block.other.insert("l1BlockNumber".to_string(), number.into()); } From 82d9e9704d48463b53d68cc7ddb17ea4fc460922 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:33:24 +0530 Subject: [PATCH 2/6] test --- crates/anvil/src/eth/backend/mem/mod.rs | 2 -- crates/anvil/tests/it/anvil_api.rs | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index 52afa595ec65..590e940e1dc0 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -1916,8 +1916,6 @@ impl Backend { NamedChain::ArbitrumTestnet, ) = NamedChain::try_from(self.env.read().env.cfg.chain_id) { - // Block number is the best number. - block.header.number = number; // Set `l1BlockNumber` field. block.other.insert("l1BlockNumber".to_string(), number.into()); } diff --git a/crates/anvil/tests/it/anvil_api.rs b/crates/anvil/tests/it/anvil_api.rs index 088eb76feae8..a6e36929cf25 100644 --- a/crates/anvil/tests/it/anvil_api.rs +++ b/crates/anvil/tests/it/anvil_api.rs @@ -871,3 +871,20 @@ async fn can_set_executor() { assert_eq!(executor, expected_addr); } + +#[tokio::test(flavor = "multi_thread")] +async fn test_arb_get_block() { + let (api, _handle) = spawn(NodeConfig::test().with_chain_id(Some(421611u64))).await; + + // Mine two blocks + api.mine_one().await; + api.mine_one().await; + + let best_number = api.block_number().unwrap().to::(); + + assert_eq!(best_number, 2); + + let block = api.block_by_number(1.into()).await.unwrap().unwrap(); + + assert_eq!(block.header.number, 1); +} From aa555dc82e86787d04c62b03a47d5d63072d5c13 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:12:20 +0530 Subject: [PATCH 3/6] ignore test_arbitrum_fork_block_number --- crates/anvil/tests/it/fork.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/anvil/tests/it/fork.rs b/crates/anvil/tests/it/fork.rs index e6db8a0635bb..7fd9293f3195 100644 --- a/crates/anvil/tests/it/fork.rs +++ b/crates/anvil/tests/it/fork.rs @@ -1218,6 +1218,7 @@ async fn test_arbitrum_fork_dev_balance() { // #[tokio::test(flavor = "multi_thread")] +#[ignore] async fn test_arbitrum_fork_block_number() { // fork to get initial block for test let (_, handle) = spawn( From a42d4fa6d7429371d4b5b12ec4c7d2cc311d207c Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 21 Oct 2024 18:16:28 +0530 Subject: [PATCH 4/6] fix(`anvil`): miner logic for arb-like chains --- crates/anvil/src/eth/backend/mem/mod.rs | 37 +++++++++++++++++-------- crates/anvil/tests/it/fork.rs | 1 - 2 files changed, 26 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..75a053bcc97d 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,17 @@ pub fn prove_storage(storage: &HashMap, keys: &[B256]) -> Vec bool { + if let Ok( + NamedChain::Arbitrum | + NamedChain::ArbitrumGoerli | + NamedChain::ArbitrumNova | + NamedChain::ArbitrumTestnet, + ) = NamedChain::try_from(chain_id) + { + true + } else { + false + } +} diff --git a/crates/anvil/tests/it/fork.rs b/crates/anvil/tests/it/fork.rs index 7fd9293f3195..e6db8a0635bb 100644 --- a/crates/anvil/tests/it/fork.rs +++ b/crates/anvil/tests/it/fork.rs @@ -1218,7 +1218,6 @@ async fn test_arbitrum_fork_dev_balance() { // #[tokio::test(flavor = "multi_thread")] -#[ignore] async fn test_arbitrum_fork_block_number() { // fork to get initial block for test let (_, handle) = spawn( From 99ec9b36401312c26db55e24bf7707aa582a2a60 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 21 Oct 2024 18:24:21 +0530 Subject: [PATCH 5/6] clippy --- crates/anvil/src/eth/backend/mem/mod.rs | 18 +++++++----------- crates/anvil/tests/it/fork.rs | 1 - 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/crates/anvil/src/eth/backend/mem/mod.rs b/crates/anvil/src/eth/backend/mem/mod.rs index 75a053bcc97d..0414b013cbdd 100644 --- a/crates/anvil/src/eth/backend/mem/mod.rs +++ b/crates/anvil/src/eth/backend/mem/mod.rs @@ -2955,15 +2955,11 @@ pub fn prove_storage(storage: &HashMap, keys: &[B256]) -> Vec bool { - if let Ok( - NamedChain::Arbitrum | - NamedChain::ArbitrumGoerli | - NamedChain::ArbitrumNova | - NamedChain::ArbitrumTestnet, - ) = NamedChain::try_from(chain_id) - { - true - } else { - false - } + 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..e6db8a0635bb 100644 --- a/crates/anvil/tests/it/fork.rs +++ b/crates/anvil/tests/it/fork.rs @@ -1218,7 +1218,6 @@ async fn test_arbitrum_fork_dev_balance() { // #[tokio::test(flavor = "multi_thread")] -#[ignore] async fn test_arbitrum_fork_block_number() { // fork to get initial block for test let (_, handle) = spawn( From 46732f4039ada14b45117a712f6239697f83fa41 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 21 Oct 2024 18:35:01 +0530 Subject: [PATCH 6/6] test --- crates/anvil/tests/it/fork.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/anvil/tests/it/fork.rs b/crates/anvil/tests/it/fork.rs index e6db8a0635bb..d8df936b1e0a 100644 --- a/crates/anvil/tests/it/fork.rs +++ b/crates/anvil/tests/it/fork.rs @@ -1216,6 +1216,27 @@ 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")] async fn test_arbitrum_fork_block_number() {