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;
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) {
if (!tip) return next();
@ -242,6 +254,7 @@ HistoricSync.prototype.updateStartBlock = function(next) {
}
);
});
}
};
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);

View File

@ -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');

View File

@ -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