Skip to content

Conversation

alexghr
Copy link
Contributor

@alexghr alexghr commented Jun 5, 2025

Motivation

Our tests sometimes flake with a BlockRangeOutOfRangeError when calling eth_call with the latest block tag. IIUC, this should not be possible.

I had a look at the code in order to see if a newer version of Anvil would fix the flake. I think there is a race condition in this code if the call is made right as a block was being produced:

  pub async fn ensure_block_number<T: Into<BlockId>>(More actions
        &self,
        block_id: Option<T>,
    ) -> Result<u64, BlockchainError> {
        let current = self.best_number(); // <- returns 20
        let requested =
            match block_id.map(Into::into).unwrap_or(BlockId::Number(BlockNumber::Latest)) {
                // ...
                BlockId::Number(num) => match num {
                    BlockNumber::Latest | BlockNumber::Pending => self.best_number(), // <- returns 21
                    // ...
                },
            };

        if requested > current { // if 21 > 20
            Err(BlockchainError::BlockOutOfRange(current, requested))
        } else {
            Ok(requested)
        }
  }

See issue #10709 for context

Solution

Instead of calling self.best_number() twice, which could return different values if a block is built at the same time, the code now uses the pinned block at the beginning of the function.

I'm not sure how best to test this fix since this is code is heavily dependent on async operations. I'd appreciate some help here :)

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Fix #10709

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

thank you, ooh, right, race could happen, not sure though how to test. CC @mattsse @yash-atreya mind to have a look before merging? thanks!

@grandizzy grandizzy changed the title fix: guard against the blockchain advancing while checking latest block fix(anvil): guard against the blockchain advancing while checking latest block Jun 5, 2025
@grandizzy grandizzy added T-bug Type: bug C-anvil Command: anvil labels Jun 5, 2025
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

ah this makes sense, ty!

@mattsse mattsse merged commit f914e01 into foundry-rs:master Jun 5, 2025
22 checks passed
@github-project-automation github-project-automation bot moved this to Done in Foundry Jun 5, 2025
github-merge-queue bot pushed a commit to AztecProtocol/aztec-packages that referenced this pull request Jun 5, 2025
Temporarily mark this Error as a flake. Once a nightly release
containing foundry-rs/foundry#10714 is released we should update the
anvil version used and remove this flake definition.
grandizzy pushed a commit to grandizzy/foundry that referenced this pull request Jun 5, 2025
…est block (foundry-rs#10714)

fix: guard against the blockchain advancing while checking latest block
grandizzy added a commit that referenced this pull request Jun 5, 2025
…est block (backport to v1.2.3) (#10716)

fix(anvil): guard against the blockchain advancing while checking latest block (#10714)

fix: guard against the blockchain advancing while checking latest block

Co-authored-by: Alex Gherghisan <alexghr@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-anvil Command: anvil T-bug Type: bug
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

BlockOutOfRangeError when called with latest for the block
3 participants