2016-04-15 05:28:23 -07:00
|
|
|
/*!
|
2016-02-20 16:48:07 -08:00
|
|
|
* 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-02-20 16:48:07 -08:00
|
|
|
*/
|
|
|
|
|
|
2016-06-13 01:06:01 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-08-23 23:26:50 -07:00
|
|
|
var bcoin = require('../env');
|
|
|
|
|
var utils = require('../utils/utils');
|
2016-06-30 17:29:00 -07:00
|
|
|
var Node = bcoin.node;
|
2016-02-20 16:48:07 -08:00
|
|
|
|
|
|
|
|
/**
|
2016-04-15 05:28:23 -07:00
|
|
|
* 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
|
2016-04-15 05:28:23 -07:00
|
|
|
* @emits SPVNode#block
|
|
|
|
|
* @emits SPVNode#tx
|
2016-08-26 05:02:08 -07:00
|
|
|
* @emits SPVNode#alert
|
2016-04-15 05:28:23 -07:00
|
|
|
* @emits SPVNode#error
|
2016-02-20 16:48:07 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function SPVNode(options) {
|
|
|
|
|
if (!(this instanceof SPVNode))
|
|
|
|
|
return new SPVNode(options);
|
|
|
|
|
|
2016-06-30 17:29:00 -07:00
|
|
|
Node.call(this, options);
|
2016-04-03 04:11:26 -07:00
|
|
|
|
2016-04-04 18:45:02 -07:00
|
|
|
this.chain = new bcoin.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-03-10 03:23:21 -08:00
|
|
|
});
|
2016-02-20 16:48:07 -08:00
|
|
|
|
2016-04-04 18:45:02 -07:00
|
|
|
this.pool = new bcoin.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,
|
2016-08-23 05:28:45 -07:00
|
|
|
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,
|
2016-04-03 21:43:50 -07:00
|
|
|
selfish: true,
|
2016-03-10 03:23:21 -08:00
|
|
|
spv: true
|
|
|
|
|
});
|
2016-02-20 16:48:07 -08:00
|
|
|
|
2016-04-04 18:45:02 -07:00
|
|
|
this.walletdb = new bcoin.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) {
|
|
|
|
|
this.http = new bcoin.http.server({
|
|
|
|
|
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-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-04-03 01:21:38 -07:00
|
|
|
|
2016-06-30 17:29:00 -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-06-30 17:29:00 -07:00
|
|
|
|
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-02-20 16:48:07 -08:00
|
|
|
|
2016-08-26 05:02:08 -07:00
|
|
|
if (this.http)
|
|
|
|
|
this.http.on('error', onError);
|
2016-02-20 16:48:07 -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.pool.on('tx', function(tx) {
|
|
|
|
|
self.emit('tx', tx);
|
2016-08-26 05:02:08 -07:00
|
|
|
self.walletdb.addTX(tx, onError);
|
2016-03-10 03:23:21 -08:00
|
|
|
});
|
2016-02-20 16:48:07 -08:00
|
|
|
|
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-08-26 05:02:08 -07:00
|
|
|
self.walletdb.addBlock(entry, block.txs, onError);
|
2016-04-03 01:21:38 -07:00
|
|
|
});
|
|
|
|
|
|
2016-08-22 14:45:02 -07:00
|
|
|
this.walletdb.on('save address', function(address, path) {
|
2016-07-05 16:21:50 -07:00
|
|
|
self.pool.watch(address.getHash());
|
2016-06-02 16:26:25 -07:00
|
|
|
});
|
2016-08-05 13:38:27 -07:00
|
|
|
|
|
|
|
|
this.walletdb.on('send', function(tx) {
|
2016-08-26 05:02:08 -07:00
|
|
|
self.sendTX(tx, onError);
|
2016-08-05 13:38:27 -07:00
|
|
|
});
|
2016-06-30 17:29:00 -07:00
|
|
|
};
|
2016-06-02 16:26:25 -07:00
|
|
|
|
2016-06-30 17:29:00 -07:00
|
|
|
/**
|
|
|
|
|
* Open the node and all its child objects,
|
|
|
|
|
* wait for the database to load.
|
|
|
|
|
* @alias SPVNode#open
|
|
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
SPVNode.prototype._open = function open(callback) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2016-04-03 01:21:38 -07:00
|
|
|
utils.serial([
|
|
|
|
|
this.chain.open.bind(this.chain),
|
|
|
|
|
this.pool.open.bind(this.pool),
|
2016-08-26 05:02:08 -07:00
|
|
|
this.walletdb.open.bind(this.walletdb),
|
2016-09-02 18:57:23 -07:00
|
|
|
// Ensure primary wallet.
|
|
|
|
|
this.openWallet.bind(this),
|
|
|
|
|
// Load bloom filter.
|
|
|
|
|
this.openFilter.bind(this),
|
|
|
|
|
// Rescan for any missed transactions.
|
|
|
|
|
this.rescan.bind(this),
|
|
|
|
|
// Rebroadcast pending transactions.
|
|
|
|
|
this.resend.bind(this),
|
2016-06-05 06:55:35 -07:00
|
|
|
function(next) {
|
|
|
|
|
if (!self.http)
|
|
|
|
|
return next();
|
|
|
|
|
self.http.open(next);
|
|
|
|
|
}
|
2016-09-02 18:57:23 -07:00
|
|
|
], function(err) {
|
|
|
|
|
if (err)
|
|
|
|
|
return callback(err);
|
|
|
|
|
|
|
|
|
|
self.logger.info('Node is loaded.');
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
2016-06-30 17:29:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close the node, wait for the database to close.
|
|
|
|
|
* @alias SPVNode#close
|
|
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
SPVNode.prototype._close = function close(callback) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
2016-06-30 17:48:46 -07:00
|
|
|
this.wallet = null;
|
|
|
|
|
|
2016-06-30 17:29:00 -07:00
|
|
|
utils.parallel([
|
|
|
|
|
function(next) {
|
|
|
|
|
if (!self.http)
|
|
|
|
|
return next();
|
|
|
|
|
self.http.close(next);
|
|
|
|
|
},
|
|
|
|
|
this.walletdb.close.bind(this.walletdb),
|
|
|
|
|
this.pool.close.bind(this.pool),
|
|
|
|
|
this.chain.close.bind(this.chain)
|
|
|
|
|
], callback);
|
2016-04-03 01:21:38 -07:00
|
|
|
};
|
2016-02-20 16:48:07 -08:00
|
|
|
|
2016-09-02 18:57:23 -07:00
|
|
|
/**
|
|
|
|
|
* Initialize p2p bloom filter for address watching.
|
|
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
SPVNode.prototype.openFilter = function openFilter(callback) {
|
|
|
|
|
var self = this;
|
|
|
|
|
var i;
|
|
|
|
|
|
|
|
|
|
this.walletdb.getAddressHashes(function(err, hashes) {
|
|
|
|
|
if (err)
|
|
|
|
|
return callback(err);
|
|
|
|
|
|
|
|
|
|
if (hashes.length > 0)
|
|
|
|
|
self.logger.info('Adding %d addresses to filter.', hashes.length);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < hashes.length; i++)
|
|
|
|
|
self.pool.watch(hashes[i], 'hex');
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rescan for any missed transactions.
|
|
|
|
|
* Note that this will replay the blockchain sync.
|
|
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
SPVNode.prototype.rescan = function rescan(callback) {
|
|
|
|
|
if (this.options.noScan) {
|
|
|
|
|
this.walletdb.setTip(
|
|
|
|
|
this.chain.tip.hash,
|
|
|
|
|
this.chain.height,
|
|
|
|
|
callback);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.walletdb.height === 0)
|
|
|
|
|
return callback();
|
|
|
|
|
|
|
|
|
|
// Always replay the last block to make
|
|
|
|
|
// sure we didn't miss anything: there
|
|
|
|
|
// is no atomicity between the chaindb
|
|
|
|
|
// and walletdb.
|
|
|
|
|
this.chain.reset(this.walletdb.height - 1, callback);
|
|
|
|
|
};
|
|
|
|
|
|
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-04-15 05:28:23 -07:00
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
|
|
|
|
|
2016-04-03 01:21:38 -07:00
|
|
|
SPVNode.prototype.broadcast = function broadcast(item, callback) {
|
|
|
|
|
return this.pool.broadcast(item, callback);
|
|
|
|
|
};
|
2016-03-10 03:23:21 -08: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} tx
|
2016-04-15 05:28:23 -07:00
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
|
|
|
|
|
2016-08-26 05:02:08 -07:00
|
|
|
SPVNode.prototype.sendTX = function sendTX(tx, wait, callback) {
|
2016-04-08 18:03:17 -07:00
|
|
|
if (!callback) {
|
|
|
|
|
callback = wait;
|
|
|
|
|
wait = null;
|
|
|
|
|
}
|
2016-04-09 04:17:33 -07:00
|
|
|
|
|
|
|
|
if (!wait) {
|
2016-08-26 05:02:08 -07:00
|
|
|
this.pool.broadcast(tx);
|
2016-04-09 04:17:33 -07:00
|
|
|
return utils.nextTick(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-26 05:02:08 -07:00
|
|
|
this.pool.broadcast(tx, callback);
|
2016-04-03 01:21:38 -07:00
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* Connect to the network.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-04-03 06:11:30 -07:00
|
|
|
SPVNode.prototype.connect = function connect() {
|
|
|
|
|
return this.pool.connect();
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* Start the blockchain sync.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-04-03 01:21:38 -07:00
|
|
|
SPVNode.prototype.startSync = function startSync() {
|
|
|
|
|
return this.pool.startSync();
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-15 05:28:23 -07:00
|
|
|
/**
|
|
|
|
|
* 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;
|