@@ -703,34 +703,60 @@ func testSyncProgress(t *testing.T, protocol uint, mode SyncMode) {
703703// TestDeepReorgDetection verifies that checkDeepReorg correctly identifies
704704// when the canonical chain has reorged past the old pivot.
705705func TestDeepReorgDetection (t * testing.T ) {
706- db := rawdb .NewMemoryDatabase ()
707- pivotNumber := uint64 (100 )
708- pivotRoot := common .HexToHash ("0xaaaa" )
709- reorgedRoot := common .HexToHash ("0xbbbb" )
706+ t .Parallel ()
707+
708+ // Case 1: Chain reorged to a shorter fork — old pivot number no longer
709+ // has a canonical hash. checkDeepReorg returns false (can't confirm reorg
710+ // without data). The syncer's catchUp guard handles this case instead
711+ // (see TestCatchUpInvertedRange in eth/protocols/snap/sync_test.go).
712+ t .Run ("ShorterChain" , func (t * testing.T ) {
713+ db := rawdb .NewMemoryDatabase ()
714+ // No canonical hash written at block 100 — chain is shorter
715+ if checkDeepReorg (db , 100 , common .HexToHash ("0xaaaa" )) {
716+ t .Fatal ("should not detect reorg when canonical hash is missing" )
717+ }
718+ })
710719
711- // Write a header at the pivot number with a different root (simulating reorg)
712- header := & types.Header {
713- Number : new (big.Int ).SetUint64 (pivotNumber ),
714- Root : reorgedRoot ,
715- Difficulty : common .Big0 ,
716- }
717- rawdb .WriteHeader (db , header )
718- rawdb .WriteCanonicalHash (db , header .Hash (), pivotNumber )
720+ // Case 2: Chain reorged to a same-length (or longer) fork — old pivot
721+ // number exists but has a different state root. checkDeepReorg detects it.
722+ t .Run ("DifferentRoot" , func (t * testing.T ) {
723+ db := rawdb .NewMemoryDatabase ()
724+ pivotNumber := uint64 (100 )
725+ pivotRoot := common .HexToHash ("0xaaaa" )
726+ reorgedRoot := common .HexToHash ("0xbbbb" )
727+
728+ header := & types.Header {
729+ Number : new (big.Int ).SetUint64 (pivotNumber ),
730+ Root : reorgedRoot ,
731+ Difficulty : common .Big0 ,
732+ }
733+ rawdb .WriteHeader (db , header )
734+ rawdb .WriteCanonicalHash (db , header .Hash (), pivotNumber )
719735
720- // Should detect reorg: canonical root differs from our pivot root
721- if ! checkDeepReorg ( db , pivotNumber , pivotRoot ) {
722- t . Fatal ( "expected deep reorg detection when roots differ" )
723- }
736+ if ! checkDeepReorg ( db , pivotNumber , pivotRoot ) {
737+ t . Fatal ( "expected deep reorg detection when roots differ" )
738+ }
739+ })
724740
725- // Should NOT detect reorg: canonical root matches our pivot root
726- if checkDeepReorg (db , pivotNumber , reorgedRoot ) {
727- t .Fatal ("should not detect reorg when roots match" )
728- }
741+ // Case 3: Same block at old pivot — no reorg at the pivot level.
742+ // Blocks above may differ, but catch-up handles that normally.
743+ t .Run ("SameRoot" , func (t * testing.T ) {
744+ db := rawdb .NewMemoryDatabase ()
745+ pivotNumber := uint64 (100 )
746+ pivotRoot := common .HexToHash ("0xaaaa" )
747+
748+ header := & types.Header {
749+ Number : new (big.Int ).SetUint64 (pivotNumber ),
750+ Root : pivotRoot ,
751+ Difficulty : common .Big0 ,
752+ }
753+ rawdb .WriteHeader (db , header )
754+ rawdb .WriteCanonicalHash (db , header .Hash (), pivotNumber )
729755
730- // Should NOT detect reorg: no canonical hash at that number
731- if checkDeepReorg ( db , 999 , pivotRoot ) {
732- t . Fatal ( "should not detect reorg when block number is unknown" )
733- }
756+ if checkDeepReorg ( db , pivotNumber , pivotRoot ) {
757+ t . Fatal ( "should not detect reorg when roots match" )
758+ }
759+ })
734760}
735761
736762// TestDeepReorgWipesProgress verifies that when a deep reorg is detected,
0 commit comments