-
Notifications
You must be signed in to change notification settings - Fork 40
/
hooks.go
55 lines (42 loc) · 1.79 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/v12/types"
"github.com/sentinel-official/hub/v12/x/session/types"
)
func (k *Keeper) SubscriptionInactivePendingHook(ctx sdk.Context, id uint64) error {
// Get the status change delay from the store.
statusChangeDelay := k.StatusChangeDelay(ctx)
// Iterate through sessions associated with the subscription.
k.IterateSessionsForSubscription(ctx, id, func(_ int, item types.Session) (stop bool) {
// Skip non-active sessions.
if !item.Status.Equal(hubtypes.StatusActive) {
return false
}
// Delete the session's entry from the InactiveAt index before updating the InactiveAt value.
k.DeleteSessionForInactiveAt(ctx, item.InactiveAt, item.ID)
// Calculate the new InactiveAt value by adding the status change delay to the current block time.
item.InactiveAt = ctx.BlockTime().Add(statusChangeDelay)
// Set the session status to 'InactivePending' to mark it for an upcoming status update.
item.Status = hubtypes.StatusInactivePending
// Record the time of the status update in 'StatusAt' field.
item.StatusAt = ctx.BlockTime()
// Update the session entry in the store with the new status and status update time.
k.SetSession(ctx, item)
// Update the session entry in the InactiveAt index with the new InactiveAt value.
k.SetSessionForInactiveAt(ctx, item.InactiveAt, item.ID)
// Emit an event to notify that the session status has been updated.
ctx.EventManager().EmitTypedEvent(
&types.EventUpdateStatus{
Status: hubtypes.StatusInactivePending,
Address: item.Address,
NodeAddress: item.NodeAddress,
ID: item.ID,
PlanID: 0,
SubscriptionID: item.SubscriptionID,
},
)
return false
})
return nil
}