Skip to content

Commit dc23de3

Browse files
authored
fix(anvil): apply Arbitrum specifics to API block (#8542)
1 parent 9b1c48d commit dc23de3

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use anvil_core::eth::{
6767
};
6868
use anvil_rpc::error::RpcError;
6969

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

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

@@ -1676,7 +1677,7 @@ impl Backend {
16761677
parent_beacon_block_root,
16771678
} = header;
16781679

1679-
AlloyBlock {
1680+
let mut block = AlloyBlock {
16801681
header: AlloyHeader {
16811682
hash: Some(hash),
16821683
parent_hash,
@@ -1709,7 +1710,23 @@ impl Backend {
17091710
uncles: vec![],
17101711
withdrawals: None,
17111712
other: Default::default(),
1712-
}
1713+
};
1714+
1715+
// If Arbitrum, apply chain specifics to converted block.
1716+
if let Ok(
1717+
NamedChain::Arbitrum |
1718+
NamedChain::ArbitrumGoerli |
1719+
NamedChain::ArbitrumNova |
1720+
NamedChain::ArbitrumTestnet,
1721+
) = NamedChain::try_from(self.env.read().env.cfg.chain_id)
1722+
{
1723+
// Block number is the best number.
1724+
block.header.number = Some(self.best_number());
1725+
// Set `l1BlockNumber` field.
1726+
block.other.insert("l1BlockNumber".to_string(), number.into());
1727+
}
1728+
1729+
block
17131730
}
17141731

17151732
/// Converts the `BlockNumber` into a numeric value

crates/anvil/tests/it/fork.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,11 @@ async fn test_arbitrum_fork_block_number() {
11651165
let block_number = api.block_number().unwrap().to::<u64>();
11661166
assert_eq!(block_number, initial_block_number + 1);
11671167

1168+
// test block by number API call returns proper block number and `l1BlockNumber` is set
1169+
let block_by_number = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap();
1170+
assert_eq!(block_by_number.header.number.unwrap(), initial_block_number + 1);
1171+
assert!(block_by_number.other.get("l1BlockNumber").is_some());
1172+
11681173
// revert to recorded snapshot and check block number
11691174
assert!(api.evm_revert(snapshot).await.unwrap());
11701175
let block_number = api.block_number().unwrap().to::<u64>();

0 commit comments

Comments
 (0)