77 sdk "github.com/cosmos/cosmos-sdk/types"
88 "github.com/gin-gonic/gin"
99 hubtypes "github.com/sentinel-official/hub/types"
10+ subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types"
1011
1112 "github.com/sentinel-official/dvpn-node/context"
1213 "github.com/sentinel-official/dvpn-node/types"
@@ -51,7 +52,7 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
5152 ).First (& item )
5253
5354 if item .ID != 0 {
54- err = fmt .Errorf ("peer %s for service already exist" , req .Body .Key )
55+ err = fmt .Errorf ("key %s for service already exist" , req .Body .Key )
5556 c .JSON (http .StatusBadRequest , types .NewResponseError (3 , err ))
5657 return
5758 }
@@ -71,8 +72,14 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
7172 c .JSON (http .StatusNotFound , types .NewResponseError (4 , err ))
7273 return
7374 }
74- if ok := account .GetPubKey ().VerifySignature (sdk .Uint64ToBigEndian (req .URI .ID ), req .Signature ); ! ok {
75- err = fmt .Errorf ("failed to verify the signature %s" , req .Signature )
75+
76+ var (
77+ pubKey = account .GetPubKey ()
78+ msg = sdk .Uint64ToBigEndian (req .URI .ID )
79+ )
80+
81+ if ok := pubKey .VerifySignature (msg , req .Signature ); ! ok {
82+ err = fmt .Errorf ("invalid signature %s" , req .Signature )
7683 c .JSON (http .StatusBadRequest , types .NewResponseError (4 , err ))
7784 return
7885 }
@@ -88,7 +95,7 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
8895 return
8996 }
9097 if ! session .Status .Equal (hubtypes .StatusActive ) {
91- err = fmt .Errorf ("invalid status for session %d; expected %s, got %s " , session .Id , hubtypes . StatusActive , session .Status )
98+ err = fmt .Errorf ("invalid status %s for session %d" , session .Status , session .ID )
9299 c .JSON (http .StatusNotFound , types .NewResponseError (5 , err ))
93100 return
94101 }
@@ -98,58 +105,104 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
98105 return
99106 }
100107
101- subscription , err := ctx .Client ().QuerySubscription (session .Subscription )
108+ subscription , err := ctx .Client ().QuerySubscription (session .SubscriptionID )
102109 if err != nil {
103110 c .JSON (http .StatusInternalServerError , types .NewResponseError (6 , err ))
104111 return
105112 }
106113 if subscription == nil {
107- err = fmt .Errorf ("subscription %d does not exist" , session .Subscription )
114+ err = fmt .Errorf ("subscription %d does not exist" , session .SubscriptionID )
108115 c .JSON (http .StatusNotFound , types .NewResponseError (6 , err ))
109116 return
110117 }
111- if ! subscription .Status .Equal (hubtypes .StatusActive ) {
112- err = fmt .Errorf ("invalid status for subscription %d; expected %s, got %s " , subscription .Id , hubtypes . StatusActive , subscription .Status )
118+ if ! subscription .GetStatus () .Equal (hubtypes .StatusActive ) {
119+ err = fmt .Errorf ("invalid status %s for subscription %d" , subscription .GetStatus (), subscription .GetID () )
113120 c .JSON (http .StatusBadRequest , types .NewResponseError (6 , err ))
114121 return
115122 }
116123
117- if subscription .Plan == 0 {
118- if subscription .Node != ctx .Address ().String () {
119- err = fmt .Errorf ("node address mismatch; expected %s, got %s" , ctx .Address (), subscription .Node )
124+ switch s := subscription .(type ) {
125+ case * subscriptiontypes.NodeSubscription :
126+ if s .NodeAddress != ctx .Address ().String () {
127+ err = fmt .Errorf ("node address mismatch; expected %s, got %s" , ctx .Address (), s .NodeAddress )
120128 c .JSON (http .StatusBadRequest , types .NewResponseError (7 , err ))
121129 return
122130 }
123- } else {
124- ok , err := ctx .Client ().HasNodeForPlan (subscription . Plan , ctx .Address ())
131+ case * subscriptiontypes. PlanSubscription :
132+ exists , err := ctx .Client ().HasNodeForPlan (s . PlanID , ctx .Address ())
125133 if err != nil {
126134 c .JSON (http .StatusInternalServerError , types .NewResponseError (7 , err ))
127135 return
128136 }
129- if ! ok {
130- err = fmt .Errorf ("node %s does not exist for plan %d" , ctx .Address (), subscription . Plan )
137+ if ! exists {
138+ err = fmt .Errorf ("node %s does not exist for plan %d" , ctx .Address (), s . PlanID )
131139 c .JSON (http .StatusBadRequest , types .NewResponseError (7 , err ))
132140 return
133141 }
142+ default :
143+ err = fmt .Errorf ("invalid type %T for subscription %d" , s , subscription .GetID ())
144+ c .JSON (http .StatusBadRequest , types .NewResponseError (7 , err ))
145+ return
134146 }
135147
136- quota , err := ctx .Client ().QueryQuota (subscription .Id , req .AccAddress )
137- if err != nil {
138- c .JSON (http .StatusInternalServerError , types .NewResponseError (8 , err ))
139- return
148+ var (
149+ checkAllocation = true
150+ remainingBytes int64 = 0
151+ )
152+
153+ if s , ok := subscription .(* subscriptiontypes.NodeSubscription ); ok {
154+ if req .URI .AccAddress != s .Address {
155+ err = fmt .Errorf ("account address mismatch; expected %s, got %s" , req .URI .AccAddress , s .Address )
156+ c .JSON (http .StatusBadRequest , types .NewResponseError (8 , err ))
157+ return
158+ }
159+ if s .Hours != 0 {
160+ checkAllocation = false
161+ }
140162 }
141- if quota == nil {
142- err = fmt .Errorf ("quota for address %s does not exist" , req .URI .AccAddress )
143- c .JSON (http .StatusNotFound , types .NewResponseError (8 , err ))
144- return
163+
164+ if checkAllocation {
165+ alloc , err := ctx .Client ().QueryAllocation (subscription .GetID (), req .AccAddress )
166+ if err != nil {
167+ c .JSON (http .StatusInternalServerError , types .NewResponseError (8 , err ))
168+ return
169+ }
170+ if alloc == nil {
171+ err = fmt .Errorf ("allocation %d/%s does not exist" , subscription .GetID (), req .AccAddress )
172+ c .JSON (http .StatusNotFound , types .NewResponseError (8 , err ))
173+ return
174+ }
175+
176+ var items []types.Session
177+ ctx .Database ().Model (
178+ & types.Session {},
179+ ).Where (
180+ & types.Session {
181+ Subscription : subscription .GetID (),
182+ Address : req .URI .AccAddress ,
183+ },
184+ ).Find (& items )
185+
186+ for i := 0 ; i < len (items ); i ++ {
187+ utilisedBytes := sdk .NewInt (items [i ].Download + items [i ].Upload )
188+ alloc .UtilisedBytes = alloc .UtilisedBytes .Add (utilisedBytes )
189+ }
190+
191+ if alloc .UtilisedBytes .GTE (alloc .GrantedBytes ) {
192+ err = fmt .Errorf ("invalid allocation; granted bytes %s, utilised bytes %s" , alloc .GrantedBytes , alloc .UtilisedBytes )
193+ c .JSON (http .StatusBadRequest , types .NewResponseError (8 , err ))
194+ return
195+ }
196+
197+ remainingBytes = alloc .GrantedBytes .Sub (alloc .UtilisedBytes ).Int64 ()
145198 }
146199
147200 var items []types.Session
148201 ctx .Database ().Model (
149202 & types.Session {},
150203 ).Where (
151204 & types.Session {
152- Subscription : subscription .Id ,
205+ Subscription : subscription .GetID () ,
153206 Address : req .URI .AccAddress ,
154207 },
155208 ).Find (& items )
@@ -159,30 +212,11 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
159212 c .JSON (http .StatusInternalServerError , types .NewResponseError (9 , err ))
160213 return
161214 }
162-
163- consumed := sdk .NewInt (items [i ].Download + items [i ].Upload )
164- if subscription .Plan == 0 {
165- quota .Consumed = quota .Consumed .Add (
166- hubtypes .NewBandwidth (
167- consumed , sdk .ZeroInt (),
168- ).CeilTo (
169- hubtypes .Gigabyte .Quo (subscription .Price .Amount ),
170- ).Sum (),
171- )
172- } else {
173- quota .Consumed = quota .Consumed .Add (consumed )
174- }
175- }
176-
177- if quota .Consumed .GTE (quota .Allocated ) {
178- err = fmt .Errorf ("quota exceeded; allocated %s, consumed %s" , quota .Allocated , quota .Consumed )
179- c .JSON (http .StatusBadRequest , types .NewResponseError (10 , err ))
180- return
181215 }
182216
183217 result , err := ctx .Service ().AddPeer (req .Key )
184218 if err != nil {
185- c .JSON (http .StatusInternalServerError , types .NewResponseError (11 , err ))
219+ c .JSON (http .StatusInternalServerError , types .NewResponseError (10 , err ))
186220 return
187221 }
188222 ctx .Log ().Info ("Added a new peer" , "key" , req .Body .Key , "count" , ctx .Service ().PeerCount ())
@@ -192,10 +226,10 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
192226 ).Create (
193227 & types.Session {
194228 ID : req .URI .ID ,
195- Subscription : subscription .Id ,
229+ Subscription : subscription .GetID () ,
196230 Key : req .Body .Key ,
197231 Address : req .URI .AccAddress ,
198- Available : quota . Allocated . Sub ( quota . Consumed ). Int64 () ,
232+ Available : remainingBytes ,
199233 },
200234 )
201235
0 commit comments