fix(node): keep slot_index canonical across side-branch imports and reorgs - #1202
Open
adust09 wants to merge 1 commit into
Open
fix(node): keep slot_index canonical across side-branch imports and reorgs#1202adust09 wants to merge 1 commit into
adust09 wants to merge 1 commit into
Conversation
…eorgs The storage interface documents get_block_root_by_slot as returning the canonical block root at a slot, but _persist_block wrote the index for every imported block: a side-branch import silently overwrote the canonical entry at its slot, and nothing repaired the index when the head moved. Track the head the database last committed and diff it against the store's head inside the persistence batch: slots on the new canonical branch are (re)written, slots only the old branch filled are deleted, and a block that did not move the head no longer touches the index. Closes leanEthereum#1201
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
Closes #1201.
get_block_root_by_slotdocumentsslot_indexas returning the canonical block root at a slot, but_persist_blockwroteput_block_root_by_slot(block.slot, block_root)for every imported block. Because the table is keyed byslot INTEGER PRIMARY KEYwithINSERT OR REPLACE, a side-branch import silently overwrote the canonical entry at its slot, and nothing repaired the index when the head moved.Changes
SyncServicenow tracks the head root the database last committed (_persisted_head_root, seeded fromget_head_root()at wiring time so a restarted node continues from the persisted chain). Inside the existing persistence batch,_reindex_canonical_slotsdiffs the old and new head branches down to their fork point: slots on the new canonical branch are (re)written, slots only the old branch filled are deleted, and a block that did not move the head no longer touches the index. The tracker advances only after the batch commits, so a rollback leaves it consistent with disk.Database/SQLiteDatabase: newdelete_block_root_by_slot(slot)for the vacated-slot case (a reorg onto a branch with an empty slot).MockForkchoiceStoregains anadvance_head_on_blockknob to simulate side-branch imports;RecordingSyncDatabasegainsget_head_root/delete_block_root_by_slot.A block that does not move the head can never be a head ancestor —
on_blockonly accepts blocks whose parent is already in the store, so every head ancestor is present before import. Skipping the index write for such blocks is therefore always safe (no canonical entry is missed).Tests
delete_block_root_by_slotremoves only the targeted slot; deleting an absent slot is a no-op.just checkclean.