implements uptosync

This commit is contained in:
Matias Alejo Garcia 2014-01-17 10:22:00 -03:00
parent 6600e941a2
commit 4442497638
2 changed files with 28 additions and 16 deletions

View File

@ -111,6 +111,11 @@ function spec() {
], ],
function (err){ function (err){
if (opts.uptoexisting && existed) {
p('DONE. Found existing block: %s ', blockHash);
return cb(err);
}
if (err) if (err)
p('ERROR: @%s: %s [count: block_count: %d]', blockHash, err, that.block_count); p('ERROR: @%s: %s [count: block_count: %d]', blockHash, err, that.block_count);
@ -126,19 +131,17 @@ function spec() {
}); });
}; };
HistoricSync.prototype.syncBlocks = function(start, end, isForward, cb) { HistoricSync.prototype.syncBlocks = function(start, end, opts, cb) {
var that = this; var that = this;
p('Starting from: ', start); p('Starting from: ', start);
p(' to : ', end); p(' to : ', end);
p(' isForward: ', isForward); p(' opts: ', JSON.stringify(opts));
return that.getPrevNextBlock( start, end, opts , cb);
return that.getPrevNextBlock( start, end,
isForward ? { next: 1 } : { prev: 1}, cb);
}; };
HistoricSync.prototype.do_import_history = function(opts, next) { HistoricSync.prototype.import_history = function(opts, next) {
var that = this; var that = this;
var retry_attemps = 100; var retry_attemps = 100;
@ -203,21 +206,20 @@ function spec() {
], ],
function(err) { function(err) {
var start, end;
function sync() { function sync() {
var start, end, isForward;
if (opts.reverse) { if (opts.reverse) {
start = block_best; start = block_best;
end = that.network.genesisBlock.hash.reverse().toString('hex'); end = that.network.genesisBlock.hash.reverse().toString('hex');
isForward = false; opts.prev = true;
} }
else { else {
start = that.network.genesisBlock.hash.reverse().toString('hex'); start = that.network.genesisBlock.hash.reverse().toString('hex');
end = null; end = null;
isForward = true; opts.next = true;
} }
that.syncBlocks(start, end, isForward, function(err) { that.syncBlocks(start, end, opts, function(err) {
if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){ if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){
setTimeout(function() { setTimeout(function() {
@ -236,9 +238,13 @@ function spec() {
}); });
}; };
HistoricSync.prototype.import_history = function(opts, next) { // Reverse Imports (upto if we have genesis block?)
HistoricSync.prototype.smart_import = function(next) {
var that = this; var that = this;
that.do_import_history(opts, next); var opts = {
prev: 1,
};
that.import_history(opts, next);
}; };

View File

@ -16,6 +16,7 @@ program
.option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet') .option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet')
.option('-D --destroy', 'Remove current DB (and start from there)', 0) .option('-D --destroy', 'Remove current DB (and start from there)', 0)
.option('-R --reverse', 'Sync backwards', 0) .option('-R --reverse', 'Sync backwards', 0)
.option('-U --uptoexisting', 'Sync only until an existing block is found', 0)
.parse(process.argv); .parse(process.argv);
var historicSync = new HistoricSync({ var historicSync = new HistoricSync({
@ -23,7 +24,7 @@ var historicSync = new HistoricSync({
}); });
if (program.remove) { if (program.remove) {
// TODO: Sure?
} }
async.series([ async.series([
@ -31,12 +32,17 @@ async.series([
historicSync.init(program, cb); historicSync.init(program, cb);
}, },
function(cb) { function(cb) {
historicSync.import_history(program, function(err, count) { historicSync.import_history({
network: program.network,
destroy: program.destroy,
reverse: program.reverse,
uptoexisting: program.uptoexisting,
}, function(err, count) {
if (err) { if (err) {
console.log('CRITICAL ERROR: ', err); console.log('CRITICAL ERROR: ', err);
} }
else { else {
console.log('Done! [%d blocks]', count, err); console.log('Finished. [%d blocks]', count);
} }
cb(); cb();
}); });