-
Notifications
You must be signed in to change notification settings - Fork 143
feat(rpc/subscription): implement state_unsubscribeStorage #1574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
1e13186
b32766a
f53d713
32a14bf
2a19a08
2c08756
dad26e9
9eb204f
2d5d79a
5653b02
71e015c
caf2191
088fb81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -28,6 +28,12 @@ type Params struct { | |||||
SubscriptionID uint `json:"subscription"` | ||||||
} | ||||||
|
||||||
// InvalidRequestCode error code returned for invalid request parameters, value derived from Substrate node output | ||||||
const InvalidRequestCode = -32600 | ||||||
|
||||||
// InvalidRequestMessage error message for invalid request parameters | ||||||
const InvalidRequestMessage = "Invalid request" | ||||||
|
||||||
func newSubcriptionBaseResponseJSON() BaseResponseJSON { | ||||||
return BaseResponseJSON{ | ||||||
Jsonrpc: "2.0", | ||||||
|
@@ -59,3 +65,18 @@ func newSubscriptionResponseJSON(subID uint, reqID float64) ResponseJSON { | |||||
ID: reqID, | ||||||
} | ||||||
} | ||||||
|
||||||
// BooleanResponseJSON for responses that return boolean values | ||||||
type BooleanResponseJSON struct { | ||||||
Jsonrpc string `json:"jsonrpc"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. |
||||||
Result bool `json:"result"` | ||||||
ID float64 `json:"id"` | ||||||
} | ||||||
|
||||||
func newBooleanResponseJSON(value bool, reqID float64) BooleanResponseJSON { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the type is public why not the constructor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason... I've updated this. |
||||||
return BooleanResponseJSON{ | ||||||
Jsonrpc: "2.0", | ||||||
Result: value, | ||||||
ID: reqID, | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,61 @@ func TestWSConn_HandleComm(t *testing.T) { | |
require.NoError(t, err) | ||
require.Equal(t, []byte(`{"jsonrpc":"2.0","result":4,"id":7}`+"\n"), msg) | ||
|
||
// test state_unsubscribeStorage | ||
c.WriteMessage(websocket.TextMessage, []byte(`{ | ||
"jsonrpc": "2.0", | ||
"method": "state_unsubscribeStorage", | ||
"params": "foo", | ||
"id": 7}`)) | ||
_, msg, err = c.ReadMessage() | ||
require.NoError(t, err) | ||
require.Equal(t, []byte(`{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":7}`+"\n"), msg) | ||
|
||
c.WriteMessage(websocket.TextMessage, []byte(`{ | ||
"jsonrpc": "2.0", | ||
"method": "state_unsubscribeStorage", | ||
"params": [], | ||
"id": 7}`)) | ||
_, msg, err = c.ReadMessage() | ||
require.NoError(t, err) | ||
require.Equal(t, []byte(`{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid request"},"id":7}`+"\n"), msg) | ||
|
||
c.WriteMessage(websocket.TextMessage, []byte(`{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to spec, this shouldn't fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I've updated so that it can handle strings as well. I've got params as a interface{}, so that it can accept any type, then when I parse the types it seems that all number are treated as float64 (I think this is happens when the json is unmashalled). I'm now taking the float64 or string and casting into uint. Let me know if there is a better approach to handling these. |
||
"jsonrpc": "2.0", | ||
"method": "state_unsubscribeStorage", | ||
"params": ["6"], | ||
"id": 7}`)) | ||
_, msg, err = c.ReadMessage() | ||
require.NoError(t, err) | ||
require.Equal(t, []byte(`{"jsonrpc":"2.0","result":false,"id":7}`+"\n"), msg) | ||
|
||
c.WriteMessage(websocket.TextMessage, []byte(`{ | ||
"jsonrpc": "2.0", | ||
"method": "state_unsubscribeStorage", | ||
"params": ["4"], | ||
"id": 7}`)) | ||
_, msg, err = c.ReadMessage() | ||
require.NoError(t, err) | ||
require.Equal(t, []byte(`{"jsonrpc":"2.0","result":true,"id":7}`+"\n"), msg) | ||
|
||
c.WriteMessage(websocket.TextMessage, []byte(`{ | ||
"jsonrpc": "2.0", | ||
"method": "state_unsubscribeStorage", | ||
"params": [6], | ||
"id": 7}`)) | ||
_, msg, err = c.ReadMessage() | ||
require.NoError(t, err) | ||
require.Equal(t, []byte(`{"jsonrpc":"2.0","result":false,"id":7}`+"\n"), msg) | ||
|
||
c.WriteMessage(websocket.TextMessage, []byte(`{ | ||
"jsonrpc": "2.0", | ||
"method": "state_unsubscribeStorage", | ||
"params": [4], | ||
"id": 7}`)) | ||
_, msg, err = c.ReadMessage() | ||
require.NoError(t, err) | ||
require.Equal(t, []byte(`{"jsonrpc":"2.0","result":true,"id":7}`+"\n"), msg) | ||
|
||
// test initBlockListener | ||
res, err = wsconn.initBlockListener(1) | ||
require.EqualError(t, err, "error BlockAPI not set") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.