add startAt opts in sync
This commit is contained in:
parent
8768eb955c
commit
7542138847
|
@ -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);
|
||||
|
|
18
lib/Sync.js
18
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');
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue