itns-sidechain/lib/node/node.js

322 lines
6.8 KiB
JavaScript
Raw Normal View History

/*!
2016-03-10 02:40:33 -08:00
* node.js - node object for bcoin
2016-02-14 18:05:21 -08:00
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
2017-02-03 22:47:26 -08:00
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
2016-06-09 16:18:50 -07:00
* https://github.com/bcoin-org/bcoin
2016-02-14 18:05:21 -08:00
*/
2016-06-13 01:06:01 -07:00
'use strict';
2017-01-14 19:21:46 -08:00
var assert = require('assert');
2017-02-04 14:28:38 -08:00
var AsyncObject = require('../utils/asyncobject');
2016-11-19 10:45:31 -08:00
var util = require('../utils/util');
2016-10-02 02:43:50 -07:00
var co = require('../utils/co');
2016-10-02 01:01:16 -07:00
var Network = require('../protocol/network');
var Logger = require('./logger');
2016-11-19 21:40:31 -08:00
var NodeClient = require('./nodeclient');
2016-11-19 06:48:55 -08:00
var workerPool = require('../workers/workerpool').pool;
var ec = require('../crypto/ec');
var native = require('../utils/native');
2016-02-14 18:05:21 -08:00
/**
* Base class from which every other
* Node-like object inherits.
2017-02-03 22:47:26 -08:00
* @alias module:node.Node
* @constructor
* @abstract
* @param {Object} options
2016-02-14 18:05:21 -08:00
*/
2016-03-10 02:40:33 -08:00
function Node(options) {
if (!(this instanceof Node))
return new Node(options);
2016-02-14 18:05:21 -08:00
AsyncObject.call(this);
2016-02-14 18:05:21 -08:00
2017-01-14 19:21:46 -08:00
this.options = {};
this.network = Network.primary;
this.prefix = util.HOME + '/.bcoin';
this.startTime = -1;
this.bound = [];
2016-02-14 18:05:21 -08:00
2017-01-14 19:21:46 -08:00
this.logger = new Logger();
2016-03-10 02:40:33 -08:00
this.chain = null;
2016-07-04 05:36:06 -07:00
this.fees = null;
this.mempool = null;
this.pool = null;
2016-03-10 02:40:33 -08:00
this.miner = null;
this.walletdb = null;
this.wallet = null;
2016-08-26 05:02:08 -07:00
this.http = null;
2017-01-14 19:21:46 -08:00
this.client = null;
2016-11-19 21:40:31 -08:00
2017-01-14 19:21:46 -08:00
this.init(options);
}
2016-07-04 05:36:06 -07:00
2017-01-14 19:21:46 -08:00
util.inherits(Node, AsyncObject);
2017-01-14 14:41:55 -08:00
2017-01-14 19:21:46 -08:00
/**
* Initialize options.
* @private
* @param {Object} options
*/
2016-07-04 05:36:06 -07:00
2017-01-14 19:21:46 -08:00
Node.prototype.initOptions = function initOptions(options) {
if (!options)
return;
2016-02-16 16:13:34 -08:00
2017-01-14 19:21:46 -08:00
assert(typeof options === 'object');
this.options = options;
2017-01-25 16:18:40 -08:00
if (options.network != null) {
2017-01-14 19:21:46 -08:00
this.network = Network.get(options.network);
2017-01-25 16:18:40 -08:00
if (this.network !== Network.main)
this.prefix += '/' + this.network.type;
}
2017-01-14 19:21:46 -08:00
if (options.prefix != null) {
assert(typeof options.prefix === 'string');
this.prefix = util.normalize(options.prefix);
}
if (options.logger != null) {
assert(typeof options.logger === 'object');
this.logger = options.logger;
}
if (options.logFile != null) {
if (typeof options.logFile === 'string') {
this.logger.setFile(options.logFile);
} else {
assert(typeof options.logFile === 'boolean');
if (options.logFile)
this.logger.setFile(this.location('debug.log'));
}
}
if (options.logLevel != null) {
assert(typeof options.logLevel === 'string');
this.logger.setLevel(options.logLevel);
}
if (options.logConsole != null) {
assert(typeof options.logConsole === 'boolean');
this.logger.console = options.logConsole;
}
};
2016-02-14 18:05:21 -08:00
2016-07-04 05:36:06 -07:00
/**
* Initialize node.
* @private
2017-01-14 19:21:46 -08:00
* @param {Object} options
2016-07-04 05:36:06 -07:00
*/
2017-01-14 19:21:46 -08:00
Node.prototype.init = function init(options) {
2016-07-04 05:36:06 -07:00
var self = this;
2017-01-14 19:21:46 -08:00
this.initOptions(options);
// Local client for walletdb
this.client = new NodeClient(this);
2016-07-04 05:36:06 -07:00
this.on('preopen', function() {
self.handlePreopen();
2016-07-04 05:36:06 -07:00
});
2017-01-14 14:41:55 -08:00
this.on('open', function() {
self.handleOpen();
2017-01-14 14:41:55 -08:00
});
2016-07-04 05:36:06 -07:00
this.on('close', function() {
2017-01-14 19:21:46 -08:00
self.handleClose();
2016-07-04 05:36:06 -07:00
});
};
/**
* Open node. Bind all events.
* @private
*/
Node.prototype.handlePreopen = function handlePreopen() {
2016-07-04 05:36:06 -07:00
var self = this;
this.logger.open();
2017-01-14 19:21:46 -08:00
this.bind(this.network.time, 'offset', function(offset) {
2016-07-04 05:36:06 -07:00
self.logger.info('Time offset: %d (%d minutes).', offset, offset / 60 | 0);
});
2017-01-14 19:21:46 -08:00
this.bind(this.network.time, 'sample', function(sample, total) {
self.logger.debug(
'Added time data: samples=%d, offset=%d (%d minutes).',
2016-07-04 05:36:06 -07:00
total, sample, sample / 60 | 0);
});
2017-01-14 19:21:46 -08:00
this.bind(this.network.time, 'mismatch', function() {
self.logger.warning('Adjusted time mismatch!');
2016-07-04 05:36:06 -07:00
self.logger.warning('Please make sure your system clock is correct!');
});
2017-01-14 19:21:46 -08:00
this.bind(workerPool, 'spawn', function(child) {
2016-07-04 05:36:06 -07:00
self.logger.info('Spawning worker process: %d.', child.id);
});
2017-01-14 19:21:46 -08:00
this.bind(workerPool, 'exit', function(code, child) {
2016-07-04 05:36:06 -07:00
self.logger.warning('Worker %d exited: %s.', child.id, code);
});
2017-01-14 19:21:46 -08:00
this.bind(workerPool, 'error', function(err, child) {
2016-07-04 05:36:06 -07:00
if (child) {
self.logger.error('Worker %d error: %s', child.id, err.message);
return;
}
self.emit('error', err);
});
};
/**
* Open node.
* @private
*/
Node.prototype.handleOpen = function handleOpen() {
this.startTime = util.now();
if (!ec.binding) {
this.logger.warning('Warning: secp256k1-node was not built.');
this.logger.warning('Verification will be slow.');
}
if (!native.binding) {
this.logger.warning('Warning: bcoin-native was not built.');
this.logger.warning('Hashing will be slow.');
}
if (!workerPool.enabled) {
this.logger.warning('Warning: worker pool is disabled.');
this.logger.warning('Verification will be slow.');
}
};
2016-07-04 05:36:06 -07:00
/**
* Close node. Unbind all events.
* @private
*/
2017-01-14 19:21:46 -08:00
Node.prototype.handleClose = function handleClose() {
2016-07-04 05:36:06 -07:00
var i, bound;
this.startTime = -1;
2016-07-04 05:36:06 -07:00
this.logger.close();
2017-01-14 19:21:46 -08:00
for (i = 0; i < this.bound.length; i++) {
bound = this.bound[i];
2016-07-04 05:36:06 -07:00
bound[0].removeListener(bound[1], bound[2]);
}
2017-01-14 19:21:46 -08:00
this.bound.length = 0;
2016-07-04 05:36:06 -07:00
};
/**
* Bind to an event on `obj`, save listener for removal.
* @private
* @param {EventEmitter} obj
* @param {String} event
* @param {Function} listener
*/
2017-01-14 19:21:46 -08:00
Node.prototype.bind = function bind(obj, event, listener) {
this.bound.push([obj, event, listener]);
2016-07-04 05:36:06 -07:00
obj.on(event, listener);
};
/**
* Emit and log an error.
* @private
* @param {Error} err
*/
2017-01-14 19:21:46 -08:00
Node.prototype.error = function error(err) {
2016-08-26 05:02:08 -07:00
if (!err)
return;
2016-11-27 23:54:18 -08:00
if (err.type === 'VerifyError') {
switch (err.reason) {
case 'insufficient priority':
case 'non-final':
this.logger.spam(err.message);
break;
default:
this.logger.error(err.message);
break;
}
} else if (typeof err.code === 'string' && err.code[0] === 'E') {
this.logger.error(err.message);
} else {
this.logger.error(err);
2016-08-26 05:02:08 -07:00
}
2016-07-04 05:36:06 -07:00
this.emit('error', err);
};
/**
* Create a file path from a name
* as well as the node's prefix.
* @param {String} name
* @returns {String}
*/
Node.prototype.location = function location(name) {
2017-01-25 16:18:40 -08:00
return this.prefix + '/' + name;
2016-07-04 05:36:06 -07:00
};
2017-01-14 14:41:55 -08:00
/**
* Get node uptime in seconds.
* @returns {Number}
*/
Node.prototype.uptime = function uptime() {
if (this.startTime === -1)
return 0;
return util.now() - this.startTime;
};
/**
* Open and ensure primary wallet.
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
2016-09-21 22:59:48 -07:00
Node.prototype.openWallet = co(function* openWallet() {
2016-09-21 22:58:27 -07:00
var options, wallet;
2016-09-21 22:58:27 -07:00
assert(!this.wallet);
2016-09-21 22:58:27 -07:00
options = {
id: 'primary',
passphrase: this.options.passphrase
};
2016-09-21 22:58:27 -07:00
wallet = yield this.walletdb.ensure(options);
2016-09-21 22:58:27 -07:00
this.logger.info(
'Loaded wallet with id=%s wid=%d address=%s',
wallet.id, wallet.wid, wallet.getAddress());
if (this.miner && this.miner.addresses.length === 0)
this.miner.addAddress(wallet.getAddress());
2016-09-21 22:58:27 -07:00
this.wallet = wallet;
if (this.http && this.http.rpc && !this.http.rpc.wallet)
this.http.rpc.wallet = wallet;
2016-09-21 22:58:27 -07:00
});
2016-05-15 18:07:06 -07:00
/*
* Expose
*/
2016-05-13 09:23:57 -07:00
module.exports = Node;