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(anvil): arb fork mining #9153

Merged
merged 7 commits into from
Oct 21, 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
33 changes: 22 additions & 11 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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",
Expand All @@ -1160,7 +1167,7 @@ impl Backend {
transactions.len(),
transactions.iter().map(|tx| tx.transaction_hash).collect::<Vec<_>>()
);

let mut storage = self.blockchain.storage.write();
// update block metadata
storage.best_number = block_number;
storage.best_hash = block_hash;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -2952,3 +2953,13 @@ pub fn prove_storage(storage: &HashMap<U256, U256>, keys: &[B256]) -> Vec<Vec<By

proofs
}

pub fn is_arbitrum(chain_id: u64) -> bool {
matches!(
NamedChain::try_from(chain_id),
Ok(NamedChain::Arbitrum |
NamedChain::ArbitrumTestnet |
NamedChain::ArbitrumGoerli |
NamedChain::ArbitrumNova)
)
}
22 changes: 21 additions & 1 deletion crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,9 +1216,29 @@ async fn test_arbitrum_fork_dev_balance() {
}
}

// <https://github.com/foundry-rs/foundry/issues/9152>
#[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::<u64>();

// Mine one
api.mine_one().await;
let mined_blk_num = api.block_number().unwrap().to::<u64>();

assert_eq!(mined_blk_num, init_blk_num + 1);
}

// <https://github.com/foundry-rs/foundry/issues/6749>
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_arbitrum_fork_block_number() {
// fork to get initial block for test
let (_, handle) = spawn(
Expand Down
Loading