itns-sidechain/lib/node/spvnode.js

402 lines
8.5 KiB
JavaScript
Raw Normal View History

/*!
2018-08-01 20:00:09 -07:00
* spvnode.js - spv node for hsd
2018-02-01 13:40:45 -08:00
* Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
2018-08-01 20:00:09 -07:00
* https://github.com/handshake-org/hsd
*/
2016-06-13 01:06:01 -07:00
'use strict';
2018-07-19 05:40:48 -07:00
const assert = require('bsert');
2018-07-31 20:05:38 -07:00
const NameState = require('../covenants/namestate');
2017-06-29 20:54:07 -07:00
const Chain = require('../blockchain/chain');
const Pool = require('../net/pool');
const Node = require('./node');
2017-10-26 12:31:08 -07:00
const HTTP = require('./http');
2017-12-12 22:32:27 -08:00
const RPC = require('./rpc');
2018-01-02 20:24:56 -08:00
const pkg = require('../pkg');
2018-07-24 23:55:53 -07:00
const {RootServer, RecursiveServer} = require('../dns/server');
/**
2017-11-16 19:43:07 -08:00
* SPV Node
* Create an spv node which only maintains
* a chain, a pool, and an http server.
2017-02-03 22:47:26 -08:00
* @alias module:node.SPVNode
* @extends Node
*/
2017-11-16 19:43:07 -08:00
class SPVNode extends Node {
/**
* Create SPV node.
* @constructor
* @param {Object?} options
* @param {Buffer?} options.sslKey
* @param {Buffer?} options.sslCert
* @param {Number?} options.httpPort
* @param {String?} options.httpHost
*/
constructor(options) {
2018-03-11 20:29:05 -07:00
super(pkg.core, pkg.cfg, 'debug.log', options);
2017-11-16 19:43:07 -08:00
this.opened = false;
// SPV flag.
this.spv = true;
this.chain = new Chain({
network: this.network,
logger: this.logger,
prefix: this.config.prefix,
2017-12-06 17:05:00 -08:00
memory: this.config.bool('memory'),
2017-11-16 19:43:07 -08:00
maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'),
entryCache: this.config.uint('entry-cache'),
checkpoints: this.config.bool('checkpoints'),
chainMigrate: this.config.uint('chain-migrate'),
2017-11-16 19:43:07 -08:00
spv: true
});
this.pool = new Pool({
network: this.network,
logger: this.logger,
chain: this.chain,
prefix: this.config.prefix,
proxy: this.config.str('proxy'),
onion: this.config.bool('onion'),
2020-03-02 18:08:38 -08:00
brontideOnly: this.config.bool('brontide-only'),
2017-11-16 19:43:07 -08:00
upnp: this.config.bool('upnp'),
seeds: this.config.array('seeds'),
nodes: this.config.array('nodes'),
only: this.config.array('only'),
identityKey: this.identityKey,
2017-11-16 19:43:07 -08:00
maxOutbound: this.config.uint('max-outbound'),
createSocket: this.config.func('create-socket'),
2017-12-06 17:05:00 -08:00
memory: this.config.bool('memory'),
agent: this.config.str('agent'),
2017-11-16 19:43:07 -08:00
listen: false
});
this.rpc = new RPC(this);
this.http = new HTTP({
network: this.network,
logger: this.logger,
node: this,
prefix: this.config.prefix,
ssl: this.config.bool('ssl'),
keyFile: this.config.path('ssl-key'),
certFile: this.config.path('ssl-cert'),
host: this.config.str('http-host'),
port: this.config.uint('http-port'),
apiKey: this.config.str('api-key'),
2018-07-10 19:16:08 -07:00
noAuth: this.config.bool('no-auth'),
cors: this.config.bool('cors')
2017-11-16 19:43:07 -08:00
});
2016-11-19 18:00:13 -08:00
if (!this.config.bool('no-dns')) {
this.ns = new RootServer({
logger: this.logger,
key: this.identityKey,
host: this.config.str('ns-host'),
port: this.config.uint('ns-port', this.network.nsPort),
lookup: key => this.pool.resolve(key),
2019-11-11 16:18:21 -08:00
publicHost: this.config.str('public-host'),
noSig0: this.config.bool('no-sig0')
});
if (!this.config.bool('no-rs')) {
this.rs = new RecursiveServer({
logger: this.logger,
key: this.identityKey,
host: this.config.str('rs-host'),
port: this.config.uint('rs-port', this.network.rsPort),
stubHost: this.ns.host,
stubPort: this.ns.port,
2019-11-11 16:18:21 -08:00
noUnbound: this.config.bool('rs-no-unbound'),
noSig0: this.config.bool('no-sig0')
});
}
}
2018-07-24 23:55:53 -07:00
2017-11-16 19:43:07 -08:00
this.init();
}
2016-10-22 13:34:28 -07:00
2017-11-16 19:43:07 -08:00
/**
* Initialize the node.
* @private
*/
init() {
// Bind to errors
this.chain.on('error', err => this.error(err));
this.chain.on('abort', err => this.abort(err));
2017-11-16 19:43:07 -08:00
this.pool.on('error', err => this.error(err));
if (this.http)
this.http.on('error', err => this.error(err));
this.pool.on('tx', (tx) => {
this.emit('tx', tx);
});
this.chain.on('block', (block) => {
this.emit('block', block);
});
this.chain.on('connect', async (entry, block) => {
this.emit('connect', entry, block);
});
2017-09-26 17:20:16 -07:00
2017-11-16 19:43:07 -08:00
this.chain.on('disconnect', (entry, block) => {
this.emit('disconnect', entry, block);
});
this.chain.on('reorganize', (tip, competitor, fork) => {
this.emit('reorganize', tip, competitor, fork);
2017-11-16 19:43:07 -08:00
});
2017-11-16 19:43:07 -08:00
this.chain.on('reset', (tip) => {
this.emit('reset', tip);
});
2017-11-16 19:43:07 -08:00
this.loadPlugins();
}
2017-11-16 19:43:07 -08:00
/**
* Open the node and all its child objects,
* wait for the database to load.
* @returns {Promise}
*/
2016-09-20 14:56:54 -07:00
2017-11-16 19:43:07 -08:00
async open() {
assert(!this.opened, 'SPVNode is already open.');
this.opened = true;
2016-09-20 14:56:54 -07:00
2017-11-16 19:43:07 -08:00
await this.handlePreopen();
await this.chain.open();
await this.pool.open();
2017-11-16 19:43:07 -08:00
await this.openPlugins();
2017-11-16 19:43:07 -08:00
await this.http.open();
if (this.ns)
await this.ns.open();
if (this.rs)
await this.rs.open();
2017-11-16 19:43:07 -08:00
await this.handleOpen();
2017-11-01 12:57:11 -07:00
2017-11-16 19:43:07 -08:00
this.logger.info('Node is loaded.');
this.emit('open');
2017-11-16 19:43:07 -08:00
}
2016-09-23 18:32:49 -07:00
2017-11-16 19:43:07 -08:00
/**
* Close the node, wait for the database to close.
* @returns {Promise}
*/
2016-09-23 18:32:49 -07:00
2017-11-16 19:43:07 -08:00
async close() {
assert(this.opened, 'SPVNode is not open.');
this.opened = false;
2017-11-16 19:43:07 -08:00
await this.handlePreclose();
await this.http.close();
if (this.rs)
await this.rs.close();
if (this.ns)
await this.ns.close();
2016-11-19 18:00:13 -08:00
2017-11-16 19:43:07 -08:00
await this.closePlugins();
2016-11-19 18:00:13 -08:00
2017-11-16 19:43:07 -08:00
await this.pool.close();
await this.chain.close();
await this.handleClose();
this.logger.info('Node is closed.');
this.emit('closed');
this.emit('close');
2017-11-16 19:43:07 -08:00
}
2016-11-19 18:00:13 -08:00
2017-11-16 19:43:07 -08:00
/**
* Scan for any missed transactions.
* Note that this will replay the blockchain sync.
* @param {Number|Hash} start - Start block.
2023-10-11 20:02:11 +04:00
* @param {BloomFilter} filter
* @param {Function} iter
2017-11-16 19:43:07 -08:00
* @returns {Promise}
*/
2023-10-11 20:02:11 +04:00
async scan(start, filter, iter) {
throw new Error('Not implemented.');
}
/**
* Interactive scan for any missed transactions.
* @param {Number|Hash} start
* @param {BloomFilter} filter
* @param {Function} iter
* @returns {Promise}
*/
scanInteractive(start, filter, iter) {
2020-01-22 16:11:59 -08:00
throw new Error('Not implemented.');
2016-11-19 18:00:13 -08:00
}
2017-11-16 19:43:07 -08:00
/**
2018-08-01 20:00:09 -07:00
* Broadcast a transaction.
2017-11-16 19:43:07 -08:00
* @param {TX|Block} item
* @returns {Promise}
*/
async broadcast(item) {
try {
await this.pool.broadcast(item);
} catch (e) {
this.emit('error', e);
}
}
2017-11-16 19:43:07 -08:00
/**
2018-08-01 20:00:09 -07:00
* Broadcast a transaction.
2017-11-16 19:43:07 -08:00
* @param {TX} tx
* @returns {Promise}
*/
2017-11-16 19:43:07 -08:00
sendTX(tx) {
return this.broadcast(tx);
}
2016-04-03 01:21:38 -07:00
2017-11-16 19:43:07 -08:00
/**
* Broadcast a transaction. Silence errors.
* @param {TX} tx
* @returns {Promise}
*/
2017-01-14 19:21:46 -08:00
2017-11-16 19:43:07 -08:00
relay(tx) {
return this.broadcast(tx);
}
2017-01-14 19:21:46 -08:00
/**
* Broadcast a claim.
* @param {Claim} claim
* @returns {Promise}
*/
sendClaim(claim) {
return this.broadcast(claim);
}
/**
* Broadcast a claim. Silence errors.
* @param {Claim} claim
* @returns {Promise}
*/
relayClaim(claim) {
return this.broadcast(claim);
}
/**
* Broadcast an airdrop proof.
* @param {AirdropProof} proof
* @returns {Promise}
*/
sendAirdrop(proof) {
const key = proof.getKey();
if (!key) {
this.emit('error', new Error('Invalid Airdrop.'));
return Promise.resolve();
}
if (this.chain.tip.height + 1 >= this.network.goosigStop) {
if (key.isGoo()) {
this.emit('error', new Error('GooSig disabled.'));
return Promise.resolve();
}
}
return this.broadcast(proof);
}
/**
* Broadcast an airdrop proof. Silence errors.
* @param {AirdropProof} proof
* @returns {Promise}
*/
relayAirdrop(proof) {
return this.broadcast(proof);
}
2017-11-16 19:43:07 -08:00
/**
* Connect to the network.
* @returns {Promise}
*/
2017-11-16 19:43:07 -08:00
connect() {
return this.pool.connect();
}
2016-04-03 06:11:30 -07:00
2017-11-16 19:43:07 -08:00
/**
* Disconnect from the network.
* @returns {Promise}
*/
2017-01-14 07:54:07 -08:00
2017-11-16 19:43:07 -08:00
disconnect() {
return this.pool.disconnect();
}
2017-01-14 07:54:07 -08:00
2017-11-16 19:43:07 -08:00
/**
* Start the blockchain sync.
*/
2017-11-16 19:43:07 -08:00
startSync() {
return this.pool.startSync();
}
2016-04-03 01:21:38 -07:00
2017-11-16 19:43:07 -08:00
/**
* Stop syncing the blockchain.
*/
2017-11-16 19:43:07 -08:00
stopSync() {
return this.pool.stopSync();
}
/**
* Get current name state.
* @param {Buffer} nameHash
* @returns {NameState}
*/
async getNameStatus(nameHash) {
2018-07-24 23:55:53 -07:00
const network = this.network;
const height = this.chain.height + 1;
const blob = await this.pool.resolve(nameHash);
if (!blob) {
const state = new NameState();
2018-07-24 23:55:53 -07:00
state.reset(height);
return state;
2018-07-24 23:55:53 -07:00
}
const state = NameState.decode(blob);
2018-07-24 23:55:53 -07:00
state.maybeExpire(height, network);
2018-07-24 23:55:53 -07:00
return state;
}
2017-11-16 19:43:07 -08:00
}
2016-04-03 01:21:38 -07:00
2016-05-15 18:07:06 -07:00
/*
* Expose
*/
2016-05-13 09:23:57 -07:00
module.exports = SPVNode;