2015-08-19 13:14:44 -07:00
|
|
|
'use strict';
|
2015-08-20 14:32:25 -07:00
|
|
|
|
|
|
|
var path = require('path');
|
|
|
|
var BitcoreNode = require('../node');
|
2015-08-26 12:18:58 -07:00
|
|
|
var index = require('../');
|
2015-08-20 14:32:25 -07:00
|
|
|
var bitcore = require('bitcore');
|
|
|
|
var _ = bitcore.deps._;
|
2015-08-31 10:37:11 -07:00
|
|
|
var $ = bitcore.util.preconditions;
|
2015-08-26 12:18:58 -07:00
|
|
|
var log = index.log;
|
2015-09-04 14:36:53 -07:00
|
|
|
var child_process = require('child_process');
|
|
|
|
var fs = require('fs');
|
2015-09-14 10:15:55 -07:00
|
|
|
var shuttingDown = false;
|
2015-09-04 14:36:53 -07:00
|
|
|
|
2015-08-20 14:32:25 -07:00
|
|
|
log.debug = function() {};
|
|
|
|
|
2015-09-01 09:47:15 -07:00
|
|
|
/**
|
|
|
|
* This function will loop over the configuration for services and require the
|
|
|
|
* specified modules, and assemble an array in this format:
|
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* name: 'bitcoind',
|
|
|
|
* config: {},
|
|
|
|
* module: BitcoinService
|
|
|
|
* }
|
|
|
|
* ]
|
|
|
|
* @param {Function} req - The require function to use
|
2015-09-02 15:30:35 -07:00
|
|
|
* @param {Array} cwd - The local path (for requiring services)
|
2015-09-01 09:47:15 -07:00
|
|
|
* @param {Object} config
|
|
|
|
* @param {Array} config.services - An array of strings of service names.
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2015-09-02 15:30:35 -07:00
|
|
|
function setupServices(req, cwd, config) {
|
|
|
|
|
|
|
|
module.paths.push(path.resolve(cwd, './node_modules'));
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
var services = [];
|
|
|
|
if (config.services) {
|
|
|
|
for (var i = 0; i < config.services.length; i++) {
|
2015-08-31 10:37:11 -07:00
|
|
|
var service = {};
|
2015-08-31 10:38:21 -07:00
|
|
|
service.name = config.services[i];
|
2015-09-01 09:47:15 -07:00
|
|
|
|
|
|
|
var hasConfig = config.servicesConfig && config.servicesConfig[service.name];
|
|
|
|
service.config = hasConfig ? config.servicesConfig[service.name] : {};
|
2015-08-31 10:37:11 -07:00
|
|
|
|
2015-08-20 14:32:25 -07:00
|
|
|
try {
|
2015-08-31 06:00:00 -07:00
|
|
|
// first try in the built-in bitcore-node services directory
|
2015-09-01 09:47:15 -07:00
|
|
|
service.module = req(path.resolve(__dirname, '../services/' + service.name));
|
2015-08-20 14:32:25 -07:00
|
|
|
} catch(e) {
|
2015-08-25 15:01:54 -07:00
|
|
|
|
|
|
|
// check if the package.json specifies a specific file to use
|
2015-09-01 09:47:15 -07:00
|
|
|
var servicePackage = req(service.name + '/package.json');
|
2015-08-31 10:37:11 -07:00
|
|
|
var serviceModule = service.name;
|
2015-08-31 06:00:00 -07:00
|
|
|
if (servicePackage.bitcoreNode) {
|
2015-08-31 10:37:11 -07:00
|
|
|
serviceModule = service.name + '/' + servicePackage.bitcoreNode;
|
2015-08-25 15:01:54 -07:00
|
|
|
}
|
2015-09-01 09:47:15 -07:00
|
|
|
service.module = req(serviceModule);
|
2015-08-20 14:32:25 -07:00
|
|
|
}
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
// check that the service supports expected methods
|
2015-08-31 10:37:11 -07:00
|
|
|
if (!service.module.prototype ||
|
|
|
|
!service.module.dependencies ||
|
|
|
|
!service.module.prototype.start ||
|
|
|
|
!service.module.prototype.stop) {
|
2015-08-25 15:01:54 -07:00
|
|
|
throw new Error(
|
2015-08-31 10:37:11 -07:00
|
|
|
'Could not load service "' + service.name + '" as it does not support necessary methods.'
|
2015-08-25 15:01:54 -07:00
|
|
|
);
|
|
|
|
}
|
2015-08-27 13:09:27 -07:00
|
|
|
|
2015-08-31 10:37:11 -07:00
|
|
|
services.push(service);
|
2015-08-20 14:32:25 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 09:47:15 -07:00
|
|
|
return services;
|
|
|
|
}
|
2015-08-20 14:32:25 -07:00
|
|
|
|
2015-09-01 09:47:15 -07:00
|
|
|
/**
|
|
|
|
* Will register event handlers to log the current db sync status.
|
|
|
|
* @param {Node} node
|
|
|
|
*/
|
|
|
|
function registerSyncHandlers(node, delay) {
|
2015-08-20 14:32:25 -07:00
|
|
|
|
2015-09-01 09:47:15 -07:00
|
|
|
delay = delay || 10000;
|
|
|
|
var interval = false;
|
|
|
|
var count = 0;
|
2015-08-20 14:32:25 -07:00
|
|
|
|
|
|
|
function logSyncStatus() {
|
|
|
|
log.info(
|
2015-09-04 12:05:32 -07:00
|
|
|
'Database Sync Status: Tip:', node.services.db.tip.hash,
|
2015-08-31 06:00:00 -07:00
|
|
|
'Height:', node.services.db.tip.__height,
|
2015-08-20 14:32:25 -07:00
|
|
|
'Rate:', count/10, 'blocks per second'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-31 06:00:00 -07:00
|
|
|
node.on('ready', function() {
|
2015-09-04 12:05:32 -07:00
|
|
|
|
|
|
|
if (node.services.db) {
|
|
|
|
node.on('synced', function() {
|
|
|
|
clearInterval(interval);
|
|
|
|
logSyncStatus();
|
|
|
|
});
|
|
|
|
node.services.db.on('addblock', function(block) {
|
|
|
|
count++;
|
|
|
|
// Initialize logging if not already instantiated
|
|
|
|
if (!interval) {
|
|
|
|
interval = setInterval(function() {
|
|
|
|
logSyncStatus();
|
|
|
|
count = 0;
|
|
|
|
}, delay);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-20 14:32:25 -07:00
|
|
|
});
|
|
|
|
|
2015-08-20 17:05:23 -07:00
|
|
|
node.on('stopping', function() {
|
|
|
|
clearInterval(interval);
|
|
|
|
});
|
2015-09-01 09:47:15 -07:00
|
|
|
}
|
|
|
|
|
2015-09-04 12:05:32 -07:00
|
|
|
/**
|
|
|
|
* Will shutdown a node and then the process
|
|
|
|
* @param {Object} _process - The Node.js process object
|
|
|
|
* @param {Node} node - The Bitcore Node instance
|
|
|
|
*/
|
|
|
|
function cleanShutdown(_process, node) {
|
|
|
|
node.stop(function(err) {
|
|
|
|
if(err) {
|
|
|
|
log.error('Failed to stop services: ' + err);
|
|
|
|
return _process.exit(1);
|
|
|
|
}
|
|
|
|
log.info('Halted');
|
|
|
|
_process.exit(0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-01 09:47:15 -07:00
|
|
|
/**
|
|
|
|
* Will register event handlers to stop the node for `process` events
|
|
|
|
* `uncaughtException` and `SIGINT`.
|
2015-09-04 14:36:53 -07:00
|
|
|
* @param {Object} _process - The Node.js process
|
2015-09-01 09:47:15 -07:00
|
|
|
* @param {Node} node
|
|
|
|
*/
|
2015-09-04 14:36:53 -07:00
|
|
|
function registerExitHandlers(_process, node) {
|
2015-08-20 17:05:23 -07:00
|
|
|
//catches uncaught exceptions
|
2015-09-11 13:26:23 -07:00
|
|
|
_process.on('uncaughtException', exitHandler.bind(null, {exit:true}, _process, node));
|
2015-08-20 17:05:23 -07:00
|
|
|
|
|
|
|
//catches ctrl+c event
|
2015-09-11 13:26:23 -07:00
|
|
|
_process.on('SIGINT', exitHandler.bind(null, {sigint:true}, _process, node));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will handle all the shutdown tasks that need to take place to ensure a safe exit
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} options.sigint - The signal given was a SIGINT
|
|
|
|
* @param {Array} options.exit - The signal given was an uncaughtException
|
|
|
|
* @param {Object} _process - The Node.js process
|
|
|
|
* @param {Node} node
|
|
|
|
* @param {Error} error
|
|
|
|
*/
|
|
|
|
function exitHandler(options, _process, node, err) {
|
|
|
|
if (err) {
|
|
|
|
log.error('uncaught exception:', err);
|
|
|
|
if(err.stack) {
|
|
|
|
log.error(err.stack);
|
|
|
|
}
|
|
|
|
node.stop(function(err) {
|
|
|
|
if(err) {
|
|
|
|
log.error('Failed to stop services: ' + err);
|
|
|
|
}
|
|
|
|
_process.exit(-1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (options.sigint) {
|
2015-09-14 10:15:55 -07:00
|
|
|
if (!shuttingDown) {
|
|
|
|
shuttingDown = true;
|
|
|
|
start.cleanShutdown(_process, node);
|
|
|
|
}
|
2015-09-11 13:26:23 -07:00
|
|
|
}
|
2015-09-01 09:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will instantiate and start a Node, requiring the necessary service
|
|
|
|
* modules, and registering event handlers.
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} options.path - The absolute path of the configuration file
|
|
|
|
* @param {Object} options.config - The parsed bitcore-node.json configuration file
|
|
|
|
* @param {Array} options.config.services - An array of services names.
|
|
|
|
* @param {Object} options.config.servicesConfig - Parameters to pass to each service
|
|
|
|
* @param {String} options.config.datadir - A relative (to options.path) or absolute path to the datadir
|
|
|
|
* @param {String} options.config.network - 'livenet', 'testnet' or 'regtest
|
|
|
|
* @param {Number} options.config.port - The port to use for the web service
|
|
|
|
*/
|
|
|
|
function start(options) {
|
|
|
|
|
|
|
|
var fullConfig = _.clone(options.config);
|
2015-09-04 14:36:53 -07:00
|
|
|
fullConfig.services = start.setupServices(require, options.path, options.config);
|
2015-09-01 09:47:15 -07:00
|
|
|
fullConfig.datadir = path.resolve(options.path, options.config.datadir);
|
|
|
|
|
2015-09-04 14:36:53 -07:00
|
|
|
if (fullConfig.daemon) {
|
|
|
|
start.spawnChildProcess(fullConfig.datadir, process);
|
|
|
|
}
|
|
|
|
|
2015-09-01 09:47:15 -07:00
|
|
|
var node = new BitcoreNode(fullConfig);
|
|
|
|
|
|
|
|
// set up the event handlers for logging sync information
|
2015-09-04 14:36:53 -07:00
|
|
|
start.registerSyncHandlers(node);
|
2015-09-01 09:47:15 -07:00
|
|
|
|
|
|
|
// setup handlers for uncaught exceptions and ctrl+c
|
2015-09-04 14:36:53 -07:00
|
|
|
start.registerExitHandlers(process, node);
|
2015-09-01 09:47:15 -07:00
|
|
|
|
|
|
|
node.on('ready', function() {
|
|
|
|
log.info('Bitcore Node ready');
|
|
|
|
});
|
|
|
|
|
|
|
|
node.on('error', function(err) {
|
|
|
|
log.error(err);
|
|
|
|
});
|
2015-08-20 17:05:23 -07:00
|
|
|
|
2015-09-04 12:05:32 -07:00
|
|
|
node.start(function(err) {
|
|
|
|
if(err) {
|
|
|
|
log.error('Failed to start services');
|
|
|
|
if (err.stack) {
|
|
|
|
log.error(err.stack);
|
|
|
|
}
|
|
|
|
start.cleanShutdown(process, node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-08-25 10:57:54 -07:00
|
|
|
return node;
|
|
|
|
|
2015-08-20 14:32:25 -07:00
|
|
|
}
|
|
|
|
|
2015-09-04 14:36:53 -07:00
|
|
|
/**
|
|
|
|
* This function will fork the passed in process and exit the parent process
|
|
|
|
* in order to daemonize the process. If there is already a daemon for this pid (process),
|
|
|
|
* then the function just returns. Stdout and stderr both append to one file, 'bitcore-node.log'
|
|
|
|
* located in the datadir.
|
|
|
|
* @param {String} datadir - The data directory where the bitcoin blockchain and config live.
|
|
|
|
* @param {Object} _process - The process that needs to fork a child and then, itself, exit.
|
|
|
|
*/
|
|
|
|
function spawnChildProcess(datadir, _process) {
|
|
|
|
|
|
|
|
if (_process.env.__bitcore_node) {
|
|
|
|
return _process.pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
var args = [].concat(_process.argv);
|
|
|
|
args.shift();
|
|
|
|
var script = args.shift();
|
|
|
|
var env = _process.env;
|
|
|
|
var cwd = _process.cwd();
|
|
|
|
env.__bitcore_node = true;
|
|
|
|
|
|
|
|
var stderr = fs.openSync(datadir + '/bitcore-node.log', 'a+');
|
|
|
|
var stdout = stderr;
|
|
|
|
|
|
|
|
var cp_opt = {
|
|
|
|
stdio: ['ignore', stdout, stderr],
|
|
|
|
env: env,
|
|
|
|
cwd: cwd,
|
|
|
|
detached: true
|
|
|
|
};
|
|
|
|
|
|
|
|
var child = child_process.spawn(_process.execPath, [script].concat(args), cp_opt);
|
|
|
|
child.unref();
|
|
|
|
return _process.exit();
|
|
|
|
}
|
|
|
|
|
2015-08-20 14:32:25 -07:00
|
|
|
module.exports = start;
|
2015-09-01 09:47:15 -07:00
|
|
|
module.exports.registerExitHandlers = registerExitHandlers;
|
2015-09-11 13:26:23 -07:00
|
|
|
module.exports.exitHandler = exitHandler;
|
2015-09-01 09:47:15 -07:00
|
|
|
module.exports.registerSyncHandlers = registerSyncHandlers;
|
|
|
|
module.exports.setupServices = setupServices;
|
2015-09-04 14:36:53 -07:00
|
|
|
module.exports.spawnChildProcess = spawnChildProcess;
|
2015-09-04 12:05:32 -07:00
|
|
|
module.exports.cleanShutdown = cleanShutdown;
|