Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

PubSub for parity-js #5830

Merged
merged 26 commits into from
Jul 6, 2017
Merged

PubSub for parity-js #5830

merged 26 commits into from
Jul 6, 2017

Conversation

kaikun213
Copy link
Contributor

In progress. Still needs testing and documentation

@kaikun213 kaikun213 added A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. M7-ui labels Jun 13, 2017
@kaikun213 kaikun213 added A0-pleasereview 🤓 Pull request needs code review. and removed A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. labels Jun 14, 2017
@kaikun213 kaikun213 requested review from jacogr and tomusdrw June 26, 2017 10:56
Copy link
Collaborator

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

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

Overall looks quite nice, couple of grumbles regarding style, code quality and usability of the pubsub() namespace.

// }

// parity API
accountsInfo (callback) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about splitting this APIs into separate namespaces, like:
api.pubsub().parity().accountsInfo() and api.pubsub().eth().newHeads() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, I had them split before but that makes it more difficult for the user again to keep track which subscriptionId belongs to which api for unsubscribing.
We could split them again but keep that the Id's are non-correlating.
Also eth only supports one method so far so the extra work for the user seemed unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually prefer @tomusdrw approach in this case - it also makes sure that we never overlap, and aligns better with teh way the APIs are structured?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, api().pubsub.eth , api().pubsub.parity and api().pubsub.net are supported now. Subscription IDs are namespace unrelated, thus unsubscribing can be done either by calling api().pubsub.unsubscribe(id) or one of the modules e.g. api().pubsub.eth.unsubscribe(id)

}

sendRawTransaction (callback, data) {
return this.addListener(this._api, 'eth_sendRawTransaction', callback, [inData(data)]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't really make sense to subscribe to that.

return this.addListener(this._api, 'eth_sendRawTransaction', callback, [inData(data)]);
}

submitTransaction (callback, data) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't really make sense to subscribe to that.

return this.addListener(this._api, 'eth_getWork', callback);
}

submitWork (callback, nonce, powHash, mixDigest) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't really make sense to subscribe to that.

return this.addListener(this._api, 'eth_submitWork', callback, [inNumber16(nonce), powHash, mixDigest]);
}

submitHashrate (callback, hashrate, clientId) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Doesn't really make sense to subscribe to that.

// Don't print error if request rejected or not is not yet up...
if (!/(rejected|not yet up)/.test(result.error.message)) {
console.error(`${method}(${JSON.stringify(params)}): ${result.error.code}: ${result.error.message}`);
}

const error = new TransportError(method, result.error.code, result.error.message);

reject(error);
result.id ? reject(error) : callback(error);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Imho better to write an if-else instead.

const json = this.encode(uMethod, params);

this._messages[id] = { id, 'method': uMethod, params, json,
resolve: _ => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would extract those methods to const resolve = () => {...} and then leave this.messages[id] as simple as in subscribe.

}

unsubscribe (messageId) {
return new Promise(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this promise is never resolved? I think it should be resolved in this._messages[id], no?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@kaikun213 That one is still valid too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had some missunderstanding there. Fixxed now!

delete this._messages[messageId];
method === 'eth_subscribe' ? delete this._subscriptions['eth_subscription'][subId] : delete this._subscriptions['parity_subscription'][subId];
}, reject: e => {
throw Error('Error unsubscribing.' + e);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of crashing when usubscribe fails you should rather reject the upper promise.

this._messages[id] = { id, 'method': uMethod, params, json,
resolve: _ => {
delete this._messages[messageId];
method === 'eth_subscribe' ? delete this._subscriptions['eth_subscription'][subId] : delete this._subscriptions['parity_subscription'][subId];
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete this._subscriptions[method === 'eth_subscribe' ? ...][subId];

Also I think this check is done more often than necessary, can't we store the subscription names and unsubscribe method names in this_messages? I think it would simplify a lot of code.

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 thought about it, now I realized it makes it much cleaner.

Copy link
Collaborator

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

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

Almost there, few more grumbles.

return this.addListener('eth', 'newHeads', callback);
}
//
// logs (callback) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Logs are now available: #5705 but this can come in a separate PR.

Imho we should either uncomment the code or remove it.

@@ -28,9 +28,9 @@ export default class Trace {
.then(outTraces);
}

call (options, blockNumber = 'latest', whatTrace = ['trace']) {
call (options, whatTrace = ['trace'], blockNumber = 'latest') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good catch 👍

this.error(event.data);

if (result.params.error) {
result[error] = result.params.error;
Copy link
Collaborator

Choose a reason for hiding this comment

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

@kaikun213 This comment is still valid.

}

unsubscribe (messageId) {
return new Promise(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@kaikun213 That one is still valid too.

throw Error('Error unsubscribing.' + e);
} };

this._messages[id] = { id, 'method': uMethod, params, json, resolve, reject };
Copy link
Collaborator

Choose a reason for hiding this comment

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

' around method are not required

@tomusdrw tomusdrw added A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jul 5, 2017
@tomusdrw tomusdrw added A8-looksgood 🦄 Pull request is reviewed well. and removed A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. labels Jul 5, 2017
@tomusdrw
Copy link
Collaborator

tomusdrw commented Jul 5, 2017

Looks good. @jacogr Could you have a look and merge if you consider it ready?

Copy link
Contributor

@jacogr jacogr left a comment

Choose a reason for hiding this comment

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

Brilliant, happy as it stands (and looking forward to actually cleaning up the polling using this).

@jacogr jacogr merged commit 349316f into master Jul 6, 2017
@jacogr jacogr deleted the kaikun branch July 6, 2017 07:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants