Skip to content

Commit

Permalink
add rollback cmd (#62)
Browse files Browse the repository at this point in the history
* add rollback cmd

* bump deps

* add height condition check

* lint

---------

Co-authored-by: beer-1 <[email protected]>
  • Loading branch information
sh-cha and beer-1 authored Sep 20, 2024
1 parent c8dd534 commit 21e2ccf
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG TARGETARCH
ARG GOARCH

# See https://github.com/initia-labs/movevm/releases
ENV LIBMOVEVM_VERSION=v0.4.5
ENV LIBMOVEVM_VERSION=v0.4.9

# Install necessary packages
RUN set -eux; apk add --no-cache ca-certificates build-base git cmake
Expand Down
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 @@ -177,6 +177,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
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ require (
github.com/cosmos/ibc-go/v8 v8.4.0
github.com/golang/mock v1.6.0
github.com/gorilla/mux v1.8.1
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/move-nft v0.1.5
github.com/initia-labs/kvindexer/submodules/pair v0.1.1
github.com/initia-labs/kvindexer/submodules/tx v0.1.0
// we also need to update `LIBMOVEVM_VERSION` of Dockerfile#5
github.com/initia-labs/movevm v0.4.5
github.com/initia-labs/movevm v0.4.9
github.com/noble-assets/forwarding/v2 v2.0.0-20240521090705-86712c4c9e43
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
Expand Down Expand Up @@ -164,7 +164,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 @@ -274,6 +274,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 @@ -790,16 +790,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 @@ -810,8 +810,8 @@ github.com/initia-labs/kvindexer/submodules/pair v0.1.1 h1:o151gA4jIbqEl+pWTOCiz
github.com/initia-labs/kvindexer/submodules/pair v0.1.1/go.mod h1:8X1GE1ZLkH7z8TKb5MUh7UClTkcqVFIwXIIRdsqeUZY=
github.com/initia-labs/kvindexer/submodules/tx v0.1.0 h1:6kbf6wmzXPN0XCQLasiFgq1AlZHkt5K3/ZG+IWw1nNs=
github.com/initia-labs/kvindexer/submodules/tx v0.1.0/go.mod h1:i0XeLbLa6xdgTR01WF8kaAO50vMmwxbeq0fKexwpFHU=
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 21e2ccf

Please sign in to comment.