From 3f9f1d2a086165ec7a7ae312d4cc4e0dc45f15fb Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Thu, 7 Nov 2024 23:30:49 +0900 Subject: [PATCH 1/5] feat(app): rollback many blocks --- client/app/rollback.go | 75 ++++++----------------------------------- client/cmd/cmd.go | 60 --------------------------------- client/cmd/flags.go | 5 +-- client/cmd/rollback.go | 74 ++++++++++++++++++++++++++++++++++++++++ client/config/config.go | 1 - 5 files changed, 88 insertions(+), 127 deletions(-) create mode 100644 client/cmd/rollback.go diff --git a/client/app/rollback.go b/client/app/rollback.go index 1614ecc6..1844de49 100644 --- a/client/app/rollback.go +++ b/client/app/rollback.go @@ -1,76 +1,23 @@ package app import ( - "context" - "fmt" - "math/big" - - "cosmossdk.io/store" - storemetrics "cosmossdk.io/store/metrics" - cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" - dbm "github.com/cosmos/cosmos-db" - "github.com/ethereum/go-ethereum/common" - + cmtcfg "github.com/cometbft/cometbft/config" "github.com/piplabs/story/lib/errors" - "github.com/piplabs/story/lib/log" ) -func Rollback(ctx context.Context, cfg Config, removeBlock bool) error { - engineCl, err := newEngineClient(ctx, cfg) - if err != nil { - return err - } - - latestHeigth, err := engineCl.BlockNumber(ctx) - if err != nil { - return err +func RollbackCometAndAppState(a *App, cometCfg cmtcfg.Config, rollbackHeights int64, removeBlock bool) (lastHeight int64, lastHash []byte, err error) { + // TODO: recovery when rollback fails mid-way + for i := int64(0); i < rollbackHeights; i++ { + lastHeight, lastHash, err = cmtcmd.RollbackState(&cometCfg, removeBlock) + if err != nil { + return lastHeight, lastHash, errors.Wrap(err, "failed to rollback CometBFT state") + } } - latestBlock, err := engineCl.BlockByNumber(ctx, big.NewInt(int64(latestHeigth))) - if err != nil { - return err - } else if latestBlock.BeaconRoot() == nil { - return errors.New("cannot rollback EVM with nil beacon root", "height", latestHeigth) + if err = a.CommitMultiStore().RollbackToVersion(lastHeight); err != nil { + return 0, nil, errors.Wrap(err, "failed to rollback to version") } - db, err := dbm.NewDB("application", cfg.BackendType(), cfg.DataDir()) - if err != nil { - return errors.Wrap(err, "create db") - } - - // Rollback CometBFT state - height, hash, err := cmtcmd.RollbackState(&cfg.Comet, removeBlock) - if err != nil { - return errors.Wrap(err, "rollback comet state") - } - - // Rollback the multistore - cms := store.NewCommitMultiStore(db, newSDKLogger(ctx), storemetrics.NewNoOpMetrics()) - if err := cms.RollbackToVersion(height); err != nil { - return errors.Wrap(err, "rollback to height") - } - - log.Info(ctx, "Rolled back consensus state", "height", height, "hash", fmt.Sprintf("%X", hash)) - - // Rollback EVM if latest EVM block built on-top of new rolled-back consensus head. - if *latestBlock.BeaconRoot() != common.BytesToHash(hash) { - return errors.New("cannot rollback EVM, latest EVM block not built on new rolled-back state", - "evm_height", latestHeigth, - "evm_beacon_root", *latestBlock.BeaconRoot(), - ) - } - - if err := engineCl.SetHead(ctx, latestHeigth-1); err != nil { - return errors.Wrap(err, "set head") - } - - rolledBackBlock, err := engineCl.BlockByNumber(ctx, big.NewInt(int64(latestHeigth-1))) - if err != nil { - return err - } - - log.Info(ctx, "Rolled back execution state", "height", rolledBackBlock.Number(), "hash", rolledBackBlock.Hash()) - - return nil + return lastHeight, lastHash, nil } diff --git a/client/cmd/cmd.go b/client/cmd/cmd.go index 91e20cdb..1e48a94b 100644 --- a/client/cmd/cmd.go +++ b/client/cmd/cmd.go @@ -3,16 +3,12 @@ package cmd import ( "context" - "fmt" - - cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" "github.com/spf13/cobra" "github.com/piplabs/story/client/app" storycfg "github.com/piplabs/story/client/config" "github.com/piplabs/story/lib/buildinfo" libcmd "github.com/piplabs/story/lib/cmd" - "github.com/piplabs/story/lib/errors" "github.com/piplabs/story/lib/log" ) @@ -65,59 +61,3 @@ func newRunCmd(name string, runFunc func(context.Context, app.Config) error) *co return cmd } - -// newRollbackCmd returns a new cobra command that rolls back one block of the story consensus client. -func newRollbackCmd(appCreateFunc func(context.Context, app.Config) *app.App) *cobra.Command { - storyCfg := storycfg.DefaultConfig() - logCfg := log.DefaultConfig() - - cmd := &cobra.Command{ - Use: "rollback", - Short: "rollback Cosmos SDK and CometBFT state by one 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 at height n with the state at height n - 1. -The application also rolls back to height n - 1. No blocks are removed, so upon -restarting CometBFT the transactions in block n will be re-executed against the -application. -`, - RunE: func(cmd *cobra.Command, _ []string) error { - ctx, err := log.Init(cmd.Context(), logCfg) - if err != nil { - return err - } - if err := libcmd.LogFlags(ctx, cmd.Flags()); err != nil { - return err - } - - cometCfg, err := parseCometConfig(ctx, storyCfg.HomeDir) - if err != nil { - return err - } - - app := appCreateFunc(ctx, app.Config{ - Config: storyCfg, - Comet: cometCfg, - }) - height, hash, err := cmtcmd.RollbackState(&cometCfg, storyCfg.RemoveBlock) - if err != nil { - return errors.Wrap(err, "failed to rollback CometBFT state") - } - - if err = app.CommitMultiStore().RollbackToVersion(height); err != nil { - return errors.Wrap(err, "failed to rollback to version") - } - - fmt.Printf("Rolled back state to height %d and hash %X", height, hash) - - return nil - }, - } - - bindRunFlags(cmd, &storyCfg) - bindRollbackFlags(cmd, &storyCfg) - log.BindFlags(cmd.Flags(), &logCfg) - - return cmd -} diff --git a/client/cmd/flags.go b/client/cmd/flags.go index 9d1bda74..30ef5668 100644 --- a/client/cmd/flags.go +++ b/client/cmd/flags.go @@ -141,8 +141,9 @@ func bindKeyConvertFlags(cmd *cobra.Command, cfg *keyConfig) { cmd.Flags().StringVar(&cfg.PubKeyHexUncompressed, "pubkey-hex-uncompressed", "", "Uncompressed public key in hex format") } -func bindRollbackFlags(cmd *cobra.Command, cfg *config.Config) { - cmd.Flags().BoolVar(&cfg.RemoveBlock, "hard", false, "remove last block as well as state") +func bindRollbackFlags(cmd *cobra.Command, cfg *rollbackConfig) { + cmd.Flags().BoolVar(&cfg.RemoveBlock, "hard", false, "remove blocks as well as states") + cmd.Flags().Int64VarP(&cfg.RollbackHeights, "number", "n", 1, "number of blocks to rollback") } func bindValidatorUnjailFlags(cmd *cobra.Command, cfg *unjailConfig) { diff --git a/client/cmd/rollback.go b/client/cmd/rollback.go new file mode 100644 index 00000000..141ff4fc --- /dev/null +++ b/client/cmd/rollback.go @@ -0,0 +1,74 @@ +package cmd + +import ( + "context" + "fmt" + "github.com/piplabs/story/client/app" + storycfg "github.com/piplabs/story/client/config" + libcmd "github.com/piplabs/story/lib/cmd" + "github.com/piplabs/story/lib/log" + "github.com/spf13/cobra" +) + +type rollbackConfig struct { + RemoveBlock bool // See cosmos-sdk/server/rollback.go + RollbackHeights int64 +} + +// newRollbackCmd returns a new cobra command that rolls back one block of the story consensus client. +func newRollbackCmd(appCreateFunc func(context.Context, app.Config) *app.App) *cobra.Command { + rollbackCfg := rollbackConfig{ + RemoveBlock: false, + RollbackHeights: 1, + } + storyCfg := storycfg.DefaultConfig() + logCfg := log.DefaultConfig() + + cmd := &cobra.Command{ + Use: "rollback", + Short: "rollback Cosmos SDK and CometBFT state by X 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 at height n with the state at height n - X. +The application also rolls back to height n - X. If --hard=true, the block +itself will also be deleted and re-downloaded from the p2p network. Note that +different blocks from n - X to n cannot be re-built/re-proposed since that would result in validator slashing. +If --hard=false, No blocks are removed, so upon restarting CometBFT the transactions in blocks [n - X + 1, n] +will be re-executed against the application. +`, + RunE: func(cmd *cobra.Command, _ []string) error { + ctx, err := log.Init(cmd.Context(), logCfg) + if err != nil { + return err + } + if err := libcmd.LogFlags(ctx, cmd.Flags()); err != nil { + return err + } + + cometCfg, err := parseCometConfig(ctx, storyCfg.HomeDir) + if err != nil { + return err + } + + a := appCreateFunc(ctx, app.Config{ + Config: storyCfg, + Comet: cometCfg, + }) + lastHeight, lastHash, err := app.RollbackCometAndAppState(a, cometCfg, rollbackCfg.RollbackHeights, rollbackCfg.RemoveBlock) + if err != nil { + return err + } + + fmt.Printf("Rolled back state to height %d and hash %X", lastHeight, lastHash) + + return nil + }, + } + + bindRunFlags(cmd, &storyCfg) + bindRollbackFlags(cmd, &rollbackCfg) + log.BindFlags(cmd.Flags(), &logCfg) + + return cmd +} diff --git a/client/config/config.go b/client/config/config.go index 30fb8e78..7fdbb6e3 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -181,7 +181,6 @@ type Config struct { ExternalAddress string Seeds string SeedMode bool - RemoveBlock bool // See cosmos-sdk/server/rollback.go } // ConfigFile returns the default path to the toml story config file. From 9cd6375be743cfc1e5b23740ac118ddd539ef7d8 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Mon, 11 Nov 2024 23:14:06 +0900 Subject: [PATCH 2/5] feat(rollback): optional EVM block rollback --- client/app/rollback.go | 43 +++++++++++++++++++++++++++++++++++++++--- client/cmd/flags.go | 3 ++- client/cmd/rollback.go | 9 ++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/client/app/rollback.go b/client/app/rollback.go index 1844de49..cdd5ae19 100644 --- a/client/app/rollback.go +++ b/client/app/rollback.go @@ -1,13 +1,15 @@ package app import ( + "context" cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" cmtcfg "github.com/cometbft/cometbft/config" + "github.com/ethereum/go-ethereum/common" "github.com/piplabs/story/lib/errors" + "math/big" ) -func RollbackCometAndAppState(a *App, cometCfg cmtcfg.Config, rollbackHeights int64, removeBlock bool) (lastHeight int64, lastHash []byte, err error) { - // TODO: recovery when rollback fails mid-way +func RollbackCometAndAppState(ctx context.Context, a *App, appCfg Config, cometCfg cmtcfg.Config, rollbackHeights int64, removeBlock bool, rollbackEVM bool) (lastHeight int64, lastHash []byte, err error) { for i := int64(0); i < rollbackHeights; i++ { lastHeight, lastHash, err = cmtcmd.RollbackState(&cometCfg, removeBlock) if err != nil { @@ -16,7 +18,42 @@ func RollbackCometAndAppState(a *App, cometCfg cmtcfg.Config, rollbackHeights in } if err = a.CommitMultiStore().RollbackToVersion(lastHeight); err != nil { - return 0, nil, errors.Wrap(err, "failed to rollback to version") + return lastHeight, lastHash, errors.Wrap(err, "failed to rollback to version") + } + + if rollbackEVM && rollbackHeights > 1 { + engineCl, err := newEngineClient(ctx, appCfg) + if err != nil { + return lastHeight, lastHash, err + } + + // Note that EVM block might not match the consensus block height, so we need to calculate the rollback + // EVM block height separately. + latestHeight, err := engineCl.BlockNumber(ctx) + if err != nil { + return lastHeight, lastHash, err + } + + latestBlock, err := engineCl.BlockByNumber(ctx, big.NewInt(int64(latestHeight))) + if err != nil { + return lastHeight, lastHash, err + } else if latestBlock.BeaconRoot() == nil { + return lastHeight, lastHash, errors.New("cannot rollback EVM with nil beacon root", "height", lastHeight) + } + + // Rollback EVM if latest EVM block built on-top of new rolled-back consensus head. + if *latestBlock.BeaconRoot() != common.BytesToHash(lastHash) { + return lastHeight, lastHash, errors.New( + "cannot rollback EVM, latest EVM block not built on new rolled-back state", + "evm_height", latestHeight, + "evm_beacon_root", *latestBlock.BeaconRoot(), + ) + } + + rollbackEVMHeight := latestHeight - uint64(rollbackHeights) + if err := engineCl.SetHead(ctx, rollbackEVMHeight); err != nil { + return lastHeight, lastHash, errors.Wrap(err, "set head") + } } return lastHeight, lastHash, nil diff --git a/client/cmd/flags.go b/client/cmd/flags.go index 30ef5668..29a5b065 100644 --- a/client/cmd/flags.go +++ b/client/cmd/flags.go @@ -142,7 +142,8 @@ func bindKeyConvertFlags(cmd *cobra.Command, cfg *keyConfig) { } func bindRollbackFlags(cmd *cobra.Command, cfg *rollbackConfig) { - cmd.Flags().BoolVar(&cfg.RemoveBlock, "hard", false, "remove blocks as well as states") + cmd.Flags().BoolVar(&cfg.RemoveBlock, "hard", false, "remove Cosmos & CometBFT blocks as well as states") + cmd.Flags().BoolVar(&cfg.RemoveBlock, "rollback-evm", false, "remove EVM blocks as well") cmd.Flags().Int64VarP(&cfg.RollbackHeights, "number", "n", 1, "number of blocks to rollback") } diff --git a/client/cmd/rollback.go b/client/cmd/rollback.go index 141ff4fc..7c8f635d 100644 --- a/client/cmd/rollback.go +++ b/client/cmd/rollback.go @@ -12,6 +12,7 @@ import ( type rollbackConfig struct { RemoveBlock bool // See cosmos-sdk/server/rollback.go + RollbackEVM bool RollbackHeights int64 } @@ -19,6 +20,7 @@ type rollbackConfig struct { func newRollbackCmd(appCreateFunc func(context.Context, app.Config) *app.App) *cobra.Command { rollbackCfg := rollbackConfig{ RemoveBlock: false, + RollbackEVM: false, RollbackHeights: 1, } storyCfg := storycfg.DefaultConfig() @@ -51,11 +53,12 @@ will be re-executed against the application. return err } - a := appCreateFunc(ctx, app.Config{ + appCfg := app.Config{ Config: storyCfg, Comet: cometCfg, - }) - lastHeight, lastHash, err := app.RollbackCometAndAppState(a, cometCfg, rollbackCfg.RollbackHeights, rollbackCfg.RemoveBlock) + } + a := appCreateFunc(ctx, appCfg) + lastHeight, lastHash, err := app.RollbackCometAndAppState(ctx, a, appCfg, cometCfg, rollbackCfg.RollbackHeights, rollbackCfg.RemoveBlock, rollbackCfg.RollbackEVM) if err != nil { return err } From 82a01783ca2327a6b07d2833d356a4efd1f7b8b9 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Tue, 19 Nov 2024 08:24:57 +0900 Subject: [PATCH 3/5] lint(client): fix lint --- client/app/rollback.go | 66 ++++++++++++++++++++++-------------------- client/cmd/cmd.go | 1 + client/cmd/rollback.go | 4 ++- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/client/app/rollback.go b/client/app/rollback.go index cdd5ae19..f38cffbd 100644 --- a/client/app/rollback.go +++ b/client/app/rollback.go @@ -2,15 +2,17 @@ package app import ( "context" + "math/big" + cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" cmtcfg "github.com/cometbft/cometbft/config" "github.com/ethereum/go-ethereum/common" + "github.com/piplabs/story/lib/errors" - "math/big" ) func RollbackCometAndAppState(ctx context.Context, a *App, appCfg Config, cometCfg cmtcfg.Config, rollbackHeights int64, removeBlock bool, rollbackEVM bool) (lastHeight int64, lastHash []byte, err error) { - for i := int64(0); i < rollbackHeights; i++ { + for range rollbackHeights { lastHeight, lastHash, err = cmtcmd.RollbackState(&cometCfg, removeBlock) if err != nil { return lastHeight, lastHash, errors.Wrap(err, "failed to rollback CometBFT state") @@ -21,39 +23,41 @@ func RollbackCometAndAppState(ctx context.Context, a *App, appCfg Config, cometC return lastHeight, lastHash, errors.Wrap(err, "failed to rollback to version") } - if rollbackEVM && rollbackHeights > 1 { - engineCl, err := newEngineClient(ctx, appCfg) - if err != nil { - return lastHeight, lastHash, err - } + if !rollbackEVM || rollbackHeights <= 1 { + return lastHeight, lastHash, nil + } - // Note that EVM block might not match the consensus block height, so we need to calculate the rollback - // EVM block height separately. - latestHeight, err := engineCl.BlockNumber(ctx) - if err != nil { - return lastHeight, lastHash, err - } + engineCl, err := newEngineClient(ctx, appCfg) + if err != nil { + return lastHeight, lastHash, err + } - latestBlock, err := engineCl.BlockByNumber(ctx, big.NewInt(int64(latestHeight))) - if err != nil { - return lastHeight, lastHash, err - } else if latestBlock.BeaconRoot() == nil { - return lastHeight, lastHash, errors.New("cannot rollback EVM with nil beacon root", "height", lastHeight) - } + // Note that EVM block might not match the consensus block height, so we need to calculate the rollback + // EVM block height separately. + latestHeight, err := engineCl.BlockNumber(ctx) + if err != nil { + return lastHeight, lastHash, err + } - // Rollback EVM if latest EVM block built on-top of new rolled-back consensus head. - if *latestBlock.BeaconRoot() != common.BytesToHash(lastHash) { - return lastHeight, lastHash, errors.New( - "cannot rollback EVM, latest EVM block not built on new rolled-back state", - "evm_height", latestHeight, - "evm_beacon_root", *latestBlock.BeaconRoot(), - ) - } + latestBlock, err := engineCl.BlockByNumber(ctx, big.NewInt(int64(latestHeight))) + if err != nil { + return lastHeight, lastHash, err + } else if latestBlock.BeaconRoot() == nil { + return lastHeight, lastHash, errors.New("cannot rollback EVM with nil beacon root", "height", lastHeight) + } - rollbackEVMHeight := latestHeight - uint64(rollbackHeights) - if err := engineCl.SetHead(ctx, rollbackEVMHeight); err != nil { - return lastHeight, lastHash, errors.Wrap(err, "set head") - } + // Rollback EVM if latest EVM block built on-top of new rolled-back consensus head. + if *latestBlock.BeaconRoot() != common.BytesToHash(lastHash) { + return lastHeight, lastHash, errors.New( + "cannot rollback EVM, latest EVM block not built on new rolled-back state", + "evm_height", latestHeight, + "evm_beacon_root", *latestBlock.BeaconRoot(), + ) + } + + rollbackEVMHeight := latestHeight - uint64(rollbackHeights) + if err := engineCl.SetHead(ctx, rollbackEVMHeight); err != nil { + return lastHeight, lastHash, errors.Wrap(err, "set head") } return lastHeight, lastHash, nil diff --git a/client/cmd/cmd.go b/client/cmd/cmd.go index 1e48a94b..96e8a72a 100644 --- a/client/cmd/cmd.go +++ b/client/cmd/cmd.go @@ -3,6 +3,7 @@ package cmd import ( "context" + "github.com/spf13/cobra" "github.com/piplabs/story/client/app" diff --git a/client/cmd/rollback.go b/client/cmd/rollback.go index 7c8f635d..1629be67 100644 --- a/client/cmd/rollback.go +++ b/client/cmd/rollback.go @@ -3,11 +3,13 @@ package cmd import ( "context" "fmt" + + "github.com/spf13/cobra" + "github.com/piplabs/story/client/app" storycfg "github.com/piplabs/story/client/config" libcmd "github.com/piplabs/story/lib/cmd" "github.com/piplabs/story/lib/log" - "github.com/spf13/cobra" ) type rollbackConfig struct { From e78cb90ea346d6d2644b861bddaf46b3f71853bd Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Tue, 26 Nov 2024 18:20:57 -0600 Subject: [PATCH 4/5] feat(rollback): remove evm rollback and fix fn args --- client/app/rollback.go | 49 ++++-------------------------------------- client/cmd/flags.go | 5 ++--- client/cmd/rollback.go | 18 +++++++++------- 3 files changed, 16 insertions(+), 56 deletions(-) diff --git a/client/app/rollback.go b/client/app/rollback.go index f38cffbd..fe0a30f1 100644 --- a/client/app/rollback.go +++ b/client/app/rollback.go @@ -1,19 +1,15 @@ package app import ( - "context" - "math/big" - cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" cmtcfg "github.com/cometbft/cometbft/config" - "github.com/ethereum/go-ethereum/common" - + "github.com/piplabs/story/client/cmd" "github.com/piplabs/story/lib/errors" ) -func RollbackCometAndAppState(ctx context.Context, a *App, appCfg Config, cometCfg cmtcfg.Config, rollbackHeights int64, removeBlock bool, rollbackEVM bool) (lastHeight int64, lastHash []byte, err error) { - for range rollbackHeights { - lastHeight, lastHash, err = cmtcmd.RollbackState(&cometCfg, removeBlock) +func RollbackCometAndAppState(a *App, cometCfg cmtcfg.Config, rollbackCfg cmd.RollbackConfig) (lastHeight int64, lastHash []byte, err error) { + for range rollbackCfg.RollbackHeights { + lastHeight, lastHash, err = cmtcmd.RollbackState(&cometCfg, rollbackCfg.RemoveBlock) if err != nil { return lastHeight, lastHash, errors.Wrap(err, "failed to rollback CometBFT state") } @@ -23,42 +19,5 @@ func RollbackCometAndAppState(ctx context.Context, a *App, appCfg Config, cometC return lastHeight, lastHash, errors.Wrap(err, "failed to rollback to version") } - if !rollbackEVM || rollbackHeights <= 1 { - return lastHeight, lastHash, nil - } - - engineCl, err := newEngineClient(ctx, appCfg) - if err != nil { - return lastHeight, lastHash, err - } - - // Note that EVM block might not match the consensus block height, so we need to calculate the rollback - // EVM block height separately. - latestHeight, err := engineCl.BlockNumber(ctx) - if err != nil { - return lastHeight, lastHash, err - } - - latestBlock, err := engineCl.BlockByNumber(ctx, big.NewInt(int64(latestHeight))) - if err != nil { - return lastHeight, lastHash, err - } else if latestBlock.BeaconRoot() == nil { - return lastHeight, lastHash, errors.New("cannot rollback EVM with nil beacon root", "height", lastHeight) - } - - // Rollback EVM if latest EVM block built on-top of new rolled-back consensus head. - if *latestBlock.BeaconRoot() != common.BytesToHash(lastHash) { - return lastHeight, lastHash, errors.New( - "cannot rollback EVM, latest EVM block not built on new rolled-back state", - "evm_height", latestHeight, - "evm_beacon_root", *latestBlock.BeaconRoot(), - ) - } - - rollbackEVMHeight := latestHeight - uint64(rollbackHeights) - if err := engineCl.SetHead(ctx, rollbackEVMHeight); err != nil { - return lastHeight, lastHash, errors.Wrap(err, "set head") - } - return lastHeight, lastHash, nil } diff --git a/client/cmd/flags.go b/client/cmd/flags.go index 29a5b065..d678274f 100644 --- a/client/cmd/flags.go +++ b/client/cmd/flags.go @@ -141,10 +141,9 @@ func bindKeyConvertFlags(cmd *cobra.Command, cfg *keyConfig) { cmd.Flags().StringVar(&cfg.PubKeyHexUncompressed, "pubkey-hex-uncompressed", "", "Uncompressed public key in hex format") } -func bindRollbackFlags(cmd *cobra.Command, cfg *rollbackConfig) { +func bindRollbackFlags(cmd *cobra.Command, cfg *RollbackConfig) { cmd.Flags().BoolVar(&cfg.RemoveBlock, "hard", false, "remove Cosmos & CometBFT blocks as well as states") - cmd.Flags().BoolVar(&cfg.RemoveBlock, "rollback-evm", false, "remove EVM blocks as well") - cmd.Flags().Int64VarP(&cfg.RollbackHeights, "number", "n", 1, "number of blocks to rollback") + cmd.Flags().Uint64VarP(&cfg.RollbackHeights, "number", "n", 1, "number of blocks to rollback") } func bindValidatorUnjailFlags(cmd *cobra.Command, cfg *unjailConfig) { diff --git a/client/cmd/rollback.go b/client/cmd/rollback.go index 1629be67..8733c1ab 100644 --- a/client/cmd/rollback.go +++ b/client/cmd/rollback.go @@ -12,19 +12,21 @@ import ( "github.com/piplabs/story/lib/log" ) -type rollbackConfig struct { +type RollbackConfig struct { RemoveBlock bool // See cosmos-sdk/server/rollback.go - RollbackEVM bool - RollbackHeights int64 + RollbackHeights uint64 } -// newRollbackCmd returns a new cobra command that rolls back one block of the story consensus client. -func newRollbackCmd(appCreateFunc func(context.Context, app.Config) *app.App) *cobra.Command { - rollbackCfg := rollbackConfig{ +func DefaultConfig() RollbackConfig { + return RollbackConfig{ RemoveBlock: false, - RollbackEVM: false, RollbackHeights: 1, } +} + +// newRollbackCmd returns a new cobra command that rolls back one block of the story consensus client. +func newRollbackCmd(appCreateFunc func(context.Context, app.Config) *app.App) *cobra.Command { + rollbackCfg := DefaultConfig() storyCfg := storycfg.DefaultConfig() logCfg := log.DefaultConfig() @@ -60,7 +62,7 @@ will be re-executed against the application. Comet: cometCfg, } a := appCreateFunc(ctx, appCfg) - lastHeight, lastHash, err := app.RollbackCometAndAppState(ctx, a, appCfg, cometCfg, rollbackCfg.RollbackHeights, rollbackCfg.RemoveBlock, rollbackCfg.RollbackEVM) + lastHeight, lastHash, err := app.RollbackCometAndAppState(a, cometCfg, rollbackCfg) if err != nil { return err } From 5cc6dd4b111a48a7099d37da9bfe6493d0da9d20 Mon Sep 17 00:00:00 2001 From: Jongwon Park Date: Tue, 26 Nov 2024 18:28:40 -0600 Subject: [PATCH 5/5] fix(rollback): config cycle import --- client/app/rollback.go | 5 +++-- client/cmd/flags.go | 2 +- client/cmd/rollback.go | 18 +++--------------- client/config/rollback.go | 13 +++++++++++++ 4 files changed, 20 insertions(+), 18 deletions(-) create mode 100644 client/config/rollback.go diff --git a/client/app/rollback.go b/client/app/rollback.go index fe0a30f1..cb967ecf 100644 --- a/client/app/rollback.go +++ b/client/app/rollback.go @@ -3,11 +3,12 @@ package app import ( cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" cmtcfg "github.com/cometbft/cometbft/config" - "github.com/piplabs/story/client/cmd" + + "github.com/piplabs/story/client/config" "github.com/piplabs/story/lib/errors" ) -func RollbackCometAndAppState(a *App, cometCfg cmtcfg.Config, rollbackCfg cmd.RollbackConfig) (lastHeight int64, lastHash []byte, err error) { +func RollbackCometAndAppState(a *App, cometCfg cmtcfg.Config, rollbackCfg config.RollbackConfig) (lastHeight int64, lastHash []byte, err error) { for range rollbackCfg.RollbackHeights { lastHeight, lastHash, err = cmtcmd.RollbackState(&cometCfg, rollbackCfg.RemoveBlock) if err != nil { diff --git a/client/cmd/flags.go b/client/cmd/flags.go index d678274f..9be5a7ed 100644 --- a/client/cmd/flags.go +++ b/client/cmd/flags.go @@ -141,7 +141,7 @@ func bindKeyConvertFlags(cmd *cobra.Command, cfg *keyConfig) { cmd.Flags().StringVar(&cfg.PubKeyHexUncompressed, "pubkey-hex-uncompressed", "", "Uncompressed public key in hex format") } -func bindRollbackFlags(cmd *cobra.Command, cfg *RollbackConfig) { +func bindRollbackFlags(cmd *cobra.Command, cfg *config.RollbackConfig) { cmd.Flags().BoolVar(&cfg.RemoveBlock, "hard", false, "remove Cosmos & CometBFT blocks as well as states") cmd.Flags().Uint64VarP(&cfg.RollbackHeights, "number", "n", 1, "number of blocks to rollback") } diff --git a/client/cmd/rollback.go b/client/cmd/rollback.go index 8733c1ab..319264f7 100644 --- a/client/cmd/rollback.go +++ b/client/cmd/rollback.go @@ -7,27 +7,15 @@ import ( "github.com/spf13/cobra" "github.com/piplabs/story/client/app" - storycfg "github.com/piplabs/story/client/config" + cfg "github.com/piplabs/story/client/config" libcmd "github.com/piplabs/story/lib/cmd" "github.com/piplabs/story/lib/log" ) -type RollbackConfig struct { - RemoveBlock bool // See cosmos-sdk/server/rollback.go - RollbackHeights uint64 -} - -func DefaultConfig() RollbackConfig { - return RollbackConfig{ - RemoveBlock: false, - RollbackHeights: 1, - } -} - // newRollbackCmd returns a new cobra command that rolls back one block of the story consensus client. func newRollbackCmd(appCreateFunc func(context.Context, app.Config) *app.App) *cobra.Command { - rollbackCfg := DefaultConfig() - storyCfg := storycfg.DefaultConfig() + rollbackCfg := cfg.DefaultRollbackConfig() + storyCfg := cfg.DefaultConfig() logCfg := log.DefaultConfig() cmd := &cobra.Command{ diff --git a/client/config/rollback.go b/client/config/rollback.go new file mode 100644 index 00000000..c02a7da4 --- /dev/null +++ b/client/config/rollback.go @@ -0,0 +1,13 @@ +package config + +type RollbackConfig struct { + RemoveBlock bool // See cosmos-sdk/server/rollback.go + RollbackHeights uint64 +} + +func DefaultRollbackConfig() RollbackConfig { + return RollbackConfig{ + RemoveBlock: false, + RollbackHeights: 1, + } +}