itns-sidechain/lib/node/spvnode.js

350 lines
7.6 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 bcoin = require('../env');
var utils = require('../utils/utils');
var assert = utils.assert;
var Node = bcoin.node;
/**
* 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
* @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-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,
profiler: this.profiler,
db: this.db,
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-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,
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-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,
db: this.db,
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,
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-04-03 01:21:38 -07:00
// Bind to errors
this.pool.on('error', function(err) {
2016-07-04 05:36:06 -07:00
self._error(err);
});
this.chain.on('error', function(err) {
2016-07-04 05:36:06 -07:00
self._error(err);
});
this.walletdb.on('error', function(err) {
2016-07-04 05:36:06 -07:00
self._error(err);
});
2016-06-05 06:55:35 -07:00
if (this.http) {
this.http.on('error', function(err) {
2016-07-04 05:36:06 -07:00
self._error(err);
2016-06-05 06:55:35 -07:00
});
}
2016-06-07 10:23:22 -07:00
this.pool.on('alert', function(details) {
self.emit('alert', details);
});
2016-07-06 16:06:11 -07:00
this.pool.on('tx', function(tx) {
self.emit('tx', tx);
self.walletdb.addTX(tx, function(err) {
if (err)
2016-07-04 05:36:06 -07:00
self._error(err);
});
});
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-07-14 15:54:17 -07:00
self.walletdb.addBlock(entry, block.txs, function(err) {
2016-04-03 01:21:38 -07:00
if (err)
2016-07-04 05:36:06 -07:00
self._error(err);
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) {
self.sendTX(tx, function(err) {
if (err)
self.emit('error', err);
});
});
};
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
* @param {Function} callback
*/
SPVNode.prototype._open = function open(callback) {
var self = this;
var options;
function done(err) {
2016-04-03 01:21:38 -07:00
if (err)
return callback(err);
2016-04-03 01:21:38 -07:00
2016-07-04 05:36:06 -07:00
self.logger.info('Node is loaded.');
callback();
2016-04-03 01:21:38 -07:00
}
2016-04-03 04:11:26 -07:00
options = utils.merge({
id: 'primary',
passphrase: this.options.passphrase
2016-04-03 04:11:26 -07:00
}, this.options.wallet || {});
2016-02-20 17:54:51 -08:00
2016-04-03 01:21:38 -07:00
// Create or load the primary wallet.
utils.serial([
this.chain.open.bind(this.chain),
this.pool.open.bind(this.pool),
function (next) {
self.walletdb.open(function(err) {
if (err)
return next(err);
self.createWallet(options, function(err, wallet) {
if (err)
return next(err);
2016-04-03 04:11:26 -07:00
self.wallet = wallet;
2016-04-03 01:21:38 -07:00
next();
});
});
},
2016-06-02 12:29:25 -07:00
function(next) {
var i;
2016-08-17 04:57:02 -07:00
self.walletdb.getAddressHashes(function(err, hashes) {
2016-06-02 12:29:25 -07:00
if (err)
2016-06-02 16:45:05 -07:00
return next(err);
2016-06-02 12:29:25 -07:00
2016-06-02 17:02:27 -07:00
if (hashes.length > 0)
2016-07-04 05:36:06 -07:00
self.logger.info('Adding %d addresses to filter.', hashes.length);
2016-06-02 12:29:25 -07:00
2016-06-02 17:02:27 -07:00
for (i = 0; i < hashes.length; i++)
self.pool.watch(hashes[i], 'hex');
2016-06-02 12:29:25 -07:00
next();
});
},
function(next) {
2016-08-13 05:33:29 -07:00
// Rebroadcast pending transactions.
self.wallet.resend(next);
2016-06-02 12:29:25 -07:00
},
2016-06-05 06:55:35 -07:00
function(next) {
if (!self.http)
return next();
self.http.open(next);
}
], done);
};
/**
* 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;
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
};
/**
* Broadcast a transaction (note that this will _not_ be verified
* by the mempool - use with care, lest you get banned from
* bitcoind nodes).
* @param {TX|MTX|Block} item
* @param {Function} callback
*/
2016-04-03 01:21:38 -07:00
SPVNode.prototype.broadcast = function broadcast(item, callback) {
return this.pool.broadcast(item, callback);
};
/**
* Broadcast a transaction (note that this will _not_ be verified
* by the mempool - use with care, lest you get banned from
* bitcoind nodes).
* @param {TX|MTX} item
* @param {Function} callback
*/
2016-04-08 18:03:17 -07:00
SPVNode.prototype.sendTX = function sendTX(item, wait, callback) {
if (!callback) {
callback = wait;
wait = null;
}
2016-04-09 04:17:33 -07:00
if (!wait) {
2016-05-18 23:21:53 -07:00
this.pool.broadcast(item);
2016-04-09 04:17:33 -07:00
return utils.nextTick(callback);
}
2016-08-17 15:56:18 -07:00
this.pool.broadcast(item, callback);
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();
};
/**
* Create a {@link Wallet} in the wallet database.
* @param {Object} options - See {@link Wallet}.
* @param {Function} callback - Returns [Error, {@link Wallet}].
*/
SPVNode.prototype.createWallet = function createWallet(options, callback) {
2016-07-04 05:36:06 -07:00
var self = this;
2016-04-09 04:53:41 -07:00
this.walletdb.ensure(options, function(err, wallet) {
if (err)
return callback(err);
assert(wallet);
2016-07-04 05:36:06 -07:00
self.logger.info('Loaded wallet with id=%s address=%s',
wallet.id, wallet.getAddress());
2016-08-17 15:56:18 -07:00
callback(null, wallet);
});
};
/**
* Retrieve a wallet from the wallet database.
* @param {String} id - Wallet ID.
* @param {Function} callback - Returns [Error, {@link Wallet}].
*/
2016-06-02 12:29:25 -07:00
SPVNode.prototype.getWallet = function getWallet(id, callback) {
2016-08-17 15:56:18 -07:00
this.walletdb.get(id, callback);
};
2016-05-15 18:07:06 -07:00
/*
* Expose
*/
2016-05-13 09:23:57 -07:00
module.exports = SPVNode;