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)
|
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-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');
|
2016-11-18 23:04:37 -08:00
|
|
|
var HTTPServer = require('../http/server');
|
2017-01-06 09:57:55 -08:00
|
|
|
var policy = require('../protocol/policy');
|
2016-03-10 02:40:33 -08:00
|
|
|
|
|
|
|
|
/**
|
2016-04-15 05:28:23 -07:00
|
|
|
* Create a fullnode complete with a chain,
|
|
|
|
|
* mempool, miner, wallet, etc.
|
2016-10-24 15:13:45 -07:00
|
|
|
* @exports FullNode
|
2016-04-15 05:28:23 -07:00
|
|
|
* @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
|
2016-04-15 05:28:23 -07:00
|
|
|
* @property {Mempool} mempool
|
|
|
|
|
* @property {Pool} pool
|
|
|
|
|
* @property {Miner} miner
|
|
|
|
|
* @property {WalletDB} walletdb
|
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
|
|
|
|
|
* @emits FullNode#alert
|
|
|
|
|
* @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
|
|
|
|
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,
|
2016-07-21 12:36:11 -07:00
|
|
|
useCheckpoints: this.options.useCheckpoints,
|
2016-08-04 13:44:45 -07:00
|
|
|
coinCache: this.options.coinCache,
|
|
|
|
|
indexTX: this.options.indexTX,
|
2016-08-24 07:44:06 -07:00
|
|
|
indexAddress: this.options.indexAddress,
|
2016-12-04 13:59:08 -08:00
|
|
|
maxFiles: this.options.maxFiles,
|
|
|
|
|
cacheSize: this.options.cacheSize
|
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(
|
2017-01-06 09:57:55 -08:00
|
|
|
policy.MIN_RELAY,
|
2016-07-04 05:36:06 -07:00
|
|
|
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,
|
2016-08-04 13:44:45 -07:00
|
|
|
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,
|
2016-04-03 21:43:50 -07:00
|
|
|
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,
|
2016-08-23 05:28:45 -07:00
|
|
|
authPeers: this.options.authPeers,
|
|
|
|
|
knownPeers: this.options.knownPeers,
|
|
|
|
|
identityKey: this.options.identityKey,
|
2016-11-28 18:52:02 -08:00
|
|
|
maxOutbound: this.options.maxOutbound,
|
|
|
|
|
maxInbound: this.options.maxInbound,
|
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'),
|
2016-11-07 23:51:48 -08:00
|
|
|
witness: false,
|
2016-07-21 12:36:11 -07:00
|
|
|
useCheckpoints: this.options.useCheckpoints,
|
2016-12-04 13:59:08 -08:00
|
|
|
maxFiles: this.options.walletMaxFiles,
|
|
|
|
|
cacheSize: this.options.walletCacheSize,
|
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.
|
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,
|
|
|
|
|
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,
|
2016-07-26 18:08:36 -07:00
|
|
|
walletAuth: this.options.walletAuth,
|
2016-07-30 20:39:13 -07:00
|
|
|
noAuth: this.options.noAuth
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-19 10:45:31 -08:00
|
|
|
util.inherits(FullNode, Node);
|
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-06-30 17:29:00 -07:00
|
|
|
var self = this;
|
2016-08-26 05:02:08 -07:00
|
|
|
var onError = this._error.bind(this);
|
2016-06-30 17:29:00 -07:00
|
|
|
|
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) {
|
2016-10-06 00:08:23 -07:00
|
|
|
self.miner.notifyEntry();
|
2016-12-14 12:03:47 -08:00
|
|
|
self.emit('tx', tx);
|
2016-03-25 20:02:23 -07:00
|
|
|
});
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2016-12-14 12:03:47 -08:00
|
|
|
this.chain.on('connect', co(function* (entry, block) {
|
|
|
|
|
if (self.chain.synced) {
|
|
|
|
|
try {
|
|
|
|
|
yield self.mempool.addBlock(entry, block.txs);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
self._error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-10 02:40:33 -08:00
|
|
|
self.emit('block', block);
|
2016-11-19 21:40:31 -08:00
|
|
|
self.emit('connect', entry, block);
|
2016-12-14 12:03:47 -08:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
this.chain.on('disconnect', co(function* (entry, block) {
|
|
|
|
|
if (self.chain.synced) {
|
|
|
|
|
try {
|
|
|
|
|
yield self.mempool.removeBlock(entry, block.txs);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
self._error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-19 21:40:31 -08:00
|
|
|
self.emit('disconnect', entry, block);
|
2016-12-14 12:03:47 -08:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
this.chain.on('reset', co(function* (tip) {
|
|
|
|
|
try {
|
|
|
|
|
yield self.mempool.reset();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
self._error(e);
|
|
|
|
|
}
|
2016-11-19 21:40:31 -08:00
|
|
|
self.emit('reset', tip);
|
2016-12-14 12:03:47 -08:00
|
|
|
}));
|
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
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
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-03-10 03:23:21 -08:00
|
|
|
|
2016-09-21 22:58:27 -07:00
|
|
|
// Ensure primary wallet.
|
|
|
|
|
yield this.openWallet();
|
2016-09-20 14:56:54 -07:00
|
|
|
|
2016-10-24 19:37:07 -07:00
|
|
|
if (this.options.listen)
|
|
|
|
|
yield this.pool.listen();
|
|
|
|
|
|
2016-09-21 22:58:27 -07:00
|
|
|
this.logger.info('Node is loaded.');
|
|
|
|
|
});
|
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
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
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-06-30 17:29:00 -07:00
|
|
|
|
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
|
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
|
|
|
*/
|
|
|
|
|
|
2016-12-17 13:50:45 -08:00
|
|
|
FullNode.prototype.broadcast = co(function* broadcast(item) {
|
|
|
|
|
try {
|
|
|
|
|
yield this.pool.broadcast(item);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.emit('error', e);
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-03-29 16:14:39 -07:00
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* Verify a transaction, add it to the mempool, and broadcast.
|
2016-10-24 15:13:45 -07:00
|
|
|
* Safer than {@link FullNode#broadcast}.
|
2016-04-15 05:28:23 -07:00
|
|
|
* @example
|
|
|
|
|
* node.sendTX(tx, callback);
|
|
|
|
|
* node.sendTX(tx, true, callback);
|
2016-08-26 05:02:08 -07:00
|
|
|
* @param {TX} tx
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
FullNode.prototype.sendTX = co(function* sendTX(tx) {
|
2016-12-17 13:50:45 -08:00
|
|
|
var missing;
|
|
|
|
|
|
2016-09-21 22:58:27 -07:00
|
|
|
try {
|
2016-12-17 13:50:45 -08:00
|
|
|
missing = yield 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) {
|
2016-09-21 22:58:27 -07: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.
|
2016-12-17 00:13:51 -08:00
|
|
|
if (this.options.selfish)
|
|
|
|
|
this.pool.announceTX(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).
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* Connect to the network.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
FullNode.prototype.connect = function connect() {
|
2016-04-03 06:11:30 -07:00
|
|
|
return this.pool.connect();
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
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))
|
2016-10-09 23:52:52 -07:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
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.
|
2016-08-26 05:02:08 -07:00
|
|
|
* @param {Address} addresses
|
2016-09-23 01:05:06 -07:00
|
|
|
* @returns {Promise} - Returns {@link Coin}[].
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-10-24 15:13:45 -07:00
|
|
|
FullNode.prototype.getCoinsByAddress = co(function* getCoinsByAddress(addresses) {
|
2016-12-11 05:34:12 -08:00
|
|
|
var mempool = this.mempool.getCoinsByAddress(addresses);
|
|
|
|
|
var chain = yield this.chain.db.getCoinsByAddress(addresses);
|
|
|
|
|
var out = [];
|
|
|
|
|
var i, coin, spent;
|
2016-08-15 15:46:37 -07:00
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
for (i = 0; i < chain.length; i++) {
|
|
|
|
|
coin = chain[i];
|
2016-09-21 22:58:27 -07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < mempool.length; i++) {
|
|
|
|
|
coin = mempool[i];
|
|
|
|
|
out.push(coin);
|
2016-09-21 22:58:27 -07:00
|
|
|
}
|
2016-03-21 16:29:02 -07:00
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
return out;
|
2016-09-21 22:58:27 -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.
|
|
|
|
|
* @param {Address} addresses
|
2016-12-11 05:34:12 -08:00
|
|
|
* @returns {Promise} - Returns {@link TXMeta}[].
|
2016-08-26 05:02:08 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
FullNode.prototype.getMetaByAddress = co(function* getTXByAddress(addresses) {
|
|
|
|
|
var mempool = this.mempool.getMetaByAddress(addresses);
|
|
|
|
|
var chain = yield this.chain.db.getMetaByAddress(addresses);
|
|
|
|
|
return chain.concat(mempool);
|
2016-09-21 22:58:27 -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
|
|
|
*/
|
|
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
FullNode.prototype.getMeta = co(function* getMeta(hash) {
|
|
|
|
|
var 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
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
return yield this.chain.db.getMeta(hash);
|
|
|
|
|
});
|
2016-03-21 16:29:02 -07: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.
|
|
|
|
|
* @param {Address} addresses
|
|
|
|
|
* @returns {Promise} - Returns {@link TX}[].
|
2016-04-15 05:28:23 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
FullNode.prototype.getTXByAddress = co(function* getTXByAddress(addresses) {
|
|
|
|
|
var mtxs = yield this.getMetaByAddress(addresses);
|
|
|
|
|
var out = [];
|
|
|
|
|
var i, mtx;
|
2016-04-01 01:37:52 -07:00
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
for (i = 0; i < mtxs.length; i++) {
|
|
|
|
|
mtx = mtxs[i];
|
|
|
|
|
out.push(mtx.tx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
});
|
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}.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
FullNode.prototype.getTX = co(function* getTX(hash) {
|
|
|
|
|
var mtx = yield this.getMeta(hash);
|
|
|
|
|
if (!mtx)
|
|
|
|
|
return;
|
|
|
|
|
return mtx.tx;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
FullNode.prototype.hasTX = function hasTX(hash) {
|
|
|
|
|
if (this.mempool.hasTX(hash))
|
2016-09-20 14:56:54 -07:00
|
|
|
return Promise.resolve(true);
|
2016-03-10 02:40:33 -08:00
|
|
|
|
2016-12-11 05:34:12 -08:00
|
|
|
return 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;
|