-
Notifications
You must be signed in to change notification settings - Fork 2k
bug(cheatcodes
): blockhash
returns zero value if vm.roll
ed beyond type(uint64).max
value, raise a warning to user
#10367
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
Comments
So this happens because |
@grandizzy @DaniPopes I think Foundry should raise an informative error if |
### 🕓 Changelog This PR adds [EIP-2935](https://eips.ethereum.org/EIPS/eip-2935)-based utility functions. These functions can be used to access the historical block hashes beyond the default `256`-block limit. We use the [EIP-2935](https://eips.ethereum.org/EIPS/eip-2935) history contract, which maintains a ring buffer of the last `8,191` block hashes stored in state. For the blocks within the last `256` blocks, we use the native [`BLOCKHASH`](https://www.evm.codes/?fork=cancun#40) opcode. For blocks between `257` and `8,191` blocks ago, the function `_block_hash` queries via the specified [`get`](https://eips.ethereum.org/EIPS/eip-2935#get) method the [EIP-2935](https://eips.ethereum.org/EIPS/eip-2935) history contract. For blocks older than `8,191` or future blocks (including the current one), we return zero, matching the [`BLOCKHASH`](https://www.evm.codes/?fork=cancun#40) behaviour. It's important to note that the Vyper built-in function [`blockhash`](https://docs.vyperlang.org/en/stable/built-in-functions.html#blockhash) reverts if the block number is more than `256` blocks behind the current block. We explicitly handle this case (i.e. `delta > 8191`) to ensure the function returns an empty `bytes32` value rather than reverting (i.e. exactly matching the [`BLOCKHASH`](https://www.evm.codes/?fork=cancun#40) opcode behaviour). In the stateless fuzzing tests we use `uint64` as upper bound for the `block.number` due to Revm's internal [saturation](https://github.com/bluealloy/revm/blob/b2c789d42d4eee93ce111f1a7d3d0708f1e34180/crates/interpreter/src/instructions/host.rs#L144) of `block.number` to `u64::MAX` (see also my issue [here](foundry-rs/foundry#10367)). If `requested_number >= block_number` (after saturation), Revm returns `U256::ZERO` [early](https://github.com/bluealloy/revm/blob/b2c789d42d4eee93ce111f1a7d3d0708f1e34180/crates/interpreter/src/instructions/host.rs#L148-L157) without querying the database. Eventually, `block.number` is replaced with the [`vm.getBlockNumber()`](https://book.getfoundry.sh/cheatcodes/get-block-number) cheat code in the tests. --------- Signed-off-by: Pascal Marco Caversaccio <[email protected]>
blockhash
returns zero value if roll
ed beyond type(uint64).max
valuecheatcodes
): blockhash
returns zero value if vm.roll
ed beyond type(uint64).max
value, raise a warning to user
Component
Forge
Have you ensured that all of these are up to date?
What version of Foundry are you on?
55802bad5f9068d969df4273b5c2a960332e8e42
What version of Foundryup are you on?
1.0.1
What command(s) is the bug in?
No response
Operating System
Linux
Describe the bug
The correct output should be:
The text was updated successfully, but these errors were encountered: