Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit 952a7db

Browse files
committed
Update syncLdk.
Remove forceSync from LightningManager class. Remove handleForceSync method from LightningManager class.
1 parent 1458b92 commit 952a7db

1 file changed

Lines changed: 57 additions & 99 deletions

File tree

lib/src/lightning-manager.ts

Lines changed: 57 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ class LightningManager {
144144
paymentSentSubscription: EmitterSubscription | undefined;
145145

146146
private isSyncing: boolean = false;
147-
private forceSync: boolean = false;
148147
private pendingSyncPromises: Array<(result: Result<string>) => void> = [];
149148

150149
private isStarting: boolean = false;
@@ -561,33 +560,27 @@ class LightningManager {
561560
return this.handleSyncError(err('No getBestBlock method provided.'));
562561
}
563562

564-
if (force && this.isSyncing && !this.forceSync) {
565-
// If syncing is already underway and force is true, set forceSync to true.
566-
this.forceSync = true;
567-
}
568-
if (this.isSyncing) {
563+
if (this.isSyncing && !force) {
569564
// If isSyncing, push to pendingSyncPromises to resolve when the current sync completes.
570565
return new Promise<Result<string>>((resolve) => {
571566
this.pendingSyncPromises.push(resolve);
572567
});
573568
}
574569
this.isSyncing = true;
575570

576-
const bestBlock = await promiseTimeout<THeader>(
577-
timeout,
578-
this.getBestBlock(),
579-
);
580-
if (!bestBlock?.height) {
581-
return this.retrySyncOrReturnError({
571+
try {
572+
const bestBlock = await promiseTimeout<THeader>(
582573
timeout,
583-
retryAttempts,
584-
e: err('Unable to get best block in syncLdk method.'),
585-
});
586-
}
587-
const height = bestBlock.height;
574+
this.getBestBlock(),
575+
);
576+
if (!bestBlock?.height) {
577+
return this.retrySyncOrReturnError({
578+
timeout,
579+
retryAttempts,
580+
e: err('Unable to get best block in syncLdk method.'),
581+
});
582+
}
588583

589-
// Don't update unnecessarily
590-
if (this.currentBlock.hash !== bestBlock?.hash) {
591584
const syncToTip = await promiseTimeout<Result<string>>(
592585
timeout,
593586
ldk.syncToTip(bestBlock),
@@ -599,67 +592,54 @@ class LightningManager {
599592
e: syncToTip,
600593
});
601594
}
602-
603-
this.currentBlock = bestBlock;
604-
}
605-
606-
let channels: TChannel[] = [];
607-
if (this.watchTxs.length > 0) {
608-
// Get fresh array of channels.
609-
const listChannelsResponse = await promiseTimeout<Result<TChannel[]>>(
610-
timeout,
611-
ldk.listChannels(),
612-
);
613-
if (listChannelsResponse.isOk()) {
614-
channels = listChannelsResponse.value;
595+
if (this.currentBlock.hash !== bestBlock?.hash) {
596+
this.currentBlock = bestBlock;
615597
}
616-
}
617598

618-
// Iterate over watch transactions/outputs and set whether they are confirmed or unconfirmed.
619-
const watchTxsRes = await promiseTimeout<Result<string>>(
620-
timeout,
621-
this.checkWatchTxs(this.watchTxs, channels, bestBlock),
622-
);
623-
if (watchTxsRes.isErr()) {
624-
return this.retrySyncOrReturnError({
625-
timeout,
626-
retryAttempts,
627-
e: watchTxsRes,
628-
});
629-
}
630-
const watchOutputsRes = await promiseTimeout<Result<string>>(
631-
timeout,
632-
this.checkWatchOutputs(this.watchOutputs),
633-
);
634-
if (watchOutputsRes.isErr()) {
635-
return this.retrySyncOrReturnError({
636-
timeout,
637-
retryAttempts,
638-
e: watchOutputsRes,
639-
});
640-
}
641-
const unconfirmedTxsRes = await promiseTimeout<Result<string>>(
642-
timeout,
643-
this.checkUnconfirmedTransactions(),
644-
);
645-
if (unconfirmedTxsRes.isErr()) {
646-
return this.retrySyncOrReturnError({
647-
timeout,
648-
retryAttempts,
649-
e: unconfirmedTxsRes,
650-
});
651-
}
652-
653-
this.isSyncing = false;
599+
let channels: TChannel[] = [];
600+
if (this.watchTxs.length > 0) {
601+
// Get fresh array of channels.
602+
const listChannelsResponse = await promiseTimeout<Result<TChannel[]>>(
603+
timeout,
604+
ldk.listChannels(),
605+
);
606+
if (listChannelsResponse.isOk()) {
607+
channels = listChannelsResponse.value;
608+
}
609+
}
654610

655-
// Handle force sync if needed.
656-
if (this.forceSync) {
657-
return this.handleForceSync({ timeout, retryAttempts });
611+
// Iterate over watch transactions/outputs and set whether they are confirmed or unconfirmed.
612+
const promises = [
613+
promiseTimeout<Result<string>>(
614+
timeout,
615+
this.checkWatchTxs(this.watchTxs, channels, bestBlock),
616+
),
617+
promiseTimeout<Result<string>>(
618+
timeout,
619+
this.checkWatchOutputs(this.watchOutputs),
620+
),
621+
promiseTimeout<Result<string>>(
622+
timeout,
623+
this.checkUnconfirmedTransactions(),
624+
),
625+
];
626+
const results = await Promise.all(promises);
627+
for (const result of results) {
628+
if (result.isErr()) {
629+
return this.retrySyncOrReturnError({
630+
timeout,
631+
retryAttempts,
632+
e: result,
633+
});
634+
}
635+
}
636+
} finally {
637+
this.isSyncing = false;
638+
const result = ok(`Synced to block ${this.currentBlock.height}`);
639+
this.resolveAllPendingSyncPromises(result);
640+
ldk.nodeStateDump().catch(console.error);
641+
return result;
658642
}
659-
const result = ok(`Synced to block ${height}`);
660-
this.resolveAllPendingSyncPromises(result);
661-
ldk.nodeStateDump().catch(console.error);
662-
return result;
663643
}
664644

665645
/**
@@ -677,27 +657,6 @@ class LightningManager {
677657
}
678658
}
679659

680-
/**
681-
* Sets forceSync to false and re-runs the sync method.
682-
* @private
683-
* @param {number} timeout
684-
* @param {number} retryAttempts
685-
* @returns {Promise<Result<string>>}
686-
*/
687-
private handleForceSync = async ({
688-
timeout,
689-
retryAttempts,
690-
}: {
691-
timeout: number;
692-
retryAttempts: number;
693-
}): Promise<Result<string>> => {
694-
this.forceSync = false;
695-
return this.syncLdk({
696-
timeout,
697-
retryAttempts,
698-
});
699-
};
700-
701660
/**
702661
* Attempts to retry the syncLdk method. Otherwise, the error gets passed to handleSyncError.
703662
* @private
@@ -731,14 +690,13 @@ class LightningManager {
731690
};
732691

733692
/**
734-
* Sets isSyncing & forceSync to false and returns error.
693+
* Sets isSyncing to false and returns error.
735694
* @private
736695
* @param {Err<string>} e
737696
* @returns {Promise<Result<string>>}
738697
*/
739698
private handleSyncError = (e: Err<string>): Result<string> => {
740699
this.isSyncing = false;
741-
this.forceSync = false;
742700
this.resolveAllPendingSyncPromises(e);
743701
return e;
744702
};

0 commit comments

Comments
 (0)