-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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: Implement rollback command #11179
Merged
+95
−20
Merged
Changes from all commits
Commits
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
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,49 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/store/rootmulti" | ||
"github.com/spf13/cobra" | ||
tmcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" | ||
) | ||
|
||
// NewRollbackCmd creates a command to rollback tendermint and multistore state by one height. | ||
func NewRollbackCmd(defaultNodeHome string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "rollback", | ||
Short: "rollback cosmos-sdk and tendermint state by one height", | ||
Long: ` | ||
A state rollback is performed to recover from an incorrect application state transition, | ||
when Tendermint 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 roll back to height n - 1. No blocks are removed, so upon | ||
restarting Tendermint the transactions in block n will be re-executed against the | ||
application. | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := GetServerContextFromCmd(cmd) | ||
cfg := ctx.Config | ||
home := cfg.RootDir | ||
db, err := openDB(home) | ||
if err != nil { | ||
return err | ||
} | ||
// rollback tendermint state | ||
height, hash, err := tmcmd.RollbackState(ctx.Config) | ||
if err != nil { | ||
return fmt.Errorf("failed to rollback tendermint state: %w", err) | ||
} | ||
// rollback the multistore | ||
cms := rootmulti.NewStore(db) | ||
cms.RollbackToVersion(height) | ||
|
||
fmt.Printf("Rolled back state to height %d and hash %X", height, hash) | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory") | ||
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 |
---|---|---|
|
@@ -906,6 +906,30 @@ func (rs *Store) buildCommitInfo(version int64) *types.CommitInfo { | |
} | ||
} | ||
|
||
// RollbackToVersion delete the versions after `target` and update the latest version. | ||
func (rs *Store) RollbackToVersion(target int64) int64 { | ||
if target < 0 { | ||
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. the input target should be always greater than 0 right? the State.InitialHeight will be 1. 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. yes, I think so, just for safety here. |
||
panic("Negative rollback target") | ||
} | ||
current := getLatestVersion(rs.db) | ||
if target >= current { | ||
return current | ||
} | ||
for ; current > target; current-- { | ||
rs.pruneHeights = append(rs.pruneHeights, current) | ||
} | ||
rs.pruneStores() | ||
|
||
// update latest height | ||
bz, err := gogotypes.StdInt64Marshal(current) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
rs.db.Set([]byte(latestVersionKey), bz) | ||
return current | ||
} | ||
|
||
type storeParams struct { | ||
key types.StoreKey | ||
db dbm.DB | ||
|
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.
hmm, we need to be sure that both tendermint an SDK will rollback to the same version. In
RollbackToVersion
we can essentially rollback to an older version than the target.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.
RollbackToVersion
only prune heights that are bigger than theheight
returned by tendermint's rollback.