diff --git a/lib/Sync.js b/lib/Sync.js index 29ebed8a..71a6e7b9 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -83,8 +83,18 @@ Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) { allowReorgs = true; } if (!b) return cb(); - var self = this; + + if ( self.storingBlock ) { + console.log('## Storing a block already. Delaying storeTipBlock with:' + + b.hash); + return setTimeout( function() { + console.log('## Retrying storeTipBlock with: ' + b.hash); + self.storeTipBlock(b,allowReorgs,cb); + }, 1000); + } + + self.storingBlock=1; var oldTip, oldNext, oldHeight, needReorg = false, height = -1; var newPrev = b.previousblockhash; @@ -164,6 +174,7 @@ Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) { if (err && err.toString().match(/WARN/)) { err = null; } + self.storingBlock=0; return cb(err, height); }); }; @@ -177,7 +188,10 @@ Sync.prototype.processReorg = function(oldTip, oldNext, newPrev, oldHeight, cb) function(c) { self.bDb.getHeight(newPrev, function(err, height) { - if (!height) return c(new Error('Could not found block:' + newPrev)); + if (!height) { + // Case 3 + allowReorgs = true + return c(new Error('Could not found block:' + newPrev)); + } if (height<0) return c(); newHeight = height + 1;