From 741050dd7795004b8102f8011bab2f795369acd4 Mon Sep 17 00:00:00 2001 From: Oren Laadan Date: Sun, 18 Jun 2023 13:51:59 +0800 Subject: [PATCH] CNS-434: fix two silent bugs in subscription logic 1. CreateSubscription() should only update the expiry time if adding a new subscription, otherwise upon update/extension. 2. GetPlanFromSubscription() should use the recorded Plan block rather than that of when the subscription created. --- x/subscription/keeper/subscription.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/x/subscription/keeper/subscription.go b/x/subscription/keeper/subscription.go index 420b2f7f81..0c1f161563 100644 --- a/x/subscription/keeper/subscription.go +++ b/x/subscription/keeper/subscription.go @@ -211,19 +211,16 @@ func (k Keeper) CreateSubscription( utils.Attribute{Key: "duration", Value: sub.DurationLeft}, ) } + + // use current block's timestamp to calculate next month's time + expiry := nextMonth(ctx.BlockTime()) + sub.MonthExpiryTime = uint64(expiry.Unix()) } // update total (last requested) duration and remaining duration sub.DurationTotal = duration sub.DurationLeft += duration - // use current block's timestamp to calculate next month's time - timestamp := ctx.BlockTime() - - expiry := nextMonth(timestamp) - - sub.MonthExpiryTime = uint64(expiry.Unix()) - if err := sub.ValidateSubscription(); err != nil { return utils.LavaFormatWarning("create subscription failed", err) } @@ -269,7 +266,7 @@ func (k Keeper) GetPlanFromSubscription(ctx sdk.Context, consumer string) (plans ) } - plan, found := k.plansKeeper.FindPlan(ctx, sub.PlanIndex, sub.Block) + plan, found := k.plansKeeper.FindPlan(ctx, sub.PlanIndex, sub.PlanBlock) if !found { err := utils.LavaFormatError("can't find plan from subscription with consumer address", sdkerrors.ErrKeyNotFound, utils.Attribute{Key: "consumer", Value: consumer},