-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
feat: add socket connection between nodes #99
base: dev
Are you sure you want to change the base?
Conversation
@@ -77,6 +77,7 @@ | |||
"blackList": [] | |||
}, | |||
"options": { | |||
"maxWsConnections": 15, |
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.
Isn't better to move it to wsNode
config and use more descriptive naming, like:
"wsNode": {
"enabled": true,
"maxIncomingConnections": 15, // maximum number of peers to receive transactions from
"maxOutgoingConnections": 25, // maximum number of peers to broadcast transactions to
}
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.
It's really confusing to have options.maxWsConnections
and wsNode.maxConnections
.
const maxReconnectDelay = 60000; | ||
const defaultReconnectionDelay = 5000; | ||
|
||
function TransportWsApi(modules, library, 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.
Can you add a JSDoc to describe the purpose of TransportWsApi
?
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.
And also JSDoc for prototype's methods below.
@@ -606,6 +604,21 @@ d.run(function () { | |||
}); | |||
}], | |||
|
|||
/** | |||
* Listens for new transacttions using websocket and links peers to the websocket server |
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.
typo: transacttions -> transactions
Peers.prototype.getByNonce = function (nonce) { | ||
for (const [peerString, peer] of Object.entries(__private.peers)) { | ||
if (peer.nonce === nonce) { | ||
return __private.peers[peerString]; |
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.
return __private.peers[peerString]; | |
return peer; |
Same behaviour?
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.
If so, you can use Object.values
instead of Object.entries
Please elaborate:
|
Please elaborate:
Does REST node exchange uses the same authentication? What's it for? |
(Updated, see the comment below #99 (comment))
If |
Why then we have Can you please explain what are |
I see that current implementation assumes that a node is connected either via ws, or http. One of reasons while adding wsNode is redundancy. It means some nodes may connect with ws AND http. Imaging that a network has low amount of nodes, and all of them ws-enabled. Can we improve that some nodes will connect both ws and http? |
const maxReconnectDelay = 60000; | ||
const defaultReconnectionDelay = 5000; | ||
|
||
function TransportWsApi(modules, library, 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.
And also JSDoc for prototype's methods below.
const { io } = require('socket.io-client'); | ||
const Peer = require('../../logic/peer.js'); | ||
|
||
const maxReconnectDelay = 60000; |
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.
Consider moving it to config
|
||
// Connect to multiple peers | ||
self.getRandomPeers(self.maxConnections, (err, peers) => { | ||
if (err || !peers.length) { |
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.
Preferred to log this event / debug
// Find a new peer to replace it | ||
self.getRandomPeer((err, newPeer) => { | ||
if (err || !newPeer) { | ||
self.logger.debug('WebSocket: Failed to find replacement peer'); |
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.
'WebSocket: Failed to find replacement' ⟶ Add 'for peer ws://${peer.ip}:${peer.port} + err || no peers'?
}; | ||
|
||
TransportWsApi.prototype.updatePeers = function() { | ||
const self = this; |
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.
Preferred to log this event / debug
}; | ||
|
||
TransportWsApi.prototype.rotatePeers = function () { | ||
const self = this; |
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.
Preferred to log this event / debug
return; | ||
} | ||
|
||
self.logger.debug(`Rotating ${newPeers.length} out of ${totalConnections} peers.`); |
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.
Consider to clarify logging everywhere that it's about ws.
E.g., [wsNode] Rotating ${newPeers.length} out of ${totalConnections} peers.
TransportWsApi.prototype.startRotation = function() { | ||
this.rotationInterval = setInterval(() => { | ||
this.rotatePeers(); | ||
}, 1000 * 60 * 30); // 30 minutes |
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.
Make const
* Changes the connection type to ws | ||
* @param {Peer} peer | ||
*/ | ||
Peers.prototype.switchToWs = function (peer) { |
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.
See my comment about redundancy
#99 (comment)
The authentication is needed to link the incoming WebSocket connection to the existing peer. REST provides
A nonce is generated on app start and never changes.
Yes
If a node receives an invalid transaction over WebSocket, the peer will be removed from the peer list, as it would be using REST. |
A node can be connected using both I suggest still preferring nodes with http-only connections when broadcasting, but also choosing some % of ws-connected nodes to broadcast to using http. |
https://trello.com/c/5Jt28f7V/63-feat-socket-support-node-node
Description
Config
Added new required config properties:
API
The
GET /api/peers
now returns info about connection type for incoming (syncing) and outcoming (broadcasting) transactions: