Skip to content

Commit

Permalink
introduce multiple rollback cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Sep 20, 2024
1 parent 847405e commit 19b7d3a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
65 changes: 65 additions & 0 deletions cmd/minitiad/rollback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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
}
if height <= 0 {
return fmt.Errorf("height must be greater than 0")
}

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)
if curHeight := app.CommitMultiStore().LatestVersion(); height >= curHeight {
return fmt.Errorf("height must be less than the current height %d", curHeight)
}

// 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
}
1 change: 1 addition & 0 deletions cmd/minitiad/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ 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
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ require (
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/go-metrics v0.5.3
github.com/initia-labs/OPinit v0.4.3
github.com/initia-labs/initia v0.4.7
github.com/initia-labs/OPinit v0.4.4
github.com/initia-labs/initia v0.4.9
github.com/initia-labs/kvindexer v0.1.8
github.com/initia-labs/kvindexer/submodules/block v0.1.0
github.com/initia-labs/kvindexer/submodules/tx v0.1.0
Expand Down Expand Up @@ -169,7 +169,7 @@ require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/initia-labs/OPinit/api v0.4.3 // indirect
github.com/initia-labs/OPinit/api v0.4.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/jsternberg/zap-logfmt v1.3.0 // indirect
Expand Down Expand Up @@ -279,6 +279,6 @@ replace (

// initia custom
replace (
github.com/cometbft/cometbft => github.com/initia-labs/cometbft v0.0.0-20240905084435-48a115dd696d
github.com/cometbft/cometbft => github.com/initia-labs/cometbft v0.0.0-20240920025550-5f9e1f33d35f
github.com/cosmos/ibc-go/v8 => github.com/initia-labs/ibc-go/v8 v8.0.0-20240802003717-19c0b4ad450d
)
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -798,16 +798,16 @@ 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/OPinit v0.4.4 h1:SIc0iPN3sokufbtkRNp7mPyddCriNzR7owilz4brg/4=
github.com/initia-labs/OPinit v0.4.4/go.mod h1:ZY1O3Ky3nUbH44WKt4AxR4WUoZsudthXCn9QyqeDCPU=
github.com/initia-labs/OPinit/api v0.4.4 h1:jA4BUh4OLR/FHDsOy9Ilqcd9kMTm1Ibasx/RlLUImXQ=
github.com/initia-labs/OPinit/api v0.4.4/go.mod h1:NorLLEBESDeLPQIzTFIT2XjvD/tkS1VRE6YL59TXYT0=
github.com/initia-labs/cometbft v0.0.0-20240920025550-5f9e1f33d35f h1:XfGDgsIQsotvTMPTEmGQvGcGaW8NQRxI6agA3u9aPaY=
github.com/initia-labs/cometbft v0.0.0-20240920025550-5f9e1f33d35f/go.mod h1:l4Dwr2F4omhnyBrb/UmKzNCe5vJcMhkT358MTHMZVSQ=
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=
github.com/initia-labs/initia v0.4.7/go.mod h1:mpu6vIEeAFisLDZkKXRtTcFcMuoQ8anS39SWnDLL/e4=
github.com/initia-labs/initia v0.4.9 h1:FoB5yi/AfUM4b6Ax/pNSbJ18bNRqgIR3eYFzsykZBjY=
github.com/initia-labs/initia v0.4.9/go.mod h1:wWX99DQsD4BIx5ZarQzRQ9KghMHumZDQMbRfyX0gOpk=
github.com/initia-labs/kvindexer v0.1.8 h1:PZ7FPYZO2zFXBdnvVlwMFVv6O59fpgCObELxVYB0hBo=
github.com/initia-labs/kvindexer v0.1.8/go.mod h1:OV85HaQ9KVrg+zGPUlxT9RF9nAaM3Yq4/3MoHqGqhWk=
github.com/initia-labs/kvindexer/submodules/block v0.1.0 h1:y+EXnksd/I2F96mzIoQA64nZUZON2P+99YrSzeLCLoY=
Expand All @@ -818,8 +818,8 @@ github.com/initia-labs/kvindexer/submodules/wasm-nft v0.1.5 h1:zDba67t/hh61x+syu
github.com/initia-labs/kvindexer/submodules/wasm-nft v0.1.5/go.mod h1:1HD0/oH6kErv8F0J5ayGfLqerWSCPveGHSCbcfiBELw=
github.com/initia-labs/kvindexer/submodules/wasm-pair v0.1.0 h1:Ns7bxZkwdby67aIq3VXFAzlpoLdGONJbDaMiqu6k408=
github.com/initia-labs/kvindexer/submodules/wasm-pair v0.1.0/go.mod h1:SEotHE6mJ3VKr9g4j0AoXHgEYz6qhL3i1bX1QRnVT6g=
github.com/initia-labs/movevm v0.4.5 h1:C1KfVQJiIqH3fcSlQ/KELRmEu2y3VAsVMSSyKTBfE0k=
github.com/initia-labs/movevm v0.4.5/go.mod h1:aUWdvFZPdULjJ2McQTE+mLnfnG3CLAz0TWJRFzFFUwg=
github.com/initia-labs/movevm v0.4.9 h1:AhX7YE+hO3i6A8GYKJTBUVw1OJ455ojsKPQgp97aF0w=
github.com/initia-labs/movevm v0.4.9/go.mod h1:aUWdvFZPdULjJ2McQTE+mLnfnG3CLAz0TWJRFzFFUwg=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
Expand Down

0 comments on commit 19b7d3a

Please sign in to comment.