@@ -671,7 +671,7 @@ func TestRollback(t *testing.T) {
671
671
672
672
// Execute rollback to height 7
673
673
rollbackToHeight := uint64 (7 )
674
- err = store .Rollback (ctx , rollbackToHeight )
674
+ err = store .Rollback (ctx , rollbackToHeight , true )
675
675
require .NoError (err )
676
676
677
677
// Verify new height
@@ -718,7 +718,7 @@ func TestRollbackToSameHeight(t *testing.T) {
718
718
require .NoError (err )
719
719
720
720
// Execute rollback to same height
721
- err = store .Rollback (ctx , height )
721
+ err = store .Rollback (ctx , height , true )
722
722
require .NoError (err )
723
723
724
724
// Verify height unchanged
@@ -753,7 +753,7 @@ func TestRollbackToHigherHeight(t *testing.T) {
753
753
754
754
// Execute rollback to higher height
755
755
rollbackToHeight := uint64 (10 )
756
- err = store .Rollback (ctx , rollbackToHeight )
756
+ err = store .Rollback (ctx , rollbackToHeight , true )
757
757
require .NoError (err )
758
758
759
759
// Verify height unchanged
@@ -778,7 +778,7 @@ func TestRollbackBatchError(t *testing.T) {
778
778
}
779
779
store := New (mock )
780
780
781
- err := store .Rollback (ctx , uint64 (5 ))
781
+ err := store .Rollback (ctx , uint64 (5 ), true )
782
782
require .Error (err )
783
783
require .Contains (err .Error (), "failed to create a new batch" )
784
784
}
@@ -795,7 +795,7 @@ func TestRollbackHeightError(t *testing.T) {
795
795
}
796
796
store := New (mock )
797
797
798
- err := store .Rollback (ctx , uint64 (5 ))
798
+ err := store .Rollback (ctx , uint64 (5 ), true )
799
799
require .Error (err )
800
800
require .Contains (err .Error (), "failed to get current height" )
801
801
}
@@ -806,7 +806,7 @@ func TestRollbackDAIncludedHeightValidation(t *testing.T) {
806
806
require := require .New (t )
807
807
808
808
// 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 ) {
810
810
ctx := context .Background ()
811
811
store := New (mustNewInMem ())
812
812
@@ -844,11 +844,59 @@ func TestRollbackDAIncludedHeightValidation(t *testing.T) {
844
844
require .NoError (err )
845
845
846
846
// Rollback to height below DA included height should fail
847
- err = store .Rollback (ctx , uint64 (6 ))
847
+ err = store .Rollback (ctx , uint64 (6 ), true )
848
848
require .Error (err )
849
849
require .Contains (err .Error (), "DA included height is greater than the rollback height: cannot rollback a finalized height" )
850
850
})
851
851
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
+
852
900
// Test case 2: Rollback to height equal to DA included height should succeed
853
901
t .Run ("rollback to DA included height succeeds" , func (t * testing.T ) {
854
902
ctx := context .Background ()
@@ -888,7 +936,7 @@ func TestRollbackDAIncludedHeightValidation(t *testing.T) {
888
936
require .NoError (err )
889
937
890
938
// 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 )
892
940
require .NoError (err )
893
941
894
942
// Verify height was rolled back to 8
@@ -936,7 +984,7 @@ func TestRollbackDAIncludedHeightValidation(t *testing.T) {
936
984
require .NoError (err )
937
985
938
986
// Rollback to height above DA included height should succeed
939
- err = store .Rollback (ctx , uint64 (9 ))
987
+ err = store .Rollback (ctx , uint64 (9 ), true )
940
988
require .NoError (err )
941
989
942
990
// Verify height was rolled back to 9
@@ -982,7 +1030,7 @@ func TestRollbackDAIncludedHeightNotSet(t *testing.T) {
982
1030
983
1031
// Don't set DA included height - it should not exist
984
1032
// 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 )
986
1034
require .NoError (err )
987
1035
988
1036
// Verify height was rolled back to 3
@@ -1031,7 +1079,7 @@ func TestRollbackDAIncludedHeightInvalidLength(t *testing.T) {
1031
1079
require .NoError (err )
1032
1080
1033
1081
// Rollback should succeed since invalid length data is ignored
1034
- err = store .Rollback (ctx , uint64 (3 ))
1082
+ err = store .Rollback (ctx , uint64 (3 ), true )
1035
1083
require .NoError (err )
1036
1084
1037
1085
// Verify height was rolled back to 3
@@ -1074,7 +1122,7 @@ func TestRollbackDAIncludedHeightGetMetadataError(t *testing.T) {
1074
1122
mock .getMetadataError = errors .New ("metadata retrieval failed" )
1075
1123
1076
1124
// Rollback should fail due to GetMetadata error
1077
- err = store .Rollback (ctx , uint64 (1 ))
1125
+ err = store .Rollback (ctx , uint64 (1 ), true )
1078
1126
require .Error (err )
1079
1127
require .Contains (err .Error (), "failed to get DA included height" )
1080
1128
require .Contains (err .Error (), "metadata retrieval failed" )
0 commit comments