Skip to content

Commit f7b35a1

Browse files
committed
feat(swapclient): add Locked status
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.
1 parent 7ef149a commit f7b35a1

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

Diff for: lib/lndclient/LndClient.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ class LndClient extends SwapClient {
210210
this.emit('locked');
211211
}
212212

213+
/** Lnd specific procedure to mark the client as unlocked. */
214+
private setUnlocked = () => {
215+
// we should close and unreference the wallet unlocker service when we set the status to Unlocked
216+
if (this.walletUnlocker) {
217+
this.walletUnlocker.close();
218+
this.walletUnlocker = undefined;
219+
}
220+
221+
if (this.isWaitingUnlock()) {
222+
this.setStatus(ClientStatus.Unlocked);
223+
} else {
224+
// we should not be calling this method we were in the WaitingUnlock status
225+
this.logger.warn(`tried to set client status to WaitingUnlock from status ${this.status}`);
226+
}
227+
}
228+
213229
protected updateCapacity = async () => {
214230
await this.channelBalance().catch(async (err) => {
215231
this.logger.error('failed to update total outbound capacity', err);
@@ -363,16 +379,15 @@ class LndClient extends SwapClient {
363379
// we are waiting for lnd to be initialized by xud and for the lnd macaroons to be created
364380
this.logger.info('waiting for wallet to be initialized...');
365381

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

370390
if (isWalletInitialized) {
371-
if (this.walletUnlocker) {
372-
this.walletUnlocker.close();
373-
this.walletUnlocker = undefined;
374-
}
375-
376391
// admin.macaroon will not necessarily be created by the time lnd responds to a successful
377392
// InitWallet call, so we watch the folder that we expect it to be in for it to be created
378393
const watchMacaroonPromise = new Promise<boolean>((resolve) => {
@@ -825,6 +840,8 @@ class LndClient extends SwapClient {
825840
if (this.initWalletResolve) {
826841
this.initWalletResolve(true);
827842
}
843+
this.setUnlocked();
844+
828845
this.logger.info('wallet initialized');
829846
return initWalletResponse.toObject();
830847
}
@@ -835,6 +852,7 @@ class LndClient extends SwapClient {
835852
await this.unaryWalletUnlockerCall<lndrpc.UnlockWalletRequest, lndrpc.UnlockWalletResponse>(
836853
'unlockWallet', request,
837854
);
855+
this.setUnlocked();
838856
this.logger.info('wallet unlocked');
839857
}
840858

Diff for: lib/swaps/SwapClient.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ enum ClientStatus {
1818
OutOfSync,
1919
/** The server is reachable but needs to be unlocked before it accepts calls. */
2020
WaitingUnlock,
21+
/** The server has been unlocked, but its status has not been verified yet. */
22+
Unlocked,
2123
/** The client could not be initialized due to faulty configuration. */
2224
Misconfigured,
2325
}
@@ -192,7 +194,11 @@ abstract class SwapClient extends EventEmitter {
192194
}
193195

194196
private reconnectionTimerCallback = async () => {
195-
if (this.status === ClientStatus.Disconnected || this.status === ClientStatus.OutOfSync || this.status === ClientStatus.WaitingUnlock) {
197+
if (this.status === ClientStatus.Disconnected
198+
|| this.status === ClientStatus.OutOfSync
199+
|| this.status === ClientStatus.WaitingUnlock
200+
|| this.status === ClientStatus.Unlocked
201+
) {
196202
try {
197203
await this.verifyConnection();
198204
} catch (err) {

0 commit comments

Comments
 (0)