Skip to content

Commit

Permalink
fix(lnd): don't default lnd clients as disabled
Browse files Browse the repository at this point in the history
This fixes a bug introduced in #672 whereby the lnd clients are not
properly initializing. That PR removed synchronous calls to methods that
had asynchronous alternatives, which necessitated moving code from the
synchronous `LndClient` constructor to an asynchronous init method.

Included in the code that was moved was logic to determine whether to
change the client's status from the default value of `Disabled`. This
impacted the xud initialization procedure which would only attempt
to initialize (or previously verify a connection for) an lnd client that
was not disabled.

This PR creates a new `NotInitialized` status as the default to reflect
the status of a newly created client. It gets changed when the `init`
method is called.
  • Loading branch information
sangaman committed Nov 19, 2018
1 parent 58687d7 commit 3030b9c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
3 changes: 2 additions & 1 deletion lib/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Logger from './Logger';
import { EventEmitter } from 'events';

enum ClientStatus {
NotInitialized,
Disabled,
Disconnected,
ConnectionVerified,
Expand All @@ -12,7 +13,7 @@ enum ClientStatus {
* A base class to represent a client for an external service such as LND or Raiden.
*/
abstract class BaseClient extends EventEmitter {
protected status: ClientStatus = ClientStatus.Disabled;
protected status: ClientStatus = ClientStatus.NotInitialized;

constructor(protected logger: Logger) {
super();
Expand Down
10 changes: 3 additions & 7 deletions lib/Xud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,11 @@ class Xud extends EventEmitter {
this.db = new DB(loggers.db, this.config.dbpath);
await this.db.init(this.config.initdb);

// setup LND clients and connect if configured
// setup LND clients and initialize
this.lndbtcClient = new LndClient(this.config.lndbtc, loggers.lnd);
if (!this.lndbtcClient.isDisabled()) {
initPromises.push(this.lndbtcClient.init());
}
initPromises.push(this.lndbtcClient.init());
this.lndltcClient = new LndClient(this.config.lndltc, loggers.lnd);
if (!this.lndltcClient.isDisabled()) {
initPromises.push(this.lndltcClient.init());
}
initPromises.push(this.lndltcClient.init());

// setup raiden client and connect if configured
this.raidenClient = new RaidenClient(this.config.raiden, loggers.raiden);
Expand Down
43 changes: 21 additions & 22 deletions lib/lndclient/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,36 @@ class LndClient extends BaseClient {

/** Initializes the client for calls to lnd and verifies that we can connect to it. */
public init = async () => {
let shouldEnable = true;
const { disable, certpath, macaroonpath, nomacaroons, host, port } = this.config;
let shouldDisable = disable;

if (disable) {
shouldEnable = false;
}
if (!(await exists(certpath))) {
this.logger.error('could not find lnd certificate, is lnd installed?');
shouldEnable = false;
shouldDisable = true;
}
if (!nomacaroons && !(await exists(macaroonpath))) {
this.logger.error('could not find lnd macaroon, is lnd installed?');
shouldEnable = false;
shouldDisable = true;
}
if (shouldEnable) {
assert(this.cltvDelta > 0, 'cltvdelta must be a positive number');
this.uri = `${host}:${port}`;
const lndCert = await readFile(certpath);
this.credentials = grpc.credentials.createSsl(lndCert);

this.meta = new grpc.Metadata();
if (!nomacaroons) {
const adminMacaroon = await readFile(macaroonpath);
this.meta.add('macaroon', adminMacaroon.toString('hex'));
} else {
this.logger.info(`macaroons are disabled for lnd at ${this.uri}`);
}
// set status as disconnected until we can verify the connection
this.setStatus(ClientStatus.Disconnected);
await this.verifyConnection();
if (shouldDisable) {
return;
}

assert(this.cltvDelta > 0, 'cltvdelta must be a positive number');
this.uri = `${host}:${port}`;
const lndCert = await readFile(certpath);
this.credentials = grpc.credentials.createSsl(lndCert);

this.meta = new grpc.Metadata();
if (!nomacaroons) {
const adminMacaroon = await readFile(macaroonpath);
this.meta.add('macaroon', adminMacaroon.toString('hex'));
} else {
this.logger.info(`macaroons are disabled for lnd at ${this.uri}`);
}
// set status as disconnected until we can verify the connection
this.setStatus(ClientStatus.Disconnected);
return this.verifyConnection();
}

public get pubKey() {
Expand Down
4 changes: 2 additions & 2 deletions lib/raidenclient/RaidenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class RaidenClient extends BaseClient {
this.port = port;
this.host = host;

if (!disable) {
this.setStatus(ClientStatus.Disconnected);
if (disable) {
this.setStatus(ClientStatus.Disabled);
}
}

Expand Down

0 comments on commit 3030b9c

Please sign in to comment.