Skip to content

Commit 1cdd8cf

Browse files
committed
fix(anvil): apply Arbitrum specifics to API block
1 parent bdd1137 commit 1cdd8cf

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},
@@ -1640,7 +1641,7 @@ impl Backend {
16401641
Some(block.into_full_block(transactions.into_iter().map(|t| t.inner).collect()))
16411642
}
16421643

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

@@ -1671,7 +1672,7 @@ impl Backend {
16711672
parent_beacon_block_root,
16721673
} = header;
16731674

1674-
AlloyBlock {
1675+
let mut block = AlloyBlock {
16751676
header: AlloyHeader {
16761677
hash: Some(hash),
16771678
parent_hash,
@@ -1704,7 +1705,23 @@ impl Backend {
17041705
uncles: vec![],
17051706
withdrawals: None,
17061707
other: Default::default(),
1707-
}
1708+
};
1709+
1710+
// If Arbitrum, apply chain specifics to converted block.
1711+
if let Ok(
1712+
NamedChain::Arbitrum |
1713+
NamedChain::ArbitrumGoerli |
1714+
NamedChain::ArbitrumNova |
1715+
NamedChain::ArbitrumTestnet,
1716+
) = NamedChain::try_from(self.env.read().env.cfg.chain_id)
1717+
{
1718+
// Block number is the best number.
1719+
block.header.number = Some(self.best_number());
1720+
// Set `l1BlockNumber` field.
1721+
block.other.insert("l1BlockNumber".to_string(), number.into());
1722+
}
1723+
1724+
block
17081725
}
17091726

17101727
/// 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)