minor refactoring.

This commit is contained in:
Christopher Jeffrey 2016-02-17 16:02:11 -08:00
parent d4aa15f9a3
commit f7ac99d24d
3 changed files with 24 additions and 8 deletions

View File

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

View File

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

View File

@ -1456,3 +1456,7 @@ utils.forEach = function forEach(arr, iter, callback) {
iter(item, next);
});
};
utils.mb = function mb(size) {
return size / 1024 / 1024 | 0;
};