Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): make rollback command generic #23321

Open
wants to merge 3 commits into
base: release/v0.52.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i

* (sims) [#23013](https://github.com/cosmos/cosmos-sdk/pull/23013) Integration with app v2
* (x/auth/ante) [#23128](https://github.com/cosmos/cosmos-sdk/pull/23128) Allow custom verifyIsOnCurve when validate tx for public key like ethsecp256k1.
* (server) [#23128](https://github.com/cosmos/cosmos-sdk/pull/23128) Add custom rollback command option. In order to use it, you need to implement the Rollback interface and remove the default rollback command with `cmd.RemoveCommand(cmd.RollbackCmd)` and then add it back with `cmd.AddCommand(cmd.NewCustomRollbackCmd(appCreator, rollbackable))`.

### Improvements

Expand Down
43 changes: 43 additions & 0 deletions server/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,46 @@ application.
cmd.Flags().BoolVar(&removeBlock, "hard", false, "remove last block as well as state")
return cmd
}

// NewRollbackCmdRollback creates a command to set custom rollback functionality and multistore state by one height.
func NewRollbackCmdRollback[T types.Application, R Rollback](appCreator types.AppCreator[T], rollbackable R) *cobra.Command {
var removeBlock bool

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, args []string) error {
ctx := GetServerContextFromCmd(cmd)

db, err := OpenDB(ctx.Config.RootDir, GetAppDBBackend(ctx.Viper))
if err != nil {
return err
}
app := appCreator(ctx.Logger, db, nil, ctx.Viper)
// rollback CometBFT state
height, hash, err := rollbackable.RollbackToVersion(ctx, removeBlock)
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
},
}

cmd.Flags().BoolVar(&removeBlock, "hard", false, "remove last block as well as state")
return cmd
}
28 changes: 25 additions & 3 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func interceptConfigs(rootViper *viper.Viper, customAppTemplate string, customCo
}

// AddCommands add server commands
func AddCommands[T types.Application](rootCmd *cobra.Command, appCreator types.AppCreator[T], opts StartCmdOptions[T]) {
func AddCommands[T types.Application, R Rollback](rootCmd *cobra.Command, appCreator types.AppCreator[T], rollbackable R, opts StartCmdOptions[T]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should revert this imho.

cometCmd := &cobra.Command{
Use: "comet",
Aliases: []string{"cometbft", "tendermint"},
Expand Down Expand Up @@ -361,8 +361,8 @@ func AddCommands[T types.Application](rootCmd *cobra.Command, appCreator types.A

// AddCommandsWithStartCmdOptions adds server commands with the provided StartCmdOptions.
// Deprecated: Use AddCommands directly instead.
func AddCommandsWithStartCmdOptions[T types.Application](rootCmd *cobra.Command, appCreator types.AppCreator[T], opts StartCmdOptions[T]) {
AddCommands(rootCmd, appCreator, opts)
func AddCommandsWithStartCmdOptions[T types.Application, R Rollback](rootCmd *cobra.Command, appCreator types.AppCreator[T], rollbackable R, opts StartCmdOptions[T]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

AddCommands(rootCmd, appCreator, rollbackable, opts)
}

// AddTestnetCreatorCommand allows chains to create a testnet from the state existing in their node's data directory.
Expand Down Expand Up @@ -580,3 +580,25 @@ func GetSnapshotStore(appOpts types.AppOptions) (*snapshots.Store, error) {

return snapshotStore, nil
}

// Rollbackable is an interface that allows for rollback operations.
// It is used to allow for custom rollback operations, such as those provided by the
// DefaultRollbackable implementation.
type Rollback interface {
RollbackToVersion(ctx *Context, removeBlock bool) (int64, []byte, error)
}

// DefaultRollbackable is a default implementation of the Rollbackable interface.
type DefaultRollbackable[T types.Application] struct {
appCreator types.AppCreator[T]
}

// NewDefaultRollbackable creates a new DefaultRollbackable instance.
func NewDefaultRollbackable[T types.Application](appCreator types.AppCreator[T]) *DefaultRollbackable[T] {
return &DefaultRollbackable[T]{appCreator}
}

// RollbackToVersion implements the Rollbackable interface.
func (d DefaultRollbackable[T]) RollbackToVersion(ctx *Context, removeBlock bool) (int64, []byte, error) {
return cmtcmd.RollbackState(ctx.Config, removeBlock)
}
2 changes: 1 addition & 1 deletion simapp/simd/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func initRootCmd(
snapshot.Cmd(newApp),
)

server.AddCommands(rootCmd, newApp, server.StartCmdOptions[servertypes.Application]{})
server.AddCommands(rootCmd, newApp, server.NewDefaultRollbackable(newApp), server.StartCmdOptions[servertypes.Application]{})

// add keybase, auxiliary RPC, query, genesis, and tx child commands
rootCmd.AddCommand(
Expand Down
Loading