Skip to content

Commit

Permalink
chore: optimize get last completed upgrade (cosmos#12268)
Browse files Browse the repository at this point in the history
* feat: improve GetLastCompletedUpgrade

* rename

* use var block
  • Loading branch information
robert-zaremba authored and randy75828 committed Aug 10, 2022
1 parent e3bf809 commit 2e28d4d
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions x/upgrade/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,20 @@ func (k Keeper) GetLastCompletedUpgrade(ctx sdk.Context) (string, int64) {
iter := sdk.KVStoreReversePrefixIterator(ctx.KVStore(k.storeKey), []byte{types.DoneByte})
defer iter.Close()

var upgrades []upgrade
var (
latest upgrade
found bool
)
for ; iter.Valid(); iter.Next() {
name := parseDoneKey(iter.Key())
value := int64(sdk.BigEndianToUint64(iter.Value()))

upgrades = append(upgrades, upgrade{Name: name, BlockHeight: value})
}

// sort upgrades in descending order by block height
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
upgradeHeight := int64(sdk.BigEndianToUint64(iter.Value()))
if !found || upgradeHeight >= latest.BlockHeight {
found = true
name := parseDoneKey(iter.Key())
latest = upgrade{Name: name, BlockHeight: upgradeHeight}
}
}

return "", 0
return latest.Name, latest.BlockHeight
}

// parseDoneKey - split upgrade name from the done key
Expand Down

0 comments on commit 2e28d4d

Please sign in to comment.