-
Notifications
You must be signed in to change notification settings - Fork 26
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(app): rollback many blocks #349
Open
jdubpark
wants to merge
5
commits into
main
Choose a base branch
from
jdub/rollback-many
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f9f1d2
feat(app): rollback many blocks
jdubpark 9cd6375
feat(rollback): optional EVM block rollback
jdubpark 82a0178
lint(client): fix lint
jdubpark e78cb90
feat(rollback): remove evm rollback and fix fn args
jdubpark 5cc6dd4
fix(rollback): config cycle import
jdubpark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,24 @@ | ||
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/client/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 | ||
} | ||
|
||
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) | ||
} | ||
|
||
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") | ||
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 { | ||
return lastHeight, lastHash, errors.Wrap(err, "failed to rollback CometBFT 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") | ||
if err = a.CommitMultiStore().RollbackToVersion(lastHeight); err != nil { | ||
return lastHeight, lastHash, errors.Wrap(err, "failed to rollback to version") | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/piplabs/story/client/app" | ||
cfg "github.com/piplabs/story/client/config" | ||
libcmd "github.com/piplabs/story/lib/cmd" | ||
"github.com/piplabs/story/lib/log" | ||
) | ||
|
||
// 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 := cfg.DefaultRollbackConfig() | ||
storyCfg := cfg.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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to update the description here. --hard is not supported any more. |
||
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 | ||
} | ||
|
||
appCfg := app.Config{ | ||
Config: storyCfg, | ||
Comet: cometCfg, | ||
} | ||
a := appCreateFunc(ctx, appCfg) | ||
lastHeight, lastHash, err := app.RollbackCometAndAppState(a, cometCfg, rollbackCfg) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably don't need to support RemoveBlock flag since as we discussed, removeBlock currently won't work in our case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a parameter accepted by
Rollback
exposed in CometBFT. We could set it tofalse
and not take the parameter externally if that's what you mean.