Fix FCU unwind: properly reorg in-memory canonical state and update latest block#17859
Closed
pycckuu wants to merge 7 commits intoparadigmxyz:matt/add-note-about-fcu-unwindfrom
Closed
Conversation
Signed-off-by: Jack Drogon <jack.xsuperman@gmail.com>
…ers with extra context (paradigmxyz#17821)
When a `forkchoiceUpdated` call points to a canonical ancestor of the current head (an unwind scenario), the engine's internal state for the "latest" block was not being reverted to match. This caused a state desynchronization where components like the transaction pool would operate on a stale, more advanced block state. This could lead to errors, such as "nonce too low", when validating new transactions against the incorrect (post-reorg) state. This commit introduces the `update_latest_block_to_canonical_ancestor` method, which is now called during FCU processing. This function ensures that both the `TreeState` and the `CanonicalInMemoryState` are correctly updated to the new head, resolving the state inconsistency after a reorg.
The previous implementation only updated the canonical head header on a forkchoice update. This was insufficient for handling reorgs where the new head is an ancestor of the current head (an unwind). This discrepancy could lead to a stale in-memory state. When the state provider attempted to access account information, it would fall back to the stale database state, causing transaction validation failures such as "nonce too low" errors. This commit refactors the logic to correctly handle unwinds. It now detects when the new head number is lower than the current one and treats it as a reorg. It collects the now-invalid blocks from the in-memory state and updates the chain by removing them and loading the new canonical ancestor. This ensures the in-memory state accurately reflects the canonical chain after a reorg.
Contributor
Author
|
Closing this PR in favor of a rebase on recent changes #17938 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix FCU when new head is an ancestor (unwind). Previously we only updated the header, leaving in-memory canonical state stale, causing tx validation issues (e.g., "nonce too low"). Now we detect unwind, reorg in-memory state (remove old blocks, load ancestor), and keep TreeState and CanonicalInMemoryState in sync.
Code Changes
log_chain_update_type,handle_chain_unwind,collect_blocks_for_removal,apply_canonical_ancestor_via_reorg,handle_chain_advance_or_same_height,ensure_block_in_memory.update_latest_block_to_canonical_ancestoradded, then superseded by full unwind handling.test_fcu_with_canonical_ancestor_updates_latest_blockasserting both TreeState and in-memory head match the ancestor after FCU.Reason for Changes
Correctness: prevent stale in-memory state on reorgs that led to txpool/state provider errors.
Impact of Changes
Test Plan
cargo test -p engine_tree test_fcu_with_canonical_ancestor_updates_latest_block.How the reviewer should test the fix
Other useful info
If ancestor block missing from storage, we warn and still update header to avoid desync.
Closes #17798