github.com/cosmos/cosmos-sdk@v0.50.10/server/rollback.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  
     6  	cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/cosmos/cosmos-sdk/client/flags"
    10  	"github.com/cosmos/cosmos-sdk/server/types"
    11  )
    12  
    13  // NewRollbackCmd creates a command to rollback CometBFT and multistore state by one height.
    14  func NewRollbackCmd(appCreator types.AppCreator, defaultNodeHome string) *cobra.Command {
    15  	var removeBlock bool
    16  
    17  	cmd := &cobra.Command{
    18  		Use:   "rollback",
    19  		Short: "rollback Cosmos SDK and CometBFT state by one height",
    20  		Long: `
    21  A state rollback is performed to recover from an incorrect application state transition,
    22  when CometBFT has persisted an incorrect app hash and is thus unable to make
    23  progress. Rollback overwrites a state at height n with the state at height n - 1.
    24  The application also rolls back to height n - 1. No blocks are removed, so upon
    25  restarting CometBFT the transactions in block n will be re-executed against the
    26  application.
    27  `,
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			ctx := GetServerContextFromCmd(cmd)
    30  			cfg := ctx.Config
    31  			home := cfg.RootDir
    32  			db, err := openDB(home, GetAppDBBackend(ctx.Viper))
    33  			if err != nil {
    34  				return err
    35  			}
    36  			app := appCreator(ctx.Logger, db, nil, ctx.Viper)
    37  			// rollback CometBFT state
    38  			height, hash, err := cmtcmd.RollbackState(ctx.Config, removeBlock)
    39  			if err != nil {
    40  				return fmt.Errorf("failed to rollback CometBFT state: %w", err)
    41  			}
    42  			// rollback the multistore
    43  
    44  			if err := app.CommitMultiStore().RollbackToVersion(height); err != nil {
    45  				return fmt.Errorf("failed to rollback to version: %w", err)
    46  			}
    47  
    48  			fmt.Printf("Rolled back state to height %d and hash %X", height, hash)
    49  			return nil
    50  		},
    51  	}
    52  
    53  	cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
    54  	cmd.Flags().BoolVar(&removeBlock, "hard", false, "remove last block as well as state")
    55  	return cmd
    56  }