@@ -23,10 +23,12 @@ import (
23
23
"io/ioutil"
24
24
"math/big"
25
25
"net/http"
26
+ "strconv"
26
27
"strings"
27
28
"sync"
28
29
29
30
"github.com/ChainSafe/gossamer/dot/rpc/modules"
31
+ "github.com/ChainSafe/gossamer/dot/state"
30
32
"github.com/ChainSafe/gossamer/dot/types"
31
33
"github.com/ChainSafe/gossamer/lib/common"
32
34
log "github.com/ChainSafe/log15"
@@ -106,6 +108,9 @@ func (c *WSConn) HandleComm() {
106
108
continue
107
109
}
108
110
c .startListener (rvl )
111
+ case "state_unsubscribeStorage" :
112
+ c .unsubscribeStorageListener (reqid , params )
113
+
109
114
}
110
115
continue
111
116
}
@@ -206,12 +211,51 @@ func (c *WSConn) initStorageChangeListener(reqID float64, params interface{}) (u
206
211
207
212
c .Subscriptions [myObs .id ] = myObs
208
213
209
- initRes := newSubscriptionResponseJSON (myObs .id , reqID )
214
+ initRes := NewSubscriptionResponseJSON (myObs .id , reqID )
210
215
c .safeSend (initRes )
211
216
212
217
return myObs .id , nil
213
218
}
214
219
220
+ func (c * WSConn ) unsubscribeStorageListener (reqID float64 , params interface {}) {
221
+ switch v := params .(type ) {
222
+ case []interface {}:
223
+ if len (v ) == 0 {
224
+ c .safeSendError (reqID , big .NewInt (InvalidRequestCode ), InvalidRequestMessage )
225
+ return
226
+ }
227
+ default :
228
+ c .safeSendError (reqID , big .NewInt (InvalidRequestCode ), InvalidRequestMessage )
229
+ return
230
+ }
231
+
232
+ var id uint
233
+ switch v := params .([]interface {})[0 ].(type ) {
234
+ case float64 :
235
+ id = uint (v )
236
+ case string :
237
+ i , err := strconv .ParseUint (v , 10 , 32 )
238
+ if err != nil {
239
+ c .safeSend (newBooleanResponseJSON (false , reqID ))
240
+ return
241
+ }
242
+ id = uint (i )
243
+ default :
244
+ c .safeSendError (reqID , big .NewInt (InvalidRequestCode ), InvalidRequestMessage )
245
+ return
246
+ }
247
+
248
+ observer , ok := c .Subscriptions [id ].(state.Observer )
249
+ if ! ok {
250
+ initRes := newBooleanResponseJSON (false , reqID )
251
+ c .safeSend (initRes )
252
+ return
253
+ }
254
+
255
+ c .StorageAPI .UnregisterStorageObserver (observer )
256
+ c .safeSend (newBooleanResponseJSON (true , reqID ))
257
+ }
258
+
215
259
func (c * WSConn ) initBlockListener (reqID float64 ) (uint , error ) {
216
260
bl := & BlockListener {
217
261
Channel : make (chan * types.Block ),
@@ -231,7 +275,7 @@ func (c *WSConn) initBlockListener(reqID float64) (uint, error) {
231
275
bl .subID = c .qtyListeners
232
276
c .Subscriptions [bl .subID ] = bl
233
277
c .BlockSubChannels [bl .subID ] = chanID
234
- initRes := newSubscriptionResponseJSON (bl .subID , reqID )
278
+ initRes := NewSubscriptionResponseJSON (bl .subID , reqID )
235
279
c .safeSend (initRes )
236
280
237
281
return bl .subID , nil
@@ -256,7 +300,7 @@ func (c *WSConn) initBlockFinalizedListener(reqID float64) (uint, error) {
256
300
bfl .subID = c .qtyListeners
257
301
c .Subscriptions [bfl .subID ] = bfl
258
302
c .BlockSubChannels [bfl .subID ] = chanID
259
- initRes := newSubscriptionResponseJSON (bfl .subID , reqID )
303
+ initRes := NewSubscriptionResponseJSON (bfl .subID , reqID )
260
304
c .safeSend (initRes )
261
305
262
306
return bfl .subID , nil
@@ -299,7 +343,7 @@ func (c *WSConn) initExtrinsicWatch(reqID float64, params interface{}) (uint, er
299
343
if err != nil {
300
344
return 0 , err
301
345
}
302
- c .safeSend (newSubscriptionResponseJSON (esl .subID , reqID ))
346
+ c .safeSend (NewSubscriptionResponseJSON (esl .subID , reqID ))
303
347
304
348
// TODO (ed) since HandleSubmittedExtrinsic has been called we assume the extrinsic is in the tx queue
305
349
// should we add a channel to tx queue so we're notified when it's in the queue (See issue #1535)
@@ -322,7 +366,7 @@ func (c *WSConn) initRuntimeVersionListener(reqID float64) (uint, error) {
322
366
c .qtyListeners ++
323
367
rvl .subID = c .qtyListeners
324
368
c .Subscriptions [rvl .subID ] = rvl
325
- initRes := newSubscriptionResponseJSON (rvl .subID , reqID )
369
+ initRes := NewSubscriptionResponseJSON (rvl .subID , reqID )
326
370
c .safeSend (initRes )
327
371
328
372
return rvl .subID , nil
0 commit comments