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();
|
2014-02-01 08:39:29 -08:00
|
|
|
var bitutil = require('bitcore/util/util');
|
|
|
|
var Address = require('bitcore/Address').class();
|
|
|
|
var Script = require('bitcore/Script').class();
|
2014-01-21 08:28:01 -08:00
|
|
|
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-30 18:16:43 -08:00
|
|
|
var BlockExtractor = require('./BlockExtractor.js').class();
|
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-19 07:33:39 -08:00
|
|
|
|
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() {
|
2014-01-16 18:23:10 -08:00
|
|
|
var args = [];
|
2014-01-21 08:28:01 -08:00
|
|
|
Array.prototype.push.apply(args, arguments);
|
2014-01-19 07:33:39 -08:00
|
|
|
|
2014-01-16 18:23:10 -08:00
|
|
|
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
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-02-01 08:39:29 -08:00
|
|
|
HistoricSync.prototype.showProgress = function(height) {
|
2014-01-19 07:33:39 -08:00
|
|
|
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 {
|
2014-01-29 10:10:36 -08:00
|
|
|
self.syncPercentage = parseFloat(100 * (self.syncedBlocks + self.skippedBlocks) / self.blockChainHeight).toFixed(3);
|
2014-01-21 14:53:00 -08:00
|
|
|
if (self.syncPercentage > 100) self.syncPercentage = 100;
|
|
|
|
|
2014-02-01 08:39:29 -08:00
|
|
|
p(util.format('status: [%d%%] skipped: %d ', self.syncPercentage, self.skippedBlocks, height));
|
2014-01-21 14:13:21 -08:00
|
|
|
}
|
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-19 07:33:39 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2014-01-16 18:23:10 -08:00
|
|
|
async.series([
|
2014-01-21 08:28:01 -08:00
|
|
|
// Already got it?
|
|
|
|
function(c) {
|
2014-01-29 10:10:36 -08:00
|
|
|
Block.fromHash(blockHash, function(err, block) {
|
2014-01-21 08:28:01 -08:00
|
|
|
if (err) {
|
|
|
|
p(err);
|
|
|
|
return c(err);
|
|
|
|
}
|
|
|
|
if (block) {
|
|
|
|
existed = true;
|
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();
|
|
|
|
},
|
2014-01-16 11:11:20 -08:00
|
|
|
|
2014-01-30 18:16:43 -08:00
|
|
|
function(c) {
|
2014-01-21 08:28:01 -08:00
|
|
|
self.rpc.getBlock(blockHash, function(err, ret) {
|
|
|
|
if (err) return c(err);
|
2014-01-16 11:11:20 -08:00
|
|
|
|
2014-01-30 18:16:43 -08:00
|
|
|
blockInfo = ret ? ret.result : null;
|
2014-01-21 08:28:01 -08:00
|
|
|
return c();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//store it
|
|
|
|
function(c) {
|
|
|
|
if (existed) return c();
|
2014-01-30 18:16:43 -08:00
|
|
|
|
|
|
|
self.sync.storeBlock(blockInfo, 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);
|
2014-01-30 18:16:43 -08:00
|
|
|
return cb(err);
|
2014-01-21 08:28:01 -08:00
|
|
|
}
|
|
|
|
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
|
2014-01-30 18:16:43 -08:00
|
|
|
if (blockInfo) {
|
2014-01-21 08:28:01 -08:00
|
|
|
|
2014-01-21 14:13:21 -08:00
|
|
|
if (existed)
|
|
|
|
self.skippedBlocks++;
|
|
|
|
else
|
|
|
|
self.syncedBlocks++;
|
|
|
|
|
|
|
|
// recursion
|
2014-01-30 18:16:43 -08:00
|
|
|
if (scanOpts.prev && blockInfo.previousblockhash)
|
|
|
|
return self.getPrevNextBlock(blockInfo.previousblockhash, blockEnd, scanOpts, cb);
|
2014-01-21 14:13:21 -08:00
|
|
|
|
2014-01-30 18:16:43 -08:00
|
|
|
if (scanOpts.next && blockInfo.nextblockhash)
|
|
|
|
return self.getPrevNextBlock(blockInfo.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-30 18:16:43 -08:00
|
|
|
|
2014-02-01 08:39:29 -08:00
|
|
|
// TODO. replace with
|
|
|
|
// Script.prototype.getAddrStrs if that one get merged in bitcore
|
|
|
|
HistoricSync.prototype.getAddrStr = function(s) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var addrStrs = [];
|
|
|
|
var type = s.classify();
|
|
|
|
var addr;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case Script.TX_PUBKEY:
|
|
|
|
var chunk = s.captureOne();
|
|
|
|
addr = new Address(self.network.addressPubkey, bitutil.sha256ripe160(chunk));
|
|
|
|
addrStrs = [ addr.toString() ];
|
|
|
|
break;
|
|
|
|
case Script.TX_PUBKEYHASH:
|
|
|
|
addr = new Address(self.network.addressPubkey, s.captureOne());
|
|
|
|
addrStrs = [ addr.toString() ];
|
|
|
|
break;
|
|
|
|
case Script.TX_SCRIPTHASH:
|
|
|
|
addr = new Address(self.network.addressScript, s.captureOne());
|
|
|
|
addrStrs = [ addr.toString() ];
|
|
|
|
break;
|
|
|
|
case Script.TX_MULTISIG:
|
|
|
|
var addrs = [];
|
|
|
|
var chunks = s.capture();
|
|
|
|
|
|
|
|
chunks.forEach(function(chunk) {
|
|
|
|
var a = new Address(self.network.addressPubkey, bitutil.sha256ripe160(chunk));
|
|
|
|
addrStrs.push(a.toString());
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case Script.TX_UNKNOWN:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrStrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-03 09:53:37 -08:00
|
|
|
|
2014-01-30 18:16:43 -08:00
|
|
|
HistoricSync.prototype.getBlockFromFile = function(height, scanOpts, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var nextHash;
|
|
|
|
var blockInfo;
|
|
|
|
var isMainChain;
|
|
|
|
var existed;
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
// Is it in mainchain?
|
|
|
|
function(c) {
|
|
|
|
self.rpc.getBlockHash(height, function(err, res) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
nextHash = res.result;
|
|
|
|
return c();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//show some (inacurate) status
|
|
|
|
function(c) {
|
|
|
|
if ( ( self.syncedBlocks + self.skippedBlocks) % self.step === 1) {
|
2014-02-01 08:39:29 -08:00
|
|
|
self.showProgress(height);
|
2014-01-30 18:16:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return c();
|
|
|
|
},
|
|
|
|
//get Info
|
|
|
|
function(c) {
|
2014-02-03 09:53:37 -08:00
|
|
|
|
2014-01-30 18:16:43 -08:00
|
|
|
self.blockExtractor.getNextBlock(function(err, b) {
|
|
|
|
if (err || ! b) return c(err);
|
|
|
|
|
2014-02-01 08:39:29 -08:00
|
|
|
blockInfo = b.getStandardizedObject(b.txs, self.network);
|
|
|
|
|
|
|
|
var ti=0;
|
|
|
|
// Get TX Address
|
|
|
|
b.txs.forEach(function(t) {
|
|
|
|
var objTx = blockInfo.tx[ti++];
|
|
|
|
var to=0;
|
|
|
|
t.outs.forEach( function(o) {
|
|
|
|
|
|
|
|
|
|
|
|
var s = new Script(o.s);
|
|
|
|
var addrs = self.getAddrStr(s);
|
|
|
|
|
|
|
|
// support only p2pubkey p2pubkeyhash and p2sh
|
|
|
|
if (addrs.length === 1) {
|
|
|
|
objTx.out[to].addrStr = addrs[0];
|
|
|
|
}
|
|
|
|
to++;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-30 18:16:43 -08:00
|
|
|
return c();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
//store it
|
|
|
|
function(c) {
|
|
|
|
|
|
|
|
isMainChain = blockInfo.hash === nextHash;
|
|
|
|
|
|
|
|
blockInfo.isOrphan = !isMainChain;
|
|
|
|
|
2014-02-01 17:39:08 -08:00
|
|
|
/*
|
|
|
|
* In file sync, orphan blocks are just ignored.
|
|
|
|
* This is to simplify our schema and the
|
|
|
|
* sync process
|
|
|
|
*/
|
|
|
|
if (blockInfo.isOrphan) return c();
|
|
|
|
|
2014-01-30 18:16:43 -08:00
|
|
|
self.sync.storeBlock(blockInfo, function(err) {
|
|
|
|
existed = err && err.toString().match(/E11000/);
|
|
|
|
|
|
|
|
if (err && ! existed) return c(err);
|
|
|
|
return c();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
], function(err) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
self.err = util.format('ERROR: @%s: %s [count: syncedBlocks: %d]', blockInfo.hash, err, self.syncedBlocks);
|
|
|
|
self.status = 'aborted';
|
|
|
|
self.showProgress();
|
|
|
|
p(err);
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2014-02-01 17:39:08 -08:00
|
|
|
// Continue
|
2014-02-03 09:53:37 -08:00
|
|
|
if (blockInfo && blockInfo.hash) {
|
2014-02-01 17:39:08 -08:00
|
|
|
self.syncedBlocks++;
|
|
|
|
self.err = null;
|
|
|
|
self.status = 'syncing';
|
2014-01-30 18:16:43 -08:00
|
|
|
|
2014-02-03 09:53:37 -08:00
|
|
|
return cb(null, true, isMainChain);
|
2014-02-01 17:39:08 -08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.err = null;
|
|
|
|
self.status = 'finished';
|
|
|
|
}
|
2014-01-30 18:16:43 -08:00
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
self.rpc.getBlockCount(function(err, res) {
|
|
|
|
if (err) return cb(err);
|
2014-01-21 14:13:21 -08:00
|
|
|
self.blockChainHeight = res.result;
|
2014-01-19 07:33:39 -08:00
|
|
|
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) {
|
2014-02-03 09:53:37 -08:00
|
|
|
if (err) {
|
|
|
|
self.setError(err);
|
|
|
|
return next(err, 0);
|
2014-01-21 08:28:01 -08:00
|
|
|
}
|
2014-01-18 13:28:24 -08:00
|
|
|
|
2014-01-21 14:13:21 -08:00
|
|
|
|
2014-02-03 09:53:37 -08:00
|
|
|
// SETUP Sync params
|
|
|
|
var start, end;
|
2014-01-29 10:10:36 -08:00
|
|
|
|
2014-02-03 09:53:37 -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-02-03 09:53:37 -08:00
|
|
|
if (scanOpts.reverse) {
|
|
|
|
start = lastBlock;
|
|
|
|
end = self.genesis;
|
|
|
|
scanOpts.prev = true;
|
2014-01-21 08:28:01 -08:00
|
|
|
}
|
|
|
|
else {
|
2014-02-03 09:53:37 -08:00
|
|
|
start = self.genesis;
|
|
|
|
end = null;
|
|
|
|
scanOpts.next = true;
|
|
|
|
}
|
|
|
|
p('Starting from: ', start);
|
|
|
|
p(' to : ', end);
|
|
|
|
p(' scanOpts: ', JSON.stringify(scanOpts));
|
|
|
|
|
|
|
|
if (scanOpts.fromFiles) {
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
2014-01-21 08:28:01 -08:00
|
|
|
}
|
2014-02-03 09:53:37 -08:00
|
|
|
else {
|
|
|
|
self.getPrevNextBlock(start, end, scanOpts, function(err) {
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-16 11:11:20 -08:00
|
|
|
});
|
2014-01-16 18:23:10 -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-30 18:16:43 -08:00
|
|
|
HistoricSync.prototype.smartImport = function(scanOpts, next) {
|
2014-01-17 11:36:34 -08:00
|
|
|
var self = this;
|
|
|
|
|
2014-01-29 10:10:36 -08:00
|
|
|
Block.fromHash(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);
|
|
|
|
|
2014-01-30 06:50:05 -08:00
|
|
|
|
2014-01-30 18:16:43 -08:00
|
|
|
if (!b || scanOpts.destroy) {
|
2014-01-17 11:36:34 -08:00
|
|
|
p('Could not find Genesis block. Running FULL SYNC');
|
2014-01-30 06:50:05 -08:00
|
|
|
if (config.bitcoind.dataDir) {
|
|
|
|
p('bitcoind dataDir configured...importing blocks from .dat files');
|
|
|
|
scanOpts.fromFiles = true;
|
2014-01-30 18:16:43 -08:00
|
|
|
self.blockExtractor = new BlockExtractor(config.bitcoind.dataDir, config.network);
|
2014-01-30 06:50:05 -08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
scanOpts.reverse = true;
|
|
|
|
}
|
2014-01-17 11:36:34 -08:00
|
|
|
}
|
|
|
|
else {
|
2014-01-18 13:28:24 -08:00
|
|
|
p('Genesis block found. Syncing upto known blocks.');
|
2014-01-30 06:50:05 -08:00
|
|
|
scanOpts.reverse = true;
|
|
|
|
scanOpts.upToExisting = true;
|
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);
|
|
|
|
|