Skip to content

Commit

Permalink
Remove active subscriptions from all quotas while cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Jun 15, 2021
1 parent 2912ae6 commit a842136
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
37 changes: 19 additions & 18 deletions x/subscription/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ func EndBlock(ctx sdk.Context, k keeper.Keeper) []abcitypes.ValidatorUpdate {
k.IterateInactiveSubscriptions(ctx, ctx.BlockTime(), func(_ int, key []byte, item types.Subscription) bool {
log.Info("inactive subscription", "key", key, "value", item)

if item.Status.Equal(hubtypes.StatusActive) {
k.DeleteInactiveSubscriptionAt(ctx, item.Expiry, item.Id)
k.IterateQuotas(ctx, item.Id, func(_ int, quota types.Quota) bool {
address := quota.GetAddress()
k.DeleteActiveSubscriptionForAddress(ctx, address, item.Id)
k.SetInactiveSubscriptionForAddress(ctx, address, item.Id)

return false
})

item.Status = hubtypes.StatusInactivePending
item.StatusAt = ctx.BlockTime()

k.SetSubscription(ctx, item)
k.SetInactiveSubscriptionAt(ctx, item.StatusAt.Add(inactiveDuration), item.Id)

return false
}

if item.Plan == 0 {
consumed := sdk.ZeroInt()
k.IterateQuotas(ctx, item.Id, func(_ int, quota types.Quota) bool {
Expand All @@ -33,27 +52,9 @@ func EndBlock(ctx sdk.Context, k keeper.Keeper) []abcitypes.ValidatorUpdate {
if err := k.SubtractDeposit(ctx, itemOwner, amount); err != nil {
log.Error("failed to subtract the deposit", "cause", err)
}
} else {
if item.Status.Equal(hubtypes.StatusActive) {
item.Status = hubtypes.StatusInactivePending
item.StatusAt = item.Expiry

k.SetSubscription(ctx, item)
k.DeleteInactiveSubscriptionAt(ctx, item.Expiry, item.Id)
k.SetInactiveSubscriptionAt(ctx, item.Expiry.Add(inactiveDuration), item.Id)

return false
}
}

k.DeleteInactiveSubscriptionAt(ctx, item.StatusAt.Add(inactiveDuration), item.Id)
k.IterateQuotas(ctx, item.Id, func(_ int, quota types.Quota) bool {
quotaAddress := quota.GetAddress()
k.DeleteActiveSubscriptionForAddress(ctx, quotaAddress, item.Id)
k.SetInactiveSubscriptionForAddress(ctx, quotaAddress, item.Id)

return false
})

item.Status = hubtypes.StatusInactive
item.StatusAt = ctx.BlockTime()
Expand Down
10 changes: 9 additions & 1 deletion x/subscription/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,19 @@ func (k *msgServer) MsgCancel(c context.Context, msg *types.MsgCancelRequest) (*
k.DeleteInactiveSubscriptionAt(ctx, subscription.Expiry, subscription.Id)
}

k.IterateQuotas(ctx, subscription.Id, func(_ int, quota types.Quota) bool {
address := quota.GetAddress()
k.DeleteActiveSubscriptionForAddress(ctx, address, subscription.Id)
k.SetInactiveSubscriptionForAddress(ctx, address, subscription.Id)

return false
})

subscription.Status = hubtypes.StatusInactivePending
subscription.StatusAt = ctx.BlockTime()
k.SetInactiveSubscriptionAt(ctx, ctx.BlockTime().Add(inactiveDuration), subscription.Id)

k.SetSubscription(ctx, subscription)
k.SetInactiveSubscriptionAt(ctx, subscription.StatusAt.Add(inactiveDuration), subscription.Id)
ctx.EventManager().EmitTypedEvent(
&types.EventCancelSubscription{
From: sdk.AccAddress(msgFrom.Bytes()).String(),
Expand Down

0 comments on commit a842136

Please sign in to comment.