itns-sidechain/lib/node/spvnode.js

369 lines
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-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');
var Lock = require('../utils/lock');
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 Pool = require('../net/pool');
var WalletDB = require('../wallet/walletdb');
var HTTPServer = require('../http/server');
/**
* 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'),
2017-01-14 14:41:55 -08:00
maxFiles: this.options.maxFiles,
cacheSize: this.options.cacheSize,
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-04-04 18:45:02 -07:00
useCheckpoints: this.options.useCheckpoints,
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,
2017-01-16 04:28:46 -08:00
seeds: this.options.seeds,
nodes: this.options.nodes,
bip151: this.options.bip151,
bip150: this.options.bip150,
authPeers: this.options.authPeers,
knownPeers: this.options.knownPeers,
identityKey: this.options.identityKey,
maxOutbound: this.options.maxOutbound,
2017-01-14 09:11:07 -08:00
noDiscovery: this.options.noDiscovery,
2016-11-10 13:46:25 -08:00
headers: this.options.headers,
selfish: true,
2017-01-14 14:41:55 -08:00
listen: false
});
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-12-04 13:59:08 -08:00
maxFiles: this.options.walletMaxFiles,
cacheSize: this.options.walletCacheSize,
2017-01-14 14:41:55 -08:00
witness: false,
useCheckpoints: this.options.useCheckpoints,
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-11-19 21:40:31 -08:00
verify: true,
spv: true
2016-04-04 18:45:02 -07:00
});
2016-02-20 17:54:51 -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,
2017-01-14 14:41:55 -08:00
port: this.options.httpPort,
host: this.options.httpHost,
2016-07-12 15:34:41 -07:00
apiKey: this.options.apiKey,
2016-10-05 07:11:13 -07:00
serviceKey: this.options.serviceKey,
walletAuth: this.options.walletAuth,
noAuth: this.options.noAuth
2016-06-05 06:55:35 -07:00
});
}
2016-04-03 01:21:38 -07:00
2016-11-19 18:00:13 -08:00
this.rescanJob = null;
this.scanLock = new Lock();
this.watchLock = new Lock();
2016-11-19 18:00:13 -08:00
this._init();
}
2016-11-19 10:45:31 -08:00
util.inherits(SPVNode, Node);
/**
* Initialize the node.
* @private
*/
SPVNode.prototype._init = function _init() {
var self = this;
2017-01-18 19:10:35 -05: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) {
2016-11-19 18:00:13 -08:00
if (self.rescanJob)
return;
self.emit('tx', tx);
});
2016-10-22 13:34:28 -07:00
this.chain.on('block', function(block) {
2016-04-03 01:21:38 -07:00
self.emit('block', block);
2016-10-22 13:34:28 -07:00
});
this.chain.on('connect', co(function* (entry, block) {
2016-11-19 18:00:13 -08:00
if (self.rescanJob) {
try {
yield self.watchBlock(entry, block);
} catch (e) {
2017-01-18 19:10:35 -05:00
self.error(e);
}
2016-11-19 18:00:13 -08:00
return;
}
2016-11-19 21:40:31 -08:00
self.emit('connect', entry, block);
}));
2016-04-03 01:21:38 -07:00
2016-10-22 13:34:28 -07:00
this.chain.on('disconnect', function(entry, block) {
2016-11-19 21:40:31 -08:00
self.emit('disconnect', entry);
2016-10-22 13:34:28 -07:00
});
this.chain.on('reset', function(tip) {
2016-11-19 21:40:31 -08:00
self.emit('reset', tip);
2016-11-19 18:00:13 -08: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
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();
});
2016-11-19 18:00:13 -08:00
/**
* Scan for any missed transactions.
* Note that this will replay the blockchain sync.
* @param {Number|Hash} start - Start block.
* @param {Bloom} filter
* @param {Function} iter - Iterator.
* @returns {Promise}
*/
SPVNode.prototype.scan = co(function* scan(start, filter, iter) {
var unlock = yield this.scanLock.lock();
var height = this.chain.height;
try {
yield this.chain.replay(start);
if (this.chain.height < height) {
// We need to somehow defer this.
2017-01-14 07:18:47 -08:00
// yield this.connect();
// this.startSync();
2016-11-19 18:00:13 -08:00
// yield this.watchUntil(height, iter);
}
} finally {
unlock();
}
});
/**
* Watch the blockchain until a certain height.
* @param {Number} height
* @param {Function} iter
* @returns {Promise}
*/
SPVNode.prototype.watchUntil = function watchUntil(height, iter) {
var self = this;
return new Promise(function(resolve, reject) {
self.rescanJob = new RescanJob(resolve, reject, height, iter);
});
};
2016-11-19 18:00:13 -08:00
/**
* Handled watched block.
* @param {ChainEntry} entry
* @param {MerkleBlock} block
* @returns {Promise}
*/
SPVNode.prototype.watchBlock = co(function* watchBlock(entry, block) {
var unlock = yield this.watchLock.lock();
try {
if (entry.height < this.rescanJob.height) {
yield this.rescanJob.iter(entry, block.txs);
return;
}
this.rescanJob.resolve();
this.rescanJob = null;
} catch (e) {
this.rescanJob.reject(e);
this.rescanJob = null;
} finally {
unlock();
}
});
/**
* 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}
*/
SPVNode.prototype.broadcast = co(function* broadcast(item) {
try {
yield this.pool.broadcast(item);
} catch (e) {
this.emit('error', e);
}
});
/**
* 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.broadcast(tx);
2016-04-03 01:21:38 -07:00
};
2017-01-14 19:21:46 -08:00
/**
* Broadcast a transaction. Silence errors.
* @param {TX} tx
* @returns {Promise}
*/
SPVNode.prototype.relay = function relay(tx) {
return this.broadcast(tx);
};
/**
* Connect to the network.
2017-01-14 07:18:47 -08:00
* @returns {Promise}
*/
2016-04-03 06:11:30 -07:00
SPVNode.prototype.connect = function connect() {
return this.pool.connect();
};
2017-01-14 07:54:07 -08:00
/**
* Disconnect from the network.
* @returns {Promise}
*/
SPVNode.prototype.disconnect = function disconnect() {
return this.pool.disconnect();
};
/**
* 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-11-19 18:00:13 -08:00
/*
* Helpers
*/
function RescanJob(resolve, reject, height, iter) {
this.resolve = resolve;
this.reject = reject;
this.height = height;
this.iter = iter;
}
2016-05-15 18:07:06 -07:00
/*
* Expose
*/
2016-05-13 09:23:57 -07:00
module.exports = SPVNode;