Skip to content

Commit

Permalink
fix(lnd): auto unlock consistently
Browse files Browse the repository at this point in the history
This fixes the logic around automatically unlocking lnd if it is
restarted and comes back online in a locked state while xud is running.
Previously, it would be possible for lnd to go down, come back online,
get unlocked, then quickly go down again leaving it in a
`WAITING_UNLOCK` status. If lnd comes back online while still in this
status, it would not be automatically unlocked. This fix ensures that
lnd would be unlocked even in this case. Although it is somewhat of an
edge case, it can manifest when lnd is unable to communicate with its
backing node as its behavior is to immediately shut down after unlock
when that is the case.
  • Loading branch information
sangaman committed Feb 17, 2020
1 parent a3f2dff commit 9329d33
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/lndclient/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ class LndClient extends SwapClient {

if (!this.isWaitingUnlock()) {
await this.setStatus(ClientStatus.WaitingUnlock);
this.emit('locked');
}

this.emit('locked');
}

protected updateCapacity = async () => {
Expand Down Expand Up @@ -306,8 +307,13 @@ class LndClient extends SwapClient {
}
(this.walletUnlocker[methodName] as Function)(params, this.meta, (err: grpc.ServiceError, response: U) => {
if (err) {
this.logger.trace(`error on ${methodName}: ${err.message}`);
reject(err);
if (err.code === grpc.status.UNIMPLEMENTED) {
this.logger.debug(`lnd already unlocked before ${methodName} call`);
resolve();
} else {
this.logger.debug(`error on ${methodName}: ${err.message}`);
reject(err);
}
} else {
resolve(response);
}
Expand Down Expand Up @@ -844,14 +850,13 @@ class LndClient extends SwapClient {
return initWalletResponse.toObject();
}

public unlockWallet = async (walletPassword: string): Promise<lndrpc.UnlockWalletResponse.AsObject> => {
public unlockWallet = async (walletPassword: string): Promise<void> => {
const request = new lndrpc.UnlockWalletRequest();
request.setWalletPassword(Uint8Array.from(Buffer.from(walletPassword, 'utf8')));
const unlockWalletResponse = await this.unaryWalletUnlockerCall<lndrpc.UnlockWalletRequest, lndrpc.UnlockWalletResponse>(
await this.unaryWalletUnlockerCall<lndrpc.UnlockWalletRequest, lndrpc.UnlockWalletResponse>(
'unlockWallet', request,
);
this.logger.info('wallet unlocked');
return unlockWalletResponse.toObject();
}

public addInvoice = async (rHash: string, units: number, expiry = this.finalLock) => {
Expand Down

0 comments on commit 9329d33

Please sign in to comment.