-
Notifications
You must be signed in to change notification settings - Fork 1.7k
PubSub for parity-js #5830
PubSub for parity-js #5830
Conversation
'parity' instead of 'parity_subscribe' calls params with empty array as options. If eth_subscribe includes empty array parity-core will send invalid request (eth api doesn't have options)
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.
Overall looks quite nice, couple of grumbles regarding style, code quality and usability of the pubsub()
namespace.
js/src/api/pubsub/pubsub.js
Outdated
// } | ||
|
||
// parity API | ||
accountsInfo (callback) { |
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.
How about splitting this APIs into separate namespaces, like:
api.pubsub().parity().accountsInfo()
and api.pubsub().eth().newHeads()
?
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.
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.
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.
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?
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.
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)
js/src/api/pubsub/pubsub.js
Outdated
} | ||
|
||
sendRawTransaction (callback, data) { | ||
return this.addListener(this._api, 'eth_sendRawTransaction', callback, [inData(data)]); |
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.
Doesn't really make sense to subscribe to that.
js/src/api/pubsub/pubsub.js
Outdated
return this.addListener(this._api, 'eth_sendRawTransaction', callback, [inData(data)]); | ||
} | ||
|
||
submitTransaction (callback, data) { |
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.
Doesn't really make sense to subscribe to that.
js/src/api/pubsub/pubsub.js
Outdated
return this.addListener(this._api, 'eth_getWork', callback); | ||
} | ||
|
||
submitWork (callback, nonce, powHash, mixDigest) { |
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.
Doesn't really make sense to subscribe to that.
js/src/api/pubsub/pubsub.js
Outdated
return this.addListener(this._api, 'eth_submitWork', callback, [inNumber16(nonce), powHash, mixDigest]); | ||
} | ||
|
||
submitHashrate (callback, hashrate, clientId) { |
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.
Doesn't really make sense to subscribe to that.
js/src/api/transport/ws/ws.js
Outdated
// 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); |
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.
Imho better to write an if-else
instead.
js/src/api/transport/ws/ws.js
Outdated
const json = this.encode(uMethod, params); | ||
|
||
this._messages[id] = { id, 'method': uMethod, params, json, | ||
resolve: _ => { |
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.
I would extract those methods to const resolve = () => {...}
and then leave this.messages[id]
as simple as in subscribe
.
js/src/api/transport/ws/ws.js
Outdated
} | ||
|
||
unsubscribe (messageId) { | ||
return new Promise(() => { |
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.
Why this promise is never resolved? I think it should be resolved in this._messages[id]
, no?
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.
@kaikun213 That one is still valid too.
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.
Had some missunderstanding there. Fixxed now!
js/src/api/transport/ws/ws.js
Outdated
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); |
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.
Instead of crashing when usubscribe fails you should rather reject
the upper promise.
js/src/api/transport/ws/ws.js
Outdated
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]; |
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.
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.
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.
Yes thought about it, now I realized it makes it much cleaner.
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.
Almost there, few more grumbles.
js/src/api/pubsub/eth/eth.js
Outdated
return this.addListener('eth', 'newHeads', callback); | ||
} | ||
// | ||
// logs (callback) { |
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.
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') { |
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.
Good catch 👍
js/src/api/transport/ws/ws.js
Outdated
this.error(event.data); | ||
|
||
if (result.params.error) { | ||
result[error] = result.params.error; |
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.
@kaikun213 This comment is still valid.
js/src/api/transport/ws/ws.js
Outdated
} | ||
|
||
unsubscribe (messageId) { | ||
return new Promise(() => { |
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.
@kaikun213 That one is still valid too.
js/src/api/transport/ws/ws.js
Outdated
throw Error('Error unsubscribing.' + e); | ||
} }; | ||
|
||
this._messages[id] = { id, 'method': uMethod, params, json, resolve, reject }; |
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.
'
around method
are not required
Looks good. @jacogr Could you have a look and merge if you consider it ready? |
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.
Brilliant, happy as it stands (and looking forward to actually cleaning up the polling using this).
In progress. Still needs testing and documentation