Skip to content

refactor(head-sync): flatten descendant processing into an iterative drain - #1112

Merged
tcoratger merged 1 commit into
leanEthereum:mainfrom
tcoratger:refactor/head-sync-worklist
Jun 17, 2026
Merged

refactor(head-sync): flatten descendant processing into an iterative drain#1112
tcoratger merged 1 commit into
leanEthereum:mainfrom
tcoratger:refactor/head-sync-worklist

Conversation

@tcoratger

Copy link
Copy Markdown
Collaborator

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 | None return. on_gossip_block returns the updated store when a block is integrated, or None when it is cached pending an unknown parent. This deletes the single-bool HeadSyncResult wrapper and the repeated two-channel (result, store) returns.
  • Iterative drain. The recursion (_process_block_with_descendants + _process_cached_descendants) becomes one deque[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).
  • Reentrancy set removed. 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. This also makes the sync service's write-back comment describe a true invariant rather than a fragile one.
  • Inlined the single-use cache-and-backfill helper into its only caller.
  • Hash once. Reuse the gossiped block root and the cached child roots instead of recomputing hash_tree_root per method.
  • Docs trimmed to the project's terseness bar (module, class, fields, inline comments), keeping the genuinely non-obvious rationale (finalized-slot floor, backfill routing dichotomy).

Net: head_sync.py drops 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 check passes.
  • tests/node/sync/test_head_sync.py and test_service.py pass. 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

…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>
@tcoratger
tcoratger merged commit b0360e2 into leanEthereum:main Jun 17, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant