-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
563419b
commit 017bf60
Showing
7 changed files
with
290 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.