-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
fix: rollback command don't actually delete multistore versions #11361
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,69 @@ | ||
package rootmulti_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" | ||
"github.com/stretchr/testify/require" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
func TestRollback(t *testing.T) { | ||
db := dbm.NewMemDB() | ||
options := simapp.SetupOptions{ | ||
Logger: log.NewNopLogger(), | ||
DB: db, | ||
AppOpts: simtestutil.NewAppOptionsWithFlagHome(simapp.DefaultNodeHome), | ||
} | ||
app := simapp.NewSimappWithCustomOptions(t, false, options) | ||
app.Commit() | ||
ver0 := app.LastBlockHeight() | ||
// commit 10 blocks | ||
for i := int64(1); i <= 10; i++ { | ||
header := tmproto.Header{ | ||
Height: ver0 + i, | ||
AppHash: app.LastCommitID().Hash, | ||
} | ||
app.BeginBlock(abci.RequestBeginBlock{Header: header}) | ||
ctx := app.NewContext(false, header) | ||
store := ctx.KVStore(app.GetKey("bank")) | ||
store.Set([]byte("key"), []byte(fmt.Sprintf("value%d", i))) | ||
app.Commit() | ||
} | ||
|
||
require.Equal(t, ver0+10, app.LastBlockHeight()) | ||
store := app.NewContext(true, tmproto.Header{}).KVStore(app.GetKey("bank")) | ||
require.Equal(t, []byte("value10"), store.Get([]byte("key"))) | ||
|
||
// rollback 5 blocks | ||
target := ver0 + 5 | ||
require.NoError(t, app.CommitMultiStore().RollbackToVersion(target)) | ||
require.Equal(t, target, app.LastBlockHeight()) | ||
|
||
// recreate app to have clean check state | ||
app = simapp.NewSimApp(options.Logger, options.DB, nil, true, simtestutil.NewAppOptionsWithFlagHome(simapp.DefaultNodeHome)) | ||
store = app.NewContext(true, tmproto.Header{}).KVStore(app.GetKey("bank")) | ||
require.Equal(t, []byte("value5"), store.Get([]byte("key"))) | ||
|
||
// commit another 5 blocks with different values | ||
for i := int64(6); i <= 10; i++ { | ||
header := tmproto.Header{ | ||
Height: ver0 + i, | ||
AppHash: app.LastCommitID().Hash, | ||
} | ||
app.BeginBlock(abci.RequestBeginBlock{Header: header}) | ||
ctx := app.NewContext(false, header) | ||
store := ctx.KVStore(app.GetKey("bank")) | ||
store.Set([]byte("key"), []byte(fmt.Sprintf("VALUE%d", i))) | ||
app.Commit() | ||
} | ||
|
||
require.Equal(t, ver0+10, app.LastBlockHeight()) | ||
store = app.NewContext(true, tmproto.Header{}).KVStore(app.GetKey("bank")) | ||
require.Equal(t, []byte("VALUE10"), store.Get([]byte("key"))) | ||
} |
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 |
---|---|---|
|
@@ -933,29 +933,26 @@ 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 { | ||
panic("Negative rollback target") | ||
} | ||
current := getLatestVersion(rs.db) | ||
if target >= current { | ||
return current | ||
} | ||
for ; current > target; current-- { | ||
rs.pruningManager.HandleHeight(current) | ||
} | ||
if err := rs.pruneStores(); err != nil { | ||
panic(err) | ||
func (rs *Store) RollbackToVersion(target int64) error { | ||
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. LGTM. Isn't there other metadata we need to flush too? 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.
is there? |
||
if target <= 0 { | ||
yihuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fmt.Errorf("invalid rollback height target: %d", target) | ||
} | ||
|
||
// update latest height | ||
bz, err := gogotypes.StdInt64Marshal(current) | ||
if err != nil { | ||
panic(err) | ||
for key, store := range rs.stores { | ||
if store.GetStoreType() == types.StoreTypeIAVL { | ||
// If the store is wrapped with an inter-block cache, we must first unwrap | ||
// it to get the underlying IAVL store. | ||
store = rs.GetCommitKVStore(key) | ||
_, err := store.(*iavl.Store).LoadVersionForOverwriting(target) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
rs.db.Set([]byte(latestVersionKey), bz) | ||
return current | ||
rs.flushMetadata(rs.db, target, rs.buildCommitInfo(target)) | ||
|
||
return rs.LoadLatestVersion() | ||
} | ||
|
||
func (rs *Store) flushMetadata(db dbm.DB, version int64, cInfo *types.CommitInfo) { | ||
|
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
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.
why we are adding a function which panics?
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.
because the method is added to the interface
CommitMultiStore
. so we need this to make the unit tests compile.