Skip to content

Commit

Permalink
feat: implemented MsgStartSession in subscription module
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Jul 8, 2024
1 parent 45683e7 commit 11408d8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 32 deletions.
1 change: 0 additions & 1 deletion x/node/keeper/msg_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func (k *Keeper) HandleMsgRegister(ctx sdk.Context, msg *v2.MsgRegisterRequest)
if !k.IsValidGigabytePrices(ctx, msg.GigabytePrices) {
return nil, types.NewErrorInvalidPrices(msg.GigabytePrices)
}

if !k.IsValidHourlyPrices(ctx, msg.HourlyPrices) {
return nil, types.NewErrorInvalidPrices(msg.HourlyPrices)
}
Expand Down
16 changes: 0 additions & 16 deletions x/session/keeper/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,6 @@ func (k *Keeper) IterateSessionsForInactiveAt(ctx sdk.Context, end time.Time, fn
}
}

func (k *Keeper) GetLatestSessionForSubscription(ctx sdk.Context, subscriptionID uint64) (session v3.Session, found bool) {
store := k.Store(ctx)

iterator := sdk.KVStoreReversePrefixIterator(store, types.GetSessionForSubscriptionKeyPrefix(subscriptionID))
defer iterator.Close()

if iterator.Valid() {
session, found = k.GetSession(ctx, types.IDFromSessionForSubscriptionKey(iterator.Key()))
if !found {
panic(fmt.Errorf("session for subscription key %X does not exist", iterator.Key()))
}
}

return session, false
}

func (k *Keeper) GetLatestSessionForAllocation(ctx sdk.Context, subscriptionID uint64, addr sdk.AccAddress) (session v3.Session, found bool) {
store := k.Store(ctx)

Expand Down
11 changes: 11 additions & 0 deletions x/subscription/expected/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package expected

import (
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -42,6 +44,15 @@ type PlanKeeper interface {
}

type SessionKeeper interface {
GetCount(ctx sdk.Context) uint64
GetSession(ctx sdk.Context, id uint64) (sessiontypes.Session, bool)
SetCount(ctx sdk.Context, count uint64)
SetSession(ctx sdk.Context, session sessiontypes.Session)
SetSessionForAccount(ctx sdk.Context, addr sdk.AccAddress, id uint64)
SetSessionForAllocation(ctx sdk.Context, subscriptionID uint64, addr sdk.AccAddress, sessionID uint64)
SetSessionForInactiveAt(ctx sdk.Context, at time.Time, id uint64)
SetSessionForNode(ctx sdk.Context, addr base.NodeAddress, id uint64)
SetSessionForSubscription(ctx sdk.Context, subscriptionID, sessionID uint64)
StatusChangeDelay(ctx sdk.Context) time.Duration
SubscriptionInactivePendingPreHook(ctx sdk.Context, id uint64) error
}
59 changes: 59 additions & 0 deletions x/subscription/keeper/msg_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,64 @@ func (k *Keeper) HandleMsgRenew(ctx sdk.Context, msg *v3.MsgRenewRequest) (*v3.M
}

func (k *Keeper) HandleMsgStartSession(ctx sdk.Context, msg *v3.MsgStartSessionRequest) (*v3.MsgStartSessionResponse, error) {
accAddr, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}

nodeAddr, err := base.NodeAddressFromBech32(msg.NodeAddress)
if err != nil {
return nil, err
}

subscription, found := k.GetSubscription(ctx, msg.ID)
if !found {
return nil, types.NewErrorSubscriptionNotFound(msg.ID)
}
if !subscription.Status.Equal(v1base.StatusActive) {
return nil, types.NewErrorInvalidSubscriptionStatus(subscription.ID, subscription.Status)
}

node, found := k.node.GetNode(ctx, nodeAddr)
if !found {
return nil, types.NewErrorNodeNotFound(nodeAddr)
}
if !node.Status.Equal(v1base.StatusActive) {
return nil, types.NewErrorInvalidNodeStatus(nodeAddr, node.Status)
}

alloc, found := k.GetAllocation(ctx, subscription.ID, accAddr)
if !found {
return nil, types.NewErrorAllocationNotFound(subscription.ID, accAddr)
}
if alloc.UtilisedBytes.GTE(alloc.GrantedBytes) {
return nil, types.NewErrorInvalidAllocation(subscription.ID, accAddr)
}

var (
count = k.session.GetCount(ctx)
delay = k.session.StatusChangeDelay(ctx)
session = &v3.Session{
ID: count + 1,
AccAddress: accAddr.String(),
NodeAddress: nodeAddr.String(),
SubscriptionID: subscription.ID,
DownloadBytes: sdkmath.ZeroInt(),
UploadBytes: sdkmath.ZeroInt(),
Duration: 0,
Status: v1base.StatusActive,
InactiveAt: ctx.BlockTime().Add(delay),
StatusAt: ctx.BlockTime(),
}
)

k.session.SetCount(ctx, count+1)
k.session.SetSession(ctx, session)
k.session.SetSessionForAccount(ctx, accAddr, session.ID)
k.session.SetSessionForNode(ctx, nodeAddr, session.ID)
k.session.SetSessionForSubscription(ctx, subscription.ID, session.ID)
k.session.SetSessionForAllocation(ctx, subscription.ID, accAddr, session.ID)
k.session.SetSessionForInactiveAt(ctx, session.InactiveAt, session.ID)

return &v3.MsgStartSessionResponse{}, nil
}
35 changes: 20 additions & 15 deletions x/subscription/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@ package types
import (
sdkerrors "cosmossdk.io/errors"

base "github.com/sentinel-official/hub/v12/types"
v1base "github.com/sentinel-official/hub/v12/types/v1"
)

var (
ErrorInvalidMessage = sdkerrors.Register(ModuleName, 101, "invalid message")

ErrorAllocationNotFound = sdkerrors.Register(ModuleName, 201, "allocation not found")
ErrorInsufficientBytes = sdkerrors.Register(ModuleName, 202, "insufficient bytes")
ErrorInvalidAllocation = sdkerrors.Register(ModuleName, 203, "invalid allocation")
ErrorInvalidPlanStatus = sdkerrors.Register(ModuleName, 204, "invalid plan status")
ErrorInvalidSubscriptionStatus = sdkerrors.Register(ModuleName, 205, "invalid subscription status")
ErrorPlanNotFound = sdkerrors.Register(ModuleName, 206, "plan not found")
ErrorPriceNotFound = sdkerrors.Register(ModuleName, 207, "price does not exist")
ErrorSubscriptionNotFound = sdkerrors.Register(ModuleName, 208, "subscription not found")
ErrorUnauthorized = sdkerrors.Register(ModuleName, 209, "unauthorized")
ErrorInsufficientBytes = sdkerrors.Register(ModuleName, 201, "insufficient bytes")
ErrorInvalidAllocation = sdkerrors.Register(ModuleName, 202, "invalid allocation")
ErrorInvalidStatus = sdkerrors.Register(ModuleName, 203, "invalid status")
ErrorNotFound = sdkerrors.Register(ModuleName, 204, "not found")
ErrorUnauthorized = sdkerrors.Register(ModuleName, 205, "unauthorized")
)

func NewErrorAllocationNotFound(id uint64, addr interface{}) error {
return sdkerrors.Wrapf(ErrorAllocationNotFound, "allocation %d/%s does not exist", id, addr)
return sdkerrors.Wrapf(ErrorNotFound, "allocation %d/%s does not exist", id, addr)
}

func NewErrorInsufficientBytes(id uint64, addr interface{}) error {
Expand All @@ -33,25 +30,33 @@ func NewErrorInvalidAllocation(id uint64, addr interface{}) error {
}

func NewErrorInvalidPlanStatus(id uint64, status v1base.Status) error {
return sdkerrors.Wrapf(ErrorInvalidPlanStatus, "invalid status %s for plan %d", status, id)
return sdkerrors.Wrapf(ErrorInvalidStatus, "invalid status %s for plan %d", status, id)
}

func NewErrorInvalidSubscriptionStatus(id uint64, status v1base.Status) error {
return sdkerrors.Wrapf(ErrorInvalidSubscriptionStatus, "invalid status %s for subscription %d", status, id)
return sdkerrors.Wrapf(ErrorInvalidStatus, "invalid status %s for subscription %d", status, id)
}

func NewErrorPlanNotFound(id uint64) error {
return sdkerrors.Wrapf(ErrorPlanNotFound, "plan %d does not exist", id)
return sdkerrors.Wrapf(ErrorNotFound, "plan %d does not exist", id)
}

func NewErrorPriceNotFound(denom string) error {
return sdkerrors.Wrapf(ErrorPriceNotFound, "price for denom %s does not exist", denom)
return sdkerrors.Wrapf(ErrorNotFound, "price for denom %s does not exist", denom)
}

func NewErrorSubscriptionNotFound(id uint64) error {
return sdkerrors.Wrapf(ErrorSubscriptionNotFound, "subscription %d does not exist", id)
return sdkerrors.Wrapf(ErrorNotFound, "subscription %d does not exist", id)
}

func NewErrorUnauthorized(addr string) error {
return sdkerrors.Wrapf(ErrorUnauthorized, "address %s is not authorized", addr)
}

func NewErrorNodeNotFound(addr base.NodeAddress) error {
return sdkerrors.Wrapf(ErrorNotFound, "node %s does not exist", addr)
}

func NewErrorInvalidNodeStatus(addr base.NodeAddress, status v1base.Status) error {
return sdkerrors.Wrapf(ErrorInvalidStatus, "invalid status %s for node %d", status, addr)
}

0 comments on commit 11408d8

Please sign in to comment.