refactor sync
This commit is contained in:
parent
ab808e5c2b
commit
64956538cc
|
@ -14,7 +14,8 @@ function spec() {
|
|||
var Sync = require('./Sync').class();
|
||||
var sockets = require('../app/controllers/socket.js');
|
||||
var BlockExtractor = require('./BlockExtractor.js').class();
|
||||
//var Deserialize = require('bitcore/Deserialize');
|
||||
// var bitcoreUtil = require('bitcore/util/util');
|
||||
// var Deserialize = require('bitcore/Deserialize');
|
||||
|
||||
|
||||
var BAD_GEN_ERROR = 'Bad genesis block. Network mismatch between Insight and bitcoind? Insight is configured for:';
|
||||
|
@ -122,10 +123,10 @@ function spec() {
|
|||
if (self.opts.shouldBroadcastSync) {
|
||||
sockets.broadcastSyncInfo(self.info());
|
||||
}
|
||||
|
||||
// if (self.syncPercentage > 10) {
|
||||
// process.exit(-1);
|
||||
// }
|
||||
//
|
||||
// if (self.syncPercentage > 10) {
|
||||
// process.exit(-1);
|
||||
// }
|
||||
};
|
||||
|
||||
HistoricSync.prototype.getPrevNextBlock = function(blockHash, blockEnd, scanOpts, cb) {
|
||||
|
@ -228,7 +229,6 @@ function spec() {
|
|||
//get Info
|
||||
self.blockExtractor.getNextBlock(function(err, b) {
|
||||
if (err || ! b) return cb(err);
|
||||
|
||||
blockInfo = b.getStandardizedObject(b.txs, self.network);
|
||||
// blockInfo.curWork = Deserialize.intFromCompact(b.bits);
|
||||
// We keep the RPC field names
|
||||
|
@ -323,62 +323,109 @@ function spec() {
|
|||
});
|
||||
};
|
||||
|
||||
HistoricSync.prototype.importHistory = function(scanOpts, next) {
|
||||
HistoricSync.prototype.smartImport = function(scanOpts, next) {
|
||||
var self = this;
|
||||
|
||||
var genesis, count;
|
||||
var lastBlock;
|
||||
var tip;
|
||||
|
||||
async.series([
|
||||
function(cb) {
|
||||
if (scanOpts.destroy) {
|
||||
function(s_c) {
|
||||
if (!scanOpts.destroy) return s_c();
|
||||
|
||||
p('Deleting DB...');
|
||||
return self.sync.destroy(cb);
|
||||
}
|
||||
return cb();
|
||||
},
|
||||
function (cb) {
|
||||
return self.updateBlockCount(cb);
|
||||
},
|
||||
function(cb) {
|
||||
if (!scanOpts.reverse) return cb();
|
||||
self.rpc.getBlockHash(self.blockChainHeight, function(err, res) {
|
||||
if (err) return cb(err);
|
||||
lastBlock = res.result;
|
||||
return cb();
|
||||
});
|
||||
},
|
||||
function(cb) {
|
||||
if (!scanOpts.reverse) return cb();
|
||||
self.sync.bDb.getTip(function(err, res) {
|
||||
if (err) return cb(err);
|
||||
tip = res;
|
||||
return self.sync.destroy(s_c);
|
||||
},
|
||||
function(s_c) {
|
||||
self.sync.bDb.has(self.genesis, function(err, b) {
|
||||
genesis = b;
|
||||
return s_c(err);
|
||||
});
|
||||
},
|
||||
function(s_c) {
|
||||
self.countNotOrphan(function(err, c) {
|
||||
count = c;
|
||||
return s_c(err);
|
||||
});
|
||||
},
|
||||
function(s_c) {
|
||||
if (!config.bitcoind.dataDir) return s_c();
|
||||
if (scanOpts.startFile) {
|
||||
self.blockExtractor = new BlockExtractor(config.bitcoind.dataDir, config.network);
|
||||
self.blockExtractor.currentFileIndex = scanOpts.startFile;
|
||||
return s_c();
|
||||
}
|
||||
self.sync.bDb.getLastFileIndex(function(err, idx) {
|
||||
self.blockExtractor = new BlockExtractor(config.bitcoind.dataDir, config.network);
|
||||
if (idx) self.blockExtractor.currentFileIndex = idx;
|
||||
return s_c(err);
|
||||
});
|
||||
},
|
||||
function(s_c) {
|
||||
self.updateBlockCount(s_c);
|
||||
},
|
||||
|
||||
console.log('Old Tip:', tip);
|
||||
return cb();
|
||||
});
|
||||
},
|
||||
|
||||
function(cb) {
|
||||
if (scanOpts.reverse) {
|
||||
// define sync strategy
|
||||
function(s_c) {
|
||||
if (!genesis || scanOpts.destroy || count < self.blockChainHeight * 0.9 ) {
|
||||
|
||||
// Full sync.
|
||||
if (!genesis)
|
||||
p('Could not find Genesis block. Running FULL SYNC');
|
||||
else
|
||||
p('Less that 90% of current blockchain is stored. Running FULL SYNC',
|
||||
parseInt(count/self.blockChainHeight*100));
|
||||
|
||||
if (config.bitcoind.dataDir) {
|
||||
p('bitcoind dataDir configured...importing blocks from .dat files');
|
||||
p('Starting from file: ' + self.blockExtractor.currentFileIndex);
|
||||
scanOpts.fromFiles = true;
|
||||
}
|
||||
else {
|
||||
scanOpts.reverse = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
p('Genesis block found. Syncing upto old TIP.');
|
||||
p('Got ' + count + ' out of ' + self.blockChainHeight + ' blocks');
|
||||
scanOpts.reverse = true;
|
||||
}
|
||||
|
||||
if (!scanOpts.reverse) return s_c();
|
||||
|
||||
self.rpc.getBlockHash(self.blockChainHeight, function(err, res) {
|
||||
if (err) return s_c(err);
|
||||
lastBlock = res.result;
|
||||
return s_c();
|
||||
});
|
||||
},
|
||||
function(s_c) {
|
||||
if (!scanOpts.reverse) return s_c();
|
||||
self.sync.bDb.getTip(function(err, res) {
|
||||
if (err) return s_c(err);
|
||||
tip = res;
|
||||
|
||||
console.log('Old Tip:', tip);
|
||||
return s_c();
|
||||
});
|
||||
},
|
||||
function(s_c) {
|
||||
if (!scanOpts.reverse) return s_c();
|
||||
self.countNotOrphan(function(err, count) {
|
||||
if (err) return cb(err);
|
||||
if (err) return s_c(err);
|
||||
|
||||
self.syncedBlocks = count || 0;
|
||||
return cb();
|
||||
return s_c();
|
||||
});
|
||||
}
|
||||
else {
|
||||
return cb();
|
||||
}
|
||||
},
|
||||
], function(err) {
|
||||
}],
|
||||
function(err) {
|
||||
// SETUP Sync params
|
||||
var start, end;
|
||||
if (err) {
|
||||
self.setError(err);
|
||||
return next(err, 0);
|
||||
}
|
||||
// SETUP Sync params
|
||||
var start, end;
|
||||
|
||||
if (!self.step) {
|
||||
var step = parseInt( (self.blockChainHeight - self.syncedBlocks) / 1000);
|
||||
|
@ -431,70 +478,6 @@ function spec() {
|
|||
return next(err);
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
HistoricSync.prototype.smartImport = function(scanOpts, next) {
|
||||
var self = this;
|
||||
|
||||
var genesis, count;
|
||||
async.series([
|
||||
function(s_c) {
|
||||
self.sync.bDb.has(self.genesis, function(err, b) {
|
||||
genesis = b;
|
||||
return s_c(err);
|
||||
});
|
||||
},
|
||||
function(s_c) {
|
||||
if (scanOpts.destroy) return s_c();
|
||||
|
||||
self.countNotOrphan(function(err, c) {
|
||||
count = c;
|
||||
return s_c(err);
|
||||
});
|
||||
},
|
||||
function(s_c) {
|
||||
if (!config.bitcoind.dataDir) return s_c();
|
||||
|
||||
self.sync.bDb.getLastFileIndex(function(err, idx) {
|
||||
self.blockExtractor = new BlockExtractor(config.bitcoind.dataDir, config.network);
|
||||
if (idx) self.blockExtractor.currentFileIndex = idx;
|
||||
return s_c(err);
|
||||
});
|
||||
},
|
||||
function(s_c) {
|
||||
self.updateBlockCount(s_c);
|
||||
}],
|
||||
function(err) {
|
||||
if (err) return next(err);
|
||||
|
||||
if (!genesis || scanOpts.destroy || count < self.blockChainHeight * 0.9 ) {
|
||||
|
||||
// Full sync.
|
||||
|
||||
if (!genesis)
|
||||
p('Could not find Genesis block. Running FULL SYNC');
|
||||
else
|
||||
p('Less that 90% of current blockchain is stored. Running FULL SYNC',
|
||||
parseInt(count/self.blockChainHeight*100));
|
||||
|
||||
if (config.bitcoind.dataDir) {
|
||||
p('bitcoind dataDir configured...importing blocks from .dat files');
|
||||
p('Starting from file: ' + self.blockExtractor.currentFileIndex);
|
||||
scanOpts.fromFiles = true;
|
||||
}
|
||||
else {
|
||||
scanOpts.reverse = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
p('Genesis block found. Syncing upto old TIP.');
|
||||
p('Got ' + count + ' out of ' + self.blockChainHeight + ' blocks');
|
||||
scanOpts.reverse = true;
|
||||
}
|
||||
return self.importHistory(scanOpts, next);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -143,7 +143,6 @@ function spec() {
|
|||
function(err) {
|
||||
if (!err) self._handleBroadcast(b.hash, null, updatedAddrs);
|
||||
if (err && err.toString().match(/WARN/) ) {
|
||||
//console.log(err);
|
||||
err=null;
|
||||
}
|
||||
return cb(err);
|
||||
|
|
23
util/sync.js
23
util/sync.js
|
@ -12,12 +12,8 @@ var async = require('async');
|
|||
|
||||
program
|
||||
.version(SYNC_VERSION)
|
||||
.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 old Tip block is found', 0)
|
||||
.option('-F --fromfiles', 'Sync using bitcoind .dat block files (faster)', 0)
|
||||
.option('-S --smart', 'genesis stored? uptoexisting = 1, fromFiles=1 [default]', true)
|
||||
.option('-S --startfile', 'Number of file from bitcoind to start(default=0)')
|
||||
.option('-v --verbose', 'Verbose 0/1', 0)
|
||||
.parse(process.argv);
|
||||
|
||||
|
@ -33,19 +29,10 @@ async.series([
|
|||
historicSync.init(program, cb);
|
||||
},
|
||||
function(cb) {
|
||||
|
||||
if (typeof program.smart === 'undefined' || parseInt(program.smart) ) {
|
||||
historicSync.smartImport({
|
||||
destroy: program.destroy,
|
||||
},cb);
|
||||
}
|
||||
else {
|
||||
historicSync.importHistory({
|
||||
destroy: program.destroy,
|
||||
reverse: program.reverse,
|
||||
fromFiles: program.fromfiles,
|
||||
}, cb);
|
||||
}
|
||||
historicSync.smartImport({
|
||||
destroy: program.destroy,
|
||||
startFile: program.startfile,
|
||||
},cb);
|
||||
},
|
||||
],
|
||||
function(err) {
|
||||
|
|
Loading…
Reference in New Issue