@sangaman/xud
Version:
Exchange Union Daemon
158 lines • 8.07 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bootstrap_1 = __importDefault(require("./bootstrap"));
const Logger_1 = __importDefault(require("./Logger"));
const Config_1 = __importDefault(require("./Config"));
const DB_1 = __importDefault(require("./db/DB"));
const OrderBook_1 = __importDefault(require("./orderbook/OrderBook"));
const LndClient_1 = __importDefault(require("./lndclient/LndClient"));
const RaidenClient_1 = __importDefault(require("./raidenclient/RaidenClient"));
const GrpcServer_1 = __importDefault(require("./grpc/GrpcServer"));
const GrpcWebProxyServer_1 = __importDefault(require("./grpc/webproxy/GrpcWebProxyServer"));
const Pool_1 = __importDefault(require("./p2p/Pool"));
const NodeKey_1 = __importDefault(require("./nodekey/NodeKey"));
const Service_1 = __importDefault(require("./service/Service"));
const events_1 = require("events");
const version = require('../package.json').version;
bootstrap_1.default();
/** Class representing a complete Exchange Union daemon. */
class Xud extends events_1.EventEmitter {
/**
* Create an Exchange Union daemon.
*/
constructor() {
super();
/**
* Start all processes necessary for the operation of an Exchange Union node.
* @param args optional arguments to override configuration parameters.
*/
this.start = (args) => __awaiter(this, void 0, void 0, function* () {
this.config.load(args);
const loggers = Logger_1.default.createLoggers(this.config.instanceId);
this.logger = loggers.global;
this.logger.info('config loaded');
try {
// TODO: wait for decryption of existing key or encryption of new key, config option to disable encryption
this.nodeKey = NodeKey_1.default.load(this.config.xudir, this.config.instanceId);
this.logger.info(`Local nodePubKey is ${this.nodeKey.nodePubKey}`);
this.db = new DB_1.default(this.config.db, loggers.db);
yield this.db.init();
const initPromises = [];
// setup LND clients and connect if configured
this.lndbtcClient = new LndClient_1.default(this.config.lndbtc, loggers.lnd);
if (!this.lndbtcClient.isDisabled()) {
initPromises.push(this.lndbtcClient.verifyConnection());
}
this.lndltcClient = new LndClient_1.default(this.config.lndltc, loggers.lnd);
if (!this.lndltcClient.isDisabled()) {
initPromises.push(this.lndltcClient.verifyConnection());
}
// setup raiden client and connect if configured
this.raidenClient = new RaidenClient_1.default(this.config.raiden, loggers.raiden);
if (!this.raidenClient.isDisabled()) {
initPromises.push(this.raidenClient.init());
}
this.pool = new Pool_1.default(this.config.p2p, loggers.p2p, this.db, this.lndbtcClient, this.lndltcClient);
this.orderBook = new OrderBook_1.default(this.logger, this.db.models, this.pool, this.lndbtcClient, this.raidenClient);
initPromises.push(this.orderBook.init());
// wait for components to initialize in parallel
yield Promise.all(initPromises);
// initialize pool and start listening/connecting only once other components are initialized
yield this.pool.init({
version,
pairs: this.orderBook.pairIds,
nodePubKey: this.nodeKey.nodePubKey,
raidenAddress: this.raidenClient.address,
lndbtcPubKey: this.lndbtcClient.pubKey,
lndltcPubKey: this.lndltcClient.pubKey,
});
this.service = new Service_1.default(loggers.global, {
version,
orderBook: this.orderBook,
lndBtcClient: this.lndbtcClient,
lndLtcClient: this.lndltcClient,
raidenClient: this.raidenClient,
pool: this.pool,
config: this.config,
shutdown: this.beginShutdown,
});
// start rpc server last
if (!this.config.rpc.disable) {
this.rpcServer = new GrpcServer_1.default(loggers.rpc, this.service);
const listening = this.rpcServer.listen(this.config.rpc.port, this.config.rpc.host);
if (!listening) {
// if rpc should be enabled but fails to start, treat it as a fatal error
this.logger.error('Could not start gRPC server, exiting...');
yield this.shutdown();
return;
}
if (!this.config.webproxy.disable) {
this.grpcAPIProxy = new GrpcWebProxyServer_1.default(loggers.rpc);
try {
yield this.grpcAPIProxy.listen(this.config.webproxy.port, this.config.rpc.port, this.config.rpc.host);
}
catch (err) {
this.logger.error('Could not start gRPC web proxy server', err);
}
}
}
else {
this.logger.warn('RPC server is disabled.');
}
}
catch (err) {
this.logger.error(err);
}
});
this.shutdown = () => __awaiter(this, void 0, void 0, function* () {
this.logger.info('XUD is shutting down');
this.lndbtcClient.close();
this.lndltcClient.close();
// TODO: ensure we are not in the middle of executing any trades
const closePromises = [];
if (this.pool) {
closePromises.push(this.pool.disconnect());
}
if (this.rpcServer) {
closePromises.push(this.rpcServer.close());
}
if (this.grpcAPIProxy) {
closePromises.push(this.grpcAPIProxy.close());
yield this.grpcAPIProxy.close();
}
yield Promise.all(closePromises);
yield this.db.close();
this.logger.info('XUD shutdown gracefully');
this.emit('shutdown');
});
/**
* Initiate graceful shutdown of xud. Emits the `shutdown` event when shutdown is complete.
*/
this.beginShutdown = () => {
// we begin the shutdown process but return a response before it completes.
void (this.shutdown());
return 'Shutting down XUD';
};
this.config = new Config_1.default();
}
get nodePubKey() {
return this.nodeKey.nodePubKey;
}
}
if (!module.parent) {
const xud = new Xud();
void xud.start();
}
exports.default = Xud;
//# sourceMappingURL=Xud.js.map