itns-sidechain/lib/node/spvnode.js

313 lines
6.8 KiB
JavaScript
Raw Normal View History

/*!
* spvnode.js - spv 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-06-13 01:06:01 -07:00
'use strict';
2016-08-23 23:26:50 -07:00
var utils = require('../utils/utils');
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');
var Chain = require('../chain/chain');
var Pool = require('../net/pool');
var WalletDB = require('../wallet/walletdb');
2016-10-03 01:45:06 -07:00
var HTTPServer;
try {
HTTPServer = require('../http/server');
} catch (e) {
;
}
/**
* Create an spv node which only maintains
* a chain, a pool, and a wallet database.
* @exports SPVNode
* @extends Node
* @constructor
* @param {Object?} options
* @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
* @property {Pool} pool
* @property {WalletDB} walletdb
2016-04-16 06:59:32 -07:00
* @property {HTTPServer} http
* @emits SPVNode#block
* @emits SPVNode#tx
2016-08-26 05:02:08 -07:00
* @emits SPVNode#alert
* @emits SPVNode#error
*/
function SPVNode(options) {
if (!(this instanceof SPVNode))
return new SPVNode(options);
Node.call(this, options);
2016-04-03 04:11:26 -07:00
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('spvchain'),
2016-07-29 15:40:39 -07:00
witness: this.options.witness,
2016-04-04 18:45:02 -07:00
useCheckpoints: this.options.useCheckpoints,
2016-08-24 07:44:06 -07:00
maxFiles: this.options.maxFiles,
2016-04-04 18:45:02 -07:00
spv: true
});
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,
2016-07-29 15:40:39 -07:00
witness: this.options.witness,
2016-07-04 05:36:06 -07:00
proxyServer: this.options.proxyServer,
preferredSeed: this.options.preferredSeed,
bip151: this.options.bip151,
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,
2016-07-29 15:40:39 -07:00
ignoreDiscovery: this.options.ignoreDiscovery,
selfish: true,
spv: true
});
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-08-26 05:02:08 -07:00
db: this.options.db,
2016-07-04 05:36:06 -07:00
location: this.location('walletdb'),
2016-07-29 15:40:39 -07:00
witness: this.options.witness,
2016-08-24 07:44:06 -07:00
maxFiles: this.options.maxFiles,
2016-04-04 18:45:02 -07:00
verify: true
});
2016-02-20 17:54:51 -08:00
2016-06-05 06:55:35 -07:00
if (!utils.isBrowser) {
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,
walletAuth: this.options.walletAuth,
noAuth: this.options.noAuth
2016-06-05 06:55:35 -07:00
});
}
2016-04-03 01:21:38 -07:00
this._init();
}
utils.inherits(SPVNode, Node);
/**
* Initialize the node.
* @private
*/
SPVNode.prototype._init = function _init() {
var self = this;
2016-08-26 05:02:08 -07:00
var onError = this._error.bind(this);
2016-04-03 01:21:38 -07:00
// Bind to errors
2016-08-26 05:02:08 -07:00
this.chain.on('error', onError);
this.pool.on('error', onError);
this.walletdb.on('error', onError);
2016-08-26 05:02:08 -07:00
if (this.http)
this.http.on('error', onError);
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.pool.on('tx', function(tx) {
self.emit('tx', tx);
2016-09-20 14:56:54 -07:00
self.walletdb.addTX(tx).catch(onError);
});
2016-07-14 15:54:17 -07:00
this.chain.on('block', function(block, entry) {
2016-04-03 01:21:38 -07:00
self.emit('block', block);
2016-09-20 14:56:54 -07:00
self.walletdb.addBlock(entry, block.txs).catch(onError);
2016-04-03 01:21:38 -07:00
});
2016-10-02 02:24:51 -07:00
this.walletdb.on('path', function(path) {
self.pool.watch(path.hash, 'hex');
2016-06-02 16:26:25 -07:00
});
2016-08-05 13:38:27 -07:00
this.walletdb.on('send', function(tx) {
2016-09-20 14:56:54 -07:00
self.sendTX(tx).catch(onError);
2016-08-05 13:38:27 -07:00
});
};
2016-06-02 16:26:25 -07:00
/**
* Open the node and all its child objects,
* wait for the database to load.
* @alias SPVNode#open
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
2016-09-21 22:59:48 -07:00
SPVNode.prototype._open = co(function* open(callback) {
2016-09-21 22:58:27 -07:00
yield this.chain.open();
yield this.pool.open();
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
2016-09-21 22:58:27 -07:00
// Load bloom filter.
yield this.openFilter();
2016-09-20 14:56:54 -07:00
2016-09-21 22:58:27 -07:00
// Rescan for any missed transactions.
yield this.rescan();
2016-09-20 14:56:54 -07:00
2016-09-21 22:58:27 -07:00
// Rebroadcast pending transactions.
yield this.resend();
2016-09-20 14:56:54 -07:00
2016-09-21 22:58:27 -07:00
if (this.http)
yield this.http.open();
2016-09-20 14:56:54 -07: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 SPVNode#close
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
2016-09-21 22:59:48 -07:00
SPVNode.prototype._close = co(function* close() {
2016-09-21 22:58:27 -07:00
if (this.http)
yield this.http.close();
2016-09-23 18:32:49 -07:00
yield this.wallet.destroy();
this.wallet = null;
2016-09-21 22:58:27 -07:00
yield this.walletdb.close();
yield this.pool.close();
yield this.chain.close();
});
/**
* Initialize p2p bloom filter for address watching.
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
2016-09-21 22:59:48 -07:00
SPVNode.prototype.openFilter = co(function* openFilter() {
2016-09-21 22:58:27 -07:00
var hashes = yield this.walletdb.getAddressHashes();
var i;
2016-09-21 22:58:27 -07:00
if (hashes.length > 0)
this.logger.info('Adding %d addresses to filter.', hashes.length);
2016-09-21 22:58:27 -07:00
for (i = 0; i < hashes.length; i++)
this.pool.watch(hashes[i], 'hex');
});
/**
* Rescan for any missed transactions.
* Note that this will replay the blockchain sync.
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
2016-09-20 14:56:54 -07:00
SPVNode.prototype.rescan = function rescan() {
if (this.options.noScan) {
2016-09-20 14:56:54 -07:00
return this.walletdb.setTip(
this.chain.tip.hash,
2016-09-20 14:56:54 -07:00
this.chain.height);
}
// Always replay the last block to make
// sure we didn't miss anything: there
// is no atomicity between the chaindb
// and walletdb.
2016-10-05 04:29:10 -07:00
return this.scan();
};
2016-10-05 04:29:10 -07:00
/**
* Scan for any missed transactions.
* Note that this will replay the blockchain sync.
* @param {Number|Hash} height
* @returns {Promise}
*/
SPVNode.prototype.scan = co(function* scan(height) {
if (height == null)
height = this.walletdb.height;
if (typeof height === 'string') {
height = yield this.chain.db.getHeight(height);
if (height === -1)
return;
}
if (height === 0)
return;
yield this.chain.reset(height - 1);
});
/**
* 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-09-20 14:56:54 -07:00
SPVNode.prototype.broadcast = function broadcast(item) {
return this.pool.broadcast(item);
2016-04-03 01:21:38 -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} tx
2016-09-23 01:05:06 -07:00
* @returns {Promise}
*/
2016-09-20 14:56:54 -07:00
SPVNode.prototype.sendTX = function sendTX(tx) {
return this.pool.broadcast(tx);
2016-04-03 01:21:38 -07:00
};
/**
* Connect to the network.
*/
2016-04-03 06:11:30 -07:00
SPVNode.prototype.connect = function connect() {
return this.pool.connect();
};
/**
* Start the blockchain sync.
*/
2016-04-03 01:21:38 -07:00
SPVNode.prototype.startSync = function startSync() {
return this.pool.startSync();
};
/**
* Stop syncing the blockchain.
*/
2016-04-03 01:21:38 -07:00
SPVNode.prototype.stopSync = function stopSync() {
return this.pool.stopSync();
};
2016-05-15 18:07:06 -07:00
/*
* Expose
*/
2016-05-13 09:23:57 -07:00
module.exports = SPVNode;