Skip to content

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
pycckuu:matt/add-note-about-fcu-unwind
Closed

Fix FCU unwind: properly reorg in-memory canonical state and update latest block#17859
pycckuu wants to merge 7 commits intoparadigmxyz:matt/add-note-about-fcu-unwindfrom
pycckuu:matt/add-note-about-fcu-unwind

Conversation

@pycckuu
Copy link
Contributor

@pycckuu pycckuu commented Aug 13, 2025

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

  • tree/mod.rs:
  • Add: 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.
  • Replace header-only update with:
    self.log_chain_update_type(...);
    self.state.tree_state.set_canonical_head(canonical_header.num_hash());
    if new_head_number < current_head_number {
        self.handle_chain_unwind(current_head_number, canonical_header)
    } else {
        self.handle_chain_advance_or_same_height(canonical_header)
    }
  • Reorg on unwind:
    self.canonical_in_memory_state.update_chain(NewCanonicalChain::Reorg { new, old });
    self.canonical_in_memory_state.set_canonical_head(canonical_header.clone());
  • Advance/same-height: ensure block committed into memory if missing.
  • Interim method update_latest_block_to_canonical_ancestor added, then superseded by full unwind handling.
  • tree/tests.rs:
  • New test test_fcu_with_canonical_ancestor_updates_latest_block asserting 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

  • Correct state after unwinds; eliminate nonce/state desync errors.
  • Low overhead; clearer logging.

Test Plan

  • Run: cargo test -p engine_tree test_fcu_with_canonical_ancestor_updates_latest_block.

How the reviewer should test the fix

  • Unwind: head=4 → FCU to 2; verify removal of [3,4] and head=2 in both states.
  • Advance: head=2 → FCU to 3; verify block 3 committed and head updated.
  • Check logs for correct classification.

Other useful info

If ancestor block missing from storage, we warn and still update header to avoid desync.

Closes #17798

SarahADavis and others added 7 commits August 13, 2025 16:52
Signed-off-by: Jack Drogon <jack.xsuperman@gmail.com>
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.
@pycckuu
Copy link
Contributor Author

pycckuu commented Aug 19, 2025

Closing this PR in favor of a rebase on recent changes #17938

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants