2016-04-15 05:28:23 -07:00
|
|
|
/*!
|
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 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');
|
2017-08-06 19:44:57 -07:00
|
|
|
const Node = require('./node');
|
2016-03-10 02:40:33 -08:00
|
|
|
|
|
|
|
|
/**
|
2017-02-07 14:17:41 -08:00
|
|
|
* Respresents a fullnode complete with a
|
2017-03-08 16:54:04 -08:00
|
|
|
* chain, mempool, miner, etc.
|
2017-02-03 22:47:26 -08:00
|
|
|
* @alias module:node.FullNode
|
2016-04-15 05:28:23 -07:00
|
|
|
* @extends Node
|
|
|
|
|
* @constructor
|
|
|
|
|
* @param {Object?} options
|
|
|
|
|
* @property {Chain} chain
|
2016-07-04 05:36:06 -07:00
|
|
|
* @property {PolicyEstimator} fees
|
2016-04-15 05:28:23 -07:00
|
|
|
* @property {Mempool} mempool
|
|
|
|
|
* @property {Pool} pool
|
|
|
|
|
* @property {Miner} miner
|
2016-04-16 06:59:32 -07:00
|
|
|
* @property {HTTPServer} http
|
2016-10-24 15:13:45 -07:00
|
|
|
* @emits FullNode#block
|
|
|
|
|
* @emits FullNode#tx
|
2017-02-07 14:17:41 -08:00
|
|
|
* @emits FullNode#connect
|
|
|
|
|
* @emits FullNode#disconnect
|
|
|
|
|
* @emits FullNode#reset
|
2016-10-24 15:13:45 -07:00
|
|
|
* @emits FullNode#error
|
2016-03-10 02:40:33 -08:00
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
function FullNode(options) {
|
|
|
|
|
if (!(this instanceof FullNode))
|
|
|
|
|
return new FullNode(options);
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2016-06-30 17:29:00 -07: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,
|
2017-03-08 07:18:53 -08:00
|
|
|
db: this.config.str('db'),
|
|
|
|
|
prefix: this.config.prefix,
|
2017-08-06 03:23:37 -07:00
|
|
|
maxFiles: this.config.uint('max-files'),
|
2017-03-08 07:18:53 -08:00
|
|
|
cacheSize: this.config.mb('cache-size'),
|
2017-07-21 11:42:09 -07:00
|
|
|
forceFlags: this.config.bool('force-flags'),
|
|
|
|
|
bip91: this.config.bool('bip91'),
|
|
|
|
|
bip148: this.config.bool('bip148'),
|
2017-03-08 07:18:53 -08:00
|
|
|
prune: this.config.bool('prune'),
|
|
|
|
|
checkpoints: this.config.bool('checkpoints'),
|
|
|
|
|
coinCache: this.config.mb('coin-cache'),
|
2017-08-06 03:23:37 -07:00
|
|
|
entryCache: this.config.uint('entry-cache'),
|
2017-03-08 07:18:53 -08:00
|
|
|
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);
|
2017-02-28 17:03:06 -08:00
|
|
|
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,
|
2017-03-08 07:18:53 -08:00
|
|
|
db: this.config.str('db'),
|
|
|
|
|
prefix: this.config.prefix,
|
|
|
|
|
persistent: this.config.bool('persistent-mempool'),
|
2017-04-12 08:23:19 -07:00
|
|
|
maxSize: this.config.mb('mempool-size'),
|
2017-03-08 07:18:53 -08:00
|
|
|
limitFree: this.config.bool('limit-free'),
|
2017-08-06 03:23:37 -07:00
|
|
|
limitFreeRelay: this.config.uint('limit-free-relay'),
|
2017-03-08 07:18:53 -08:00
|
|
|
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,
|
2017-03-08 07:18:53 -08:00
|
|
|
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'),
|
2017-08-06 03:23:37 -07:00
|
|
|
maxOutbound: this.config.uint('max-outbound'),
|
|
|
|
|
maxInbound: this.config.uint('max-inbound'),
|
2017-03-08 07:18:53 -08:00
|
|
|
proxy: this.config.str('proxy'),
|
|
|
|
|
onion: this.config.bool('onion'),
|
|
|
|
|
upnp: this.config.bool('upnp'),
|
2017-03-09 22:07:31 -08:00
|
|
|
seeds: this.config.array('seeds'),
|
|
|
|
|
nodes: this.config.array('nodes'),
|
2017-05-12 12:02:55 -07:00
|
|
|
only: this.config.array('only'),
|
2017-03-08 07:18:53 -08:00
|
|
|
publicHost: this.config.str('public-host'),
|
2017-08-06 03:23:37 -07:00
|
|
|
publicPort: this.config.uint('public-port'),
|
2017-03-08 07:18:53 -08:00
|
|
|
host: this.config.str('host'),
|
2017-08-06 03:23:37 -07:00
|
|
|
port: this.config.uint('port'),
|
2017-03-14 05:47:12 -07:00
|
|
|
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,
|
2017-03-10 20:25:00 -08:00
|
|
|
address: this.config.array('coinbase-address'),
|
2017-03-08 07:18:53 -08:00
|
|
|
coinbaseFlags: this.config.str('coinbase-flags'),
|
|
|
|
|
preverify: this.config.bool('preverify'),
|
2017-08-06 03:23:37 -07:00
|
|
|
maxWeight: this.config.uint('max-weight'),
|
|
|
|
|
reservedWeight: this.config.uint('reserved-weight'),
|
|
|
|
|
reservedSigops: this.config.uint('reserved-sigops')
|
2016-03-10 02:40:33 -08:00
|
|
|
});
|
|
|
|
|
|
2017-03-10 20:25:00 -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.
|
2016-11-18 23:04:37 -08:00
|
|
|
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,
|
2017-03-08 07:18:53 -08:00
|
|
|
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'),
|
2017-08-06 03:23:37 -07:00
|
|
|
port: this.config.uint('http-port'),
|
2017-03-08 07:18:53 -08:00
|
|
|
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
|
|
|
|
2016-06-30 17:29:00 -07:00
|
|
|
this._init();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 19:44:57 -07:00
|
|
|
Object.setPrototypeOf(FullNode.prototype, Node.prototype);
|
2016-06-30 17:29:00 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the node.
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
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) => {
|
2017-03-05 13:01:30 -08:00
|
|
|
try {
|
2017-06-29 20:54:07 -07:00
|
|
|
await this.mempool._addBlock(entry, block.txs);
|
2017-03-05 13:01:30 -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('block', block);
|
|
|
|
|
this.emit('connect', entry, block);
|
2017-06-24 02:37:55 -07:00
|
|
|
});
|
2016-12-14 12:03:47 -08:00
|
|
|
|
2017-06-29 20:54:07 -07:00
|
|
|
this.chain.hook('disconnect', async (entry, block) => {
|
2017-03-05 13:01:30 -08:00
|
|
|
try {
|
2017-06-29 20:54:07 -07:00
|
|
|
await this.mempool._removeBlock(entry, block.txs);
|
2017-03-05 13:01:30 -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('disconnect', entry, block);
|
2017-06-24 02:37:55 -07:00
|
|
|
});
|
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);
|
2017-06-24 02:37:55 -07:00
|
|
|
});
|
2017-03-08 16:54:04 -08:00
|
|
|
|
|
|
|
|
this.loadPlugins();
|
2016-06-30 17:29:00 -07:00
|
|
|
};
|
2016-04-04 18:45:02 -07:00
|
|
|
|
2016-06-30 17:29:00 -07:00
|
|
|
/**
|
|
|
|
|
* Open the node and all its child objects,
|
|
|
|
|
* wait for the database to load.
|
2016-10-24 15:13:45 -07:00
|
|
|
* @alias FullNode#open
|
2016-09-23 01:05:06 -07:00
|
|
|
* @returns {Promise}
|
2016-06-30 17:29:00 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-07-30 16:49:24 -07:00
|
|
|
FullNode.prototype._open = async function _open() {
|
2017-06-24 02:37:55 -07:00
|
|
|
await this.chain.open();
|
|
|
|
|
await this.mempool.open();
|
|
|
|
|
await this.miner.open();
|
|
|
|
|
await this.pool.open();
|
2016-03-10 03:23:21 -08:00
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
await this.openPlugins();
|
2016-09-20 14:56:54 -07:00
|
|
|
|
2017-02-28 14:10:45 -08:00
|
|
|
if (this.http)
|
2017-06-24 02:37:55 -07:00
|
|
|
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.');
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
2016-06-30 17:29:00 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close the node, wait for the database to close.
|
2016-10-24 15:13:45 -07:00
|
|
|
* @alias FullNode#close
|
2016-09-23 01:05:06 -07:00
|
|
|
* @returns {Promise}
|
2016-06-30 17:29:00 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-07-30 16:49:24 -07:00
|
|
|
FullNode.prototype._close = async function _close() {
|
2016-09-21 22:58:27 -07:00
|
|
|
if (this.http)
|
2017-06-24 02:37:55 -07:00
|
|
|
await this.http.close();
|
2016-09-20 14:56:54 -07:00
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
await this.closePlugins();
|
2016-09-23 18:32:49 -07:00
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
await this.pool.close();
|
|
|
|
|
await this.miner.close();
|
|
|
|
|
await this.mempool.close();
|
|
|
|
|
await this.chain.close();
|
2016-06-30 17:29:00 -07:00
|
|
|
|
2016-09-21 22:58:27 -07:00
|
|
|
this.logger.info('Node is closed.');
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
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
|
2016-10-24 15:13:45 -07:00
|
|
|
* @param {Function} iter - Iterator.
|
|
|
|
|
* @returns {Promise}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
FullNode.prototype.scan = function scan(start, filter, iter) {
|
2016-11-14 15:45:32 -08:00
|
|
|
return this.chain.scan(start, filter, iter);
|
2016-10-24 15:13:45 -07:00
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* 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}
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
FullNode.prototype.broadcast = async function broadcast(item) {
|
2016-12-17 13:50:45 -08:00
|
|
|
try {
|
2017-06-24 02:37:55 -07:00
|
|
|
await this.pool.broadcast(item);
|
2016-12-17 13:50:45 -08:00
|
|
|
} catch (e) {
|
|
|
|
|
this.emit('error', e);
|
|
|
|
|
}
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
2016-03-29 16:14:39 -07:00
|
|
|
|
2016-04-15 05:28:23 -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
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
FullNode.prototype.sendTX = async function sendTX(tx) {
|
2017-06-29 20:54:07 -07:00
|
|
|
let missing;
|
2016-12-17 13:50:45 -08:00
|
|
|
|
2016-09-21 22:58:27 -07:00
|
|
|
try {
|
2017-06-24 02:37:55 -07:00
|
|
|
missing = await this.mempool.addTX(tx);
|
2016-09-21 22:58:27 -07:00
|
|
|
} catch (err) {
|
2016-12-17 13:50:45 -08:00
|
|
|
if (err.type === 'VerifyError' && err.score === 0) {
|
2017-01-14 19:21:46 -08:00
|
|
|
this.error(err);
|
2016-11-30 21:31:52 -08:00
|
|
|
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...');
|
2016-12-17 13:50:45 -08:00
|
|
|
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
|
|
|
|
2016-12-17 13:50:45 -08: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);
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
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}
|
|
|
|
|
*/
|
|
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
FullNode.prototype.relay = async function relay(tx) {
|
2017-01-14 19:21:46 -08:00
|
|
|
try {
|
2017-06-24 02:37:55 -07:00
|
|
|
await this.sendTX(tx);
|
2017-01-14 19:21:46 -08:00
|
|
|
} catch (e) {
|
|
|
|
|
this.error(e);
|
|
|
|
|
}
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
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
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
2017-01-14 07:54:07 -08:00
|
|
|
* Disconnect from the network.
|
2017-01-14 07:18:47 -08:00
|
|
|
* @returns {Promise}
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* Start the blockchain sync.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
FullNode.prototype.startSync = function startSync() {
|
2016-03-22 17:36:58 -07:00
|
|
|
return this.pool.startSync();
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* Stop syncing the blockchain.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
FullNode.prototype.stopSync = function stopSync() {
|
2016-03-22 17:36:58 -07:00
|
|
|
return this.pool.stopSync();
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* Retrieve a block from the chain database.
|
|
|
|
|
* @param {Hash} hash
|
2016-09-23 01:05:06 -07:00
|
|
|
* @returns {Promise} - Returns {@link Block}.
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07: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}.
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-07-30 10:41:58 -07:00
|
|
|
FullNode.prototype.getCoin = async function getCoin(hash, index) {
|
2017-07-27 09:27:00 -07:00
|
|
|
const coin = this.mempool.getCoin(hash, index);
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2016-08-15 15:46:37 -07:00
|
|
|
if (coin)
|
2017-07-30 10:41:58 -07:00
|
|
|
return coin;
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2016-08-15 15:46:37 -07:00
|
|
|
if (this.mempool.isSpent(hash, index))
|
2017-07-30 10:41:58 -07:00
|
|
|
return null;
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2017-07-30 10:41:58 -07:00
|
|
|
return await this.chain.db.getCoin(hash, index);
|
2016-03-10 02:40:33 -08:00
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07: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}[].
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-06-29 04:30:14 -07:00
|
|
|
FullNode.prototype.getCoinsByAddress = async function getCoinsByAddress(addrs) {
|
2017-07-27 09:27:00 -07:00
|
|
|
const mempool = this.mempool.getCoinsByAddress(addrs);
|
|
|
|
|
const chain = await this.chain.db.getCoinsByAddress(addrs);
|
|
|
|
|
const out = [];
|
2016-08-15 15:46:37 -07:00
|
|
|
|
2017-07-27 09:27:00 -07:00
|
|
|
for (const coin of chain) {
|
|
|
|
|
const spent = this.mempool.isSpent(coin.hash, coin.index);
|
2016-03-21 16:29:02 -07:00
|
|
|
|
2016-12-08 15:51:25 -08:00
|
|
|
if (spent)
|
|
|
|
|
continue;
|
|
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
out.push(coin);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 09:27:00 -07:00
|
|
|
for (const coin of mempool)
|
2016-12-11 05:34:12 -08:00
|
|
|
out.push(coin);
|
2016-03-21 16:29:02 -07:00
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
return out;
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
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
|
2016-12-11 05:34:12 -08:00
|
|
|
* @returns {Promise} - Returns {@link TXMeta}[].
|
2016-08-26 05:02:08 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-07-30 16:49:24 -07:00
|
|
|
FullNode.prototype.getMetaByAddress = async function getMetaByAddress(addrs) {
|
2017-07-27 09:27:00 -07:00
|
|
|
const mempool = this.mempool.getMetaByAddress(addrs);
|
|
|
|
|
const chain = await this.chain.db.getMetaByAddress(addrs);
|
2016-12-11 05:34:12 -08:00
|
|
|
return chain.concat(mempool);
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
2016-08-26 05:02:08 -07:00
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* Retrieve a transaction from the mempool or chain database.
|
|
|
|
|
* @param {Hash} hash
|
2016-12-11 05:34:12 -08:00
|
|
|
* @returns {Promise} - Returns {@link TXMeta}.
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
FullNode.prototype.getMeta = async function getMeta(hash) {
|
2017-07-27 09:27:00 -07:00
|
|
|
const meta = this.mempool.getMeta(hash);
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
if (meta)
|
|
|
|
|
return meta;
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
return await this.chain.db.getMeta(hash);
|
|
|
|
|
};
|
2016-03-21 16:29:02 -07:00
|
|
|
|
2017-02-28 22:41:21 -08:00
|
|
|
/**
|
|
|
|
|
* Retrieve a spent coin viewpoint from mempool or chain database.
|
|
|
|
|
* @param {TXMeta} meta
|
|
|
|
|
* @returns {Promise} - Returns {@link CoinView}.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
FullNode.prototype.getMetaView = async function getMetaView(meta) {
|
2017-02-28 22:41:21 -08:00
|
|
|
if (meta.height === -1)
|
|
|
|
|
return this.mempool.getSpentView(meta.tx);
|
|
|
|
|
return this.chain.getSpentView(meta.tx);
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
2017-02-28 22:41:21 -08:00
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
2016-12-11 05:34:12 -08:00
|
|
|
* Retrieve transactions pertaining to an
|
|
|
|
|
* address from the mempool or chain database.
|
2017-06-29 04:30:14 -07:00
|
|
|
* @param {Address} addrs
|
2016-12-11 05:34:12 -08:00
|
|
|
* @returns {Promise} - Returns {@link TX}[].
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-06-29 04:30:14 -07:00
|
|
|
FullNode.prototype.getTXByAddress = async function getTXByAddress(addrs) {
|
2017-07-27 09:27:00 -07:00
|
|
|
const mtxs = await this.getMetaByAddress(addrs);
|
|
|
|
|
const out = [];
|
2016-04-01 01:37:52 -07:00
|
|
|
|
2017-07-27 09:27:00 -07:00
|
|
|
for (const mtx of mtxs)
|
2016-12-11 05:34:12 -08:00
|
|
|
out.push(mtx.tx);
|
|
|
|
|
|
|
|
|
|
return out;
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
2016-12-11 05:34:12 -08:00
|
|
|
* Retrieve a transaction from the mempool or chain database.
|
|
|
|
|
* @param {Hash} hash
|
|
|
|
|
* @returns {Promise} - Returns {@link TX}.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
FullNode.prototype.getTX = async function getTX(hash) {
|
2017-07-27 09:27:00 -07:00
|
|
|
const mtx = await this.getMeta(hash);
|
2017-07-30 10:41:58 -07:00
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
if (!mtx)
|
2017-07-30 10:41:58 -07:00
|
|
|
return null;
|
|
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
return mtx.tx;
|
2017-06-24 02:37:55 -07:00
|
|
|
};
|
2016-12-11 05:34:12 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test whether the mempool or chain contains a transaction.
|
2016-04-15 05:28:23 -07:00
|
|
|
* @param {Hash} hash
|
2016-09-23 01:05:06 -07:00
|
|
|
* @returns {Promise} - Returns Boolean.
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-06-24 02:37:55 -07:00
|
|
|
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
|
|
|
|
2017-06-24 02:37:55 -07: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
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
module.exports = FullNode;
|