diff --git a/cmd/minitiad/rollback.go b/cmd/minitiad/rollback.go new file mode 100644 index 00000000..33b9c6ca --- /dev/null +++ b/cmd/minitiad/rollback.go @@ -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 +} diff --git a/cmd/minitiad/root.go b/cmd/minitiad/root.go index 6d64c1d3..998d1c68 100644 --- a/cmd/minitiad/root.go +++ b/cmd/minitiad/root.go @@ -207,6 +207,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) { diff --git a/go.mod b/go.mod index 851ca8f0..18b2391a 100644 --- a/go.mod +++ b/go.mod @@ -34,8 +34,8 @@ require ( github.com/hashicorp/go-metrics v0.5.3 github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/holiman/uint256 v1.3.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/evm-tx v0.1.2 @@ -173,7 +173,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/initia-labs/kvindexer/submodules/tx v0.1.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect @@ -281,7 +281,7 @@ 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 github.com/ethereum/go-ethereum => github.com/initia-labs/evm v0.0.0-20240826053041-8320cc435dfd ) diff --git a/go.sum b/go.sum index cd430f47..b2995e4d 100644 --- a/go.sum +++ b/go.sum @@ -805,18 +805,18 @@ 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/evm v0.0.0-20240826053041-8320cc435dfd h1:WJGpV8iEm9Z/HcYITduwfHcBcembX32DeIepT843kQU= github.com/initia-labs/evm v0.0.0-20240826053041-8320cc435dfd/go.mod h1:Mq0biU2jbdmKSZoqOj29017ygFrMnB5/Rifwp980W4o= 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= @@ -827,8 +827,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/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= diff --git a/integration-tests/go.mod b/integration-tests/go.mod index e147a23d..ab539900 100644 --- a/integration-tests/go.mod +++ b/integration-tests/go.mod @@ -8,7 +8,7 @@ require ( github.com/cosmos/cosmos-sdk v0.50.9 github.com/cosmos/ibc-go/v8 v8.4.0 github.com/ethereum/go-ethereum v1.14.7 - github.com/initia-labs/initia v0.4.7 + github.com/initia-labs/initia v0.4.9 github.com/initia-labs/minievm v0.4.0 github.com/stretchr/testify v1.9.0 ) @@ -265,7 +265,7 @@ 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 github.com/ethereum/go-ethereum => github.com/initia-labs/evm v0.0.0-20240826053041-8320cc435dfd )