itns-sidechain/lib/node/fullnode.js

520 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)
2016-04-06 18:20:03 -07:00
* Copyright (c) 2014-2016, 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';
2016-10-02 01:01:16 -07:00
var constants = require('../protocol/constants');
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 Node = require('./node');
2016-11-19 11:22:10 -08:00
var Chain = require('../blockchain/chain');
2016-10-02 01:01:16 -07:00
var Fees = require('../mempool/fees');
var Mempool = require('../mempool/mempool');
var Pool = require('../net/pool');
2016-11-19 07:10:49 -08:00
var Miner = require('../mining/miner');
2016-10-02 01:01:16 -07:00
var WalletDB = require('../wallet/walletdb');
var HTTPServer = require('../http/server');
2016-03-10 02:40:33 -08:00
/**
* Create a fullnode complete with a chain,
* mempool, miner, wallet, etc.
* @exports FullNode
* @extends Node
* @constructor
* @param {Object?} options
* @param {Boolean?} options.limitFree
* @param {Number?} options.limitFreeRelay
* @param {Boolean?} options.requireStandard
* @param {Boolean?} options.rejectInsaneFees
* @param {Boolean?} options.replaceByFee
* @param {Boolean?} options.selfish
* @param {Base58Address?} options.payoutAddress
* @param {String?} options.coinbaseFlags
* @param {Buffer?} options.sslKey
* @param {Buffer?} options.sslCert
* @param {Number?} options.httpPort
* @param {String?} options.httpHost
* @param {Object?} options.wallet - Primary {@link Wallet} options.
* @property {Boolean} loaded
* @property {Chain} chain
2016-07-04 05:36:06 -07:00
* @property {PolicyEstimator} fees
* @property {Mempool} mempool
* @property {Pool} pool
* @property {Miner} miner
* @property {WalletDB} walletdb
2016-04-16 06:59:32 -07:00
* @property {HTTPServer} http
* @emits FullNode#block
* @emits FullNode#tx
* @emits FullNode#alert
* @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
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,
2016-08-26 05:02:08 -07:00
db: this.options.db,
2016-07-04 05:36:06 -07:00
location: this.location('chain'),
2016-03-11 15:53:15 -08:00
preload: false,
2016-03-25 20:02:23 -07:00
spv: false,
2016-07-29 15:40:39 -07:00
witness: this.options.witness,
2016-10-23 05:35:37 -07:00
forceWitness: this.options.forceWitness,
2016-03-11 23:09:07 -08:00
prune: this.options.prune,
useCheckpoints: this.options.useCheckpoints,
coinCache: this.options.coinCache,
indexTX: this.options.indexTX,
2016-08-24 07:44:06 -07:00
indexAddress: this.options.indexAddress,
maxFiles: this.options.maxFiles
2016-03-10 02:40:33 -08:00
});
2016-07-04 05:36:06 -07:00
// Fee estimation.
2016-10-02 01:01:16 -07:00
this.fees = new Fees(
2016-07-04 05:36:06 -07:00
constants.tx.MIN_RELAY,
this.network,
this.logger);
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,
2016-04-04 18:45:02 -07:00
chain: this.chain,
2016-07-04 05:36:06 -07:00
fees: this.fees,
2016-03-25 20:02:23 -07:00
limitFree: this.options.limitFree,
limitFreeRelay: this.options.limitFreeRelay,
requireStandard: this.options.requireStandard,
rejectInsaneFees: this.options.rejectInsaneFees,
replaceByFee: this.options.replaceByFee,
indexAddress: this.options.indexAddress
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,
2016-07-29 15:40:39 -07:00
witness: this.options.witness,
selfish: this.options.selfish,
2016-05-23 00:49:52 -07:00
headers: this.options.headers,
2016-07-20 19:30:52 -07:00
compact: this.options.compact,
2016-07-25 18:26:29 -07:00
bip151: this.options.bip151,
2016-08-23 03:59:51 -07:00
bip150: this.options.bip150,
authPeers: this.options.authPeers,
knownPeers: this.options.knownPeers,
identityKey: this.options.identityKey,
2016-07-28 22:33:11 -07:00
maxPeers: this.options.maxPeers,
maxLeeches: this.options.maxLeeches,
2016-07-04 05:36:06 -07:00
proxyServer: this.options.proxyServer,
preferredSeed: this.options.preferredSeed,
2016-07-29 15:40:39 -07:00
ignoreDiscovery: this.options.ignoreDiscovery,
2016-08-09 19:16:52 -07:00
port: this.options.port,
2016-03-10 02:40:33 -08:00
spv: false
});
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,
2016-04-04 18:45:02 -07:00
chain: this.chain,
mempool: this.mempool,
2016-07-04 05:36:06 -07:00
fees: this.fees,
2016-03-10 02:40:33 -08:00
address: this.options.payoutAddress,
2016-09-22 23:58:19 -07:00
coinbaseFlags: this.options.coinbaseFlags
2016-03-10 02:40:33 -08:00
});
2016-07-04 05:36:06 -07:00
// Wallet database needs access to fees.
2016-10-02 01:01:16 -07:00
this.walletdb = new WalletDB({
2016-05-13 09:23:57 -07:00
network: this.network,
2016-07-04 05:36:06 -07:00
logger: this.logger,
2016-11-19 21:40:31 -08:00
client: this.client,
2016-08-26 05:02:08 -07:00
db: this.options.db,
2016-07-04 05:36:06 -07:00
location: this.location('walletdb'),
witness: false,
useCheckpoints: this.options.useCheckpoints,
2016-08-24 07:44:06 -07:00
maxFiles: this.options.maxFiles,
2016-11-02 04:40:26 -07:00
startHeight: this.options.startHeight,
2016-10-22 13:34:28 -07:00
wipeNoReally: this.options.wipeNoReally,
2016-10-19 17:30:54 -07:00
resolution: false,
2016-04-04 18:45:02 -07:00
verify: false
});
2016-03-10 02:40:33 -08:00
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,
key: this.options.sslKey,
cert: this.options.sslCert,
port: this.options.httpPort || this.network.rpcPort,
2016-07-12 15:34:41 -07:00
host: this.options.httpHost || '0.0.0.0',
apiKey: this.options.apiKey,
2016-10-05 07:11:13 -07:00
serviceKey: this.options.serviceKey,
walletAuth: this.options.walletAuth,
noAuth: this.options.noAuth
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() {
var self = this;
2016-08-26 05:02:08 -07:00
var onError = this._error.bind(this);
2016-03-10 02:40:33 -08:00
// Bind to errors
2016-08-26 05:02:08 -07:00
this.chain.on('error', onError);
this.mempool.on('error', onError);
this.pool.on('error', onError);
this.miner.on('error', onError);
this.walletdb.on('error', onError);
2016-04-03 04:20:18 -07:00
2016-08-26 05:02:08 -07:00
if (this.http)
this.http.on('error', onError);
2016-03-10 02:40:33 -08:00
2016-08-26 05:02:08 -07:00
this.pool.on('alert', function(alert) {
self.emit('alert', alert);
2016-06-07 10:23:22 -07:00
});
2016-07-06 16:06:11 -07:00
this.mempool.on('tx', function(tx) {
self.emit('tx', tx);
2016-10-06 00:08:23 -07:00
self.miner.notifyEntry();
2016-03-25 20:02:23 -07:00
});
2016-03-10 02:40:33 -08:00
this.chain.on('block', function(block) {
self.emit('block', block);
});
2016-07-06 16:06:11 -07:00
this.chain.on('connect', function(entry, block) {
2016-11-19 21:40:31 -08:00
self.emit('connect', entry, block);
2016-03-10 02:40:33 -08:00
2016-08-26 05:02:08 -07:00
if (self.chain.synced)
2016-09-20 14:56:54 -07:00
self.mempool.addBlock(block).catch(onError);
2016-03-25 20:02:23 -07:00
});
2016-03-11 22:48:16 -08:00
2016-07-06 16:06:11 -07:00
this.chain.on('disconnect', function(entry, block) {
2016-11-19 21:40:31 -08:00
self.emit('disconnect', entry, block);
2016-06-13 08:23:32 -07:00
2016-08-26 05:02:08 -07:00
if (self.chain.synced)
2016-09-20 14:56:54 -07:00
self.mempool.removeBlock(block).catch(onError);
2016-03-25 20:02:23 -07:00
});
2016-03-10 02:40:33 -08:00
this.chain.on('reset', function(tip) {
2016-11-19 21:40:31 -08:00
self.emit('reset', tip);
});
2016-04-04 18:45:02 -07:00
this.miner.on('block', function(block) {
2016-10-06 02:07:53 -07:00
self.broadcast(block.toInv()).catch(onError);
2016-04-04 18:45:02 -07:00
});
};
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 = co(function* open() {
2016-09-21 22:58:27 -07:00
yield this.chain.open();
yield this.mempool.open();
yield this.miner.open();
yield this.pool.open();
2016-11-19 21:40:48 -08:00
if (this.http)
yield this.http.open();
2016-09-21 22:58:27 -07:00
yield this.walletdb.open();
2016-09-21 22:58:27 -07:00
// Ensure primary wallet.
yield this.openWallet();
2016-09-20 14:56:54 -07:00
if (this.options.listen)
yield this.pool.listen();
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 = co(function* close() {
2016-09-21 22:58:27 -07:00
if (this.http)
yield this.http.close();
2016-09-20 14:56:54 -07:00
2016-09-23 18:32:49 -07:00
yield this.wallet.destroy();
this.wallet = null;
yield this.walletdb.close();
yield this.pool.close();
yield this.miner.close();
yield this.mempool.close();
yield 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 = function broadcast(item, callback) {
2016-03-29 16:14:39 -07:00
return this.pool.broadcast(item, callback);
};
/**
* Verify a transaction, add it to the mempool, and broadcast.
* Safer than {@link FullNode#broadcast}.
* @example
* node.sendTX(tx, callback);
* node.sendTX(tx, true, callback);
2016-08-26 05:02:08 -07:00
* @param {TX} tx
*/
FullNode.prototype.sendTX = co(function* sendTX(tx) {
2016-09-21 22:58:27 -07:00
try {
yield this.mempool.addTX(tx);
} catch (err) {
if (err.type === 'VerifyError') {
this._error(err);
this.logger.warning('Verification failed for tx: %s.', tx.rhash);
this.logger.warning('Attempting to broadcast anyway...');
2016-10-02 17:45:45 -07:00
yield this.pool.broadcast(tx);
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
2016-09-21 22:58:27 -07:00
if (!this.options.selfish)
tx = tx.toInv();
2016-05-23 06:01:52 -07:00
2016-10-02 17:45:45 -07:00
yield this.pool.broadcast(tx);
2016-09-21 22:58:27 -07:00
});
2016-03-29 16:14:39 -07:00
2016-05-19 11:56:11 -07:00
/**
* Listen on a server socket on
* the p2p network (accepts leech peers).
*/
FullNode.prototype.listen = function listen() {
2016-09-20 14:56:54 -07:00
return this.pool.listen();
2016-05-19 11:56:11 -07:00
};
/**
* Connect to the network.
*/
FullNode.prototype.connect = function connect() {
2016-04-03 06:11:30 -07:00
return this.pool.connect();
};
/**
* 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 block from the chain database, filled with coins.
* @param {Hash} hash
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link Block}.
*/
FullNode.prototype.getFullBlock = function getFullBlock(hash) {
2016-09-20 14:56:54 -07:00
return this.chain.db.getFullBlock(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) {
2016-08-15 15:46:37 -07:00
var 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.
2016-08-26 05:02:08 -07:00
* @param {Address} addresses
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link Coin}[].
*/
FullNode.prototype.getCoinsByAddress = co(function* getCoinsByAddress(addresses) {
2016-09-21 22:58:27 -07:00
var coins = this.mempool.getCoinsByAddress(addresses);
var i, blockCoins, coin, spent;
2016-08-15 15:46:37 -07:00
2016-09-21 22:58:27 -07:00
blockCoins = yield this.chain.db.getCoinsByAddress(addresses);
2016-03-10 02:40:33 -08:00
2016-09-21 22:58:27 -07:00
for (i = 0; i < blockCoins.length; i++) {
coin = blockCoins[i];
spent = this.mempool.isSpent(coin.hash, coin.index);
2016-03-21 16:29:02 -07:00
2016-09-21 22:58:27 -07:00
if (!spent)
coins.push(coin);
}
2016-03-21 16:29:02 -07:00
2016-09-21 22:58:27 -07:00
return coins;
});
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.
* @param {Address} addresses
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link TX}[].
2016-08-26 05:02:08 -07:00
*/
FullNode.prototype.getTXByAddress = co(function* getTXByAddress(addresses) {
2016-09-21 22:58:27 -07:00
var mempool = this.mempool.getTXByAddress(addresses);
var txs = yield this.chain.db.getTXByAddress(addresses);
return mempool.concat(txs);
});
2016-08-26 05:02:08 -07:00
/**
* Retrieve a transaction from the mempool or chain database.
* @param {Hash} hash
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link TX}.
*/
FullNode.prototype.getTX = function getTX(hash) {
2016-08-15 15:46:37 -07:00
var tx = this.mempool.getTX(hash);
2016-03-10 02:40:33 -08:00
2016-08-15 15:46:37 -07:00
if (tx)
2016-09-20 14:56:54 -07:00
return Promise.resolve(tx);
2016-03-10 02:40:33 -08:00
2016-09-20 14:56:54 -07:00
return this.chain.db.getTX(hash);
2016-03-21 16:29:02 -07:00
};
/**
* 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 = function hasTX(hash) {
2016-08-15 15:46:37 -07:00
if (this.mempool.hasTX(hash))
2016-09-20 14:56:54 -07:00
return Promise.resolve(true);
2016-09-20 14:56:54 -07:00
return this.chain.db.hasTX(hash);
2016-03-10 02:40:33 -08:00
};
/**
* Check whether a coin has been spent.
* @param {Hash} hash
* @param {Number} index
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns Boolean.
*/
FullNode.prototype.isSpent = function isSpent(hash, index) {
2016-08-15 15:46:37 -07:00
if (this.mempool.isSpent(hash, index))
2016-09-20 14:56:54 -07:00
return Promise.resolve(true);
2016-03-10 02:40:33 -08:00
2016-09-20 14:56:54 -07:00
return this.chain.db.isSpent(hash, index);
2016-03-10 02:40:33 -08:00
};
/**
* Fill a transaction with coins from the mempool
* and chain database (unspent only).
* @param {TX} tx
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link TX}.
*/
FullNode.prototype.fillCoins = function fillCoins(tx) {
2016-09-20 14:56:54 -07:00
return this.mempool.fillAllCoins(tx);
2016-03-10 02:40:33 -08:00
};
/**
* Fill a transaction with all historical coins
* from the mempool and chain database.
* @param {TX} tx
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link TX}.
*/
FullNode.prototype.fillHistory = function fillHistory(tx) {
2016-09-20 14:56:54 -07:00
return this.mempool.fillAllHistory(tx);
2016-03-31 21:59:36 -07:00
};
2016-03-10 02:40:33 -08:00
/**
* Return bitcoinj-style confidence for a transaction.
* @param {Hash|TX} tx
2016-09-23 01:05:06 -07:00
* @returns {Promise} - Returns {@link Confidence}.
*/
FullNode.prototype.getConfidence = function getConfidence(tx) {
2016-09-20 14:56:54 -07:00
return this.mempool.getConfidence(tx);
2016-03-10 02:40:33 -08:00
};
2016-05-15 18:07:06 -07:00
/*
* Expose
*/
module.exports = FullNode;