Skip to content

The state at a block inside a hardhat_mine range is empty #1859

Open

Description

@aelmanaa

After hardhat_mine mines 10 or more blocks, the state at the blocks in the middle of the range is empty. Balances, nonces, code and storage all read as zero. The state at the last block of the range, and at blocks mined before the range, is correct.

Reproduced with hardhat 3.20.0 (EDR 0.23.0) and npx hardhat node, using the first default account A0:

eth_sendTransaction {"from": A0, "to": "0x…aa", "value": "0x7"}   # block 1
hardhat_mine "0x3e8"                                               # blocks 2 to 1001

eth_getBalance A0 "0x1f4"              → "0x0"   (expected 10000 ETH minus the gas of the first transaction)
eth_getTransactionCount A0 "0x1f4"     → "0x0"   (expected "0x1")
eth_getBalance "0x…aa" "0x1f4"         → "0x0"   (expected "0x7")
eth_getBalance A0 "latest"             → "0x21e19e0b49405035461"

With hardhat_mine "0x5", the same queries at a block inside the range are correct. When fewer than 6 blocks remain, they are mined one by one instead of reserved.

The cause is in ReservableSparseBlockStorage:

  1. hardhat_mine reserves the blocks in the middle of the range.
  2. Any query that names a reserved block reads it. The read creates the block and splits its reservation around it (try_fulfilling_reservation).
  3. The block's number is now in neither number_to_diff_index nor a reservation. state_diffs_until_block returns None, and compute_state_at_block applies no diffs at all (unwrap_or_default).

The same lookup makes revert_to_block panic for such a block, with "There must either be a block or a reservation matching the block number".

I have a fix with tests. state_diffs is ordered by block number, so the diffs that apply to a block are those of the blocks up to its number. I can open a PR if that works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Inbox

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions