From 72e5d5fbc7f391fb80789692408ad580df4336a1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 20 May 2016 04:15:01 -0700 Subject: [PATCH] better orphan handling. --- lib/bcoin/peer.js | 2 +- lib/bcoin/pool.js | 38 ++++++++++++++++++++++-------------- lib/bcoin/protocol/parser.js | 4 +++- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 1048c472..0beb38f9 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -1207,7 +1207,7 @@ Peer.prototype._handleGetData = function handleGetData(items) { self.emit('error', err); bcoin.debug( - 'Served %d items s with getdata (notfound=%d) (%s).', + 'Served %d items with getdata (notfound=%d) (%s).', items.length - notfound.length, notfound.length, self.hostname); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 1d065652..fbb1e2b6 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -820,7 +820,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { var self = this; var requested; - callback = utils.asyncify(callback); + callback = utils.ensure(callback); // Fulfill our request. requested = self.fulfill(block); @@ -831,24 +831,29 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { bcoin.debug( 'Received unrequested block: %s (%s).', block.rhash, peer.hostname); - return callback(); + return utils.nextTick(callback); } this.chain.add(block, function(err) { if (err) { - if (err.type === 'VerifyError') { - if (err.score >= 0) - peer.sendReject(block, err.code, err.reason, err.score); - - if (err.reason === 'bad-prevblk') { - if (peer === self.peers.load) - self.resolveOrphan(peer, null, block.hash('hex')); - } - + if (err.type !== 'VerifyError') { self.scheduleRequests(peer); - return callback(err); } + + if (err.score >= 0) + peer.sendReject(block, err.code, err.reason, err.score); + + if (err.reason === 'bad-prevblk' && peer === self.peers.load) { + self.resolveOrphan(peer, null, block.hash('hex'), function(e) { + self.scheduleRequests(peer); + return callback(e || err); + }); + return; + } + + self.scheduleRequests(peer); + return callback(err); } @@ -857,12 +862,15 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { self.emit('chain-progress', self.chain.getProgress(), peer); if (self.chain.total % 20 === 0) { - bcoin.debug( - 'Status: tip=%s ts=%s height=%d blocks=%d orphans=%d active=%d' - + ' queue=%d target=%s peers=%d pending=%d highest=%d jobs=%d', + bcoin.debug('Status:' + + ' tip=%s ts=%s height=%d progress=%s' + + ' blocks=%d orphans=%d active=%d' + + ' queue=%d target=%s peers=%d' + + ' pending=%d highest=%d jobs=%d', block.rhash, utils.date(block.ts), self.chain.height, + (Math.floor(self.chain.getProgress() * 10000) / 100) + '%', self.chain.total, self.chain.orphan.count, self.request.activeBlocks, diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index eb24aa1f..24f2c02b 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -621,7 +621,7 @@ Parser.parseInv = function parseInv(p) { count = p.readVarint(); - assert(count < 50000, 'Item count too high.'); + assert(count <= 50000, 'Item count too high.'); for (i = 0; i < count; i++) { items.push({ @@ -1182,6 +1182,8 @@ Parser.parseAddr = function parseAddr(p) { count = p.readVarint(); + assert(count <= 10000, 'Too many addresses.'); + for (i = 0; i < count; i++) addrs.push(Parser.parseAddress(p, true));