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,11 +189,23 @@ 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;
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) { self.sync.bDb.getTip(function(err,tip, height) {
if (!tip) return next(); if (!tip) return next();
@ -242,6 +254,7 @@ HistoricSync.prototype.updateStartBlock = function(next) {
} }
); );
}); });
}
}; };
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