Skip to content
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: invalid return value from keeper GetLastCompleteUpgrade #11705

24 changes: 22 additions & 2 deletions x/upgrade/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,32 @@ func (k Keeper) GetUpgradedConsensusState(ctx sdk.Context, lastHeight int64) ([]
return bz, true
}

type upgrade struct {
Name string
BlockHeight int64
}

// GetLastCompletedUpgrade returns the last applied upgrade name and height.
func (k Keeper) GetLastCompletedUpgrade(ctx sdk.Context) (string, int64) {
iter := sdk.KVStoreReversePrefixIterator(ctx.KVStore(k.storeKey), []byte{types.DoneByte})
defer iter.Close()
if iter.Valid() {
return parseDoneKey(iter.Key()), int64(binary.BigEndian.Uint64(iter.Value()))

upgrades := []upgrade{}
for iter.Valid() {
name := parseDoneKey(iter.Key())
value := int64(binary.BigEndian.Uint64(iter.Value()))
upgrades = append(upgrades, struct {
Name string
BlockHeight int64
}{Name: name, BlockHeight: value})
daeMOn63 marked this conversation as resolved.
Show resolved Hide resolved
iter.Next()
}
sort.SliceStable(upgrades, func(i, j int) bool {
return upgrades[i].BlockHeight > upgrades[j].BlockHeight
})

if len(upgrades) > 0 {
return upgrades[0].Name, upgrades[0].BlockHeight
}

return "", 0
Expand Down
34 changes: 34 additions & 0 deletions x/upgrade/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,40 @@ func (s *KeeperTestSuite) TestLastCompletedUpgrade() {
require.Equal(int64(15), height)
}

func (s *KeeperTestSuite) TestLastCompletedUpgradeOrdering() {
keeper := s.app.UpgradeKeeper
require := s.Require()

// apply first upgrade
keeper.SetUpgradeHandler("test-v0.9", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
return vm, nil
})

keeper.ApplyUpgrade(s.ctx, types.Plan{
Name: "test-v0.9",
Height: 10,
})

name, height := keeper.GetLastCompletedUpgrade(s.ctx)
require.Equal("test-v0.9", name)
require.Equal(int64(10), height)

// apply second upgrade
keeper.SetUpgradeHandler("test-v0.10", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
return vm, nil
})

newCtx := s.ctx.WithBlockHeight(15)
keeper.ApplyUpgrade(newCtx, types.Plan{
Name: "test-v0.10",
Height: 15,
})

name, height = keeper.GetLastCompletedUpgrade(newCtx)
require.Equal("test-v0.10", name)
require.Equal(int64(15), height)
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}