Skip to content

Conversation

@Flowneee
Copy link
Contributor

@Flowneee Flowneee commented Jun 3, 2019

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_id and message_id (message_id as in derive macro TransactionSet);
  • 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:

  • success: {"result": "success"} or {"result": "success", "response": ...}, if action assume some response;
  • error: {"result": "error", "description": "..."}.

Note: you can also set multiple subscription and set transactions through v1/blocks/subscribe and v1/transactions/subscribe, so they are like v1/ws, but with default filter.

Examples

v1/blocks/subscribe

  • for subscribing to new blocks: ws://localhost:8080/api/explorer/v1/blocks/subscribe.

v1/transactions/subscribe

  • for subscribing to all new transactions: ws://localhost:8080/api/explorer/v1/transactions/subscribe;
  • for subscribing to new transactions of service with id 1: ws://localhost:8080/api/explorer/v1/transactions/subscribe?service_id=1;
  • for subscribing to new transactions of service with id 1 and message id 2: ws://localhost:8080/api/explorer/v1/transactions/subscribe?service_id=1&message_id=2.

v1/ws (multiple filters)

  1. Initiate connection: ws://localhost:8080/api/explorer/v1/ws;
  2. Send text message (via websocket) with list of filter (for example, blocks and transactions of service1 with message id 2): {"type":"set-subscriptions","payload":[{"type":"blocks"},{"type":"transactions","filter":{"service_id":1,"message_id":2}}]};
  3. Update filter (leave only subscription for blocks), send text message: {"type":"set-subscriptions","payload":[{"type":"blocks"}]}.

v1/ws (send transaction)

  1. Initiate connection: ws://localhost:8080/api/explorer/v1/ws;
  2. Send text message (via websocket) with transaction hex (like in explorer): {'type":"transaction","payload":{"tx_body":"53ef10e1b06e60d69ad72b21c628d11ef5beb1a1cfe828d6dcf1e4055093e1470000000000000a220a2053ef10e1b06e60d69ad72b21c628d11ef5beb1a1cfe828d6dcf1e4055093e1471205416c696365c50061fe2442b846179420c51cd19e6efafb4c20dc5272247dbb1d2549bcead9aee0805161fac631ed8483a41fdff3957a44cbacdbd8b13fcb827a0f913a120e"}}.

Flowneee added 2 commits June 3, 2019 15:35
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.
@Flowneee Flowneee marked this pull request as ready for review June 3, 2019 14:18
@codecov
Copy link

codecov bot commented Jun 3, 2019

Codecov Report

Merging #1335 into master will increase coverage by 0.3%.
The diff coverage is 86.8%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
exonum/src/api/mod.rs 98.11% <ø> (+0.94%) ⬆️
exonum/src/messages/mod.rs 95.34% <100%> (+0.22%) ⬆️
exonum/src/explorer/mod.rs 93.55% <100%> (ø) ⬆️
exonum/tests/websocket/blockchain/mod.rs 77.77% <77.77%> (ø)
exonum/src/api/websocket.rs 88.05% <86.07%> (+88.05%) ⬆️
exonum/src/api/node/public/explorer.rs 83.82% <89.65%> (+7.93%) ⬆️
exonum/tests/websocket/main.rs 90% <90%> (ø)
exonum/src/node/mod.rs 92.13% <0%> (+0.68%) ⬆️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 61992e9...6904bc5. Read the comment docs.

Copy link
Contributor

@alekseysidorov alekseysidorov left a 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?

Flowneee added 3 commits June 4, 2019 12:00
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`.
@aleksuss aleksuss requested a review from ivan-ochc June 4, 2019 10:24
@aleksuss
Copy link
Contributor

aleksuss commented Jun 4, 2019

@Flowneee good job. Thanks a lot. But what do you think about to rename a query parameter tranisaction_id to message_id ? We use a message_id definition to differ messages (transactions) in our codebase. Take a look here for example.

@Flowneee
Copy link
Contributor Author

Flowneee commented Jun 4, 2019

@aleksuss Yes, this make sense. Done. Also added in changelog how to determine message_id (just mentioned TransactionSet macro).

@Flowneee
Copy link
Contributor Author

Flowneee commented Jun 4, 2019

@aleksuss Error occurred during tests, on command cd $TRAVIS_BUILD_DIR/testkit/server && npm run proto && npm run test:unix. I don't think that it is because of my change. Can you look into it?

@aleksuss
Copy link
Contributor

aleksuss commented Jun 4, 2019

@Flowneee I've restarted the job. I suppose it will fix that issue. It happens some times.

@Flowneee
Copy link
Contributor Author

Flowneee commented Jun 5, 2019

At the first sight, this PR looking good for me except of a several moments. Also may you write entry to the CHANGELOG.md?

@alekseysidorov I added entry to CHANGELOG.md and fixed all your comments. Can you check it?

alekseysidorov
alekseysidorov previously approved these changes Jun 5, 2019
popzxc
popzxc previously approved these changes Jun 6, 2019
aleksuss
aleksuss previously approved these changes Jun 6, 2019
where
T: AsRef<dyn Snapshot> + IndexAccess,
{
let tx = schema.transactions().get(tx_hash).unwrap();
Copy link
Contributor

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 ?

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

@Flowneee Flowneee dismissed stale reviews from aleksuss, popzxc, and alekseysidorov via 6904bc5 June 6, 2019 10:12
Copy link
Contributor

@aleksuss aleksuss left a comment

Choose a reason for hiding this comment

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

LGTM

@aleksuss aleksuss merged commit 8e5dafd into exonum:master Jun 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants