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

some refactoring #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions lib/gossiper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ var PeerState = require('./peer_state').PeerState,
util = require('util'),
child_process = require('child_process'),
dns = require('dns'),
msgpack = require('msgpack');
msgpack = require('msgpack');

const dnsPromises = dns.promises;

var Gossiper = exports.Gossiper = function(port, seeds, ip_to_bind) {
EventEmitter.call(this);
Expand Down Expand Up @@ -41,14 +43,19 @@ Gossiper.prototype.start = function(callback) {
// we don't listen on any ip because it's important that we listen
// on the same ip that the server identifies itself as
child_process.exec('hostname', function(error, stdout, stderr) {

var l = stdout.length;
var hostname = stdout.slice(0, l - 1);
dns.lookup(hostname, 4, function(err,address, family) {
self.peer_name = [address, self.port.toString()].join(':');

dnsPromises.lookup(hostname, {family: 4}).then((result) => {

self.peer_name = [result.address, self.port.toString()].join(':');
self.peers[self.peer_name] = self.my_state;
self.my_state.name = self.peer_name;
self.server.listen(self.port, address, callback);

});

});
}

Expand All @@ -75,9 +82,9 @@ Gossiper.prototype.gossip = function() {
}

// Possilby gossip to a dead peer
var prob = this.deadPeers().length / (this.livePeers().length + 1)
const prob = this.deadPeers().length / (this.livePeers().length + 1)
if(Math.random() < prob) {
var dead_peer = this.chooseRandom(this.deadPeers());
const dead_peer = this.chooseRandom(this.deadPeers());
this.gossipToPeer(dead_peer);
}

Expand All @@ -89,8 +96,8 @@ Gossiper.prototype.gossip = function() {
}

// Check health of peers
for(var i in this.peers) {
var peer = this.peers[i];
for(let i in this.peers) {
let peer = this.peers[i];
if(peer != this.my_state) {
peer.isSuspect();
}
Expand All @@ -99,7 +106,7 @@ Gossiper.prototype.gossip = function() {

Gossiper.prototype.chooseRandom = function(peers) {
// Choose random peer to gossip to
var i = Math.floor(Math.random()*1000000) % peers.length;
const i = Math.floor(Math.random()*1000000) % peers.length;
return peers[i];
}

Expand Down Expand Up @@ -146,7 +153,7 @@ Gossiper.prototype.handleMessage = function(net_stream, mp_stream, msg) {

Gossiper.prototype.handleNewPeers = function(new_peers) {
var self = this;
for(var i in new_peers) {
for(let i in new_peers) {
var peer_name = new_peers[i];
this.peers[peer_name] = new PeerState(peer_name);
this.emit('new_peer', peer_name);
Expand Down Expand Up @@ -214,18 +221,18 @@ Gossiper.prototype.peerValue = function(peer, k) {

Gossiper.prototype.allPeers = function() {
var keys = [];
for(var k in this.peers) { keys.push(k) };
for(let k in this.peers) { keys.push(k) };
return keys;
}

Gossiper.prototype.livePeers = function() {
var keys = [];
for(var k in this.peers) { if(this.peers[k].alive) { keys.push(k)} };
for(let k in this.peers) { if(this.peers[k].alive) { keys.push(k)} };
return keys;
}

Gossiper.prototype.deadPeers = function() {
var keys = [];
for(var k in this.peers) { if(!this.peers[k].alive) { keys.push(k) } };
for(let k in this.peers) { if(!this.peers[k].alive) { keys.push(k) } };
return keys;
}
72 changes: 37 additions & 35 deletions lib/scuttle.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var PeerState = require('./peer_state').PeerState;
var Scuttle = exports.Scuttle = function(peers, local_peer) {
this.peers = peers;
var Scuttle = exports.Scuttle = function (peers, local_peer) {
this.peers = peers;
this.local_peer = local_peer;
};

Scuttle.prototype.digest = function() {
Scuttle.prototype.digest = function () {
var digest = {};
for(i in this.peers) {
for (i in this.peers) {
var p = this.peers[i];
digest[i] = p.max_version_seen;
}
Expand All @@ -15,23 +15,23 @@ Scuttle.prototype.digest = function() {

// HEART OF THE BEAST

Scuttle.prototype.scuttle = function(digest) {
var deltas_with_peer = [];
var requests = {}
var new_peers = [];
for(var peer in digest) {
var local_version = this.maxVersionSeenForPeer(peer);
var local_peer = this.peers[peer];
var digest_version = digest[peer];
Scuttle.prototype.scuttle = function (digest) {
var deltas_with_peer = [];
var requests = {}
var new_peers = [];
for (var peer in digest) {
var local_version = this.maxVersionSeenForPeer(peer);
var local_peer = this.peers[peer];
var digest_version = digest[peer];

if(!this.peers[peer]) {
if (!this.peers[peer]) {
// We don't know about this peer. Request all information.
requests[peer] = 0;
new_peers.push(peer);
} else if(local_version > digest[peer]) {
} else if (local_version > digest[peer]) {
// We have more recent information for this peer. Build up deltas.
deltas_with_peer.push( { peer : peer, deltas : local_peer.deltasAfterVersion(digest[peer]) });
} else if(local_version < digest[peer]) {
deltas_with_peer.push({ peer: peer, deltas: local_peer.deltasAfterVersion(digest[peer]) });
} else if (local_version < digest[peer]) {
// They have more recent information, request it.
requests[peer] = local_version;
} else {
Expand All @@ -40,54 +40,56 @@ Scuttle.prototype.scuttle = function(digest) {
}

// Sort by peers with most deltas
deltas_with_peer.sort( function(a,b) { return b.deltas.length - a.deltas.length } );
deltas_with_peer.sort(function (a, b) { return b.deltas.length - a.deltas.length });

var deltas = [];
for(i in deltas_with_peer) {
for (i in deltas_with_peer) {
var peer = deltas_with_peer[i];
var peer_deltas = peer.deltas;

// Sort deltas by version number
peer_deltas.sort(function(a,b) { return a[2] - b[2]; })
if(peer_deltas.length > 1) {
// console.log(peer_deltas);
peer_deltas.sort(function (a, b) { return a[2] - b[2]; })
if (peer_deltas.length > 1) {
// console.log(peer_deltas);
}
for(j in peer_deltas) {
for (j in peer_deltas) {
var delta = peer_deltas[j];
delta.unshift(peer.peer);
deltas.push(delta);
}
}

return { 'deltas' : deltas,
'requests' : requests,
'new_peers' : new_peers };
return {
deltas,
requests,
new_peers
};
}

Scuttle.prototype.maxVersionSeenForPeer = function(peer) {
if(this.peers[peer]) {
Scuttle.prototype.maxVersionSeenForPeer = function (peer) {
if (this.peers[peer]) {
return this.peers[peer].max_version_seen;
} else {
return 0;
}
}

Scuttle.prototype.updateKnownState = function(deltas) {
for(i in deltas) {
Scuttle.prototype.updateKnownState = function (deltas) {
for (i in deltas) {
var d = deltas[i];

var peer_name = d.shift();
var peer_name = d.shift();
var peer_state = this.peers[peer_name];
peer_state.updateWithDelta(d[0],d[1],d[2]);
peer_state.updateWithDelta(d[0], d[1], d[2]);
}
};

Scuttle.prototype.fetchDeltas = function(requests) {
Scuttle.prototype.fetchDeltas = function (requests) {
var deltas = []
for(i in requests) {
for (i in requests) {
var peer_deltas = this.peers[i].deltasAfterVersion(requests[i]);
peer_deltas.sort(function(a,b) { return a[2] - b[2]; });
for(j in peer_deltas) {
peer_deltas.sort(function (a, b) { return a[2] - b[2]; });
for (j in peer_deltas) {
peer_deltas[j].unshift(i);
deltas.push(peer_deltas[j]);
}
Expand Down