Skip to content

Commit

Permalink
Added no active sessions condition while cancelling the subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Sep 19, 2021
1 parent 2415ade commit 031c6ac
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions x/subscription/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
5 changes: 5 additions & 0 deletions x/subscription/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
10 changes: 5 additions & 5 deletions x/subscription/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions x/subscription/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions x/subscription/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
1 change: 1 addition & 0 deletions x/vpn/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func NewKeeper(
subscriptionKeeper.WithBankKeeper(bankKeeper)
subscriptionKeeper.WithNodeKeeper(&nodeKeeper)
subscriptionKeeper.WithPlanKeeper(&planKeeper)
subscriptionKeeper.WithSessionKeeper(&sessionKeeper)

sessionKeeper.WithAccountKeeper(accountKeeper)
sessionKeeper.WithDepositKeeper(&depositKeeper)
Expand Down

0 comments on commit 031c6ac

Please sign in to comment.