From f7ac99d24dea4372b09eee19101a3976ea74b6ec Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 17 Feb 2016 16:02:11 -0800 Subject: [PATCH] minor refactoring. --- lib/bcoin/chain.js | 16 +++++++++++++++- lib/bcoin/pool.js | 12 +++++------- lib/bcoin/utils.js | 4 ++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index dcad8645..1ef46053 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -45,7 +45,10 @@ function Chain(options) { this.locked = false; this.pending = []; this.pendingBlocks = {}; + this.pendingSize = 0; + this.total = 0; this.orphanLimit = options.orphanLimit || 20 * 1024 * 1024; + this.pendingLimit = options.pendingLimit || 20 * 1024 * 1024; this.invalid = {}; this.orphan = { @@ -106,6 +109,10 @@ Chain.prototype._init = function _init() { utils.debug('Handled orphan %s (%s)', utils.revHex(data.hash), host); }); + this.on('purge', function(count, size) { + utils.debug('Warning: %dmb of orphans cleared!', utils.mb(size)); + }); + this.loading = true; utils.debug('Chain is loading.'); @@ -813,6 +820,11 @@ Chain.prototype.add = function add(initial, peer, callback) { if (this.locked) { this.pending.push([initial, peer, callback]); this.pendingBlocks[initial.hash('hex')] = true; + this.pendingSize += initial.getSize(); + if (this.pendingSize > this.pendingLimit) { + utils.debug('Warning: %dmb of pending blocks.', + utils.mb(this.pendingSize)); + } return; } @@ -863,6 +875,7 @@ Chain.prototype.add = function add(initial, peer, callback) { // If the orphan chain forked, simply // reset the orphans and find a new peer. if (orphan.hash('hex') !== hash) { + self.emit('purge', self.orphan.count, self.orphan.size); self.orphan.map = {}; self.orphan.bmap = {}; self.orphan.count = 0; @@ -1079,7 +1092,6 @@ Chain.prototype.add = function add(initial, peer, callback) { self.orphan.bmap = {}; self.orphan.count = 0; self.orphan.size = 0; - utils.debug('Warning: 20mb of orphans cleared!'); } // We intentionally did not asyncify the @@ -1094,6 +1106,7 @@ Chain.prototype.add = function add(initial, peer, callback) { else callback(null, total); + self.total += total; self.locked = false; // Start resolving the queue @@ -1103,6 +1116,7 @@ Chain.prototype.add = function add(initial, peer, callback) { item = self.pending.shift(); delete self.pendingBlocks[item[0].hash('hex')]; + self.pendingSize -= item[0].getSize(); self.add(item[0], item[1], item[2]); }); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 5a586758..b8c17ebc 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -120,9 +120,7 @@ function Pool(options) { this.block = { bestHeight: 0, bestHash: null, - type: !options.spv ? 'block' : 'filtered', - invalid: {}, - total: 0 + type: !options.spv ? 'block' : 'filtered' }; this.tx = { @@ -650,15 +648,13 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { self.emit('chain-progress', self.chain.fillPercent(), peer); - self.block.total += added; - if (self.chain.height % 20 === 0) { utils.debug( 'Got: %s from %s chain len %d blocks %d orp %d act %d queue %d target %s peers %d pending %d', block.rhash, new Date(block.ts * 1000).toString(), self.chain.height, - self.block.total, + self.chain.total, self.chain.orphan.count, self.request.activeBlocks, peer._blockQueue.length, @@ -1799,7 +1795,9 @@ Pool.prototype.removeSeed = function removeSeed(seed) { }; Pool.prototype.isOrphaning = function isOrphaning(peer) { - return false; + if (!this.options.orphanDOS) + return false; + if (utils.now() > peer.orphanTime + 3 * 60) { peer.orphans = 0; peer.orphanTime = utils.now(); diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 3d865768..4b2f7a16 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1456,3 +1456,7 @@ utils.forEach = function forEach(arr, iter, callback) { iter(item, next); }); }; + +utils.mb = function mb(size) { + return size / 1024 / 1024 | 0; +};