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

chore: optimize get last completed upgrade #12268

Merged
merged 3 commits into from
Jul 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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