Skip to content

Fix reorg recovery erasing the whole DB to genesis - #7

Open
thomas-kroes wants to merge 2 commits into
mainfrom
fix/reorg-common-ancestor
Open

thomas-kroes wants to merge 2 commits into
mainfrom
fix/reorg-common-ancestor

Conversation

@thomas-kroes

Copy link
Copy Markdown
Contributor

Summary

Fixes #5 — any real reorg caused chain_reorged_impl to walk %elders all the way down and revert the DB to height 0, as seen on the height-112,500 mainnet fork.

Two defects combined (both confirmed against +get-elders in dumbnet's consensus.hoon):

  • The elders peek returns the height of the oldest entry with ids ordered newest → oldest, but the local comparison window was anchored at that height, so every compared pair was offset by len − 1 blocks and a match was essentially impossible.
  • Even on a match, break only exited the inner for; the outer while kept walking to genesis and the fall-through revert_to(0) wiped the database.

Fix

Every elder id is a block on the node's canonical chain, and block ids are unique hashes — so no height alignment is needed at all: the highest local block whose id appears in the elders batch is the common ancestor. Each batch is now checked with a single indexed id IN (...) lookup (primary key), and on a match we immediately revert_to(ancestor.height + 1). If a batch has no overlap, the walk continues from the oldest elder; empty or non-descending responses error out (UnableToPullElders) instead of looping forever. Reverting to genesis now only happens when the chains genuinely share no blocks.

Test plan

  • New regression tests with a mock Scryable that mirrors +get-elders semantics (newest → oldest ids, oldest height returned, 24-entry cap, genesis stop):
    • shallow reorg resolved within one elders batch
    • deep reorg walking multiple batches (the 112,500-fork shape)
    • no common ancestor → full revert to genesis
  • cargo test --features binary --all-targets (31 passed)
  • cargo clippy --features binary --all-targets (no new warnings), cargo fmt --check

The elders peek returns the height of the *oldest* entry with ids
ordered newest -> oldest, but chain_reorged_impl anchored the local
comparison window at that height, so every zip pair was offset by
(len - 1) blocks and a common ancestor could never match. On top of
that, `break` on a match only exited the inner loop, so the walk always
continued to genesis and reverted the entire database.

Since every elder id is a block on the node's canonical chain and block
ids are unique, the highest local block appearing among the elders is
the common ancestor: find it with an indexed id lookup and revert to
just above it. Bail out on empty or non-descending elders responses
instead of walking forever.

Fixes #5
@cursor

cursor Bot commented Aug 4, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

This branch has not been deployed

No deployments
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.

Reorg handling wipes the entire DB to genesis (elders response misaligned; walk never terminates on match)

1 participant