Skip to content

Commit

Permalink
feat(swapclient): add Locked status
Browse files Browse the repository at this point in the history
This adds a status to the swap clients for after a client has been
unlocked but before we have verified its connectivity. There can be
additional initialization steps after a client has been unlocked and
a possibility that it will encounter an error and shutdown, so we can
not assume that the client will be available and set the status to
`ConnectionVerified`. However, leaving the status as `WaitingUnlock`
can be misleading or lead to additional unlock attempt that will
fail. The new `Unlocked` status prevents further unlock attempts.

Closes #1411.
  • Loading branch information
sangaman committed Mar 13, 2020
1 parent 7ef149a commit f7b35a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
28 changes: 23 additions & 5 deletions lib/lndclient/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ class LndClient extends SwapClient {
this.emit('locked');
}

/** Lnd specific procedure to mark the client as unlocked. */
private setUnlocked = () => {
// we should close and unreference the wallet unlocker service when we set the status to Unlocked
if (this.walletUnlocker) {
this.walletUnlocker.close();
this.walletUnlocker = undefined;
}

if (this.isWaitingUnlock()) {
this.setStatus(ClientStatus.Unlocked);
} else {
// we should not be calling this method we were in the WaitingUnlock status
this.logger.warn(`tried to set client status to WaitingUnlock from status ${this.status}`);
}
}

protected updateCapacity = async () => {
await this.channelBalance().catch(async (err) => {
this.logger.error('failed to update total outbound capacity', err);
Expand Down Expand Up @@ -363,16 +379,15 @@ class LndClient extends SwapClient {
// we are waiting for lnd to be initialized by xud and for the lnd macaroons to be created
this.logger.info('waiting for wallet to be initialized...');

/**
* A promise that resolves to `true` when the lnd wallet is created via an InitWallet call,
* resolves to `false` if we close the client before the lnd wallet is created.
*/
const isWalletInitialized = await new Promise<boolean>((resolve) => {
this.initWalletResolve = resolve;
});

if (isWalletInitialized) {
if (this.walletUnlocker) {
this.walletUnlocker.close();
this.walletUnlocker = undefined;
}

// admin.macaroon will not necessarily be created by the time lnd responds to a successful
// InitWallet call, so we watch the folder that we expect it to be in for it to be created
const watchMacaroonPromise = new Promise<boolean>((resolve) => {
Expand Down Expand Up @@ -825,6 +840,8 @@ class LndClient extends SwapClient {
if (this.initWalletResolve) {
this.initWalletResolve(true);
}
this.setUnlocked();

this.logger.info('wallet initialized');
return initWalletResponse.toObject();
}
Expand All @@ -835,6 +852,7 @@ class LndClient extends SwapClient {
await this.unaryWalletUnlockerCall<lndrpc.UnlockWalletRequest, lndrpc.UnlockWalletResponse>(
'unlockWallet', request,
);
this.setUnlocked();
this.logger.info('wallet unlocked');
}

Expand Down
8 changes: 7 additions & 1 deletion lib/swaps/SwapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enum ClientStatus {
OutOfSync,
/** The server is reachable but needs to be unlocked before it accepts calls. */
WaitingUnlock,
/** The server has been unlocked, but its status has not been verified yet. */
Unlocked,
/** The client could not be initialized due to faulty configuration. */
Misconfigured,
}
Expand Down Expand Up @@ -192,7 +194,11 @@ abstract class SwapClient extends EventEmitter {
}

private reconnectionTimerCallback = async () => {
if (this.status === ClientStatus.Disconnected || this.status === ClientStatus.OutOfSync || this.status === ClientStatus.WaitingUnlock) {
if (this.status === ClientStatus.Disconnected
|| this.status === ClientStatus.OutOfSync
|| this.status === ClientStatus.WaitingUnlock
|| this.status === ClientStatus.Unlocked
) {
try {
await this.verifyConnection();
} catch (err) {
Expand Down

0 comments on commit f7b35a1

Please sign in to comment.