Skip to content

Commit

Permalink
Added subscription refund
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Jul 14, 2023
1 parent 563419b commit 017bf60
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 220 deletions.
52 changes: 1 addition & 51 deletions x/session/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
abcitypes "github.com/tendermint/tendermint/abci/types"

hubtypes "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/session/keeper"
"github.com/sentinel-official/hub/x/session/types"
)

func EndBlock(ctx sdk.Context, k keeper.Keeper) []abcitypes.ValidatorUpdate {
inactiveDuration := k.InactiveDuration(ctx)
k.IterateSessionsForExpiryAt(ctx, ctx.BlockTime(), func(_ int, item types.Session) bool {
k.DeleteSessionForExpiryAt(ctx, item.ExpiryAt, item.ID)

if item.Status.Equal(hubtypes.StatusActive) {
item.ExpiryAt = ctx.BlockTime().Add(inactiveDuration)
k.SetSessionForExpiryAt(ctx, item.ExpiryAt, item.ID)

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

k.SetSession(ctx, item)
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
ID: item.ID,
SubscriptionID: item.SubscriptionID,
NodeAddress: item.NodeAddress,
Status: item.Status,
},
)

return false
}

var (
accAddr = item.GetAddress()
nodeAddr = item.GetNodeAddress()
)

// TODO: call EndSessionHook

k.DeleteSession(ctx, item.ID)
k.DeleteSessionForAccount(ctx, accAddr, item.ID)
k.DeleteSessionForNode(ctx, nodeAddr, item.ID)
k.DeleteSessionForSubscription(ctx, item.SubscriptionID, item.ID)
k.DeleteSessionForAllocation(ctx, item.SubscriptionID, accAddr, item.ID)
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
ID: item.ID,
SubscriptionID: item.SubscriptionID,
NodeAddress: item.NodeAddress,
Status: item.Status,
},
)

return false
})

return nil
return k.EndBlock(ctx)
}
1 change: 1 addition & 0 deletions x/session/expected/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ type SubscriptionKeeper interface {
GetAllocation(ctx sdk.Context, id uint64, addr sdk.AccAddress) (subscriptiontypes.Allocation, bool)
SetAllocation(ctx sdk.Context, alloc subscriptiontypes.Allocation)
GetSubscription(ctx sdk.Context, id uint64) (subscriptiontypes.Subscription, bool)
HookEndSession(ctx sdk.Context, subscriptionID uint64, accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAddress, bytes sdk.Int) error
}
63 changes: 63 additions & 0 deletions x/session/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
abcitypes "github.com/tendermint/tendermint/abci/types"

hubtypes "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/session/types"
)

func (k *Keeper) EndBlock(ctx sdk.Context) []abcitypes.ValidatorUpdate {
inactiveDuration := k.InactiveDuration(ctx)
k.IterateSessionsForExpiryAt(ctx, ctx.BlockTime(), func(_ int, item types.Session) bool {
k.DeleteSessionForExpiryAt(ctx, item.ExpiryAt, item.ID)

if item.Status.Equal(hubtypes.StatusActive) {
item.ExpiryAt = ctx.BlockTime().Add(inactiveDuration)
k.SetSessionForExpiryAt(ctx, item.ExpiryAt, item.ID)

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

k.SetSession(ctx, item)
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
ID: item.ID,
SubscriptionID: item.SubscriptionID,
NodeAddress: item.NodeAddress,
Status: item.Status,
},
)

return false
}

var (
accAddr = item.GetAddress()
nodeAddr = item.GetNodeAddress()
)

if err := k.subscription.HookEndSession(ctx, item.SubscriptionID, accAddr, nodeAddr, item.Bandwidth.Sum()); err != nil {
panic(err)
}

k.DeleteSession(ctx, item.ID)
k.DeleteSessionForAccount(ctx, accAddr, item.ID)
k.DeleteSessionForNode(ctx, nodeAddr, item.ID)
k.DeleteSessionForSubscription(ctx, item.SubscriptionID, item.ID)
k.DeleteSessionForAllocation(ctx, item.SubscriptionID, accAddr, item.ID)
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
ID: item.ID,
SubscriptionID: item.SubscriptionID,
NodeAddress: item.NodeAddress,
Status: item.Status,
},
)

return false
})

return nil
}
111 changes: 2 additions & 109 deletions x/subscription/abci.go
Original file line number Diff line number Diff line change
@@ -1,123 +1,16 @@
package subscription

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
abcitypes "github.com/tendermint/tendermint/abci/types"

hubtypes "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/subscription/keeper"
"github.com/sentinel-official/hub/x/subscription/types"
)

func BeginBlock(ctx sdk.Context, k keeper.Keeper) {
k.IteratePayoutsForTimestamp(ctx, ctx.BlockTime(), func(_ int, item types.Payout) (stop bool) {
k.DeletePayoutForTimestamp(ctx, item.Timestamp, item.ID)

var (
accAddr = item.GetAddress()
nodeAddr = item.GetNodeAddress()
)

// TODO: Staking

if err := k.SendCoinFromDepositToAccount(ctx, accAddr, nodeAddr.Bytes(), item.Price); err != nil {
panic(err)
}

item.Hours = item.Hours - 1
if item.Hours > 0 {
item.Timestamp = item.Timestamp.Add(time.Hour)
k.SetPayoutForTimestamp(ctx, item.Timestamp, item.ID)
}

k.SetPayout(ctx, item)
ctx.EventManager().EmitTypedEvent(
&types.EventPayout{
ID: item.ID,
Address: item.Address,
NodeAddress: item.NodeAddress,
},
)

return false
})
k.BeginBlock(ctx)
}

func EndBlock(ctx sdk.Context, k keeper.Keeper) []abcitypes.ValidatorUpdate {
expiryDuration := k.ExpiryDuration(ctx)
k.IterateSubscriptionsForExpiryAt(ctx, ctx.BlockTime(), func(_ int, item types.Subscription) bool {
k.DeleteSubscriptionForExpiryAt(ctx, item.GetExpiryAt(), item.GetID())

if item.GetStatus().Equal(hubtypes.StatusActive) {
item.SetExpiryAt(
item.GetExpiryAt().Add(expiryDuration),
)
item.SetStatus(hubtypes.StatusInactivePending)
item.SetStatusAt(ctx.BlockTime())

k.SetSubscription(ctx, item)
k.SetSubscriptionForExpiryAt(ctx, item.GetExpiryAt(), item.GetID())
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
ID: item.GetID(),
Status: item.GetStatus(),
},
)

payout, found := k.GetPayout(ctx, item.GetID())
if !found {
return false
}

k.DeletePayoutForTimestamp(ctx, payout.Timestamp, payout.ID)

payout.Timestamp = time.Time{}
k.SetPayout(ctx, payout)

return false
}

// TODO: refund amount to account address

k.IterateAllocations(ctx, item.GetID(), func(_ int, alloc types.Allocation) bool {
addr := alloc.GetAddress()
k.DeleteAllocation(ctx, item.GetID(), addr)
k.DeleteSubscriptionForAccount(ctx, addr, item.GetID())

return false
})

switch s := item.(type) {
case *types.NodeSubscription:
k.DeleteSubscriptionForNode(ctx, s.GetNodeAddress(), s.GetID())
case *types.PlanSubscription:
k.DeleteSubscriptionForPlan(ctx, s.PlanID, s.GetID())
default:
panic(fmt.Errorf("invalid subscription %d with type %T", item.GetID(), item))
}

k.DeleteSubscription(ctx, item.GetID())
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
ID: item.GetID(),
Status: hubtypes.StatusInactive,
},
)

payout, found := k.GetPayout(ctx, item.GetID())
if !found {
return false
}

k.DeletePayout(ctx, payout.ID)
k.DeletePayoutForAccount(ctx, payout.GetAddress(), payout.ID)
k.DeletePayoutForNode(ctx, payout.GetNodeAddress(), payout.ID)

return false
})

return nil
return k.EndBlock(ctx)
}
Loading

0 comments on commit 017bf60

Please sign in to comment.