diff --git a/lib/HistoricSync.js b/lib/HistoricSync.js index 2c432d9c..908f3414 100644 --- a/lib/HistoricSync.js +++ b/lib/HistoricSync.js @@ -111,6 +111,11 @@ function spec() { ], function (err){ + if (opts.uptoexisting && existed) { + p('DONE. Found existing block: %s ', blockHash); + return cb(err); + } + if (err) 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; p('Starting from: ', start); p(' to : ', end); - p(' isForward: ', isForward); + p(' opts: ', JSON.stringify(opts)); - - return that.getPrevNextBlock( start, end, - isForward ? { next: 1 } : { prev: 1}, cb); + return that.getPrevNextBlock( start, end, opts , cb); }; - HistoricSync.prototype.do_import_history = function(opts, next) { + HistoricSync.prototype.import_history = function(opts, next) { var that = this; var retry_attemps = 100; @@ -203,21 +206,20 @@ function spec() { ], function(err) { + var start, end; function sync() { - var start, end, isForward; - if (opts.reverse) { start = block_best; end = that.network.genesisBlock.hash.reverse().toString('hex'); - isForward = false; + opts.prev = true; } else { start = that.network.genesisBlock.hash.reverse().toString('hex'); 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--){ 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; - that.do_import_history(opts, next); + var opts = { + prev: 1, + }; + that.import_history(opts, next); }; diff --git a/util/sync.js b/util/sync.js index 569ca782..a4e3f441 100755 --- a/util/sync.js +++ b/util/sync.js @@ -16,6 +16,7 @@ program .option('-N --network [livenet]', 'Set bitcoin network [testnet]', 'testnet') .option('-D --destroy', 'Remove current DB (and start from there)', 0) .option('-R --reverse', 'Sync backwards', 0) + .option('-U --uptoexisting', 'Sync only until an existing block is found', 0) .parse(process.argv); var historicSync = new HistoricSync({ @@ -23,7 +24,7 @@ var historicSync = new HistoricSync({ }); if (program.remove) { - + // TODO: Sure? } async.series([ @@ -31,12 +32,17 @@ async.series([ historicSync.init(program, 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) { console.log('CRITICAL ERROR: ', err); } else { - console.log('Done! [%d blocks]', count, err); + console.log('Finished. [%d blocks]', count); } cb(); });