use getheaders by default. fix header parsing and handling.

This commit is contained in:
Christopher Jeffrey 2016-01-04 13:14:09 -08:00
parent 50e606f625
commit d1595c0ec4
2 changed files with 69 additions and 38 deletions

View File

@ -32,8 +32,8 @@ function Pool(options) {
network.set(this.options.network); network.set(this.options.network);
this.options.fullNode = !!this.options.fullNode; this.options.fullNode = !!this.options.fullNode;
this.options.headers = !!this.options.headers; this.options.headers = this.options.headers;
this.options.multiplePeers = !!this.options.multiplePeers; this.options.multiplePeers = this.options.multiplePeers;
this.options.relay = this.options.relay == null this.options.relay = this.options.relay == null
? (this.options.fullNode ? true : false) ? (this.options.fullNode ? true : false)
: this.options.relay; : this.options.relay;
@ -45,8 +45,15 @@ function Pool(options) {
this.redundancy = options.redundancy || 2; this.redundancy = options.redundancy || 2;
if (!this.options.fullNode) { if (!this.options.fullNode) {
this.options.headers = true; if (this.options.headers == null)
this.options.multiplePeers = true; this.options.headers = true;
if (this.options.multiplePeers == null)
this.options.multiplePeers = true;
} else {
if (this.options.headers == null)
this.options.headers = true;
if (this.options.multiplePeers == null)
this.options.multiplePeers = false;
} }
this.backoff = { this.backoff = {
@ -266,6 +273,10 @@ Pool.prototype._addLoader = function _addLoader() {
}); });
peer.once('ack', function() { peer.once('ack', function() {
if (!peer.socket) {
peer.destroy();
return;
}
peer.updateWatch(); peer.updateWatch();
if (!self._load()) if (!self._load())
self._stopTimer(); self._stopTimer();
@ -308,14 +319,19 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
assert(this.options.headers); assert(this.options.headers);
if (headers.length === 0)
return;
this.emit('debug', this.emit('debug',
'Recieved %s headers from %s', 'Recieved %s headers from %s',
headers.length, headers.length,
peer.address); peer.address);
if (headers.length === 0)
return;
if (headers.length > 2000) {
peer.destroy();
return;
}
this.emit('headers', headers); this.emit('headers', headers);
for (i = 0; i < headers.length; i++) { for (i = 0; i < headers.length; i++) {
@ -337,8 +353,12 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
} }
// Restart the getheaders process // Restart the getheaders process
if (last && headers.length >= 1999) if (last && headers.length === 2000) {
peer.loadHeaders(this.chain.locatorHashes(last), null); // Ideally we should use chain.locatorHashes here, but we can't since we
// didn't add the headers to the chain (in non-headers-first mode)
// peer.loadHeaders(this.chain.locatorHashes(last), null);
peer.loadHeaders([last.hash('hex')], null);
}
// Push our getdata packet // Push our getdata packet
this._scheduleRequests(); this._scheduleRequests();
@ -351,7 +371,7 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer) {
this.emit('debug', this.emit('debug',
'Requesting %s block packets from %s with getdata', 'Requesting %s block packets from %s with getdata',
this.request.active, this.request.queue.length,
peer.address peer.address
); );
}; };
@ -361,16 +381,21 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) {
assert(!this.options.headers); assert(!this.options.headers);
if (hashes.length === 0)
return;
this.emit('blocks', hashes);
this.emit('debug', this.emit('debug',
'Recieved %s block hashes from %s', 'Recieved %s block hashes from %s',
hashes.length, hashes.length,
peer.address); peer.address);
if (hashes.length === 0)
return;
if (hashes.length > 500) {
peer.destroy();
return;
}
this.emit('blocks', hashes);
for (i = 0; i < hashes.length; i++) { for (i = 0; i < hashes.length; i++) {
hash = hashes[i]; hash = hashes[i];
@ -402,7 +427,7 @@ Pool.prototype._handleBlocks = function _handleBlocks(hashes, peer) {
this.emit('debug', this.emit('debug',
'Requesting %s block packets from %s with getdata', 'Requesting %s block packets from %s with getdata',
this.request.active, this.request.queue.length,
peer.address peer.address
); );
}; };
@ -582,6 +607,11 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
peer.once('ack', function() { peer.once('ack', function() {
var i; var i;
if (!peer.socket) {
peer.destroy();
return;
}
if (self.destroyed) if (self.destroyed)
return; return;

View File

@ -256,28 +256,29 @@ Parser.prototype.parseHeaders = function parseHeaders(p) {
off = result.off; off = result.off;
count = result.r; count = result.r;
if (p.length >= off + 81) { if (p.length < count * 80)
for (i = 0; i < count && off + 81 < p.length; i++) { return this._error('Invalid headers size');
header = {};
start = off; for (i = 0; i < count; i++) {
header.version = readU32(p, off); header = {};
off += 4; start = off;
header.prevBlock = p.slice(off, off + 32); header.version = readU32(p, off);
off += 32; off += 4;
header.merkleRoot = p.slice(off, off + 32); header.prevBlock = p.slice(off, off + 32);
off += 32; off += 32;
header.ts = readU32(p, off); header.merkleRoot = p.slice(off, off + 32);
off += 4; off += 32;
header.bits = readU32(p, off); header.ts = readU32(p, off);
off += 4; off += 4;
header.nonce = readU32(p, off); header.bits = readU32(p, off);
off += 4; off += 4;
r = utils.readIntv(p, off); header.nonce = readU32(p, off);
header.totalTX = r.r; off += 4;
off = r.off; r = utils.readIntv(p, off);
header._raw = p.slice(start, start + 80); header.totalTX = r.r;
headers.push(header); off = r.off;
} header._raw = p.slice(start, start + 80);
headers.push(header);
} }
return headers; return headers;