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): use header.number not best_number #9151

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ impl Backend {
) = NamedChain::try_from(self.env.read().env.cfg.chain_id)
{
// Block number is the best number.
block.header.number = self.best_number();
block.header.number = number;
Copy link
Collaborator

@grandizzy grandizzy Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the proper fix would be

            if self.is_fork() {
                // Block number is the best number.
                block.header.number = self.best_number();
            }

and a test for could be

// <https://github.com/foundry-rs/foundry/issues/9143>
#[tokio::test(flavor = "multi_thread")]
async fn test_arbitrum_non_fork_block_number() {
    let (api, _) =
        spawn(NodeConfig::test().with_chain_id(42161u64.into())).await;
    // Mine blocks and check properly returned by `eth_getBlockByNumber` API.
    api.mine_one().await;
    api.mine_one().await;
    let first_block = api.block_by_number(1.into()).await.unwrap().unwrap();
    assert_eq!(1, first_block.header.number);
    let latest_block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap();
    assert_eq!(2, latest_block.header.number);
}

Copy link
Member Author

@yash-atreya yash-atreya Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Wouldn't that cause the same issue in fork mode?

E.g:
If anvil was forked at X block number and then two blocks were mined i.e self.best_number() == X + 2.

We call eth_getBlockByNumber(X), the resultant block.number field would be X + 2 whereas it should be X.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing something?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, I guess you're right, should be the same

// Set `l1BlockNumber` field.
block.other.insert("l1BlockNumber".to_string(), number.into());
}
Expand Down
Loading