-
Notifications
You must be signed in to change notification settings - Fork 246
Extended websocket interface #1335
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
Extended websocket interface #1335
Conversation
Added: - new endpoint: "v1/transactions/subscribe". This endpoint automatically subscribe to new transaction events. This endpoint accept optional parameters: `service_id` and `transaction_id`; - new enspoint: "v1/ws". This endpoint open websocket connection and allow to set multiple subscription (for blocks and transaction, filtered by service and transaction id) and send transactions (in hex, like explorer) to blockchain.
Codecov Report
@@ Coverage Diff @@
## master #1335 +/- ##
=========================================
+ Coverage 93.23% 93.53% +0.3%
=========================================
Files 162 164 +2
Lines 25032 25354 +322
=========================================
+ Hits 23339 23716 +377
+ Misses 1693 1638 -55
Continue to review full report at Codecov.
|
alekseysidorov
left a comment
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.
At the first sight, this PR looking good for me except of a several moments. Also may you write entry to the CHANGELOG.md?
Changed: - `usize` type replaced by `u64` in websocket.rs; - `sub_types` field in `Subscribe` type renamed to `subscribers`; - `subscribers` field of `Server` now use `BTreeMap`.
…y with rest of code base).
|
@aleksuss Yes, this make sense. Done. Also added in changelog how to determine |
|
@Flowneee I've restarted the job. I suppose it will fix that issue. It happens some times. |
@alekseysidorov I added entry to CHANGELOG.md and fixed all your comments. Can you check it? |
exonum/src/api/websocket.rs
Outdated
| where | ||
| T: AsRef<dyn Snapshot> + IndexAccess, | ||
| { | ||
| let tx = schema.transactions().get(tx_hash).unwrap(); |
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.
@Flowneee What if we don't have a transaction with hash tx_hash in the blockchain ? What do you think about to return a Result or Option here ?
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.
@aleksuss Currently there is always will be a transaction, because the only place where TransactionInfo called is here (tx_hash in that case is always already committed transaction). Generally yes, probably good idea. And I even think that method should be not new, but something like new_committed to indicate that we want info about committed transaction, and this method should return None instead of each .unwrap(). Another option is to rename type to something like CommittedTransactionInfo. What do you say?
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.
@Flowneee I've got your point and it's true. But unwrap() hurts my eyes in this place. I'd prefer a case with None instead of each .unwrap() but also you can use .expect() with a message like ("BUG. We don't have committed transaction in transactions index with hash: {:?}, tx_hash). Because the transaction is really MUST BE in the index. So. It's up to you.
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.
Okay, done. TransactionInfo renamed to CommittedTransactionSummary, added logging of situation, when summary cannot be built.
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.
@alekseysidorov @aleksuss @popzxc Can you check it again?
6904bc5
aleksuss
left a comment
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.
LGTM
Overview
This PR introduce additional functionality to websocket interface. Old version allowed to subscribe only for new blocks events. New version add support of subscribing to new transactions (all or only from specific services with specific ID) and sending transactions via websocket connection.
Details
Added new explorer endpoints:
v1/transactions/subscribe: this endpoint automatically subscribe to new transaction events. This endpoint accept optional parameters:service_idandmessage_id(message_idas in derive macroTransactionSet);v1/ws: this endpoint open websocket connection and allow to set multiple subscription (for new blocks and transaction, filtered by service and transaction id) and send transactions (in hex, like explorer) to blockchain.Messages, sent to websocket, must have format:
{"type": "...", "payload": ...}(currently possible types:set-subsctiptions,transaction), where payload differs for different types.Response for each sent message can be:
{"result": "success"}or{"result": "success", "response": ...}, if action assume some response;{"result": "error", "description": "..."}.Note: you can also set multiple subscription and set transactions through
v1/blocks/subscribeandv1/transactions/subscribe, so they are likev1/ws, but with default filter.Examples
v1/blocks/subscribews://localhost:8080/api/explorer/v1/blocks/subscribe.v1/transactions/subscribews://localhost:8080/api/explorer/v1/transactions/subscribe;ws://localhost:8080/api/explorer/v1/transactions/subscribe?service_id=1;ws://localhost:8080/api/explorer/v1/transactions/subscribe?service_id=1&message_id=2.v1/ws(multiple filters)ws://localhost:8080/api/explorer/v1/ws;{"type":"set-subscriptions","payload":[{"type":"blocks"},{"type":"transactions","filter":{"service_id":1,"message_id":2}}]};{"type":"set-subscriptions","payload":[{"type":"blocks"}]}.v1/ws(send transaction)ws://localhost:8080/api/explorer/v1/ws;{'type":"transaction","payload":{"tx_body":"53ef10e1b06e60d69ad72b21c628d11ef5beb1a1cfe828d6dcf1e4055093e1470000000000000a220a2053ef10e1b06e60d69ad72b21c628d11ef5beb1a1cfe828d6dcf1e4055093e1471205416c696365c50061fe2442b846179420c51cd19e6efafb4c20dc5272247dbb1d2549bcead9aee0805161fac631ed8483a41fdff3957a44cbacdbd8b13fcb827a0f913a120e"}}.