peer handling.

This commit is contained in:
Christopher Jeffrey 2016-03-23 18:49:51 -07:00
parent cf9a62a75b
commit cc7004e43a
3 changed files with 90 additions and 28 deletions

View File

@ -1021,8 +1021,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
seen: true seen: true
}, peer); }, peer);
if (self.options.headers) self.emit('verify-error', block, 'bad-prevblk', 0, peer);
self.emit('verify-error', block, 'bad-prevblk', 0, peer);
return done(); return done();
} }
@ -1046,8 +1045,7 @@ Chain.prototype.add = function add(initial, peer, callback, force) {
hash: hash, hash: hash,
seen: false seen: false
}, peer); }, peer);
if (self.options.headers) self.emit('verify-error', block, 'bad-prevblk', 0, peer);
self.emit('verify-error', block, 'bad-prevblk', 0, peer);
return done(); return done();
} }

View File

@ -120,7 +120,7 @@ Peer.prototype._init = function init() {
this.socket.once('error', function(err) { this.socket.once('error', function(err) {
self._error(err); self._error(err);
self.pool.setMisbehavior(self, 100); self.setMisbehavior(100);
}); });
this.socket.once('close', function() { this.socket.once('close', function() {
@ -141,7 +141,7 @@ Peer.prototype._init = function init() {
self._error(err); self._error(err);
// Something is wrong here. // Something is wrong here.
// Ignore this peer. // Ignore this peer.
self.pool.setMisbehavior(self, 100); self.setMisbehavior(100);
}); });
this.challenge = utils.nonce(); this.challenge = utils.nonce();
@ -506,35 +506,25 @@ Peer.prototype._handleAddr = function handleAddr(addrs) {
var now = utils.now(); var now = utils.now();
addrs.forEach(function(addr) { addrs.forEach(function(addr) {
var ip, address4, address6; var ts = addr.ts;
var host = addr.ipv4 !== '0.0.0.0'
if (addr.ts <= 100000000 || addr.ts > now + 10 * 60)
addr.ts = now - 5 * 24 * 60 * 60;
ip = addr.ipv4 !== '0.0.0.0'
? addr.ipv4 ? addr.ipv4
: addr.ipv6; : addr.ipv6;
address4 = addr.ipv4 !== '0.0.0.0' if (ts <= 100000000 || ts > now + 10 * 60)
? addr.ipv4 + ':' + addr.port ts = now - 5 * 24 * 60 * 60;
: null;
address6 = '[' + addr.ipv6 + ']:' + addr.port;
this.emit('addr', { this.emit('addr', {
date: new Date(addr.ts * 1000), ts: ts,
ts: addr.ts,
services: addr.services, services: addr.services,
ip: ip, host: host,
ipv4: addr.ipv4,
ipv6: addr.ipv6,
host: ip,
host4: addr.ipv4,
host6: addr.ipv6,
port: addr.port || network.port, port: addr.port || network.port,
address: address4 || address6, network: addr.network,
address4: address4, bloom: addr.bloom,
address6: address6 getutxo: addr.getutxo,
witness: addr.witness,
headers: addr.version >= 31800,
spv: addr.bloom && addr.version >= 70011
}); });
}, this); }, this);

View File

@ -814,6 +814,15 @@ Pool.prototype._createPeer = function _createPeer(options) {
if (self.options.discoverPeers === false) if (self.options.discoverPeers === false)
return; return;
if (!data.network)
return;
if (self.options.spv && !data.spv)
return;
if (self.options.witness && !data.witness)
return;
if (self.seeds.length > 300) if (self.seeds.length > 300)
self.setSeeds(self.seeds.slice(-150)); self.setSeeds(self.seeds.slice(-150));
@ -1805,6 +1814,71 @@ Pool.prototype.getSeed = function getSeed(priority) {
} }
}; };
Pool.prototype.getSeed = function getSeed(priority) {
var addr;
if (priority) {
addr = this._getRandom(this.originalSeeds);
if (addr)
return addr;
addr = this._getRandom(this.seeds);
if (addr)
return addr;
addr = this.seeds[Math.random() * (this.seeds.length - 1) | 0];
if (addr)
return addr;
return this.originalSeeds[Math.random() * (this.originalSeeds.length - 1) | 0];
}
// Hang back if we don't have a loader peer yet.
if (!this.peers.load)
return;
// Need at least one block peer.
if (this.originalSeeds.length + this.seeds.length === 1) {
assert(this.originalSeeds[0]);
return this.originalSeeds[0];
}
addr = this._getRandom(this.originalSeeds, true);
if (addr)
return addr;
addr = this._getRandom(this.seeds, true);
if (addr)
return addr;
};
Pool.prototype._getRandom = function _getRandom(seeds, uniq) {
var tried = {};
var tries = 0;
var index, addr;
for (;;) {
if (tries === seeds.length)
return;
index = Math.round(Math.random() * (seeds.length - 1));
addr = seeds[index];
if (!tried[index]) {
tried[index] = true;
tries++;
}
if (this.isMisbehaving(addr.host))
continue;
if (uniq && this.getPeer(addr.host))
continue;
return addr;
}
};
Pool.prototype.setSeeds = function setSeeds(seeds) { Pool.prototype.setSeeds = function setSeeds(seeds) {
this.seeds = []; this.seeds = [];
this.hosts = {}; this.hosts = {};