Skip to content

Commit

Permalink
fix(anvil): apply Arbitrum specifics to API block (foundry-rs#8542)
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy authored and benwjhack committed Sep 11, 2024
1 parent c7f26e8 commit 495bfb1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 20 additions & 3 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use anvil_core::eth::{
};
use anvil_rpc::error::RpcError;

use alloy_chains::NamedChain;
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use foundry_evm::{
backend::{DatabaseError, DatabaseResult, RevertSnapshotAction},
Expand Down Expand Up @@ -1645,7 +1646,7 @@ impl Backend {
Some(block.into_full_block(transactions.into_iter().map(|t| t.inner).collect()))
}

/// Takes a block as it's stored internally and returns the eth api conform block format
/// Takes a block as it's stored internally and returns the eth api conform block format.
pub fn convert_block(&self, block: Block) -> AlloyBlock {
let size = U256::from(alloy_rlp::encode(&block).len() as u32);

Expand Down Expand Up @@ -1676,7 +1677,7 @@ impl Backend {
parent_beacon_block_root,
} = header;

AlloyBlock {
let mut block = AlloyBlock {
header: AlloyHeader {
hash: Some(hash),
parent_hash,
Expand Down Expand Up @@ -1709,7 +1710,23 @@ impl Backend {
uncles: vec![],
withdrawals: None,
other: Default::default(),
}
};

// 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)
{
// Block number is the best number.
block.header.number = Some(self.best_number());
// Set `l1BlockNumber` field.
block.other.insert("l1BlockNumber".to_string(), number.into());
}

block
}

/// Converts the `BlockNumber` into a numeric value
Expand Down
5 changes: 5 additions & 0 deletions crates/anvil/tests/it/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,11 @@ async fn test_arbitrum_fork_block_number() {
let block_number = api.block_number().unwrap().to::<u64>();
assert_eq!(block_number, initial_block_number + 1);

// test block by number API call returns proper block number and `l1BlockNumber` is set
let block_by_number = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap();
assert_eq!(block_by_number.header.number.unwrap(), initial_block_number + 1);
assert!(block_by_number.other.get("l1BlockNumber").is_some());

// revert to recorded snapshot and check block number
assert!(api.evm_revert(snapshot).await.unwrap());
let block_number = api.block_number().unwrap().to::<u64>();
Expand Down

0 comments on commit 495bfb1

Please sign in to comment.