insight-ui-zcash/lib/HistoricSync.js

358 lines
8.5 KiB
JavaScript
Raw Normal View History

2014-01-16 11:11:20 -08:00
'use strict';
require('classtool');
2014-01-21 14:13:21 -08:00
2014-01-16 11:11:20 -08:00
function spec() {
2014-01-21 08:28:01 -08:00
var util = require('util');
var RpcClient = require('bitcore/RpcClient').class();
var networks = require('bitcore/networks');
var async = require('async');
var config = require('../config/config');
var Block = require('../app/models/Block');
var Sync = require('./Sync').class();
var sockets = require('../app/controllers/socket.js');
2014-01-16 11:11:20 -08:00
2014-01-21 14:13:21 -08:00
var BAD_GEN_ERROR = 'Bad genesis block. Network mismatch between Insight and bitcoind? Insight is configured for:';
2014-01-23 10:10:52 -08:00
function HistoricSync() {
2014-01-21 08:28:01 -08:00
this.network = config.network === 'testnet' ? networks.testnet: networks.livenet;
2014-01-17 11:36:34 -08:00
var genesisHashReversed = new Buffer(32);
this.network.genesisBlock.hash.copy(genesisHashReversed);
this.genesis = genesisHashReversed.reverse().toString('hex');
2014-01-23 10:10:52 -08:00
//available status: starting / syncing / finished / aborted
this.status = 'starting';
2014-01-21 14:13:21 -08:00
this.error = null;
this.syncPercentage = 0;
this.syncedBlocks = 0;
this.skippedBlocks = 0;
2014-01-16 11:11:20 -08:00
}
function p() {
var args = [];
2014-01-21 08:28:01 -08:00
Array.prototype.push.apply(args, arguments);
args.unshift('[historic_sync]');
/*jshint validthis:true */
console.log.apply(this, args);
2014-01-16 11:11:20 -08:00
}
2014-01-21 14:13:21 -08:00
HistoricSync.prototype.setError = function(err) {
var self = this;
self.error = err.toString();
self.status='error';
self.showProgress();
};
2014-01-21 08:28:01 -08:00
HistoricSync.prototype.init = function(opts, cb) {
2014-01-21 11:43:05 -08:00
var self = this;
self.opts = opts;
2014-01-23 10:10:52 -08:00
self.rpc = new RpcClient(config.bitcoind);
self.sync = new Sync(opts);
2014-01-21 11:43:05 -08:00
self.sync.init(opts, function(err) {
if (err) {
2014-01-21 14:13:21 -08:00
self.setError(err);
2014-01-21 11:43:05 -08:00
return cb(err);
}
else {
// check testnet?
self.rpc.getBlockHash(0, function(err, res){
if (!err && ( res && res.result !== self.genesis)) {
2014-01-21 14:13:21 -08:00
err = new Error(BAD_GEN_ERROR + config.network);
self.setError(err);
2014-01-21 11:43:05 -08:00
}
2014-01-23 08:00:27 -08:00
if (err) self.setError(err);
2014-01-21 11:43:05 -08:00
return cb(err);
});
}
});
2014-01-16 11:11:20 -08:00
};
HistoricSync.prototype.close = function() {
this.sync.close();
};
2014-01-21 14:13:21 -08:00
HistoricSync.prototype.info = function() {
return {
status: this.status,
blockChainHeight: this.blockChainHeight,
syncPercentage: this.syncPercentage,
skippedBlocks: this.skippedBlocks,
syncedBlocks: this.syncedBlocks,
2014-01-23 08:00:27 -08:00
error: this.error,
2014-01-21 14:13:21 -08:00
};
};
HistoricSync.prototype.showProgress = function() {
var self = this;
2014-01-21 14:13:21 -08:00
if (self.error) {
2014-01-23 08:00:27 -08:00
p('ERROR: ' + self.error);
2014-01-21 14:13:21 -08:00
}
else {
self.syncPercentage = parseFloat(100 * self.syncedBlocks / self.blockChainHeight).toFixed(3);
2014-01-21 14:53:00 -08:00
if (self.syncPercentage > 100) self.syncPercentage = 100;
2014-01-21 14:13:21 -08:00
p(util.format('status: [%d%%] skipped: %d', self.syncPercentage, self.skippedBlocks));
}
2014-01-21 08:28:01 -08:00
if (self.opts.shouldBroadcast) {
2014-01-21 14:13:21 -08:00
sockets.broadcastSyncInfo(self.info());
}
};
2014-01-23 10:10:52 -08:00
HistoricSync.prototype.getPrevNextBlock = function(blockHash, blockEnd, scanOpts, cb) {
2014-01-17 11:36:34 -08:00
var self = this;
2014-01-16 11:11:20 -08:00
// recursion end.
2014-01-21 08:28:01 -08:00
if (!blockHash) return cb();
2014-01-16 11:11:20 -08:00
2014-01-21 08:28:01 -08:00
var existed = false;
2014-01-16 11:11:20 -08:00
var blockInfo;
var blockObj;
async.series([
2014-01-21 08:28:01 -08:00
// Already got it?
function(c) {
Block.findOne({
hash: blockHash
2014-01-16 11:11:20 -08:00
},
2014-01-21 08:28:01 -08:00
function(err, block) {
if (err) {
p(err);
return c(err);
}
if (block) {
existed = true;
blockObj = block;
2014-01-16 11:11:20 -08:00
}
return c();
2014-01-21 08:28:01 -08:00
});
},
//show some (inacurate) status
function(c) {
2014-01-21 14:13:21 -08:00
if ( ( self.syncedBlocks + self.skippedBlocks) % self.step === 1) {
2014-01-21 08:28:01 -08:00
self.showProgress();
}
2014-01-21 14:13:21 -08:00
2014-01-21 08:28:01 -08:00
return c();
},
//get Info from RPC
function(c) {
2014-01-16 11:11:20 -08:00
2014-01-21 08:28:01 -08:00
// TODO: if we store prev/next, no need to go to RPC
// if (blockObj && blockObj.nextBlockHash) return c();
self.rpc.getBlock(blockHash, function(err, ret) {
if (err) return c(err);
2014-01-16 11:11:20 -08:00
2014-01-21 08:28:01 -08:00
blockInfo = ret;
return c();
});
},
//store it
function(c) {
if (existed) return c();
self.sync.storeBlock(blockInfo.result, function(err) {
2014-01-17 11:36:34 -08:00
2014-01-21 08:28:01 -08:00
existed = err && err.toString().match(/E11000/);
2014-01-17 11:36:34 -08:00
2014-01-21 08:28:01 -08:00
if (err && ! existed) return c(err);
return c();
});
},
/* TODO: Should Start to sync backwards? (this is for partial syncs)
2014-01-16 11:11:20 -08:00
function(c) {
if (blockInfo.result.prevblockhash != current.blockHash) {
p("reorg?");
2014-01-23 10:10:52 -08:00
scanOpts.prev = 1;
2014-01-16 11:11:20 -08:00
}
return c();
}
*/
2014-01-21 08:28:01 -08:00
], function(err) {
2014-01-17 11:36:34 -08:00
2014-01-21 08:28:01 -08:00
if (err) {
2014-01-21 14:13:21 -08:00
self.err = util.format('ERROR: @%s: %s [count: syncedBlocks: %d]', blockHash, err, self.syncedBlocks);
2014-01-21 08:28:01 -08:00
self.status = 'aborted';
2014-01-21 14:13:21 -08:00
self.showProgress();
2014-01-21 08:28:01 -08:00
p(self.err);
}
else {
self.err = null;
self.status = 'syncing';
}
2014-01-17 05:22:00 -08:00
2014-01-23 10:10:52 -08:00
if ( (scanOpts.upToExisting && existed && self.syncedBlocks >= self.blockChainHeight) ||
2014-01-21 14:13:21 -08:00
(blockEnd && blockEnd === blockHash)) {
2014-01-18 13:28:24 -08:00
self.status = 'finished';
2014-01-21 08:28:01 -08:00
p('DONE. Found existing block: ', blockHash);
2014-01-21 14:13:21 -08:00
self.showProgress();
2014-01-17 11:36:34 -08:00
return cb(err);
2014-01-21 08:28:01 -08:00
}
// Continue
if (blockInfo && blockInfo.result) {
2014-01-21 14:13:21 -08:00
if (existed)
self.skippedBlocks++;
else
self.syncedBlocks++;
// recursion
2014-01-23 10:10:52 -08:00
if (scanOpts.prev && blockInfo.result.previousblockhash)
return self.getPrevNextBlock(blockInfo.result.previousblockhash, blockEnd, scanOpts, cb);
2014-01-21 14:13:21 -08:00
2014-01-23 10:10:52 -08:00
if (scanOpts.next && blockInfo.result.nextblockhash)
return self.getPrevNextBlock(blockInfo.result.nextblockhash, blockEnd, scanOpts, cb);
2014-01-21 08:28:01 -08:00
}
return cb(err);
2014-01-16 11:11:20 -08:00
});
};
2014-01-23 10:10:52 -08:00
HistoricSync.prototype.importHistory = function(scanOpts, next) {
2014-01-17 11:36:34 -08:00
var self = this;
2014-01-16 11:11:20 -08:00
2014-01-21 08:28:01 -08:00
var retry_secs = 2;
2014-01-16 11:11:20 -08:00
2014-01-22 05:01:08 -08:00
var lastBlock;
2014-01-16 11:11:20 -08:00
async.series([
2014-01-21 08:28:01 -08:00
function(cb) {
2014-01-23 10:10:52 -08:00
if (scanOpts.destroy) {
2014-01-21 08:28:01 -08:00
p('Deleting DB...');
return self.sync.destroy(cb);
}
return cb();
},
// We are not using getBestBlockHash, because is not available in all clients
function(cb) {
2014-01-23 10:10:52 -08:00
if (!scanOpts.reverse) return cb();
2014-01-21 08:28:01 -08:00
self.rpc.getBlockCount(function(err, res) {
if (err) return cb(err);
2014-01-21 14:13:21 -08:00
self.blockChainHeight = res.result;
return cb();
2014-01-21 08:28:01 -08:00
});
},
function(cb) {
2014-01-23 10:10:52 -08:00
if (!scanOpts.reverse) return cb();
2014-01-21 08:28:01 -08:00
2014-01-21 14:13:21 -08:00
self.rpc.getBlockHash(self.blockChainHeight, function(err, res) {
2014-01-21 08:28:01 -08:00
if (err) return cb(err);
2014-01-22 05:01:08 -08:00
lastBlock = res.result;
2014-01-21 08:28:01 -08:00
return cb();
});
},
function(cb) {
2014-01-23 10:10:52 -08:00
if (scanOpts.upToExisting) {
2014-01-21 08:28:01 -08:00
// should be isOrphan = true or null to be more accurate.
Block.count({
isOrphan: null
},
function(err, count) {
2014-01-17 05:44:14 -08:00
if (err) return cb(err);
2014-01-16 11:11:20 -08:00
2014-01-21 14:13:21 -08:00
self.syncedBlocks = count || 0;
2014-01-16 11:11:20 -08:00
return cb();
});
2014-01-21 08:28:01 -08:00
}
2014-01-23 10:10:52 -08:00
else {
return cb();
}
2014-01-21 08:28:01 -08:00
},
], function(err) {
var start, end;
function sync() {
2014-01-23 10:10:52 -08:00
if (scanOpts.reverse) {
2014-01-22 05:01:08 -08:00
start = lastBlock;
2014-01-21 08:28:01 -08:00
end = self.genesis;
2014-01-23 10:10:52 -08:00
scanOpts.prev = true;
2014-01-18 13:28:24 -08:00
}
else {
2014-01-21 08:28:01 -08:00
start = self.genesis;
end = null;
2014-01-23 10:10:52 -08:00
scanOpts.next = true;
2014-01-21 08:28:01 -08:00
}
p('Starting from: ', start);
p(' to : ', end);
2014-01-23 10:10:52 -08:00
p(' scanOpts: ', JSON.stringify(scanOpts));
2014-01-21 08:28:01 -08:00
2014-01-23 10:10:52 -08:00
self.getPrevNextBlock(start, end, scanOpts, function(err) {
2014-01-21 08:28:01 -08:00
if (err && err.message.match(/ECONNREFUSED/)) {
setTimeout(function() {
p('Retrying in %d secs', retry_secs);
sync();
},
retry_secs * 1000);
2014-01-16 11:11:20 -08:00
}
2014-01-21 08:28:01 -08:00
else return next(err);
});
}
2014-01-18 13:28:24 -08:00
2014-01-21 14:13:21 -08:00
if (!self.step) {
2014-01-21 14:40:41 -08:00
var step = parseInt( (self.blockChainHeight - self.syncedBlocks) / 1000);
2014-01-21 14:13:21 -08:00
if (self.opts.progressStep) {
step = self.opts.progressStep;
}
2014-01-21 14:40:41 -08:00
if (step < 10) step = 10;
2014-01-21 14:13:21 -08:00
self.step = step;
}
2014-01-21 08:28:01 -08:00
if (err) {
2014-01-21 14:13:21 -08:00
self.setError(err);
2014-01-21 08:28:01 -08:00
return next(err, 0);
}
else {
sync();
}
2014-01-16 11:11:20 -08:00
});
};
2014-01-16 11:11:20 -08:00
2014-01-17 11:36:34 -08:00
// upto if we have genesis block?
2014-01-21 14:13:21 -08:00
HistoricSync.prototype.smartImport = function(next) {
2014-01-17 11:36:34 -08:00
var self = this;
2014-01-21 08:28:01 -08:00
Block.findOne({
hash: self.genesis
},
function(err, b) {
2014-01-18 13:28:24 -08:00
2014-01-17 11:36:34 -08:00
if (err) return next(err);
if (!b) {
p('Could not find Genesis block. Running FULL SYNC');
}
else {
2014-01-18 13:28:24 -08:00
p('Genesis block found. Syncing upto known blocks.');
2014-01-17 11:36:34 -08:00
}
2014-01-23 10:10:52 -08:00
var scanOpts = {
2014-01-21 08:28:01 -08:00
reverse: true,
upToExisting: b ? true: false,
2014-01-17 11:36:34 -08:00
};
2014-01-23 10:10:52 -08:00
return self.importHistory(scanOpts, next);
2014-01-17 11:36:34 -08:00
});
2014-01-16 11:11:20 -08:00
};
return HistoricSync;
}
module.defineClass(spec);