From 2c31dd3858a28afcd1967fd1244a3dc3d7480210 Mon Sep 17 00:00:00 2001 From: Daniel T <30197399+danwt@users.noreply.github.com> Date: Wed, 27 Nov 2024 20:33:19 +0000 Subject: [PATCH] fix(iro): last plan id invariant fix (#1568) --- x/iro/keeper/invariants.go | 8 ++++++-- x/iro/keeper/iro.go | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/x/iro/keeper/invariants.go b/x/iro/keeper/invariants.go index 81ffa4e67..68cf56de9 100644 --- a/x/iro/keeper/invariants.go +++ b/x/iro/keeper/invariants.go @@ -35,8 +35,12 @@ func InvariantPlan(k Keeper) uinv.Func { } lastPlanID := k.GetLastPlanId(ctx) - if lastPlanID != plans[len(plans)-1].Id { - return fmt.Errorf("last plan id mismatch: lastPlanID: %d, lastPlanInListID: %d", lastPlanID, plans[len(plans)-1].Id) + max_ := plans[0].Id + for _, plan := range plans { + max_ = max(plan.Id, max_) + } + if lastPlanID != max_ { + return fmt.Errorf("last plan id mismatch: lastPlanID: %d, max: %d", lastPlanID, max_) } var errs []error diff --git a/x/iro/keeper/iro.go b/x/iro/keeper/iro.go index eafd7cb9a..0ea7aa971 100644 --- a/x/iro/keeper/iro.go +++ b/x/iro/keeper/iro.go @@ -64,7 +64,7 @@ func (k Keeper) MustGetPlanByRollapp(ctx sdk.Context, rollappId string) types.Pl return plan } -// GetAllPlans returns all plans +// GetAllPlans returns plans sorted lexically by ID e.g. 1,10,100... func (k Keeper) GetAllPlans(ctx sdk.Context, tradableOnly bool) (list []types.Plan) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PlanKeyPrefix) iterator := sdk.KVStorePrefixIterator(store, []byte{})