bitcore-node-zcash/lib/node.js

601 lines
15 KiB
JavaScript
Raw Normal View History

'use strict';
2015-07-21 06:09:59 -07:00
var fs = require('fs');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var async = require('async');
var mkdirp = require('mkdirp');
var bitcore = require('bitcore');
2015-08-27 07:10:07 -07:00
var BufferUtil = bitcore.util.buffer;
var Networks = bitcore.Networks;
var _ = bitcore.deps._;
2015-07-21 06:09:59 -07:00
var $ = bitcore.util.preconditions;
2015-08-27 07:10:07 -07:00
var Block = bitcore.Block;
var Chain = require('./chain');
var DB = require('./db');
var index = require('./');
var log = index.log;
2015-07-16 14:03:43 -07:00
var daemon = require('./daemon');
2015-07-29 10:36:23 -07:00
var Bus = require('./bus');
2015-08-27 13:09:27 -07:00
var BaseModule = require('./module');
function Node(config) {
if(!(this instanceof Node)) {
return new Node(config);
}
this.db = null;
this.chain = null;
this.network = null;
2015-08-27 13:09:27 -07:00
this.modules = {};
this._unloadedModules = [];
// TODO type check the arguments of config.modules
if (config.modules) {
$.checkArgument(Array.isArray(config.modules));
this._unloadedModules = config.modules;
}
this._loadConfiguration(config);
this._initialize();
}
util.inherits(Node, EventEmitter);
2015-07-29 10:36:23 -07:00
Node.prototype.openBus = function() {
2015-08-28 13:16:51 -07:00
return new Bus({node: this});
2015-07-29 10:36:23 -07:00
};
2015-08-27 13:09:27 -07:00
Node.prototype.addModule = function(service) {
var self = this;
var mod = new service.module({
node: this
});
$.checkState(
mod instanceof BaseModule,
'Unexpected module instance type for module:' + service.name
);
// include in loaded modules
this.modules[service.name] = mod;
// add API methods
var methodData = mod.getAPIMethods();
methodData.forEach(function(data) {
var name = data[0];
var instance = data[1];
var method = data[2];
if (self[name]) {
throw new Error('Existing API method exists:' + name);
} else {
self[name] = function() {
return method.apply(instance, arguments);
};
}
});
};
Node.prototype.getAllAPIMethods = function() {
var methods = this.db.getAPIMethods();
2015-08-27 13:09:27 -07:00
for(var i in this.modules) {
var mod = this.modules[i];
methods = methods.concat(mod.getAPIMethods());
}
return methods;
};
Node.prototype.getAllPublishEvents = function() {
2015-08-06 13:19:36 -07:00
var events = this.db.getPublishEvents();
2015-08-27 13:09:27 -07:00
for (var i in this.modules) {
var mod = this.modules[i];
events = events.concat(mod.getPublishEvents());
}
return events;
};
Node.prototype._loadConfiguration = function(config) {
2015-07-21 06:09:59 -07:00
this._loadBitcoinConf(config);
this._loadBitcoind(config);
this._loadNetwork(config);
this._loadDB(config);
this._loadAPI();
this._loadConsensus(config);
};
Node.DEFAULT_DAEMON_CONFIG = 'whitelist=127.0.0.1\n' + 'txindex=1\n';
2015-07-21 06:09:59 -07:00
Node.prototype._loadBitcoinConf = function(config) {
$.checkArgument(config.datadir, 'Please specify "datadir" in configuration options');
var configPath = config.datadir + '/bitcoin.conf';
2015-07-21 06:09:59 -07:00
this.bitcoinConfiguration = {};
if (!fs.existsSync(config.datadir)) {
mkdirp.sync(config.datadir);
}
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, Node.DEFAULT_DAEMON_CONFIG);
}
var file = fs.readFileSync(configPath);
2015-07-21 06:09:59 -07:00
var unparsed = file.toString().split('\n');
for(var i = 0; i < unparsed.length; i++) {
var line = unparsed[i];
if (!line.match(/^\#/) && line.match(/\=/)) {
var option = line.split('=');
2015-07-21 09:17:28 -07:00
var value;
if (!Number.isNaN(Number(option[1]))) {
value = Number(option[1]);
} else {
value = option[1];
}
this.bitcoinConfiguration[option[0]] = value;
2015-07-21 06:09:59 -07:00
}
}
2015-08-24 11:46:48 -07:00
$.checkState((this.bitcoinConfiguration.txindex && this.bitcoinConfiguration.txindex == 1),
'Txindex option is required in order to use most of the features of bitcore-node. \
2015-08-11 06:45:38 -07:00
Please add "txindex=1" to your configuration and reindex an existing database if necessary with reindex=1');
2015-07-21 06:09:59 -07:00
};
Node.prototype._loadBitcoind = function(config) {
var bitcoindConfig = {};
2015-07-21 06:09:59 -07:00
bitcoindConfig.datadir = config.datadir;
bitcoindConfig.network = config.network;
bitcoindConfig.node = this;
// start the bitcoind daemon
2015-07-16 14:03:43 -07:00
this.bitcoind = daemon(bitcoindConfig);
};
/**
* This function will find the common ancestor between the current chain and a forked block,
* by moving backwards from the forked block until it meets the current chain.
* @param {Block} block - The new tip that forks the current chain.
* @param {Function} done - A callback function that is called when complete.
*/
Node.prototype._syncBitcoindAncestor = function(block, done) {
var self = this;
// The current chain of hashes will likely already be available in a cache.
self.chain.getHashes(self.chain.tip.hash, function(err, currentHashes) {
if (err) {
done(err);
}
// Create a hash map for faster lookups
var currentHashesMap = {};
var length = currentHashes.length;
for (var i = 0; i < length; i++) {
currentHashesMap[currentHashes[i]] = true;
}
2015-08-27 07:10:07 -07:00
// TODO: expose prevHash as a string from bitcore
var ancestorHash = BufferUtil.reverse(block.header.prevHash).toString('hex');
// We only need to go back until we meet the main chain for the forked block
// and thus don't need to find the entire chain of hashes.
2015-08-18 14:36:54 -07:00
while(ancestorHash && !currentHashesMap[ancestorHash]) {
var blockIndex = self.bitcoind.getBlockIndex(ancestorHash);
ancestorHash = blockIndex ? blockIndex.prevHash : null;
}
2015-08-18 14:36:54 -07:00
// Hash map is no-longer needed, quickly let
// scavenging garbage collection know to cleanup
currentHashesMap = null;
if (!ancestorHash) {
return done(new Error('Unknown common ancestor.'));
}
done(null, ancestorHash);
});
};
/**
* This function will attempt to rewind the chain to the common ancestor
* between the current chain and a forked block.
* @param {Block} block - The new tip that forks the current chain.
* @param {Function} done - A callback function that is called when complete.
*/
Node.prototype._syncBitcoindRewind = function(block, done) {
var self = this;
self._syncBitcoindAncestor(block, function(err, ancestorHash) {
2015-08-21 08:17:26 -07:00
if (err) {
return done(err);
}
// Rewind the chain to the common ancestor
2015-07-24 10:45:31 -07:00
async.whilst(
function() {
// Wait until the tip equals the ancestor hash
return self.chain.tip.hash !== ancestorHash;
},
function(removeDone) {
2015-07-24 10:45:31 -07:00
var tip = self.chain.tip;
2015-08-27 07:10:07 -07:00
// TODO: expose prevHash as a string from bitcore
var prevHash = BufferUtil.reverse(tip.header.prevHash).toString('hex');
self.getBlock(prevHash, function(err, previousTip) {
if (err) {
removeDone(err);
}
2015-07-24 10:45:31 -07:00
// Undo the related indexes for this block
self.db._onChainRemoveBlock(tip, function(err) {
if (err) {
return removeDone(err);
}
2015-07-24 10:45:31 -07:00
// Set the new tip
previousTip.__height = self.chain.tip.__height - 1;
self.chain.tip = previousTip;
self.chain.saveMetadata();
2015-07-24 10:45:31 -07:00
self.chain.emit('removeblock', tip);
removeDone();
});
});
}, done
);
});
};
/**
* This function will synchronize additional indexes for the chain based on
* the current active chain in the bitcoin daemon. In the event that there is
* a reorganization in the daemon, the chain will rewind to the last common
* ancestor and then resume syncing.
*/
Node.prototype._syncBitcoind = function() {
var self = this;
if (self.bitcoindSyncing) {
return;
}
if (!self.chain.tip) {
return;
}
self.bitcoindSyncing = true;
self.chain.lastSavedMetadataThreshold = 30000;
var height;
async.whilst(function() {
height = self.chain.tip.__height;
return height < self.bitcoind.height && !self.stopping;
}, function(done) {
self.bitcoind.getBlock(height + 1, function(err, blockBuffer) {
if (err) {
return done(err);
}
2015-08-21 08:53:20 -07:00
2015-08-27 07:10:07 -07:00
var block = Block.fromBuffer(blockBuffer);
// TODO: expose prevHash as a string from bitcore
var prevHash = BufferUtil.reverse(block.header.prevHash).toString('hex');
2015-08-21 08:17:26 -07:00
2015-08-27 07:10:07 -07:00
if (prevHash === self.chain.tip.hash) {
// This block appends to the current chain tip and we can
// immediately add it to the chain and create indexes.
// Populate height
block.__height = self.chain.tip.__height + 1;
2015-08-18 14:36:54 -07:00
// Update chain.cache.hashes
2015-08-27 07:10:07 -07:00
self.chain.cache.hashes[block.hash] = prevHash;
2015-07-24 20:18:14 -07:00
2015-08-18 14:36:54 -07:00
// Update chain.cache.chainHashes
self.chain.getHashes(block.hash, function(err, hashes) {
if (err) {
return done(err);
}
2015-08-18 14:36:54 -07:00
// Create indexes
self.db._onChainAddBlock(block, function(err) {
if (err) {
return done(err);
}
self.chain.tip = block;
log.debug('Saving metadata');
self.chain.saveMetadata();
log.debug('Chain added block to main chain');
self.chain.emit('addblock', block);
setImmediate(done);
2015-08-24 11:46:48 -07:00
});
});
} else {
// This block doesn't progress the current tip, so we'll attempt
// to rewind the chain to the common ancestor of the block and
// then we can resume syncing.
self._syncBitcoindRewind(block, done);
}
});
}, function(err) {
if (err) {
Error.captureStackTrace(err);
return self.emit('error', err);
}
2015-08-21 13:13:11 -07:00
if(self.stopping) {
return;
}
self.bitcoindSyncing = false;
self.chain.lastSavedMetadataThreshold = 0;
// If bitcoind is completely synced
if (self.bitcoind.isSynced()) {
self.emit('synced');
}
});
};
Node.prototype._loadNetwork = function(config) {
if (config.network === 'testnet') {
this.network = Networks.get('testnet');
} else if (config.network === 'regtest') {
Networks.remove(Networks.testnet);
Networks.add({
name: 'regtest',
alias: 'regtest',
pubkeyhash: 0x6f,
privatekey: 0xef,
scripthash: 0xc4,
xpubkey: 0x043587cf,
xprivkey: 0x04358394,
networkMagic: 0xfabfb5da,
port: 18444,
dnsSeeds: [ ]
});
this.network = Networks.get('regtest');
} else {
this.network = Networks.get('livenet');
}
2015-07-21 06:09:59 -07:00
$.checkState(this.network, 'Unrecognized network');
};
Node.prototype._loadDB = function(config) {
var options = _.clone(config.db || {});
if (config.DB) {
// Other modules can inherit from our DB and replace it with their own
DB = config.DB;
}
2015-07-16 12:53:44 -07:00
2015-07-21 06:09:59 -07:00
// Store the additional indexes in a new directory
// based on the network configuration and the datadir
$.checkArgument(config.datadir, 'Please specify "datadir" in configuration options');
$.checkState(this.network, 'Network property not defined');
var regtest = Networks.get('regtest');
if (this.network === Networks.livenet) {
2015-08-24 16:00:31 -07:00
options.path = config.datadir + '/bitcore-node.db';
} else if (this.network === Networks.testnet) {
2015-08-24 16:00:31 -07:00
options.path = config.datadir + '/testnet3/bitcore-node.db';
} else if (this.network === regtest) {
2015-08-24 16:00:31 -07:00
options.path = config.datadir + '/regtest/bitcore-node.db';
} else {
throw new Error('Unknown network: ' + this.network);
2015-07-21 06:09:59 -07:00
}
options.network = this.network;
if (!fs.existsSync(options.path)) {
mkdirp.sync(options.path);
}
2015-07-21 06:09:59 -07:00
options.node = this;
this.db = new DB(options);
};
Node.prototype._loadConsensus = function(config) {
var options;
if (!config) {
options = {};
} else {
options = _.clone(config.consensus || {});
}
options.node = this;
this.chain = new Chain(options);
};
Node.prototype._loadAPI = function() {
var self = this;
var methodData = self.db.getAPIMethods();
methodData.forEach(function(data) {
var name = data[0];
var instance = data[1];
var method = data[2];
self[name] = function() {
return method.apply(instance, arguments);
};
});
};
2015-08-20 14:50:14 -07:00
Node.prototype._initialize = function() {
var self = this;
2015-08-24 11:46:48 -07:00
this._initializeBitcoind();
this._initializeDatabase();
this._initializeChain();
2015-08-24 11:46:48 -07:00
this.start(function(err) {
if(err) {
return self.emit('error', err);
}
self.emit('ready');
});
};
Node.prototype._initializeBitcoind = function() {
var self = this;
// Notify that there is a new tip
this.bitcoind.on('ready', function() {
log.info('Bitcoin Daemon Ready');
});
// Notify that there is a new tip
this.bitcoind.on('tip', function(height) {
2015-08-21 12:39:46 -07:00
if(!self.stopping) {
var percentage = self.bitcoind.syncPercentage();
log.info('Bitcoin Core Daemon New Height:', height, 'Percentage:', percentage);
self._syncBitcoind();
}
});
this.bitcoind.on('error', function(err) {
Error.captureStackTrace(err);
self.emit('error', err);
});
2015-08-24 11:46:48 -07:00
};
2015-08-24 11:46:48 -07:00
Node.prototype._initializeDatabase = function() {
var self = this;
this.db.on('ready', function() {
log.info('Bitcoin Database Ready');
});
this.db.on('error', function(err) {
Error.captureStackTrace(err);
self.emit('error', err);
});
2015-08-24 11:46:48 -07:00
};
2015-08-24 11:46:48 -07:00
Node.prototype._initializeChain = function() {
var self = this;
this.chain.on('ready', function() {
log.info('Bitcoin Chain Ready');
self._syncBitcoind();
});
this.chain.on('error', function(err) {
Error.captureStackTrace(err);
self.emit('error', err);
2015-08-20 14:50:14 -07:00
});
};
Node.prototype.getServices = function() {
2015-08-27 13:09:27 -07:00
var services = [
{
name: 'bitcoind',
dependencies: []
},
{
name: 'db',
dependencies: ['bitcoind'],
},
{
name: 'chain',
dependencies: ['db']
}
];
services = services.concat(this._unloadedModules);
return services;
};
2015-08-27 13:09:27 -07:00
Node.prototype.getServiceOrder = function() {
var services = this.getServices();
2015-08-27 13:09:27 -07:00
// organize data for sorting
var names = [];
var servicesByName = {};
for (var i = 0; i < services.length; i++) {
var service = services[i];
names.push(service.name);
servicesByName[service.name] = service;
2015-08-20 14:50:14 -07:00
}
2015-08-27 13:09:27 -07:00
var stackNames = {};
var stack = [];
function addToStack(names) {
for(var i = 0; i < names.length; i++) {
var name = names[i];
var service = servicesByName[name];
// first add the dependencies
addToStack(service.dependencies);
// add to the stack if it hasn't been added
if(!stackNames[name]) {
stack.push(service);
stackNames[name] = true;
}
2015-08-20 14:50:14 -07:00
}
}
2015-08-27 13:09:27 -07:00
addToStack(names);
2015-08-20 14:50:14 -07:00
return stack;
};
2015-08-20 14:50:14 -07:00
Node.prototype.start = function(callback) {
var self = this;
var servicesOrder = this.getServiceOrder();
2015-08-20 14:50:14 -07:00
async.eachSeries(
servicesOrder,
2015-08-20 14:50:14 -07:00
function(service, next) {
2015-08-27 13:09:27 -07:00
log.info('Starting ' + service.name);
if (service.module) {
self.addModule(service);
self.modules[service.name].start(next);
} else {
// TODO: implement bitcoind, chain and db as modules
self[service.name].start(next);
}
2015-08-20 14:50:14 -07:00
},
callback
);
};
2015-08-20 14:50:14 -07:00
Node.prototype.stop = function(callback) {
2015-08-21 12:39:46 -07:00
log.info('Beginning shutdown');
2015-08-20 14:50:14 -07:00
var self = this;
var services = this.getServiceOrder().reverse();
2015-08-21 08:53:20 -07:00
this.stopping = true;
this.emit('stopping');
2015-08-20 14:50:14 -07:00
async.eachSeries(
services,
function(service, next) {
2015-08-27 13:09:27 -07:00
log.info('Stopping ' + service.name);
if (service.module) {
self.modules[service.name].stop(next);
} else {
self[service.name].stop(next);
}
2015-08-20 14:50:14 -07:00
},
callback
);
};
module.exports = Node;