Skip to content

Commit

Permalink
cherry-pick from cosmos#7817
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Nov 16, 2020
1 parent 36be3f4 commit 79a6cd8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
4 changes: 3 additions & 1 deletion store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ func (rs *Store) TracingEnabled() bool {
// LastCommitID implements Committer/CommitStore.
func (rs *Store) LastCommitID() types.CommitID {
if rs.lastCommitInfo == nil {
return types.CommitID{}
return types.CommitID{
Version: getLatestVersion(rs.db),
}
}

return rs.lastCommitInfo.CommitID()
Expand Down
6 changes: 2 additions & 4 deletions x/upgrade/types/storeloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
store "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -11,11 +10,10 @@ import (
// pattern. This is useful for custom upgrade loading logic.
func UpgradeStoreLoader(upgradeHeight int64, storeUpgrades *store.StoreUpgrades) baseapp.StoreLoader {
return func(ms sdk.CommitMultiStore) error {
latestVersion := ms.(*rootmulti.Store).GetLatestVersion()
if upgradeHeight == latestVersion+1 {
if upgradeHeight == ms.LastCommitID().Version+1 {
// Check if the current commit version and upgrade height matches
if len(storeUpgrades.Renamed) > 0 ||
len(storeUpgrades.Deleted) > 0 || len(storeUpgrades.Added) > 0 {
len(storeUpgrades.Deleted) > 0 || len(storeUpgrades.Added) > 0 || len(storeUpgrades.Added) > 0 {
return ms.LoadLatestVersionAndUpgrade(storeUpgrades)
}
}
Expand Down
42 changes: 25 additions & 17 deletions x/upgrade/types/storeloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ func checkStore(t *testing.T, db dbm.DB, ver int64, storeKey string, k, v []byte
// Test that we can make commits and then reload old versions.
// Test that LoadLatestVersion actually does.
func TestSetLoader(t *testing.T) {
upgradeHeight := int64(5)

// set a temporary home dir
homeDir := t.TempDir()
upgradeInfoFilePath := filepath.Join(homeDir, "upgrade-info.json")
upgradeInfo := &store.UpgradeInfo{
Name: "test", Height: 0,
Name: "test", Height: upgradeHeight,
}

data, err := json.Marshal(upgradeInfo)
Expand All @@ -82,8 +84,6 @@ func TestSetLoader(t *testing.T) {
_, err = os.Stat(upgradeInfoFilePath)
require.NoError(t, err)

upgradeHeight := int64(2)

cases := map[string]struct {
setLoader func(*baseapp.BaseApp)
origStoreKey string
Expand Down Expand Up @@ -113,33 +113,41 @@ func TestSetLoader(t *testing.T) {
t.Run(name, func(t *testing.T) {
// prepare a db with some data
db := dbm.NewMemDB()

initStore(t, db, tc.origStoreKey, k, v)

// load the app with the existing db
opts := []func(*baseapp.BaseApp){baseapp.SetPruning(store.PruneNothing)}

origapp := baseapp.NewBaseApp(t.Name(), defaultLogger(), db, nil, opts...)
origapp.MountStores(sdk.NewKVStoreKey(tc.origStoreKey))
err := origapp.LoadLatestVersion()
require.Nil(t, err)

for i := int64(2); i <= upgradeHeight-1; i++ {
origapp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: i}})
res := origapp.Commit()
require.NotNil(t, res.Data)
}

if tc.setLoader != nil {
opts = append(opts, tc.setLoader)
}

// load the new app with the original app db
app := baseapp.NewBaseApp(t.Name(), defaultLogger(), db, nil, opts...)
capKey := sdk.NewKVStoreKey("main")
app.MountStores(capKey)
app.MountStores(sdk.NewKVStoreKey(tc.loadStoreKey))
err := app.LoadLatestVersion()
err = app.LoadLatestVersion()
require.Nil(t, err)

for i := app.LastBlockHeight() + 1; i <= upgradeHeight * 2; i++ {
// "execute" one block
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: i}})
res := app.Commit()
require.NotNil(t, res.Data)
// "execute" one block
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: upgradeHeight}})
res := app.Commit()
require.NotNil(t, res.Data)

if i >= upgradeHeight {
// check db is properly updated
checkStore(t, db, i, tc.loadStoreKey, k, v)
checkStore(t, db, i, tc.loadStoreKey, []byte("foo"), nil)
}
}
// check db is properly updated
checkStore(t, db, upgradeHeight, tc.loadStoreKey, k, v)
checkStore(t, db, upgradeHeight, tc.loadStoreKey, []byte("foo"), nil)
})
}
}

0 comments on commit 79a6cd8

Please sign in to comment.