check orphan size. misc.

This commit is contained in:
Christopher Jeffrey 2016-01-05 03:25:53 -08:00
parent 60e01b261c
commit ff08f73fa9
2 changed files with 18 additions and 11 deletions

View File

@ -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;
};

View File

@ -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);
};