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): apply Arbitrum specifics to API block #8542

Merged
merged 1 commit into from
Jul 29, 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
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 @@ -1640,7 +1641,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 @@ -1671,7 +1672,7 @@ impl Backend {
parent_beacon_block_root,
} = header;

AlloyBlock {
let mut block = AlloyBlock {
header: AlloyHeader {
hash: Some(hash),
parent_hash,
Expand Down Expand Up @@ -1704,7 +1705,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
Loading