@@ -431,3 +431,56 @@ func TestExecuteTxsWithInvalidPrevStateRoot(t *testing.T) {
431431 t .Errorf ("Expected pending root to be stored for height %d" , blockHeight )
432432 }
433433}
434+ func TestRollback (t * testing.T ) {
435+ executor := NewDummyExecutor ()
436+ ctx := context .Background ()
437+
438+ // Test rollback from height 1 (should fail)
439+ _ , err := executor .Rollback (ctx , 1 )
440+ if err == nil {
441+ t .Error ("Expected error when rolling back from height 1" )
442+ }
443+ expectedError := "cannot rollback from height 1: must be > 1"
444+ if err .Error () != expectedError {
445+ t .Errorf ("Expected error message '%s', got '%s'" , expectedError , err .Error ())
446+ }
447+
448+ // Setup state for rollback test
449+ _ , _ , err = executor .InitChain (ctx , time .Now (), 1 , "test-chain" )
450+ if err != nil {
451+ t .Fatalf ("Failed to initialize chain: %v" , err )
452+ }
453+
454+ // Execute some transactions to create pending state
455+ txs := [][]byte {[]byte ("test-tx-1" ), []byte ("test-tx-2" )}
456+ prevStateRoot := executor .GetStateRoot ()
457+ _ , _ , err = executor .ExecuteTxs (ctx , txs , 2 , time .Now (), prevStateRoot )
458+ if err != nil {
459+ t .Fatalf ("Failed to execute transactions: %v" , err )
460+ }
461+
462+ // Verify pending state exists
463+ if _ , exists := executor .pendingRoots [2 ]; ! exists {
464+ t .Error ("Expected pending root to exist for height 2" )
465+ }
466+
467+ // Test successful rollback from height 2
468+ stateRoot , err := executor .Rollback (ctx , 2 )
469+ if err != nil {
470+ t .Errorf ("Expected no error for rollback from height 2, got: %v" , err )
471+ }
472+
473+ if stateRoot == nil {
474+ t .Error ("Expected non-nil state root from rollback" )
475+ }
476+
477+ // Verify pending state was removed
478+ if _ , exists := executor .pendingRoots [2 ]; exists {
479+ t .Error ("Expected pending root to be removed after rollback" )
480+ }
481+
482+ // Verify returned state root is the finalized state root
483+ if ! bytes .Equal (stateRoot , executor .GetStateRoot ()) {
484+ t .Error ("Expected rollback to return the current finalized state root" )
485+ }
486+ }
0 commit comments