add adding state to chain.

This commit is contained in:
Christopher Jeffrey 2016-05-05 03:00:24 -07:00
parent 019617162c
commit 2faf8513c4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 18 additions and 3 deletions

View File

@ -69,6 +69,7 @@ function Chain(options) {
this.loaded = false;
this.db = new bcoin.chaindb(this, options);
this.total = 0;
this.adding = false;
this.orphanLimit = options.orphanLimit || (20 << 20);
this.pendingLimit = options.pendingLimit || (1024 << 20);
this.locker = new bcoin.locker(this, this.add, this.pendingLimit);
@ -1302,6 +1303,15 @@ Chain.prototype.onFlush = function onFlush(callback) {
return this.locker.onFlush(callback);
};
/**
* Test whether the chain is in the process of adding blocks.
* @returns {Boolean}
*/
Chain.prototype.isBusy = function isBusy() {
return this.adding || this.locker.pending.length > 0;
};
/**
* Add a block to the chain, perform all necessary verification.
* @param {Block|MerkleBlock|CompactBlock} block
@ -1321,6 +1331,8 @@ Chain.prototype.add = function add(block, callback, force) {
callback = utils.wrap(callback, unlock);
this.adding = true;
(function next(block, initial) {
var hash = block.hash('hex');
var prevHash = block.prevBlock;
@ -1539,7 +1551,7 @@ Chain.prototype.add = function add(block, callback, force) {
if (entry.chainwork.cmp(self.tip.chainwork) <= 0) {
return self.db.save(entry, block, false, function(err) {
if (err)
return callback(err);
return done(err);
// Keep track of the number of blocks we
// added and the number of orphans resolved.
@ -1599,6 +1611,8 @@ Chain.prototype.add = function add(block, callback, force) {
self.emit('full');
}
self.adding = false;
if (err)
callback(err);
else

View File

@ -467,10 +467,11 @@ Pool.prototype._startInterval = function _startInterval() {
if (self.chain.isFull())
return;
if (self.chain.locker.pending.length > 0)
if (self.chain.isBusy())
return self._startTimer();
bcoin.debug('Stall recovery: loading again.');
// self._load();
}
@ -1643,7 +1644,7 @@ Pool.prototype.scheduleRequests = function scheduleRequests(peer) {
Pool.prototype._sendRequests = function _sendRequests(peer) {
var size, items;
if (this.chain.locker.pending.length > 0)
if (this.chain.isBusy())
return;
if (peer.queue.block.length === 0)