refactor(head-sync): flatten descendant processing into an iterative drain - #1112
Merged
tcoratger merged 1 commit intoJun 17, 2026
Merged
Conversation
…drain Rework the gossip head follower so the descendant cascade is a single iterative loop instead of mutual recursion through three methods. - Collapse the on_gossip_block return into Store | None. None means the block was cached pending an unknown parent; a store means it was integrated. This deletes the single-bool HeadSyncResult wrapper and the repeated two-channel returns. - Replace the recursion with a deque worklist over block roots. A long backfilled chain can no longer exhaust the stack, and the gossiped block is processed explicitly so a failure is a direct early return. - Delete the reentrancy set. The processed path is now fully synchronous (no awaits), so a concurrent entry for the same root cannot interleave; the already-in-store check is the sufficient guard. - Inline the cache-and-backfill helper into its only caller. - Hash each block once: reuse the gossiped block root and the cached child roots rather than recomputing. - Trim the module, class, field, and inline documentation; correct the service write-back comment, which now describes a true invariant. Tests move from asserting a result wrapper to asserting the store contract directly, and the reentrancy-guard test is removed with the set. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Summary
Rework the gossip head follower so the cached-descendant cascade is a single iterative loop instead of mutual recursion through three methods. The behavior is preserved; the structure, return shape, and docs are simplified.
Changes
Store | Nonereturn.on_gossip_blockreturns the updated store when a block is integrated, orNonewhen it is cached pending an unknown parent. This deletes the single-boolHeadSyncResultwrapper and the repeated two-channel(result, store)returns._process_block_with_descendants+_process_cached_descendants) becomes onedeque[Bytes32]worklist. A long backfilled chain can no longer exhaust the stack. The gossiped block is processed explicitly, so its failure is a direct early return; the loop then drains only cached descendants (each already carries its root and slot).hash_tree_rootper method.Net:
head_sync.pydrops from 379 to ~118 lines — one public method, no helpers, no result wrapper, no recursion, no reentrancy set.Out of scope (flagged for a follow-up)
Review surfaced a liveness question: backfill caches a fetched parent but nothing re-drives the cascade unless that parent is later re-gossiped. Closing it is a behavioral change (backfill → head-sync notification) that needs confirmation of the intended sync state machine, so it is not bundled here.
Testing
just checkpasses.tests/node/sync/test_head_sync.pyandtest_service.pypass. Assertions now check the store contract directly (is None/is not None); the reentrancy-guard test is removed with the set it exercised.🤖 Generated with Claude Code