Skip to content
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

dot/rpc: implement chain_subscribeNewHeads #836

Merged
merged 44 commits into from
May 15, 2020

Conversation

edwardmack
Copy link
Member

@edwardmack edwardmack commented May 2, 2020

Changes

Basic implementation of WebSocket subscription to push new head data to client.

  • Implements chain_subscribeNewHeads
  • Hard code methods needed for Polkadot-js integration

Tests:

Start node with RPC enabled:

./bin/gossamer --key alice --roles 4 --rpc --rpcmods chain,system,author,state,rpc

Make WebSocket call to ws://localhost:8546/

{
    "jsonrpc": "2.0",
    "method": "chain_subscribeNewHeads",
    "params": [],
    "id": 1
}

OR interact via Polkadot-js
Then browse to https://polkadot.js.org/apps/#/settings and configure for custom endpoint ws://127.0.0.1:8546/

Then interact with node through polkadot-js and note errors and issues so that we can determine next areas of focus.

Checklist:

  • I have read CONTRIBUTING and CODE_OF_CONDUCT
  • I have provided as much information as possible and necessary
  • I have reviewed my own pull request before requesting a review
  • I have linked any relevant issues in my pull request comments
  • All integration tests and required coverage checks are passing

Issues:

@edwardmack edwardmack changed the title Draft: implement chain_subscribeNewHeads dot/rpc: implement chain_subscribeNewHeads May 11, 2020
@edwardmack
Copy link
Member Author

This is ready for another look.

Copy link
Contributor

@ryanchristo ryanchristo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the code walkthrough on this one. Nice work! Looks good to me.

method := msg["method"]
// if method contains subscribe, then register subscription
if strings.Contains(fmt.Sprintf("%s", method), "subscribe") {
mid := msg["id"].(float64)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the subscription ID provided by the user?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the id provide by the user is the message id, so that can track that to the responses. The subscription id is created by the service and returned to the requester.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay makes sense!

dot/rpc/websocket.go Outdated Show resolved Hide resolved
dot/rpc/websocket_test.go Outdated Show resolved Hide resolved
dot/rpc/websocket_test.go Outdated Show resolved Hide resolved
dot/rpc/http.go Outdated
// init and start block received listener routine
if h.serverConfig.BlockAPI != nil {
h.serverConfig.BlockAddedReceiver = make(chan *types.Block)
h.serverConfig.BlockAddedReceiverDone = make(chan bool)
Copy link
Contributor

@noot noot May 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be changed to make(chan struct{}) and then in Stop() you can do close(BlockAddedReceiverDone) to notify the BlockState that it's done. it'll receive empty struct and unblock on <-bs.doneNotifying

it's a nitpick but I think it's usually what's used in go if you want a channel that only notifies

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, updated. I like the idea of applying patterns consistently (although I don't always do it), so thanks for pointing this out.

Copy link
Contributor

@noot noot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall looking really good! just a few small things

@edwardmack
Copy link
Member Author

@noot I think I address all comments, so it's ready for a re-review.

Copy link
Contributor

@noot noot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me!

@edwardmack edwardmack merged commit b487bef into development May 15, 2020
@edwardmack edwardmack deleted the ed/rpc_subscription_a branch May 15, 2020 15:12
ryanchristo pushed a commit that referenced this pull request Jun 24, 2020
* fix formating of header digest logs

* update digest type values to conform to spec

* hardcode RPC results for testing

* implement RPC subscribe_newHeads WebSocket call

* implement subscription

* header number as hex string in json responses

* rpc getStorage

* implement basic rpc state_getStorage call

* added test for state_getStorage

* cleanup comments

* implement state_getPairs

* implement state_getStorageHash

* implement state_getStorageSize

* refactor tests

* cleanup comments

* added tests

* fix lint issues

* update empty request test

* lint issues

* map for holding subscription connections

* use channel for block listener subscription

* make json results

* lint issues

* update tests to init coreAPI service

* changed if statement to switch

* Use HeaderToJSON function

* add tests for WebSocket

* clean up

* update comments

* add channel to BlockState service to notify RPC serivce

* add done channel to rpc newBlock receiver

* add check for open websocket connection

* update chan done notifier to use struct instead of bool

clean-up comments, move map init.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants