11package keeper
22
33import (
4+ sdkmath "cosmossdk.io/math"
45 sdk "github.com/cosmos/cosmos-sdk/types"
56
67 base "github.com/sentinel-official/hub/v12/types"
@@ -9,58 +10,94 @@ import (
910 "github.com/sentinel-official/hub/v12/x/subscription/types/v3"
1011)
1112
12- // SessionInactivePreHook handles the necessary operations when a session becomes inactive.
13+ // SessionInactivePreHook performs cleanup operations when a session transitions to an inactive state .
1314func (k * Keeper ) SessionInactivePreHook (ctx sdk.Context , id uint64 ) error {
1415 k .Logger (ctx ).Info ("Running session inactive pre-hook" , "id" , id )
1516
16- // Retrieve the session by ID; return an error if not found .
17+ // Retrieve the session by ID; return an error if it doesn't exist .
1718 item , found := k .GetSession (ctx , id )
1819 if ! found {
1920 return types .NewErrorSessionNotFound (id )
2021 }
2122
22- // Assert the retrieved session to the v3.Session type; return nil if the assertion fails .
23+ // Ensure the session is of type v3.Session; do nothing if it's not .
2324 session , ok := item .(* v3.Session )
2425 if ! ok {
2526 return nil
2627 }
2728
28- // Ensure the session status is "InactivePending"; return an error if it has a different status .
29+ // Verify that the session's status is "InactivePending"; otherwise, return an error.
2930 if ! session .Status .Equal (v1base .StatusInactivePending ) {
3031 return types .NewErrorInvalidSessionStatus (session .ID , session .Status )
3132 }
3233
33- // Retrieve the subscription associated with the session; return an error if not found .
34+ // Fetch the subscription associated with the session; return an error if it doesn't exist .
3435 subscription , found := k .GetSubscription (ctx , session .SubscriptionID )
3536 if ! found {
3637 return types .NewErrorSubscriptionNotFound (session .SubscriptionID )
3738 }
3839
39- // Convert the session's account address from Bech32 format.
40+ // Decode the session's account address from Bech32 format.
4041 accAddr , err := sdk .AccAddressFromBech32 (session .AccAddress )
4142 if err != nil {
4243 return err
4344 }
4445
45- // Retrieve the allocation for the subscription and account; return an error if not found.
46- alloc , found := k .GetAllocation (ctx , subscription .ID , accAddr )
46+ // Decode the session's node address from Bech32 format.
47+ nodeAddr , err := base .NodeAddressFromBech32 (session .NodeAddress )
48+ if err != nil {
49+ return err
50+ }
51+
52+ // Remove session references for allocation, node, plan, and subscription.
53+ k .DeleteSessionForAllocation (ctx , subscription .ID , accAddr , session .ID )
54+ k .DeleteSessionForPlanByNode (ctx , subscription .PlanID , nodeAddr , session .ID )
55+ k .DeleteSessionForSubscription (ctx , subscription .ID , session .ID )
56+
57+ return nil
58+ }
59+
60+ // SessionUpdatePreHook updates session and allocation details during a session update.
61+ func (k * Keeper ) SessionUpdatePreHook (ctx sdk.Context , id uint64 , currBytes sdkmath.Int ) error {
62+ k .Logger (ctx ).Info ("Running session update pre-hook" , "id" , id )
63+
64+ // Retrieve the session by ID; return an error if it doesn't exist.
65+ item , found := k .GetSession (ctx , id )
4766 if ! found {
48- return types .NewErrorAllocationNotFound (subscription .ID , accAddr )
67+ return types .NewErrorSessionNotFound (id )
68+ }
69+
70+ // Ensure the session is of type v3.Session; do nothing if it's not.
71+ session , ok := item .(* v3.Session )
72+ if ! ok {
73+ return nil
4974 }
5075
51- // Calculate the total utilised bytes as the sum of download and upload bytes.
52- utilisedBytes := session .DownloadBytes .Add (session .UploadBytes )
76+ // Ensure the session is not in the "Inactive" state; return an error if it is.
77+ if session .Status .Equal (v1base .StatusInactive ) {
78+ return types .NewErrorInvalidSessionStatus (session .ID , session .Status )
79+ }
80+
81+ // Decode the session's account address from Bech32 format.
82+ accAddr , err := sdk .AccAddressFromBech32 (session .AccAddress )
83+ if err != nil {
84+ return err
85+ }
5386
54- // Update the utilised bytes in the allocation; cap it at the granted bytes if it exceeds the limit .
55- alloc . UtilisedBytes = alloc . UtilisedBytes . Add ( utilisedBytes )
56- if alloc . UtilisedBytes . GT ( alloc . GrantedBytes ) {
57- alloc . UtilisedBytes = alloc . GrantedBytes
87+ // Fetch the allocation for the subscription and account; return an error if it doesn't exist .
88+ alloc , found := k . GetAllocation ( ctx , session . SubscriptionID , accAddr )
89+ if ! found {
90+ return types . NewErrorAllocationNotFound ( session . SubscriptionID , accAddr )
5891 }
5992
60- // Save the updated allocation in the store.
93+ // Update allocation's utilised bytes based on the difference between current and previous session bytes.
94+ diffBytes := currBytes .Sub (session .Bytes ())
95+ alloc .UtilisedBytes = alloc .UtilisedBytes .Add (diffBytes )
96+
97+ // Store the updated allocation in the keeper.
6198 k .SetAllocation (ctx , alloc )
6299
63- // Emit an event to log the allocation update .
100+ // Emit an event logging the updated allocation details .
64101 ctx .EventManager ().EmitTypedEvent (
65102 & v3.EventAllocate {
66103 ID : alloc .ID ,
@@ -70,16 +107,5 @@ func (k *Keeper) SessionInactivePreHook(ctx sdk.Context, id uint64) error {
70107 },
71108 )
72109
73- // Convert the session's node address from Bech32 format.
74- nodeAddr , err := base .NodeAddressFromBech32 (session .NodeAddress )
75- if err != nil {
76- return err
77- }
78-
79- // Delete the session records associated with allocation, node, plan, and subscription from the store.
80- k .DeleteSessionForAllocation (ctx , subscription .ID , accAddr , session .ID )
81- k .DeleteSessionForPlanByNode (ctx , subscription .PlanID , nodeAddr , session .ID )
82- k .DeleteSessionForSubscription (ctx , subscription .ID , session .ID )
83-
84110 return nil
85111}
0 commit comments