Skip to content

Commit

Permalink
Keep the peers list up to date when nodes are added/removed
Browse files Browse the repository at this point in the history
  • Loading branch information
goldfire committed Apr 8, 2018
1 parent dca21ab commit 5051a14
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 129 deletions.
16 changes: 15 additions & 1 deletion lib/democracy.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ class Democracy extends EventEmitter {
channels: data.channels || [],
};

// Add this to the peers list.
const source = data.source.split(':');
const index = this.options.peers.findIndex(p => p[0] === source[0] && p[1] === source[1]);
if (index < 0) {
this.options.peers.push(data.source.split(':'));
}

// Emit that this node has been added.
this.emit('added', this._nodes[data.id]);

Expand Down Expand Up @@ -317,7 +324,14 @@ class Democracy extends EventEmitter {

// If we have concensus, remove this node from the list.
if (node.voters.length >= numVoters) {
this.emit('removed', this._nodes[candidate]);
this.emit('removed', node);

// Remove from the nodes/peers.
const source = node.source.split(':');
const index = this.options.peers.findIndex(p => p[0] === source[0] && p[1] === source[1]);
if (index >= 0) {
this.options.peers.splice(index, 1);
}
this._nodes[candidate] = null;
}

Expand Down
Loading

0 comments on commit 5051a14

Please sign in to comment.