Open
Conversation
The update_tip_num SQL used GREATEST(current, new) which prevented tip_num from ever decreasing. During a reorg, handle_reorg needs to roll tip_num back to the fork point so the sync engine re-fetches the canonical chain. Added rollback_tip_num() that uses a direct SET instead of GREATEST, and updated handle_reorg to use it. The normal forward-sync path still uses update_tip_num with GREATEST for monotonic advancement. Tests verify: - tip_num decreases during reorg via rollback_tip_num - tip_num still advances monotonically via update_tip_num (GREATEST) - After rollback, forward sync resumes and advances past old tip Amp-Thread-ID: https://ampcode.com/threads/T-019d458d-01b9-76ca-9fb1-bfbd3729d359 Co-authored-by: Amp <amp@ampcode.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.
Problem
update_tip_numusesGREATEST(sync_state.tip_num, EXCLUDED.tip_num)which makes tip_num monotonically increasing. During a chain reorganization,handle_reorgneeds to roll tip_num back to the fork point so the sync engine re-fetches the canonical chain — but GREATEST prevents any decrease.Severity: High — reorgs silently fail to update tip_num, causing the sync engine to skip re-syncing orphaned blocks with their canonical replacements.
Fix
Added
rollback_tip_num()which uses a directSET tip_num = $1instead ofGREATEST. Updatedhandle_reorgto use this new function. The normal forward-sync path still usesupdate_tip_numwithGREATESTfor safe monotonic advancement.Changes
src/sync/writer.rs: Addedrollback_tip_num()— unconditionally sets tip_num and head_num to the fork pointsrc/sync/engine.rs:handle_reorg()now callsrollback_tip_numinstead ofupdate_tip_numtests/smoke_test.rs: 3 new tests:test_rollback_tip_num_decreases_during_reorg— verifies tip_num can decreasetest_update_tip_num_still_monotonic_forward— verifies GREATEST still works for forward synctest_rollback_then_forward_sync_resumes— verifies full rollback→resume cycleTesting