From ff08f73fa935eb341e14ad2dd70b3849131afc1c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 5 Jan 2016 03:25:53 -0800 Subject: [PATCH] check orphan size. misc. --- lib/bcoin/chain.js | 19 +++++++++++-------- lib/bcoin/pool.js | 10 +++++++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 30aa9d52..d35077f7 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -34,7 +34,8 @@ function Chain(options) { this.orphan = { map: {}, bmap: {}, - count: 0 + count: 0, + size: 0 }; this.index = { @@ -194,6 +195,7 @@ Chain.prototype.resetHeight = function resetHeight(height) { this.orphan.map = {}; this.orphan.bmap = {}; this.orphan.count = 0; + this.orhpan.size = 0; this.index.entries.length = height + 1; this.index.heights = this.index.entries.reduce(function(out, entry) { if (!self.options.fullNode) { @@ -262,6 +264,7 @@ Chain.prototype.add = function add(block, peer) { // If previous block wasn't ever seen - add current to orphans if (i == null) { this.orphan.count++; + this.orphan.size += block.size(); this.orphan.map[prev] = block; this.orphan.bmap[hash] = block; code = Chain.codes.newOrphan; @@ -310,13 +313,15 @@ Chain.prototype.add = function add(block, peer) { delete this.orphan.bmap[block.hash('hex')]; delete this.orphan.map[hash]; this.orphan.count--; + this.orphan.size -= block.size(); } // Failsafe for large orphan chains - if (this.orphan.count > 10000) { + if (this.orphan.size > 20971520) { this.orphan.map = {}; this.orphan.bmap = {}; this.orphan.count = 0; + this.orphan.size = 0; } // No need to have a huge chain @@ -328,12 +333,7 @@ Chain.prototype.add = function add(block, peer) { return code; }; -Chain.prototype.has = function has(hash, noIndex, cb) { - if (typeof noIndex === 'function') { - cb = noIndex; - noIndex = false; - } - +Chain.prototype.has = function has(hash, cb) { if (this.loading) { this.once('load', function() { this.has(hash, noIndex, cb); @@ -510,6 +510,9 @@ Chain.prototype.getNextBlock = function getNextBlock(hash) { }; Chain.prototype.size = function size() { + // This would get called alot. TODO: Optimize. + if (!this.options.fullNode) + return this.index.entries.filter(Boolean).length; return this.index.entries.length; }; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 1d693de3..116248f0 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -520,8 +520,12 @@ Pool.prototype._addIndex = function _addIndex(block, peer) { orphans = this.chain.orphan.count; res = this.chain.add(block, peer); - if (res) - this.emit('chain-error', bcoin.chain.msg(res), peer); + + if (res) { + this.emit('debug', + 'Chain Error: ' + bcoin.chain.msg(res), + peer); + } // Do not emit if nothing was added to the chain if (this.chain.size() === size) { @@ -1137,7 +1141,7 @@ Pool.prototype._request = function _request(type, hash, options, cb) { // Block should be not in chain, or be requested // Do not use with headers-first if (!options.force && (type === 'block' || type === 'filtered')) - return this.chain.has(hash, true, next); + return this.chain.has(hash, next); return next(false); };