From ed787d58cbda5e1690a819a1abde416eb514bdac Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 6 Mar 2014 00:01:28 -0300 Subject: [PATCH] recover from race condition in interrupted sync --- lib/HistoricSync.js | 37 +++++++++++++++++++++++++++---------- lib/TransactionDb.js | 1 - 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/HistoricSync.js b/lib/HistoricSync.js index b87f54a1..75ed2989 100644 --- a/lib/HistoricSync.js +++ b/lib/HistoricSync.js @@ -238,22 +238,32 @@ function spec() { function(err) { if (err) return next(err); var ret = false; - if ( self.blockChainHeight === blockInfo.height || - blockInfo.confirmations > 0) { + if ( self.blockChainHeight === blockInfo.height || // Still the tip + blockInfo.confirmations > 0) { // Or is confirmed ret = false; } else { oldtip = tip; tip = blockInfo.previousblockhash; - assert(tip); - p('Previous TIP is now orphan. Back to:' + tip); - ret = true; + p('Previous TIP is now orphan.'); + if (tip) { + p('\tGoing back to:' + tip); + ret = true; + } + else { + p('\tNo able to recover last tip. A Total resync is needed.'); + tip = self.genesis; + self.needResync = 1; + ret = false; + } } return ret; }, function(err) { self.startBlock = tip; - p('Resuming sync from block:'+tip); + if (!self.needResync) { + p('Resuming sync from block:'+tip); + } return next(err); } ); @@ -354,15 +364,22 @@ function spec() { function(s_c) { self.checkNetworkSettings(s_c); }, - function(s_c) { - self.updateConnectedCountDB(s_c); - }, - function(s_c) { + function(s_c) { self.updateBlockChainHeight(s_c); }, function(s_c) { self.updateStartBlock(s_c); }, + function(s_c) { + if (self.needResync) { + self.needResync=0; + self.sync.destroy(s_c); + } + else s_c(); + }, + function(s_c) { + self.updateConnectedCountDB(s_c); + }, function(s_c) { self.prepareFileSync(opts, s_c); }, diff --git a/lib/TransactionDb.js b/lib/TransactionDb.js index 03415402..7c7ac978 100644 --- a/lib/TransactionDb.js +++ b/lib/TransactionDb.js @@ -673,7 +673,6 @@ isspent var k = FROM_BLK_PREFIX + hash; var k2 = IN_BLK_PREFIX; - // This is slow, but prevent us to create a new block->tx index. db.createReadStream({ start: k, end: k + '~'