bitcore-node-zcash/lib/HistoricSync.js

273 lines
6.9 KiB
JavaScript
Raw Normal View History

2014-01-16 11:11:20 -08:00
'use strict';
require('classtool');
function spec() {
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();
function HistoricSync(opts) {
this.block_count= 0;
this.block_total= 0;
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-16 11:11:20 -08:00
this.sync = new Sync(opts);
}
function p() {
var args = [];
Array.prototype.push.apply( args, arguments );
2014-01-16 11:11:20 -08:00
args.unshift('[historic_sync]');
/*jshint validthis:true */
console.log.apply(this, args);
2014-01-16 11:11:20 -08:00
}
var progress_bar = function(string, current, total) {
p(util.format('%s %d/%d [%d%%]', string, current, total, parseInt(100 * current / total)));
};
HistoricSync.prototype.init = function(opts,cb) {
this.rpc = new RpcClient(config.bitcoind);
this.opts = opts;
this.sync.init(opts, cb);
};
HistoricSync.prototype.close = function() {
this.sync.close();
};
HistoricSync.prototype.getPrevNextBlock = function(blockHash, blockEnd, opts, cb) {
2014-01-17 11:36:34 -08:00
var self = this;
2014-01-16 11:11:20 -08:00
// recursion end.
2014-01-17 11:36:34 -08:00
if (!blockHash ) return cb();
2014-01-16 11:11:20 -08:00
var existed = 0;
var blockInfo;
var blockObj;
async.series([
2014-01-16 11:11:20 -08:00
// Already got it?
function(c) {
Block.findOne({hash:blockHash}, function(err,block){
if (err) { p(err); return c(err); }
2014-01-16 11:11:20 -08:00
if (block) {
2014-01-17 11:36:34 -08:00
existed =1;
blockObj =block;
2014-01-16 11:11:20 -08:00
}
return c();
});
},
//show some (inacurate) status
function(c) {
2014-01-17 11:36:34 -08:00
if (self.block_count % 1000 === 1) {
progress_bar('sync status:', self.block_count, self.block_total);
2014-01-16 11:11:20 -08:00
}
return c();
},
//get Info from RPC
function(c) {
// TODO: if we store prev/next, no need to go to RPC
// if (blockObj && blockObj.nextBlockHash) return c();
2014-01-17 11:36:34 -08:00
self.rpc.getBlock(blockHash, function(err, ret) {
2014-01-16 11:11:20 -08:00
if (err) return c(err);
blockInfo = ret;
return c();
});
},
//store it
function(c) {
if (existed) return c();
2014-01-17 11:36:34 -08:00
self.sync.storeBlock(blockInfo.result, function(err) {
2014-01-16 11:11:20 -08:00
existed = err && err.toString().match(/E11000/);
2014-01-17 11:36:34 -08:00
2014-01-16 11:11:20 -08:00
if (err && ! existed) return c(err);
return c();
});
},
/* TODO: Should Start to sync backwards? (this is for partial syncs)
function(c) {
if (blockInfo.result.prevblockhash != current.blockHash) {
p("reorg?");
opts.prev = 1;
}
return c();
}
*/
],
function (err){
2014-01-17 11:36:34 -08:00
if (err)
p('ERROR: @%s: %s [count: block_count: %d]', blockHash, err, self.block_count);
2014-01-17 05:22:00 -08:00
if (opts.uptoexisting && existed) {
2014-01-17 05:44:14 -08:00
p('DONE. Found existing block: ', blockHash);
2014-01-17 05:22:00 -08:00
return cb(err);
}
2014-01-17 11:36:34 -08:00
if (blockEnd && blockEnd === blockHash) {
p('DONE. Found END block: ', blockHash);
return cb(err);
}
2014-01-16 11:11:20 -08:00
2014-01-17 11:36:34 -08:00
// Continue
2014-01-16 11:11:20 -08:00
if (blockInfo && blockInfo.result) {
2014-01-17 11:36:34 -08:00
self.block_count++;
2014-01-16 11:11:20 -08:00
if (opts.prev && blockInfo.result.previousblockhash) {
2014-01-17 11:36:34 -08:00
return self.getPrevNextBlock(blockInfo.result.previousblockhash, blockEnd, opts, cb);
2014-01-16 11:11:20 -08:00
}
if (opts.next && blockInfo.result.nextblockhash)
2014-01-17 11:36:34 -08:00
return self.getPrevNextBlock(blockInfo.result.nextblockhash, blockEnd, opts, cb);
2014-01-16 11:11:20 -08:00
}
return cb(err);
});
};
2014-01-17 05:22:00 -08:00
HistoricSync.prototype.import_history = function(opts, next) {
2014-01-17 11:36:34 -08:00
var self = this;
2014-01-16 11:11:20 -08:00
var retry_attemps = 100;
var retry_secs = 2;
var block_best;
var block_height;
async.series([
function(cb) {
if (opts.destroy) {
p('Deleting Blocks...');
2014-01-17 11:36:34 -08:00
self.db.collections.blocks.drop(cb);
2014-01-16 11:11:20 -08:00
} else {
return cb();
}
},
function(cb) {
if (opts.destroy) {
p('Deleting TXs...');
2014-01-17 11:36:34 -08:00
self.db.collections.transactions.drop(cb);
2014-01-16 11:11:20 -08:00
} else {
return cb();
}
},
function(cb) {
if (opts.destroy) {
p('Deleting TXItems...');
2014-01-17 11:36:34 -08:00
self.db.collections.transactionitems.drop(cb);
2014-01-16 11:11:20 -08:00
} else {
return cb();
}
},
function(cb) {
2014-01-17 11:36:34 -08:00
self.rpc.getInfo(function(err, res) {
2014-01-17 05:44:14 -08:00
if (err) return cb(err);
2014-01-16 11:11:20 -08:00
2014-01-17 11:36:34 -08:00
self.block_total = res.result.blocks;
2014-01-16 11:11:20 -08:00
return cb();
});
},
// We are not using getBestBlockHash, because is not available in all clients
function(cb) {
if (!opts.reverse) return cb();
2014-01-17 11:36:34 -08:00
self.rpc.getBlockCount(function(err, res) {
2014-01-17 05:44:14 -08:00
if (err) return cb(err);
2014-01-16 11:11:20 -08:00
block_height = res.result;
return cb();
});
},
function(cb) {
if (!opts.reverse) return cb();
2014-01-17 11:36:34 -08:00
self.rpc.getBlockHash(block_height, function(err, res) {
2014-01-17 05:44:14 -08:00
if (err) return cb(err);
2014-01-16 11:11:20 -08:00
block_best = res.result;
return cb();
});
},
],
2014-01-16 11:11:20 -08:00
function(err) {
2014-01-17 05:22:00 -08:00
var start, end;
2014-01-16 11:11:20 -08:00
function sync() {
if (opts.reverse) {
start = block_best;
2014-01-17 11:36:34 -08:00
end = self.genesis;
2014-01-17 05:22:00 -08:00
opts.prev = true;
2014-01-16 11:11:20 -08:00
}
else {
2014-01-17 11:36:34 -08:00
start = self.genesis;
2014-01-16 11:11:20 -08:00
end = null;
2014-01-17 05:22:00 -08:00
opts.next = true;
2014-01-16 11:11:20 -08:00
}
2014-01-17 11:36:34 -08:00
p('Starting from: ', start);
p(' to : ', end);
p(' opts: ', JSON.stringify(opts));
2014-01-16 11:11:20 -08:00
2014-01-17 11:36:34 -08:00
self.getPrevNextBlock( start, end, opts , function(err) {
2014-01-16 11:11:20 -08:00
if (err && err.message.match(/ECONNREFUSED/) && retry_attemps--){
setTimeout(function() {
p('Retrying in %d secs', retry_secs);
2014-01-16 11:11:20 -08:00
sync();
}, retry_secs * 1000);
}
else
2014-01-17 11:36:34 -08:00
return next(err, self.block_count);
2014-01-16 11:11:20 -08:00
});
}
if (!err)
sync();
else
return next(err, 0);
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-17 05:22:00 -08:00
HistoricSync.prototype.smart_import = function(next) {
2014-01-17 11:36:34 -08:00
var self = this;
Block.findOne({hash:self.genesis}, function(err, b){
if (err) return next(err);
if (!b) {
p('Could not find Genesis block. Running FULL SYNC');
}
else {
p('Genesis block found. Syncing upto know blocks.');
}
var opts = {
reverse: 1,
uptoexisting: b ? true: false,
};
return self.import_history(opts, next);
});
2014-01-16 11:11:20 -08:00
};
return HistoricSync;
}
module.defineClass(spec);