itns-sidechain/lib/node/fullnode.js

509 lines
12 KiB
JavaScript
Raw Normal View History

/*!
2016-03-10 02:40:33 -08:00
* fullnode.js - full node for bcoin
* 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-03-10 02:40:33 -08:00
*/
2016-06-13 01:06:01 -07:00
'use strict';
2017-06-29 20:54:07 -07:00
const util = require('../utils/util');
const Node = require('./node');
const Chain = require('../blockchain/chain');
const Fees = require('../mempool/fees');
const Mempool = require('../mempool/mempool');
const Pool = require('../net/pool');
const Miner = require('../mining/miner');
const HTTPServer = require('../http/server');
const RPC = require('../http/rpc');
2016-03-10 02:40:33 -08:00
/**
2017-02-07 14:17:41 -08:00
* Respresents a fullnode complete with a
* chain, mempool, miner, etc.
2017-02-03 22:47:26 -08:00
* @alias module:node.FullNode
* @extends Node
* @constructor
* @param {Object?} options
* @property {Chain} chain
2016-07-04 05:36:06 -07:00
* @property {PolicyEstimator} fees
* @property {Mempool} mempool
* @property {Pool} pool
* @property {Miner} miner
2016-04-16 06:59:32 -07:00
* @property {HTTPServer} http
* @emits FullNode#block
* @emits FullNode#tx
2017-02-07 14:17:41 -08:00
* @emits FullNode#connect
* @emits FullNode#disconnect
* @emits FullNode#reset
* @emits FullNode#error
2016-03-10 02:40:33 -08:00
*/
function FullNode(options) {
if (!(this instanceof FullNode))
return new FullNode(options);
2016-03-10 02:40:33 -08:00
Node.call(this, options);
2016-04-03 04:11:26 -07:00
2017-03-09 18:15:35 -08:00
// SPV flag.
this.spv = false;
2016-07-04 05:36:06 -07:00
// Instantiate blockchain.
2016-10-02 01:01:16 -07:00
this.chain = new Chain({
2016-05-13 09:23:57 -07:00
network: this.network,
2016-07-04 05:36:06 -07:00
logger: this.logger,
2017-06-30 03:03:39 -07:00
workers: this.workers,
db: this.config.str('db'),
prefix: this.config.prefix,
maxFiles: this.config.num('max-files'),
cacheSize: this.config.mb('cache-size'),
forceWitness: this.config.bool('force-witness'),
2017-05-14 15:24:14 -07:00
forcePrune: this.config.bool('force-prune'),
prune: this.config.bool('prune'),
checkpoints: this.config.bool('checkpoints'),
coinCache: this.config.mb('coin-cache'),
2017-05-19 21:05:07 -07:00
entryCache: this.config.num('entry-cache'),
indexTX: this.config.bool('index-tx'),
indexAddress: this.config.bool('index-address')
2016-03-10 02:40:33 -08:00
});
2016-07-04 05:36:06 -07:00
// Fee estimation.
2017-03-01 09:47:56 -08:00
this.fees = new Fees(this.logger);
this.fees.init();
2016-07-04 05:36:06 -07:00
2016-04-04 18:45:02 -07:00
// Mempool needs access to the chain.
2016-10-02 01:01:16 -07:00
this.mempool = new Mempool({
2016-05-13 09:23:57 -07:00
network: this.network,
2016-07-04 05:36:06 -07:00
logger: this.logger,
2017-06-30 03:03:39 -07:00
workers: this.workers,
2016-04-04 18:45:02 -07:00
chain: this.chain,
2016-07-04 05:36:06 -07:00
fees: this.fees,
db: this.config.str('db'),
prefix: this.config.prefix,
persistent: this.config.bool('persistent-mempool'),
maxSize: this.config.mb('mempool-size'),
limitFree: this.config.bool('limit-free'),
limitFreeRelay: this.config.num('limit-free-relay'),
requireStandard: this.config.bool('require-standard'),
rejectAbsurdFees: this.config.bool('reject-absurd-fees'),
replaceByFee: this.config.bool('replace-by-fee'),
indexAddress: this.config.bool('index-address')
2016-03-10 02:40:33 -08:00
});
2016-04-04 18:45:02 -07:00
// Pool needs access to the chain and mempool.
2016-10-02 01:01:16 -07:00
this.pool = new Pool({
2016-05-13 09:23:57 -07:00
network: this.network,
2016-07-04 05:36:06 -07:00
logger: this.logger,
2016-04-04 18:45:02 -07:00
chain: this.chain,
mempool: this.mempool,
prefix: this.config.prefix,
selfish: this.config.bool('selfish'),
compact: this.config.bool('compact'),
bip37: this.config.bool('bip37'),
bip151: this.config.bool('bip151'),
bip150: this.config.bool('bip150'),
identityKey: this.config.buf('identity-key'),
maxOutbound: this.config.num('max-outbound'),
maxInbound: this.config.num('max-inbound'),
proxy: this.config.str('proxy'),
onion: this.config.bool('onion'),
upnp: this.config.bool('upnp'),
seeds: this.config.array('seeds'),
nodes: this.config.array('nodes'),
2017-05-12 12:02:55 -07:00
only: this.config.array('only'),
publicHost: this.config.str('public-host'),
publicPort: this.config.num('public-port'),
host: this.config.str('host'),
port: this.config.num('port'),
listen: this.config.bool('listen'),
persistent: this.config.bool('persistent')
2016-03-10 02:40:33 -08:00
});
2016-04-04 18:45:02 -07:00
// Miner needs access to the chain and mempool.
2016-10-02 01:01:16 -07:00
this.miner = new Miner({
2016-05-13 09:23:57 -07:00
network: this.network,
2016-07-04 05:36:06 -07:00
logger: this.logger,
2017-06-30 03:03:39 -07:00
workers: this.workers,
2016-04-04 18:45:02 -07:00
chain: this.chain,
mempool: this.mempool,
address: this.config.array('coinbase-address'),
coinbaseFlags: this.config.str('coinbase-flags'),
preverify: this.config.bool('preverify'),
maxWeight: this.config.num('max-weight'),
reservedWeight: this.config.num('reserved-weight'),
reservedSigops: this.config.num('reserved-sigops')
2016-03-10 02:40:33 -08:00
});
// RPC needs access to the node.
this.rpc = new RPC(this);
2016-04-04 18:45:02 -07:00
// HTTP needs access to the node.
if (!HTTPServer.unsupported) {
2016-10-03 01:45:06 -07:00
this.http = new HTTPServer({
2016-06-05 06:55:35 -07:00
network: this.network,
2016-07-04 05:36:06 -07:00
logger: this.logger,
2016-06-05 06:55:35 -07:00
node: this,
prefix: this.config.prefix,
ssl: this.config.bool('ssl'),
keyFile: this.config.path('ssl-key'),
certFile: this.config.path('ssl-cert'),
host: this.config.str('http-host'),
port: this.config.num('http-port'),
apiKey: this.config.str('api-key'),
noAuth: this.config.bool('no-auth')
2016-06-05 06:55:35 -07:00
});
}
2016-03-10 02:40:33 -08:00
this._init();
}
2016-11-19 10:45:31 -08:00
util.inherits(FullNode, Node);
/**
* Initialize the node.
* @private
*/
FullNode.prototype._init = function _init() {
2016-03-10 02:40:33 -08:00
// Bind to errors
2017-06-29 20:54:07 -07:00
this.chain.on('error', err => this.error(err));
this.mempool.on('error', err => this.error(err));
this.pool.on('error', err => this.error(err));
this.miner.on('error', err => this.error(err));
2016-04-03 04:20:18 -07:00
2016-08-26 05:02:08 -07:00
if (this.http)
2017-06-29 20:54:07 -07:00
this.http.on('error', err => this.error(err));
2016-03-10 02:40:33 -08:00
2017-06-29 20:54:07 -07:00
this.mempool.on('tx', (tx) => {
this.miner.cpu.notifyEntry();
this.emit('tx', tx);
2016-03-25 20:02:23 -07:00
});
2016-03-10 02:40:33 -08:00
2017-06-29 20:54:07 -07:00
this.chain.hook('connect', async (entry, block) => {
try {
2017-06-29 20:54:07 -07:00
await this.mempool._addBlock(entry, block.txs);
} catch (e) {
2017-06-29 20:54:07 -07:00
this.error(e);
2016-12-14 12:03:47 -08:00
}
2017-06-29 20:54:07 -07:00
this.emit('block', block);
this.emit('connect', entry, block);
});
2016-12-14 12:03:47 -08:00
2017-06-29 20:54:07 -07:00
this.chain.hook('disconnect', async (entry, block) => {
try {
2017-06-29 20:54:07 -07:00
await this.mempool._removeBlock(entry, block.txs);
} catch (e) {
2017-06-29 20:54:07 -07:00
this.error(e);
2016-12-14 12:03:47 -08:00
}
2017-06-29 20:54:07 -07:00
this.emit('disconnect', entry, block);
});
2016-12-14 12:03:47 -08:00
2017-06-29 20:54:07 -07:00
this.chain.hook('reset', async (tip) => {
2016-12-14 12:03:47 -08:00
try {
2017-06-29 20:54:07 -07:00
await this.mempool._reset();
2016-12-14 12:03:47 -08:00
} catch (e) {
2017-06-29 20:54:07 -07:00
this.error(e);
2016-12-14 12:03:47 -08:00
}
2017-06-29 20:54:07 -07:00
this.emit('reset', tip);
});
this.loadPlugins();
};
2016-04-04 18:45:02 -07:00
/**
* Open the node and all its child objects,
* wait for the database to load.
* @alias FullNode#open
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
FullNode.prototype._open = async function open() {
await this.chain.open();
await this.mempool.open();
await this.miner.open();
await this.pool.open();
await this.openPlugins();
2016-09-20 14:56:54 -07:00
2017-02-28 14:10:45 -08:00
if (this.http)
await this.http.open();
2017-02-28 14:10:45 -08:00
2016-09-21 22:58:27 -07:00
this.logger.info('Node is loaded.');
};
/**
* Close the node, wait for the database to close.
* @alias FullNode#close
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
FullNode.prototype._close = async function close() {
2016-09-21 22:58:27 -07:00
if (this.http)
await this.http.close();
2016-09-20 14:56:54 -07:00
await this.closePlugins();
2016-09-23 18:32:49 -07:00
await this.pool.close();
await this.miner.close();
await this.mempool.close();
await this.chain.close();
2016-09-21 22:58:27 -07:00
this.logger.info('Node is closed.');
};
2016-03-10 02:40:33 -08:00
2016-10-05 04:29:10 -07:00
/**
* Rescan for any missed transactions.
2016-11-19 18:00:13 -08:00
* @param {Number|Hash} start - Start block.
* @param {Bloom} filter
* @param {Function} iter - Iterator.
* @returns {Promise}
*/
FullNode.prototype.scan = function scan(start, filter, iter) {
return this.chain.scan(start, filter, iter);
};
/**
* Broadcast a transaction (note that this will _not_ be verified
* by the mempool - use with care, lest you get banned from
* bitcoind nodes).
2016-08-26 05:02:08 -07:00
* @param {TX|Block} item
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
FullNode.prototype.broadcast = async function broadcast(item) {
try {
await this.pool.broadcast(item);
} catch (e) {
this.emit('error', e);
}
};
2016-03-29 16:14:39 -07:00
/**
2017-01-14 19:21:46 -08:00
* Add transaction to mempool, broadcast.
2016-08-26 05:02:08 -07:00
* @param {TX} tx
*/
FullNode.prototype.sendTX = async function sendTX(tx) {
2017-06-29 20:54:07 -07:00
let missing;
2016-09-21 22:58:27 -07:00
try {
missing = await this.mempool.addTX(tx);
2016-09-21 22:58:27 -07:00
} catch (err) {
if (err.type === 'VerifyError' && err.score === 0) {
2017-01-14 19:21:46 -08:00
this.error(err);
this.logger.warning('Verification failed for tx: %s.', tx.txid());
2016-09-21 22:58:27 -07:00
this.logger.warning('Attempting to broadcast anyway...');
this.broadcast(tx);
2016-10-02 17:45:45 -07:00
return;
2016-08-26 05:02:08 -07:00
}
2016-09-21 22:58:27 -07:00
throw err;
}
2016-04-08 18:03:17 -07:00
if (missing) {
this.logger.warning('TX was orphaned in mempool: %s.', tx.txid());
this.logger.warning('Attempting to broadcast anyway...');
this.broadcast(tx);
return;
}
// We need to announce by hand if
// we're running in selfish mode.
2017-01-14 19:21:46 -08:00
if (this.pool.options.selfish)
2017-02-07 14:15:33 -08:00
this.pool.broadcast(tx);
};
2016-03-29 16:14:39 -07:00
2017-01-14 19:21:46 -08:00
/**
* Add transaction to mempool, broadcast. Silence errors.
* @param {TX} tx
* @returns {Promise}
*/
FullNode.prototype.relay = async function relay(tx) {
2017-01-14 19:21:46 -08:00
try {
await this.sendTX(tx);
2017-01-14 19:21:46 -08:00
} catch (e) {
this.error(e);
}
};
2017-01-14 19:21:46 -08:00
2016-05-19 11:56:11 -07:00
/**
2017-01-14 07:54:07 -08:00
* Connect to the network.
* @returns {Promise}
2016-05-19 11:56:11 -07:00
*/
2017-01-14 07:54:07 -08:00
FullNode.prototype.connect = function connect() {
return this.pool.connect();
2016-05-19 11:56:11 -07:00
};
/**
2017-01-14 07:54:07 -08:00
* Disconnect from the network.
2017-01-14 07:18:47 -08:00
* @returns {Promise}
*/
2017-01-14 07:54:07 -08:00
FullNode.prototype.disconnect = function disconnect() {
return this.pool.disconnect();
2016-04-03 06:11:30 -07:00
};
/**
* Start the blockchain sync.
*/
FullNode.prototype.startSync = function startSync() {
2016-03-22 17:36:58 -07:00
return this.pool.startSync();
};
/**
* Stop syncing the blockchain.
*/
FullNode.prototype.stopSync = function stopSync() {
2016-03-22 17:36:58 -07:00
return this.pool.stopSync();
};
/**
* Retrieve a block from the chain database.
* @param {Hash} hash
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link Block}.
*/
FullNode.prototype.getBlock = function getBlock(hash) {
2016-09-20 14:56:54 -07:00
return this.chain.db.getBlock(hash);
2016-03-10 02:40:33 -08:00
};
/**
* Retrieve a coin from the mempool or chain database.
* Takes into account spent coins in the mempool.
* @param {Hash} hash
* @param {Number} index
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link Coin}.
*/
FullNode.prototype.getCoin = function getCoin(hash, index) {
2017-06-29 20:54:07 -07:00
let coin = this.mempool.getCoin(hash, index);
2016-03-10 02:40:33 -08:00
2016-08-15 15:46:37 -07:00
if (coin)
2016-09-20 14:56:54 -07:00
return Promise.resolve(coin);
2016-03-10 02:40:33 -08:00
2016-08-15 15:46:37 -07:00
if (this.mempool.isSpent(hash, index))
return Promise.resolve();
2016-03-10 02:40:33 -08:00
2016-09-20 14:56:54 -07:00
return this.chain.db.getCoin(hash, index);
2016-03-10 02:40:33 -08:00
};
/**
* Get coins that pertain to an address from the mempool or chain database.
* Takes into account spent coins in the mempool.
2017-06-29 04:30:14 -07:00
* @param {Address} addrs
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link Coin}[].
*/
2017-06-29 04:30:14 -07:00
FullNode.prototype.getCoinsByAddress = async function getCoinsByAddress(addrs) {
2017-06-29 20:54:07 -07:00
let mempool = this.mempool.getCoinsByAddress(addrs);
let chain = await this.chain.db.getCoinsByAddress(addrs);
let out = [];
let coin;
2016-08-15 15:46:37 -07:00
2017-06-29 04:30:14 -07:00
for (coin of chain) {
2017-06-29 20:54:07 -07:00
let spent = this.mempool.isSpent(coin.hash, coin.index);
2016-03-21 16:29:02 -07:00
if (spent)
continue;
out.push(coin);
}
2017-06-29 04:30:14 -07:00
for (coin of mempool)
out.push(coin);
2016-03-21 16:29:02 -07:00
return out;
};
2016-03-10 02:40:33 -08:00
2016-08-26 05:02:08 -07:00
/**
* Retrieve transactions pertaining to an
* address from the mempool or chain database.
2017-06-29 04:30:14 -07:00
* @param {Address} addrs
* @returns {Promise} - Returns {@link TXMeta}[].
2016-08-26 05:02:08 -07:00
*/
2017-06-29 04:30:14 -07:00
FullNode.prototype.getMetaByAddress = async function getTXByAddress(addrs) {
2017-06-29 20:54:07 -07:00
let mempool = this.mempool.getMetaByAddress(addrs);
let chain = await this.chain.db.getMetaByAddress(addrs);
return chain.concat(mempool);
};
2016-08-26 05:02:08 -07:00
/**
* Retrieve a transaction from the mempool or chain database.
* @param {Hash} hash
* @returns {Promise} - Returns {@link TXMeta}.
*/
FullNode.prototype.getMeta = async function getMeta(hash) {
2017-06-29 20:54:07 -07:00
let meta = this.mempool.getMeta(hash);
2016-03-10 02:40:33 -08:00
if (meta)
return meta;
2016-03-10 02:40:33 -08:00
return await this.chain.db.getMeta(hash);
};
2016-03-21 16:29:02 -07:00
/**
* Retrieve a spent coin viewpoint from mempool or chain database.
* @param {TXMeta} meta
* @returns {Promise} - Returns {@link CoinView}.
*/
FullNode.prototype.getMetaView = async function getMetaView(meta) {
if (meta.height === -1)
return this.mempool.getSpentView(meta.tx);
return this.chain.getSpentView(meta.tx);
};
/**
* Retrieve transactions pertaining to an
* address from the mempool or chain database.
2017-06-29 04:30:14 -07:00
* @param {Address} addrs
* @returns {Promise} - Returns {@link TX}[].
*/
2017-06-29 04:30:14 -07:00
FullNode.prototype.getTXByAddress = async function getTXByAddress(addrs) {
2017-06-29 20:54:07 -07:00
let mtxs = await this.getMetaByAddress(addrs);
let out = [];
2017-06-29 20:54:07 -07:00
for (let mtx of mtxs)
out.push(mtx.tx);
return out;
};
2016-03-10 02:40:33 -08:00
/**
* Retrieve a transaction from the mempool or chain database.
* @param {Hash} hash
* @returns {Promise} - Returns {@link TX}.
*/
FullNode.prototype.getTX = async function getTX(hash) {
2017-06-29 20:54:07 -07:00
let mtx = await this.getMeta(hash);
if (!mtx)
return;
return mtx.tx;
};
/**
* Test whether the mempool or chain contains a transaction.
* @param {Hash} hash
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns Boolean.
*/
FullNode.prototype.hasTX = async function hasTX(hash) {
2017-03-05 10:02:34 -08:00
if (this.mempool.hasEntry(hash))
return true;
2016-03-10 02:40:33 -08:00
return await this.chain.db.hasTX(hash);
};
2016-03-10 02:40:33 -08:00
2016-05-15 18:07:06 -07:00
/*
* Expose
*/
module.exports = FullNode;