Merge pull request #126 from matiu/feature/startAt

add startAt opts in sync
This commit is contained in:
Matias Alejo Garcia 2014-06-08 16:26:56 -03:00
commit 47b70f6164
3 changed files with 73 additions and 58 deletions

View File

@ -189,59 +189,72 @@ HistoricSync.prototype.checkNetworkSettings = function(next) {
}); });
}; };
HistoricSync.prototype.updateStartBlock = function(next) { HistoricSync.prototype.updateStartBlock = function(opts, next) {
var self = this; var self = this;
self.startBlock = self.genesis; self.startBlock = self.genesis;
self.sync.bDb.getTip(function(err,tip, height) { if (opts.startAt) {
if (!tip) return next(); self.sync.bDb.fromHashWithInfo(opts.startAt, function(err, bi) {
var blockInfo = bi ? bi.info : {};
var blockInfo; if (blockInfo.height) {
var oldtip; self.startBlock = opts.startAt;
self.height = blockInfo.height;
//check that the tip is still on the mainchain info('Resuming sync from block: %s #%d',opts.startAt, self.height);
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); 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) { HistoricSync.prototype.prepareFileSync = function(opts, next) {
@ -363,7 +376,7 @@ HistoricSync.prototype.prepareToSync = function(opts, next) {
self.updateBlockChainHeight(s_c); self.updateBlockChainHeight(s_c);
}, },
function(s_c) { function(s_c) {
self.updateStartBlock(s_c); self.updateStartBlock(opts,s_c);
}, },
function(s_c) { function(s_c) {
self.prepareFileSync(opts, s_c); self.prepareFileSync(opts, s_c);

View File

@ -97,15 +97,16 @@ Sync.prototype.storeTipBlock = function(b, allowReorgs, cb) {
self.storingBlock=1; self.storingBlock=1;
var oldTip, oldNext, oldHeight, needReorg = false, height = -1; var oldTip, oldNext, oldHeight, needReorg = false, height = -1;
var newPrev = b.previousblockhash; var newPrev = b.previousblockhash;
async.series([ async.series([
function(c) {
// TODO? remove this check? // This seems unnecesary.
self.bDb.has(b.hash, function(err, val) { // function(c) {
return c(err || // // TODO? remove this check?
(val ? new Error('WARN: Ignoring already existing block:' + b.hash) : null)); // self.bDb.has(b.hash, function(err, val) {
}); // return c(err ||
}, // (val ? new Error('WARN: Ignoring already existing block:' + b.hash) : null));
// });
// },
function(c) { function(c) {
if (!allowReorgs || newPrev === self.cachedLastHash) return c(); if (!allowReorgs || newPrev === self.cachedLastHash) return c();
self.bDb.has(newPrev, function(err, val) { 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) { self.bDb.getTip(function(err, hash, h) {
oldTip = hash; oldTip = hash;
oldHeight = hash ? (h || 0) : -1 oldHeight = hash ? (h || 0) : -1
if (oldTip && newPrev !== oldTip) { if (oldTip && newPrev !== oldTip) {
needReorg = true; needReorg = true;
console.log('## REORG Triggered, tip mismatch'); console.log('## REORG Triggered, tip mismatch');

View File

@ -15,7 +15,8 @@ program
.option('-D --destroy', 'Remove current DB (and start from there)', 0) .option('-D --destroy', 'Remove current DB (and start from there)', 0)
.option('-S --startfile', 'Number of file from bitcoind to start(default=0)') .option('-S --startfile', 'Number of file from bitcoind to start(default=0)')
.option('-R --rpc', 'Force sync with RPC') .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) .option('-v --verbose', 'Verbose 0/1', 0)
.parse(process.argv); .parse(process.argv);
@ -34,6 +35,7 @@ async.series([
var opts= { var opts= {
forceStartFile: program.startfile, forceStartFile: program.startfile,
forceRPC: program.rpc, forceRPC: program.rpc,
startAt: program.start,
stopAt: program.stop, stopAt: program.stop,
}; };
console.log('[options]',opts); //TODO console.log('[options]',opts); //TODO