Skip to content

Commit 5746516

Browse files
authored
fix(lnd): handling hold invoice check errors (#1969)
This adds better error handling for when the test calls to verify lnd hold invoices are available fail due to connectivity reasons. Previously any error that occurred at this step would cause us to set lnd's status to `NoHoldInvoiceSupport` including connection issues. There was also a bug that caused us to try to set the status to connected even when a hold invoice status check failed. This could result in the unusual behavior of status going to `Disconnected` upon a call failing due to the grpc `UNAVAILABLE` error status, then being set to `NoHoldInvoiceSupport` and then to `ConnectionVerified`. Now we only set `NoHoldInvoiceSupport` when the test calls fail for a reason other than `UNAVAILABLE`, and we only set the status to `ConnectionVerified` when the hold invoice calls succeed. Closes #1968.
1 parent f0b2ac9 commit 5746516

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

Diff for: lib/lndclient/LndClient.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class LndClient extends SwapClient {
497497
this.setStatus(ClientStatus.Misconfigured);
498498
}
499499

500+
if (this.walletUnlocker) {
501+
// WalletUnlocker service is disabled when the main Lightning service is available
502+
this.walletUnlocker.close();
503+
this.walletUnlocker = undefined;
504+
}
505+
500506
this.invoices = new InvoicesClient(this.uri, this.credentials);
501507
try {
502508
const randomHash = crypto.randomBytes(32).toString('hex');
@@ -505,16 +511,13 @@ class LndClient extends SwapClient {
505511
await this.addInvoice({ rHash: randomHash, units: 1 });
506512
await this.removeInvoice(randomHash);
507513
} catch (err) {
508-
const errStr = typeof(err) === 'string' ? err : JSON.stringify(err);
509-
510-
this.logger.error(`could not add hold invoice, error: ${errStr}`);
511-
this.setStatus(ClientStatus.NoHoldInvoiceSupport);
512-
}
513-
514-
if (this.walletUnlocker) {
515-
// WalletUnlocker service is disabled when the main Lightning service is available
516-
this.walletUnlocker.close();
517-
this.walletUnlocker = undefined;
514+
if (err.code !== grpc.status.UNAVAILABLE) {
515+
// mark the client as not having hold invoice support if the invoice calls failed due to
516+
// reasons other than generic grpc connectivity errors
517+
this.logger.error('could not add hold invoice', err);
518+
this.setStatus(ClientStatus.NoHoldInvoiceSupport);
519+
}
520+
throw err; // we don't want to proceed with marking the client as connected, regardless of the error
518521
}
519522

520523
await this.setConnected(newPubKey, newUris);

Diff for: lib/swaps/SwapClient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ abstract class SwapClient extends EventEmitter {
206206
case ClientStatus.WaitingUnlock:
207207
case ClientStatus.OutOfSync:
208208
case ClientStatus.NoHoldInvoiceSupport:
209-
// these statuses can only be set on an operational, initalized client
209+
// these statuses can only be set on an operational, initialized client
210210
validStatusTransition = this.isOperational();
211211
break;
212212
case ClientStatus.NotInitialized:
@@ -349,7 +349,7 @@ abstract class SwapClient extends EventEmitter {
349349
* Returns `true` if the client is enabled and configured properly.
350350
*/
351351
public isOperational(): boolean {
352-
return !this.isDisabled() && !this.isMisconfigured() && !this.isNotInitialized() && !this.hasNoInvoiceSupport();
352+
return !this.isDisabled() && !this.isMisconfigured() && !this.isNotInitialized();
353353
}
354354
public isDisconnected(): boolean {
355355
return this.status === ClientStatus.Disconnected;

0 commit comments

Comments
 (0)