@@ -671,7 +671,7 @@ func TestRollback(t *testing.T) {
671671
672672 // Execute rollback to height 7
673673 rollbackToHeight := uint64 (7 )
674- err = store .Rollback (ctx , rollbackToHeight )
674+ err = store .Rollback (ctx , rollbackToHeight , true )
675675 require .NoError (err )
676676
677677 // Verify new height
@@ -718,7 +718,7 @@ func TestRollbackToSameHeight(t *testing.T) {
718718 require .NoError (err )
719719
720720 // Execute rollback to same height
721- err = store .Rollback (ctx , height )
721+ err = store .Rollback (ctx , height , true )
722722 require .NoError (err )
723723
724724 // Verify height unchanged
@@ -753,7 +753,7 @@ func TestRollbackToHigherHeight(t *testing.T) {
753753
754754 // Execute rollback to higher height
755755 rollbackToHeight := uint64 (10 )
756- err = store .Rollback (ctx , rollbackToHeight )
756+ err = store .Rollback (ctx , rollbackToHeight , true )
757757 require .NoError (err )
758758
759759 // Verify height unchanged
@@ -778,7 +778,7 @@ func TestRollbackBatchError(t *testing.T) {
778778 }
779779 store := New (mock )
780780
781- err := store .Rollback (ctx , uint64 (5 ))
781+ err := store .Rollback (ctx , uint64 (5 ), true )
782782 require .Error (err )
783783 require .Contains (err .Error (), "failed to create a new batch" )
784784}
@@ -795,7 +795,7 @@ func TestRollbackHeightError(t *testing.T) {
795795 }
796796 store := New (mock )
797797
798- err := store .Rollback (ctx , uint64 (5 ))
798+ err := store .Rollback (ctx , uint64 (5 ), true )
799799 require .Error (err )
800800 require .Contains (err .Error (), "failed to get current height" )
801801}
@@ -806,7 +806,7 @@ func TestRollbackDAIncludedHeightValidation(t *testing.T) {
806806 require := require .New (t )
807807
808808 // Test case 1: Rollback to height below DA included height should fail
809- t .Run ("rollback below DA included height fails" , func (t * testing.T ) {
809+ t .Run ("rollback below DA included height fails as aggregator " , func (t * testing.T ) {
810810 ctx := context .Background ()
811811 store := New (mustNewInMem ())
812812
@@ -844,11 +844,59 @@ func TestRollbackDAIncludedHeightValidation(t *testing.T) {
844844 require .NoError (err )
845845
846846 // Rollback to height below DA included height should fail
847- err = store .Rollback (ctx , uint64 (6 ))
847+ err = store .Rollback (ctx , uint64 (6 ), true )
848848 require .Error (err )
849849 require .Contains (err .Error (), "DA included height is greater than the rollback height: cannot rollback a finalized height" )
850850 })
851851
852+ // Test case 2: Rollback to height below DA included height should succeed as sync node
853+ t .Run ("rollback below DA included succeed as sync node" , func (t * testing.T ) {
854+ ctx := context .Background ()
855+ store := New (mustNewInMem ())
856+
857+ // Setup: create and save multiple blocks
858+ chainID := "test-rollback-da-sync-success"
859+ maxHeight := uint64 (10 )
860+
861+ for h := uint64 (1 ); h <= maxHeight ; h ++ {
862+ header , data := types .GetRandomBlock (h , 2 , chainID )
863+ sig := & header .Signature
864+
865+ err := store .SaveBlockData (ctx , header , data , sig )
866+ require .NoError (err )
867+
868+ err = store .SetHeight (ctx , h )
869+ require .NoError (err )
870+
871+ // Create and update state for this height
872+ state := types.State {
873+ ChainID : chainID ,
874+ InitialHeight : 1 ,
875+ LastBlockHeight : h ,
876+ LastBlockTime : header .Time (),
877+ AppHash : header .AppHash ,
878+ }
879+ err = store .UpdateState (ctx , state )
880+ require .NoError (err )
881+ }
882+
883+ // Set DA included height to 8
884+ daIncludedHeight := uint64 (8 )
885+ heightBytes := make ([]byte , 8 )
886+ binary .LittleEndian .PutUint64 (heightBytes , daIncludedHeight )
887+ err := store .SetMetadata (ctx , DAIncludedHeightKey , heightBytes )
888+ require .NoError (err )
889+
890+ // Rollback to height below DA included height should fail
891+ err = store .Rollback (ctx , uint64 (6 ), false )
892+ require .NoError (err )
893+
894+ // Verify height was rolled back to 6
895+ currentHeight , err := store .Height (ctx )
896+ require .NoError (err )
897+ require .Equal (uint64 (6 ), currentHeight )
898+ })
899+
852900 // Test case 2: Rollback to height equal to DA included height should succeed
853901 t .Run ("rollback to DA included height succeeds" , func (t * testing.T ) {
854902 ctx := context .Background ()
@@ -888,7 +936,7 @@ func TestRollbackDAIncludedHeightValidation(t *testing.T) {
888936 require .NoError (err )
889937
890938 // Rollback to height equal to DA included height should succeed
891- err = store .Rollback (ctx , uint64 (8 ))
939+ err = store .Rollback (ctx , uint64 (8 ), true )
892940 require .NoError (err )
893941
894942 // Verify height was rolled back to 8
@@ -936,7 +984,7 @@ func TestRollbackDAIncludedHeightValidation(t *testing.T) {
936984 require .NoError (err )
937985
938986 // Rollback to height above DA included height should succeed
939- err = store .Rollback (ctx , uint64 (9 ))
987+ err = store .Rollback (ctx , uint64 (9 ), true )
940988 require .NoError (err )
941989
942990 // Verify height was rolled back to 9
@@ -982,7 +1030,7 @@ func TestRollbackDAIncludedHeightNotSet(t *testing.T) {
9821030
9831031 // Don't set DA included height - it should not exist
9841032 // Rollback should succeed since no DA included height is set
985- err := store .Rollback (ctx , uint64 (3 ))
1033+ err := store .Rollback (ctx , uint64 (3 ), true )
9861034 require .NoError (err )
9871035
9881036 // Verify height was rolled back to 3
@@ -1031,7 +1079,7 @@ func TestRollbackDAIncludedHeightInvalidLength(t *testing.T) {
10311079 require .NoError (err )
10321080
10331081 // Rollback should succeed since invalid length data is ignored
1034- err = store .Rollback (ctx , uint64 (3 ))
1082+ err = store .Rollback (ctx , uint64 (3 ), true )
10351083 require .NoError (err )
10361084
10371085 // Verify height was rolled back to 3
@@ -1074,7 +1122,7 @@ func TestRollbackDAIncludedHeightGetMetadataError(t *testing.T) {
10741122 mock .getMetadataError = errors .New ("metadata retrieval failed" )
10751123
10761124 // Rollback should fail due to GetMetadata error
1077- err = store .Rollback (ctx , uint64 (1 ))
1125+ err = store .Rollback (ctx , uint64 (1 ), true )
10781126 require .Error (err )
10791127 require .Contains (err .Error (), "failed to get DA included height" )
10801128 require .Contains (err .Error (), "metadata retrieval failed" )
0 commit comments