github.com/Finschia/finschia-sdk@v0.48.1/server/rollback.go (about)

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