You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
24 KiB

8 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: node/node.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: node/node.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*!
* node.js - node object for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
'use strict';
var assert = require('assert');
var AsyncObject = require('../utils/asyncobject');
var util = require('../utils/util');
var co = require('../utils/co');
var Network = require('../protocol/network');
var Logger = require('./logger');
var NodeClient = require('./nodeclient');
var workerPool = require('../workers/workerpool').pool;
var ec = require('../crypto/ec');
var native = require('../utils/native');
/**
* Base class from which every other
* Node-like object inherits.
* @alias module:node.Node
* @constructor
* @abstract
* @param {Object} options
*/
function Node(options) {
if (!(this instanceof Node))
return new Node(options);
AsyncObject.call(this);
this.options = {};
this.network = Network.primary;
this.prefix = util.HOME + '/.bcoin';
this.startTime = -1;
this.bound = [];
this.logger = new Logger();
this.chain = null;
this.fees = null;
this.mempool = null;
this.pool = null;
this.miner = null;
this.walletdb = null;
this.wallet = null;
this.http = null;
this.client = null;
this.init(options);
}
util.inherits(Node, AsyncObject);
/**
* Initialize options.
* @private
* @param {Object} options
*/
Node.prototype.initOptions = function initOptions(options) {
if (!options)
return;
assert(typeof options === 'object');
this.options = options;
if (options.network != null) {
this.network = Network.get(options.network);
if (this.network !== Network.main)
this.prefix += '/' + this.network.type;
}
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;
}
};
/**
* Initialize node.
* @private
* @param {Object} options
*/
Node.prototype.init = function init(options) {
var self = this;
this.initOptions(options);
// Local client for walletdb
this.client = new NodeClient(this);
this.on('preopen', function() {
self.handlePreopen();
});
this.on('open', function() {
self.handleOpen();
});
this.on('close', function() {
self.handleClose();
});
};
/**
* Open node. Bind all events.
* @private
*/
Node.prototype.handlePreopen = function handlePreopen() {
var self = this;
this.logger.open();
this.bind(this.network.time, 'offset', function(offset) {
self.logger.info('Time offset: %d (%d minutes).', offset, offset / 60 | 0);
});
this.bind(this.network.time, 'sample', function(sample, total) {
self.logger.debug(
'Added time data: samples=%d, offset=%d (%d minutes).',
total, sample, sample / 60 | 0);
});
this.bind(this.network.time, 'mismatch', function() {
self.logger.warning('Adjusted time mismatch!');
self.logger.warning('Please make sure your system clock is correct!');
});
this.bind(workerPool, 'spawn', function(child) {
self.logger.info('Spawning worker process: %d.', child.id);
});
this.bind(workerPool, 'exit', function(code, child) {
self.logger.warning('Worker %d exited: %s.', child.id, code);
});
this.bind(workerPool, 'error', function(err, child) {
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.');
}
};
/**
* Close node. Unbind all events.
* @private
*/
Node.prototype.handleClose = function handleClose() {
var i, bound;
this.startTime = -1;
this.logger.close();
for (i = 0; i &lt; this.bound.length; i++) {
bound = this.bound[i];
bound[0].removeListener(bound[1], bound[2]);
}
this.bound.length = 0;
};
/**
* Bind to an event on `obj`, save listener for removal.
* @private
* @param {EventEmitter} obj
* @param {String} event
* @param {Function} listener
*/
Node.prototype.bind = function bind(obj, event, listener) {
this.bound.push([obj, event, listener]);
obj.on(event, listener);
};
/**
* Emit and log an error.
* @private
* @param {Error} err
*/
Node.prototype.error = function error(err) {
if (!err)
return;
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' &amp;&amp; err.code[0] === 'E') {
this.logger.error(err.message);
} else {
this.logger.error(err);
}
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) {
return this.prefix + '/' + name;
};
/**
* 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.
* @returns {Promise}
*/
Node.prototype.openWallet = co(function* openWallet() {
var options, wallet;
assert(!this.wallet);
options = {
id: 'primary',
passphrase: this.options.passphrase
};
wallet = yield this.walletdb.ensure(options);
this.logger.info(
'Loaded wallet with id=%s wid=%d address=%s',
wallet.id, wallet.wid, wallet.getAddress());
if (this.miner)
this.miner.addAddress(wallet.getAddress());
this.wallet = wallet;
});
/*
* Expose
*/
module.exports = Node;
</code></pre>
</article>
</section>
</div>
<nav>
8 years ago
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bcoin.html">bcoin</a></li><li><a href="module-bip70.html">bip70</a></li><li><a href="module-bip70_pk.html">bip70/pk</a></li><li><a href="module-bip70_x509.html">bip70/x509</a></li><li><a href="module-blockchain.html">blockchain</a></li><li><a href="module-blockchain_common.html">blockchain/common</a></li><li><a href="module-btc.html">btc</a></li><li><a href="module-coins.html">coins</a></li><li><a href="module-crypto.html">crypto</a></li><li><a href="module-crypto_chachapoly.html">crypto/chachapoly</a></li><li><a href="module-crypto_ec.html">crypto/ec</a></li><li><a href="module-crypto_pk.html">crypto/pk</a></li><li><a href="module-crypto_schnorr.html">crypto/schnorr</a></li><li><a href="module-crypto_siphash.html">crypto/siphash</a></li><li><a href="module-db.html">db</a></li><li><a href="module-hd.html">hd</a></li><li><a href="module-http.html">http</a></li><li><a href="module-mempool.html">mempool</a></li><li><a href="module-mining.html">mining</a></li><li><a href="module-net.html">net</a></li><li><a href="module-net_bip152.html">net/bip152</a></li><li><a href="module-net_common.html">net/common</a></li><li><a href="module-net_dns.html">net/dns</a></li><li><a href="module-net_packets.html">net/packets</a></li><li><a href="module-net_socks.html">net/socks</a></li><li><a href="module-net_tcp.html">net/tcp</a></li><li><a href="module-node.html">node</a></li><li><a href="module-node_config.html">node/config</a></li><li><a href="module-primitives.html">primitives</a></li><li><a href="module-protocol.html">protocol</a></li><li><a href="module-protocol_consensus.html">protocol/consensus</a></li><li><a href="module-protocol_errors.html">protocol/errors</a></li><li><a href="module-protocol_networks.html">protocol/networks</a></li><li><a href="module-protocol_policy.html">protocol/policy</a></li><li><a href="module-script.html">script</a></li><li><a href="module-script_common.html">script/common</a></li><li><a href="module-utils.html">utils</a></li><li><a href="module-utils_asn1.html">utils/asn1</a></li><li><a href="module-utils_base32.html">utils/base32</a></li><li><a href="module-utils_base58.html">utils/base58</a></li><li><a href="module-utils_co.html">utils/co</a></li><li><a href="module-utils_encoding.html">utils/encoding</a></li><li><a href="module-utils_ip.html">utils/ip</a></li><li><a href="module-utils_pem.html">utils/pem</a></li><li><a href="module-utils_protobuf.html">utils/protobuf</a></li><li><a href="module-utils_util.html">utils/util</a></li><li><a href="module-wallet.html">wallet</a></li><li><a href="module-wallet_common.html">wallet/common</a></li><li><a href="module-wallet_records.html">wallet/records</a></li><li><a href="module-workers.html">workers</a></li><li><a href="module-workers_jobs.html">workers/jobs</a></li><li><a href="module-workers_packets.html">workers/packets</a></li></ul><h3>Classes</h3><ul><li><a href="Environment.html">Environment</a></li><li><a href="module-bip70.Payment.html">Payment</a></li><li><a href="module-bip70.PaymentACK.html">PaymentACK</a></li><li><a href="module-bip70.PaymentDetails.html">PaymentDetails</a></li><li><a href="module-bip70.PaymentRequest.html">PaymentRequest</a></li><li><a href="module-blockchain.Chain.html">Chain</a></li><li><a href="module-blockchain.ChainDB.html">ChainDB</a></li><li><a href="module-blockchain.ChainEntry.html">ChainEntry</a></li><li><a href="module-blockchain.ChainFlags.html">ChainFlags</a></li><li><a href="module-blockchain.ChainOptions.html">ChainOptions</a></li><li><a href="module-blockchain.ChainState.html">ChainState</a></li><li><a href="module-blockchain.DeploymentState.html">DeploymentState</a></li><li><a href="module-blockchain.StateCache.html">StateCache</a></li><li><a href="module-btc.Amount.html">Amount</a></li><li><a href="module-btc.URI.html">URI</a></li><li><a href="module-coins.CoinEntry.html">CoinEntry</a></li><li><a href="module-coins.Coins.html">Coins</a></li><li><a href="module-coins.CoinView.html">CoinView</a></li><li><a href="module-coins.UndoCoi
8 years ago
</nav>
<br class="clear">
<footer>
8 years ago
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Feb 07 2017 16:09:44 GMT-0800 (PST)
8 years ago
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>