From 031c6ac2ed6da91655f4b755936d82335febd3d2 Mon Sep 17 00:00:00 2001 From: Srinivas Baride Date: Sun, 19 Sep 2021 19:56:36 +0530 Subject: [PATCH] Added no active sessions condition while cancelling the subscription --- x/subscription/expected/keeper.go | 5 +++++ x/subscription/keeper/alias.go | 5 +++++ x/subscription/keeper/keeper.go | 10 +++++----- x/subscription/keeper/msg_server.go | 10 ++++++++++ x/subscription/types/errors.go | 1 + x/vpn/keeper/keeper.go | 1 + 6 files changed, 27 insertions(+), 5 deletions(-) diff --git a/x/subscription/expected/keeper.go b/x/subscription/expected/keeper.go index 51363e83..30759e9f 100644 --- a/x/subscription/expected/keeper.go +++ b/x/subscription/expected/keeper.go @@ -7,6 +7,7 @@ import ( hubtypes "github.com/sentinel-official/hub/types" nodetypes "github.com/sentinel-official/hub/x/node/types" plantypes "github.com/sentinel-official/hub/x/plan/types" + sessiontypes "github.com/sentinel-official/hub/x/session/types" ) type AccountKeeper interface { @@ -30,3 +31,7 @@ type NodeKeeper interface { type PlanKeeper interface { GetPlan(ctx sdk.Context, id uint64) (plantypes.Plan, bool) } + +type SessionKeeper interface { + GetActiveSessionsForAddress(ctx sdk.Context, address sdk.AccAddress, skip, limit int64) sessiontypes.Sessions +} diff --git a/x/subscription/keeper/alias.go b/x/subscription/keeper/alias.go index 2eb146c2..dbc97d84 100644 --- a/x/subscription/keeper/alias.go +++ b/x/subscription/keeper/alias.go @@ -6,6 +6,7 @@ import ( hubtypes "github.com/sentinel-official/hub/types" nodetypes "github.com/sentinel-official/hub/x/node/types" plantypes "github.com/sentinel-official/hub/x/plan/types" + sessiontypes "github.com/sentinel-official/hub/x/session/types" ) func (k *Keeper) SendCoin(ctx sdk.Context, from sdk.AccAddress, to sdk.AccAddress, coin sdk.Coin) error { @@ -39,3 +40,7 @@ func (k *Keeper) GetNode(ctx sdk.Context, address hubtypes.NodeAddress) (nodetyp func (k *Keeper) GetPlan(ctx sdk.Context, id uint64) (plantypes.Plan, bool) { return k.plan.GetPlan(ctx, id) } + +func (k *Keeper) GetActiveSessionsForAddress(ctx sdk.Context, address sdk.AccAddress, skip, limit int64) sessiontypes.Sessions { + return k.session.GetActiveSessionsForAddress(ctx, address, skip, limit) +} diff --git a/x/subscription/keeper/keeper.go b/x/subscription/keeper/keeper.go index d1a51014..3376f1fb 100644 --- a/x/subscription/keeper/keeper.go +++ b/x/subscription/keeper/keeper.go @@ -21,7 +21,7 @@ type Keeper struct { deposit expected.DepositKeeper node expected.NodeKeeper plan expected.PlanKeeper - account expected.AccountKeeper + session expected.SessionKeeper } func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey, params paramstypes.Subspace) Keeper { @@ -32,10 +32,6 @@ func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey, params paramstypes.S } } -func (k *Keeper) WithAccountKeeper(keeper expected.AccountKeeper) { - k.account = keeper -} - func (k *Keeper) WithBankKeeper(keeper expected.BankKeeper) { k.bank = keeper } @@ -52,6 +48,10 @@ func (k *Keeper) WithPlanKeeper(keeper expected.PlanKeeper) { k.plan = keeper } +func (k *Keeper) WithSessionKeeper(keeper expected.SessionKeeper) { + k.session = keeper +} + func (k *Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", "x/"+types.ModuleName) } diff --git a/x/subscription/keeper/msg_server.go b/x/subscription/keeper/msg_server.go index f01599a6..cc05575e 100644 --- a/x/subscription/keeper/msg_server.go +++ b/x/subscription/keeper/msg_server.go @@ -175,6 +175,11 @@ func (k *msgServer) MsgSubscribeToPlan(c context.Context, msg *types.MsgSubscrib func (k *msgServer) MsgCancel(c context.Context, msg *types.MsgCancelRequest) (*types.MsgCancelResponse, error) { ctx := sdk.UnwrapSDKContext(c) + msgFrom, err := sdk.AccAddressFromBech32(msg.From) + if err != nil { + return nil, err + } + subscription, found := k.GetSubscription(ctx, msg.Id) if !found { return nil, types.ErrorSubscriptionDoesNotExist @@ -186,6 +191,11 @@ func (k *msgServer) MsgCancel(c context.Context, msg *types.MsgCancelRequest) (* return nil, types.ErrorInvalidSubscriptionStatus } + items := k.GetActiveSessionsForAddress(ctx, msgFrom, 0, 1) + if len(items) > 0 { + return nil, types.ErrorCanNotCancel + } + inactiveDuration := k.InactiveDuration(ctx) if subscription.Plan > 0 { k.DeleteInactiveSubscriptionAt(ctx, subscription.Expiry, subscription.Id) diff --git a/x/subscription/types/errors.go b/x/subscription/types/errors.go index 12869afb..7dd4f661 100644 --- a/x/subscription/types/errors.go +++ b/x/subscription/types/errors.go @@ -28,4 +28,5 @@ var ( ErrorDuplicateQuota = errors.Register(ModuleName, 211, "duplicate quota") ErrorQuotaDoesNotExist = errors.Register(ModuleName, 212, "quota does not exist") ErrorCanNotAddQuota = errors.Register(ModuleName, 213, "can not add quota") + ErrorCanNotCancel = errors.Register(ModuleName, 214, "can not cancel") ) diff --git a/x/vpn/keeper/keeper.go b/x/vpn/keeper/keeper.go index 6741f46c..8248ed37 100644 --- a/x/vpn/keeper/keeper.go +++ b/x/vpn/keeper/keeper.go @@ -61,6 +61,7 @@ func NewKeeper( subscriptionKeeper.WithBankKeeper(bankKeeper) subscriptionKeeper.WithNodeKeeper(&nodeKeeper) subscriptionKeeper.WithPlanKeeper(&planKeeper) + subscriptionKeeper.WithSessionKeeper(&sessionKeeper) sessionKeeper.WithAccountKeeper(accountKeeper) sessionKeeper.WithDepositKeeper(&depositKeeper)