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

Websockets support #458

Merged
merged 15 commits into from
May 10, 2018
Merged

Websockets support #458

merged 15 commits into from
May 10, 2018

Conversation

mushketyk
Copy link
Contributor

@mushketyk mushketyk commented Mar 28, 2018

Implements WebSocketService that allows calling JSON-RPC methods via WebSocket protocol and allows to subscribe to real-time notifications.
Also adds new methods to the Web3jRx that allows subscribing to JSON-RPC notifications.

The PR is already too big for a review, so I had to stop, but there are few things that are still missing:

  • Support for Parity pub-sub method. Parity allows subscribing to more notifications than Geth and PR allows using Parity's pub-sub mechanism
  • Integration tests for WebSockets
  • User documentation (I write it once this PR is approved, since we can change the design during the review)

@codecov
Copy link

codecov bot commented Mar 28, 2018

Codecov Report

Merging #458 into master will increase coverage by 0.23%.
The diff coverage is 78.62%.

Impacted file tree graph

@@             Coverage Diff             @@
##             master    #458      +/-   ##
===========================================
+ Coverage     76.87%   77.1%   +0.23%     
- Complexity     1686    1770      +84     
===========================================
  Files           222     236      +14     
  Lines          6295    6581     +286     
  Branches        972     984      +12     
===========================================
+ Hits           4839    5074     +235     
- Misses         1215    1262      +47     
- Partials        241     245       +4
Impacted Files Coverage Δ Complexity Δ
...th/src/main/java/org/web3j/protocol/geth/Geth.java 100% <ø> (ø) 1 <0> (ø) ⬇️
.../java/org/web3j/protocol/websocket/events/Log.java 0% <0%> (ø) 0 <0> (?)
...protocol/core/methods/response/EthUnsubscribe.java 0% <0%> (ø) 0 <0> (?)
.../protocol/websocket/events/SyncingNotfication.java 0% <0%> (ø) 0 <0> (?)
...bsocket/events/PendingTransactionNotification.java 0% <0%> (ø) 0 <0> (?)
...b3j/protocol/websocket/events/LogNotification.java 0% <0%> (ø) 0 <0> (?)
...main/java/org/web3j/protocol/http/HttpService.java 83.33% <0%> (+3.33%) 17 <0> (ø) ⬇️
...rotocol/websocket/events/NewHeadsNotification.java 100% <100%> (ø) 1 <1> (?)
core/src/main/java/org/web3j/protocol/Service.java 84.61% <100%> (+4.61%) 4 <1> (+1) ⬆️
...org/web3j/protocol/websocket/WebSocketRequest.java 100% <100%> (ø) 3 <3> (?)
... and 27 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 f477549...e574576. Read the comment docs.

@mushketyk
Copy link
Contributor Author

@conor10 I've made few more additional modifications to my PR, but now everything is ready for review.

@conor10
Copy link
Contributor

conor10 commented Apr 1, 2018

Awesome work @mushketyk 💯 - I'll get to it shortly.

@mushketyk
Copy link
Contributor Author

Thank you @conor10! I look forward to your review.

Copy link
Contributor

@conor10 conor10 left a comment

Choose a reason for hiding this comment

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

Great work so far, apologies for the delay in reviewing.

testCompile project(path: ':crypto', configuration: 'archives'),
"nl.jqno.equalsverifier:equalsverifier:$equalsverifierVersion"
"nl.jqno.equalsverifier:equalsverifier:$equalsverifierVersion",
group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
Copy link
Contributor

Choose a reason for hiding this comment

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

Please can this dependency be included using the same style as the others?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

* @param <T> type of a data item returned by the request
* @return deserialized JSON-RPC response
* @throws IOException thrown if failed to perform a request
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

*
* @return Observable that emits a notification for every new header
*/
Observable<NewHeadsNotification> newHeadsNotifications();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is Geth specific right? And some of the other calls?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

newHeads and logs are not specific to Geth. Parity supports it as well: https://wiki.parity.io/JSONRPC-Eth-Pub-Sub-Module.html

I'll move other two to the Geth related class

try {
log.debug("Received message {} from server {}", s, uri);
listener.onMessage(s);
log.debug("Processed message {} from server {}", s, uri);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need both debug statements

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed second message.


@Override
public void onError(Exception e) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Please can we have a log statement 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.

Do we need it here?

We have a log in the WebSocketClient class:

    @Override
    public void onError(Exception e) {
        log.error(String.format("WebSocket connection to {} failed with error", uri), e);
        listener.onError(e);
    }

this.responseType = responseType;
}

public CompletableFuture<T> getCompletableFuture() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please can you use something more specific for this method name & instance variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. What do you think about onReply?


import rx.subjects.BehaviorSubject;

public class WebSocketSubscription<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please can you ensure all classes have a brief Javadoc description - a single sentence is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


import java.util.List;

public class Log {
Copy link
Contributor

Choose a reason for hiding this comment

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

I presume this Log differs from the other Log objects already defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, existing Log class contains the following fields:

    private boolean removed;
    private String logIndex;
    private String transactionIndex;
    private String transactionHash;
    private String blockHash;
    private String blockNumber;
    private String address;
    private String data;
    private String type;
    private List<String> topics;

while this Log class contains the following fields:

    private String address;
    private String blockHash;
    private String blockNumber;
    private String data;
    private String logIndex;
    private List<String> topics;
    private String transactionHash;
    private String transactionIndex;

private Web3j web3j = Web3j.build(service, 10, scheduledExecutorService);

@Test
public void testStopExecutorOnShutdown() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

How come this test was moved to the websocket test class?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably renamed this class accidentally in IDEA. Will revert this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Undone :)

requestSent.await(2, TimeUnit.SECONDS);
sendGethVersionReply();
} catch (Exception e) {
e.printStackTrace();
Copy link
Contributor

Choose a reason for hiding this comment

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

Please can this be rethrown instead of discarded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@mushketyk
Copy link
Contributor Author

Hi @conor10,

Thank you for your review! I am a bit busy at the moment, but I'll try to update the PR next week.

@conor10 conor10 added this to the 4.0 milestone Apr 25, 2018
@mushketyk
Copy link
Contributor Author

@conor10 Sorry for the long delay. I've updated the PR and replied to your comments. Could you please review the PR again?

@conor10 conor10 merged commit 254afb8 into hyperledger:master May 10, 2018
@conor10
Copy link
Contributor

conor10 commented May 10, 2018

Awesome work 🥇

If you can get those docs written next it would be fantastic.

franz-see pushed a commit to franz-see/web3j that referenced this pull request Aug 3, 2018
@snazha-blkio snazha-blkio removed this from the 4.0.0-alpha-1 milestone Oct 28, 2018
@mushketyk
Copy link
Contributor Author

@conor10 Sorry, I somehow missed your message. I'll write docs during the next week.

@mushketyk
Copy link
Contributor Author

@conor10 I've added documentation in this PR: #768

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.

None yet

3 participants