From a8ecdc7453dfbb491a4fb141cd4e3c90c3333b06 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 2 Jun 2014 20:09:48 -0300 Subject: [PATCH] quick hack to handle blocks that arrive at the sametime --- lib/Sync.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/Sync.js b/lib/Sync.js index 29ebed8..71a6e7b 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;