From 754213884710696b7530766eaa99d0017c419c20 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sun, 8 Jun 2014 16:24:17 -0300 Subject: [PATCH] add startAt opts in sync --- lib/HistoricSync.js | 109 +++++++++++++++++++++++++------------------- lib/Sync.js | 18 ++++---- util/sync.js | 4 +- 3 files changed, 73 insertions(+), 58 deletions(-) diff --git a/lib/HistoricSync.js b/lib/HistoricSync.js index 8e6bb980..6c0e6a60 100644 --- a/lib/HistoricSync.js +++ b/lib/HistoricSync.js @@ -189,59 +189,72 @@ HistoricSync.prototype.checkNetworkSettings = function(next) { }); }; -HistoricSync.prototype.updateStartBlock = function(next) { +HistoricSync.prototype.updateStartBlock = function(opts, next) { var self = this; self.startBlock = self.genesis; - self.sync.bDb.getTip(function(err,tip, height) { - if (!tip) return next(); - - var blockInfo; - var oldtip; - - //check that the tip is still on the mainchain - async.doWhilst( - function(cb) { - self.sync.bDb.fromHashWithInfo(tip, function(err, bi) { - blockInfo = bi ? bi.info : {}; - if (oldtip) - self.sync.bDb.setBlockNotMain(oldtip, cb); - else - return cb(); - }); - }, - function(err) { - if (err) return next(err); - var ret = false; - - var d = Math.abs(height-blockInfo.height); - if (d>6) { - error('Previous Tip block tip height differs by %d. Please delete and resync (-D)',d); - process.exit(1); - } - if ( self.blockChainHeight === blockInfo.height || - blockInfo.confirmations > 0) { - ret = false; - } - else { - oldtip = tip; - if (!tip) - throw new Error('Previous blockchain tip was not found on bitcoind. Please reset Insight DB. Tip was:'+tip) - tip = blockInfo.previousblockhash; - info('Previous TIP is now orphan. Back to:' + tip); - ret = true; - } - return ret; - }, - function(err) { - self.startBlock = tip; - self.height = height; - info('Resuming sync from block: %s #%d',tip,height); + if (opts.startAt) { + self.sync.bDb.fromHashWithInfo(opts.startAt, function(err, bi) { + var blockInfo = bi ? bi.info : {}; + if (blockInfo.height) { + self.startBlock = opts.startAt; + self.height = blockInfo.height; + info('Resuming sync from block: %s #%d',opts.startAt, self.height); return next(err); } - ); - }); + }); + } + else { + self.sync.bDb.getTip(function(err,tip, height) { + if (!tip) return next(); + + var blockInfo; + var oldtip; + + //check that the tip is still on the mainchain + async.doWhilst( + function(cb) { + self.sync.bDb.fromHashWithInfo(tip, function(err, bi) { + blockInfo = bi ? bi.info : {}; + if (oldtip) + self.sync.bDb.setBlockNotMain(oldtip, cb); + else + return cb(); + }); + }, + function(err) { + if (err) return next(err); + var ret = false; + + var d = Math.abs(height-blockInfo.height); + if (d>6) { + error('Previous Tip block tip height differs by %d. Please delete and resync (-D)',d); + process.exit(1); + } + if ( self.blockChainHeight === blockInfo.height || + blockInfo.confirmations > 0) { + ret = false; + } + else { + oldtip = tip; + if (!tip) + throw new Error('Previous blockchain tip was not found on bitcoind. Please reset Insight DB. Tip was:'+tip) + tip = blockInfo.previousblockhash; + info('Previous TIP is now orphan. Back to:' + tip); + ret = true; + } + return ret; + }, + function(err) { + self.startBlock = tip; + self.height = height; + info('Resuming sync from block: %s #%d',tip,height); + return next(err); + } + ); + }); + } }; HistoricSync.prototype.prepareFileSync = function(opts, next) { @@ -363,7 +376,7 @@ HistoricSync.prototype.prepareToSync = function(opts, next) { self.updateBlockChainHeight(s_c); }, function(s_c) { - self.updateStartBlock(s_c); + self.updateStartBlock(opts,s_c); }, function(s_c) { self.prepareFileSync(opts, s_c); diff --git a/lib/Sync.js b/lib/Sync.js index 71a6e7b9..60f160fb 100644 --- a/lib/Sync.js +++ b/lib/Sync.js @@ -97,15 +97,16 @@ Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) { self.storingBlock=1; var oldTip, oldNext, oldHeight, needReorg = false, height = -1; var newPrev = b.previousblockhash; - async.series([ - function(c) { - // TODO? remove this check? - self.bDb.has(b.hash, function(err, val) { - return c(err || - (val ? new Error('WARN: Ignoring already existing block:' + b.hash) : null)); - }); - }, + + // This seems unnecesary. + // function(c) { + // // TODO? remove this check? + // self.bDb.has(b.hash, function(err, val) { + // return c(err || + // (val ? new Error('WARN: Ignoring already existing block:' + b.hash) : null)); + // }); + // }, function(c) { if (!allowReorgs || newPrev === self.cachedLastHash) return c(); self.bDb.has(newPrev, function(err, val) { @@ -120,7 +121,6 @@ Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) { self.bDb.getTip(function(err, hash, h) { oldTip = hash; oldHeight = hash ? (h || 0) : -1 - if (oldTip && newPrev !== oldTip) { needReorg = true; console.log('## REORG Triggered, tip mismatch'); diff --git a/util/sync.js b/util/sync.js index 1cfccd7f..2bb11cdd 100755 --- a/util/sync.js +++ b/util/sync.js @@ -15,7 +15,8 @@ program .option('-D --destroy', 'Remove current DB (and start from there)', 0) .option('-S --startfile', 'Number of file from bitcoind to start(default=0)') .option('-R --rpc', 'Force sync with RPC') - .option('--stop [hash]', 'StopAt block',1) + .option('--start [hash]', 'StartAt block') + .option('--stop [hash]', 'StopAt block') .option('-v --verbose', 'Verbose 0/1', 0) .parse(process.argv); @@ -34,6 +35,7 @@ async.series([ var opts= { forceStartFile: program.startfile, forceRPC: program.rpc, + startAt: program.start, stopAt: program.stop, }; console.log('[options]',opts); //TODO