Skip to content

Commit

Permalink
add rollback cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Sep 13, 2024
1 parent eafd1d6 commit 6657481
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
58 changes: 58 additions & 0 deletions cmd/minitiad/rollback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"path/filepath"
"strconv"

cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
"github.com/spf13/cobra"

dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/types"
)

// NewMultipleRollbackCmd creates a command to rollback CometBFT and multistore state by one height.
func NewMultipleRollbackCmd(appCreator types.AppCreator) *cobra.Command {
cmd := &cobra.Command{
Use: "mrollback [height]",
Short: "rollback Cosmos SDK and CometBFT state to the given height",
Long: `
A state rollback is performed to recover from an incorrect application state transition,
when CometBFT has persisted an incorrect app hash and is thus unable to make
progress. Rollback overwrites a state with the state at the given height. All
blocks after the given height are removed from the blockchain.
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := server.GetServerContextFromCmd(cmd)

height, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}

dataDir := filepath.Join(ctx.Config.RootDir, "data")
db, err := dbm.NewDB("application", server.GetAppDBBackend(ctx.Viper), dataDir)
if err != nil {
return err
}
app := appCreator(ctx.Logger, db, nil, ctx.Viper)
// rollback CometBFT state
height, hash, err := cmtcmd.RollbackMultipleState(ctx.Config, height)
if err != nil {
return fmt.Errorf("failed to rollback CometBFT state: %w", err)
}
// rollback the multistore

if err := app.CommitMultiStore().RollbackToVersion(height); err != nil {
return fmt.Errorf("failed to rollback to version: %w", err)
}

fmt.Printf("Rolled back state to height %d and hash %X\n", height, hash)
return nil
},
}
return cmd
}
3 changes: 3 additions & 0 deletions cmd/minitiad/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, b

// add launch commands
rootCmd.AddCommand(LaunchCommand(a, encodingConfig, basicManager))
rootCmd.AddCommand(
NewMultipleRollbackCmd(a.AppCreator()),
)
}

func addModuleInitFlags(startCmd *cobra.Command) {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ replace (

// initia custom
replace (
github.com/cometbft/cometbft => github.com/initia-labs/cometbft v0.0.0-20240905084435-48a115dd696d
github.com/cometbft/cometbft => ../cometbft
github.com/cosmos/ibc-go/v8 => github.com/initia-labs/ibc-go/v8 v8.0.0-20240802003717-19c0b4ad450d
github.com/initia-labs/OPinit => ../opinit
github.com/initia-labs/OPinit/api => ../opinit/api
)
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -790,12 +790,6 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/initia-labs/OPinit v0.4.3 h1:tFpcT9qeOLS49tFdEeK9ACEibeCEYd+V4Oz69gQPvp8=
github.com/initia-labs/OPinit v0.4.3/go.mod h1:1bf2//8NDHa2geXj81wm+2tLOW7zD6PQiGR6eakim00=
github.com/initia-labs/OPinit/api v0.4.3 h1:qljFODGw3F2ClGgJD4uiw1QXb3Px9tJX3jqWolPco/Q=
github.com/initia-labs/OPinit/api v0.4.3/go.mod h1:NorLLEBESDeLPQIzTFIT2XjvD/tkS1VRE6YL59TXYT0=
github.com/initia-labs/cometbft v0.0.0-20240905084435-48a115dd696d h1:7/ZGJiuoyYwWMEiBjdu42QHwpRJ4Uf0tKFOKtsj8Uw8=
github.com/initia-labs/cometbft v0.0.0-20240905084435-48a115dd696d/go.mod h1:zwFquy+oo2ovVoGkfCHwQFDQJtKnVZ7O6BNFLt9lGNk=
github.com/initia-labs/ibc-go/v8 v8.0.0-20240802003717-19c0b4ad450d h1:TLq8lB1PtQ0pjGf+bN8YgGVeLMuytZ26SBGMOs1seKY=
github.com/initia-labs/ibc-go/v8 v8.0.0-20240802003717-19c0b4ad450d/go.mod h1:zh6x1osR0hNvEcFrC/lhGD08sMfQmr9wHVvZ/mRWMCs=
github.com/initia-labs/initia v0.4.7 h1:QujywOBUsG6GXxT7RNc+q+mWHnUeTq9W49/IRGnwWys=
Expand Down

0 comments on commit 6657481

Please sign in to comment.