remove recursion from HistoricSync for speed
This commit is contained in:
parent
e438fb53b7
commit
fc509f6e7e
|
@ -260,6 +260,7 @@ function spec() {
|
|||
};
|
||||
|
||||
|
||||
|
||||
HistoricSync.prototype.getBlockFromFile = function(height, scanOpts, cb) {
|
||||
var self = this;
|
||||
|
||||
|
@ -288,6 +289,7 @@ function spec() {
|
|||
},
|
||||
//get Info
|
||||
function(c) {
|
||||
|
||||
self.blockExtractor.getNextBlock(function(err, b) {
|
||||
if (err || ! b) return c(err);
|
||||
|
||||
|
@ -348,16 +350,12 @@ function spec() {
|
|||
else {
|
||||
|
||||
// Continue
|
||||
if (blockInfo) {
|
||||
|
||||
// mainchain
|
||||
if (isMainChain) height++;
|
||||
|
||||
if (blockInfo && blockInfo.hash) {
|
||||
self.syncedBlocks++;
|
||||
self.err = null;
|
||||
self.status = 'syncing';
|
||||
|
||||
return self.getBlockFromFile(height, scanOpts, cb);
|
||||
return cb(null, true, isMainChain);
|
||||
}
|
||||
else {
|
||||
self.err = null;
|
||||
|
@ -421,8 +419,26 @@ function spec() {
|
|||
}
|
||||
},
|
||||
], function(err) {
|
||||
if (err) {
|
||||
self.setError(err);
|
||||
return next(err, 0);
|
||||
}
|
||||
|
||||
|
||||
// SETUP Sync params
|
||||
var start, end;
|
||||
function sync() {
|
||||
|
||||
if (!self.step) {
|
||||
var step = parseInt( (self.blockChainHeight - self.syncedBlocks) / 1000);
|
||||
|
||||
if (self.opts.progressStep) {
|
||||
step = self.opts.progressStep;
|
||||
}
|
||||
|
||||
if (step < 10) step = 10;
|
||||
self.step = step;
|
||||
}
|
||||
|
||||
if (scanOpts.reverse) {
|
||||
start = lastBlock;
|
||||
end = self.genesis;
|
||||
|
@ -438,44 +454,34 @@ function spec() {
|
|||
p(' scanOpts: ', JSON.stringify(scanOpts));
|
||||
|
||||
if (scanOpts.fromFiles) {
|
||||
self.getBlockFromFile(0, scanOpts, function(err) {
|
||||
return next(err);
|
||||
|
||||
var keepGoing = true;
|
||||
var height = 0;
|
||||
|
||||
async.whilst(function() {
|
||||
return keepGoing;
|
||||
}, function (w_cb) {
|
||||
self.getBlockFromFile(height, scanOpts, function(err, inKeepGoing, wasMainChain) {
|
||||
keepGoing = inKeepGoing;
|
||||
if (wasMainChain) height++;
|
||||
//Black magic from http://stackoverflow.com/questions/20936486/node-js-maximum-call-stack-size-exceeded
|
||||
setImmediate(function(){
|
||||
|
||||
return w_cb(err);
|
||||
});
|
||||
})
|
||||
}, function(err) {
|
||||
|
||||
console.log('[HistoricSync.js.468]'); //TODO
|
||||
return next();
|
||||
});
|
||||
}
|
||||
else {
|
||||
self.getPrevNextBlock(start, end, scanOpts, function(err) {
|
||||
if (err && err.message.match(/ECONNREFUSED/)) {
|
||||
setTimeout(function() {
|
||||
p('Retrying in %d secs', retry_secs);
|
||||
sync();
|
||||
},
|
||||
retry_secs * 1000);
|
||||
}
|
||||
else return next(err);
|
||||
return next(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!self.step) {
|
||||
|
||||
var step = parseInt( (self.blockChainHeight - self.syncedBlocks) / 1000);
|
||||
|
||||
if (self.opts.progressStep) {
|
||||
step = self.opts.progressStep;
|
||||
}
|
||||
|
||||
if (step < 10) step = 10;
|
||||
self.step = step;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
self.setError(err);
|
||||
return next(err, 0);
|
||||
}
|
||||
else {
|
||||
sync();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
|
||||
'use strict';
|
||||
|
||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||
|
|
Loading…
Reference in New Issue