-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: invalid return value from keeper GetLastCompleteUpgrade - break…
- Loading branch information
1 parent
7e4d5d7
commit ec252e2
Showing
6 changed files
with
175 additions
and
17 deletions.
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,43 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/binary" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
) | ||
|
||
// Migrator is a struct for handling in-place store migrations. | ||
type Migrator struct { | ||
keeper Keeper | ||
} | ||
|
||
// NewMigrator returns a new Migrator. | ||
func NewMigrator(keeper Keeper) Migrator { | ||
return Migrator{keeper: keeper} | ||
} | ||
|
||
// Migrate1to2 migrates from version 1 to 2. | ||
func (m Migrator) Migrate1to2(ctx sdk.Context) error { | ||
return migrateDoneUpgradeKeys(ctx, m.keeper.storeKey) | ||
} | ||
|
||
func migrateDoneUpgradeKeys(ctx sdk.Context, storeKey storetypes.StoreKey) error { | ||
store := ctx.KVStore(storeKey) | ||
oldDoneStore := prefix.NewStore(store, []byte{types.DoneByte}) | ||
oldDoneStoreIter := oldDoneStore.Iterator(nil, nil) | ||
defer oldDoneStoreIter.Close() | ||
|
||
for ; oldDoneStoreIter.Valid(); oldDoneStoreIter.Next() { | ||
oldKey := oldDoneStoreIter.Key() | ||
upgradeName := string(oldKey) | ||
upgradeHeight := int64(binary.BigEndian.Uint64(oldDoneStoreIter.Value())) | ||
newKey := encodeDoneKey(upgradeName, upgradeHeight) | ||
|
||
store.Set(newKey, []byte{1}) | ||
oldDoneStore.Delete(oldKey) | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/binary" | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/testutil" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type storedUpgrade struct { | ||
name string | ||
height int64 | ||
} | ||
|
||
func encodeOldDoneKey(upgrade storedUpgrade) []byte { | ||
return append([]byte{types.DoneByte}, []byte(upgrade.name)...) | ||
} | ||
|
||
func TestMigrateDoneUpgradeKeys(t *testing.T) { | ||
upgradeKey := sdk.NewKVStoreKey("upgrade") | ||
ctx := testutil.DefaultContext(upgradeKey, sdk.NewTransientStoreKey("transient_test")) | ||
store := ctx.KVStore(upgradeKey) | ||
|
||
testCases := []struct { | ||
name string | ||
upgrades []storedUpgrade | ||
}{ | ||
{ | ||
name: "valid upgrades", | ||
upgrades: []storedUpgrade{ | ||
{name: "some-other-upgrade", height: 1}, | ||
{name: "test02", height: 2}, | ||
{name: "test01", height: 3}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
for _, upgrade := range tc.upgrades { | ||
bz := make([]byte, 8) | ||
binary.BigEndian.PutUint64(bz, uint64(upgrade.height)) | ||
oldKey := encodeOldDoneKey(upgrade) | ||
store.Set(oldKey, bz) | ||
} | ||
|
||
err := migrateDoneUpgradeKeys(ctx, upgradeKey) | ||
require.NoError(t, err) | ||
|
||
for _, upgrade := range tc.upgrades { | ||
newKey := encodeDoneKey(upgrade.name, upgrade.height) | ||
oldKey := encodeOldDoneKey(upgrade) | ||
require.Nil(t, store.Get(oldKey)) | ||
require.Equal(t, []byte{1}, store.Get(newKey)) | ||
} | ||
} | ||
} |
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