watchBlocks
function indexes by block number, causing issues with reorg simulations
#2110
Unanswered
ameya-deshmukh
asked this question in
Idea / Feature Request
Replies: 1 comment 2 replies
-
Hi @jxom would love if you could take a look into this :) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've encountered an issue while trying to simulate reorgs using Anvil and the watchBlocks function from the public client in Viem. The problem arises because
watchBlocks
seems to index purely byblock.number
, which leads to new blocks in the intended canonical chain not being detected after a reorg.Here's an example scenario:
Start with an initial chain 8 blocks long (blocks 1 to 8).
All blocks are detected with no issues using watchBlocks.
Simulate a 3-block reorg by reverting back to block 3 using anvil_revert.
The new blocks 4', 5', 6', 7', and 8' in the new canonical chain are not detected by watchBlocks.
It appears that watchBlocks indexes the blocks solely by their block number, without considering the block hash or timestamp. As a result, when a reorg occurs, and new blocks with the same block numbers as previously "consumed" blocks are added to the canonical chain, watchBlocks fails to detect and process these new blocks.
This behavior poses challenges when working with reorg simulations, as it becomes difficult to track and respond to the updated state of the chain after a reorg.
To illustrate the issue further, consider:
Initial chain: Blocks 1, 2, 3, 4, 5, 6, 7, 8
After reorg (reverting to block 3): Blocks 1, 2, 3, 4', 5', 6', 7', 8'
In this scenario, blocks 4' to 8' of the new canonical chain are not detected by watchBlocks because they have the same block numbers as the previously "consumed" blocks, even though they have different block hashes and represent a different state of the blockchain.
I believe this is a limitation of the current implementation of
watchBlocks
in Viem, and it would be beneficial to explore alternative approaches or enhancements to better handle reorg scenarios. Some potential solutions could include:Indexing blocks by their hash instead of or in addition to the block number.
Streaming all new incoming blocks based on their timestamp, regardless of the block number.
Providing a mechanism to reset or reconfigure watchBlocks after a reorg event to ensure it captures the updated state of the chain.
I would appreciate any insights, suggestions, or discussions on how to address this issue effectively and improve the experience of working with reorg simulations using Viem and Anvil.
Beta Was this translation helpful? Give feedback.
All reactions