Does Hardhat Network's evm_revert restore block metadata when automine is disabled?
0 reputation · 17 Aug 2021, 01:05 UTC
0 reputation · 17 Aug 2021, 01:05 UTC
Testing upgrade scenarios with Hardhat Network often relies on evm_snapshot and evm_revert to isolate state changes between test cases. The documentation confirms that account balances, contract storage, and nonces are restored, but the behavior for blockchain metadata—block number, timestamp, difficulty, and coinbase—is not explicitly guaranteed across all mining configurations.
When automine is disabled, transactions sit in the mempool until an explicit mine call. A snapshot taken after queuing a transaction but before mining captures a state that includes pending transactions. It is unclear whether evm_revert in this mode also rewinds the block header fields to their values at the snapshot point, or whether those fields continue advancing from the current head while only the EVM state rolls back.
This distinction matters for tests that assert specific block timestamps or numbers after a revert, and for any logic that reads block.number or block.timestamp during upgrade simulations.
Does evm_revert reset block metadata to the snapshot point when automine is disabled? If metadata is not restored, which fields diverge and under what conditions? Can the behavior be controlled through Hardhat Network configuration or is it fixed per version?
35275 reputation · 17 Aug 2021, 05:03 UTC
Yes. Under standard Hardhat Network behavior, evm_revert restores the blockchain state—including block header metadata such as block.number, block.timestamp, block.difficulty, and block.coinbase—to the values captured at the evm_snapshot point. This applies whether automine is enabled or disabled.
With automine off, the network does not create a new block for each transaction. The chain head stays at the last mined block until you explicitly call evm_mine (or an equivalent mining method). Because the head is frozen, a snapshot taken while transactions are pending records that frozen header state. When you later call evm_revert, the provider rolls the chain back to that exact header configuration and also restores or removes pending mempool transactions to match the snapshot moment.
The key distinction is that block metadata does not continue advancing independently of the EVM state during a revert. If you mine blocks after taking a snapshot and then revert, the block number and timestamp return to the snapshot values rather than remaining at the higher, post-mine height.
The rollback of header fields and mempool contents is the documented and expected behavior of Hardhat Network’s snapshot mechanism. However, the exact set of restored fields and the interaction with custom provider wrappers can vary by Hardhat version. If your tests rely on precise header values after a revert, pin the Hardhat version and avoid provider-level plugins that mutate block height outside the core network.
You can confirm the behavior with these JSON-RPC calls against a local Hardhat node:
// 1. Disable automine in hardhat.config.js
// networks: { hardhat: { mining: { auto: false } } }
// 2. Take a snapshot and record the current block number
await provider.send('evm_snapshot');
const before = await provider.send('eth_blockNumber');
// 3. Queue a transaction (it stays pending) and mine a block
await provider.send('eth_sendTransaction', [tx]);
await provider.send('evm_mine');
const afterMine = await provider.send('eth_blockNumber');
// 4. Revert and compare
await provider.send('evm_revert', [snapshotId]);
const afterRevert = await provider.send('eth_blockNumber');
// afterRevert should equal before, not afterMine
Also inspect txpool status (if exposed by your provider) before and after the revert to confirm pending transactions are restored or dropped according to the snapshot.
If you observe metadata that does not return to the snapshot values, the next diagnostic detail that changes the recommendation is the exact Hardhat version and whether any custom network provider or middleware is intercepting evm_revert. Core Hardhat Network should restore headers automatically; divergence usually points to an external layer.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.