@@ -146,7 +146,6 @@ class LightningManager {
146146 paymentSentSubscription : EmitterSubscription | undefined ;
147147
148148 private isSyncing : boolean = false ;
149- private forceSync : boolean = false ;
150149 private pendingSyncPromises : Array < ( result : Result < string > ) => void > = [ ] ;
151150
152151 private isStarting : boolean = false ;
@@ -571,33 +570,27 @@ class LightningManager {
571570 return this . handleSyncError ( err ( 'No getBestBlock method provided.' ) ) ;
572571 }
573572
574- if ( force && this . isSyncing && ! this . forceSync ) {
575- // If syncing is already underway and force is true, set forceSync to true.
576- this . forceSync = true ;
577- }
578- if ( this . isSyncing ) {
573+ if ( this . isSyncing && ! force ) {
579574 // If isSyncing, push to pendingSyncPromises to resolve when the current sync completes.
580575 return new Promise < Result < string > > ( ( resolve ) => {
581576 this . pendingSyncPromises . push ( resolve ) ;
582577 } ) ;
583578 }
584579 this . isSyncing = true ;
585580
586- const bestBlock = await promiseTimeout < THeader > (
587- timeout ,
588- this . getBestBlock ( ) ,
589- ) ;
590- if ( ! bestBlock ?. height ) {
591- return this . retrySyncOrReturnError ( {
581+ try {
582+ const bestBlock = await promiseTimeout < THeader > (
592583 timeout ,
593- retryAttempts,
594- e : err ( 'Unable to get best block in syncLdk method.' ) ,
595- } ) ;
596- }
597- const height = bestBlock . height ;
584+ this . getBestBlock ( ) ,
585+ ) ;
586+ if ( ! bestBlock ?. height ) {
587+ return this . retrySyncOrReturnError ( {
588+ timeout,
589+ retryAttempts,
590+ e : err ( 'Unable to get best block in syncLdk method.' ) ,
591+ } ) ;
592+ }
598593
599- // Don't update unnecessarily
600- if ( this . currentBlock . hash !== bestBlock ?. hash ) {
601594 const syncToTip = await promiseTimeout < Result < string > > (
602595 timeout ,
603596 ldk . syncToTip ( bestBlock ) ,
@@ -609,67 +602,54 @@ class LightningManager {
609602 e : syncToTip ,
610603 } ) ;
611604 }
612-
613- this . currentBlock = bestBlock ;
614- }
615-
616- let channels : TChannel [ ] = [ ] ;
617- if ( this . watchTxs . length > 0 ) {
618- // Get fresh array of channels.
619- const listChannelsResponse = await promiseTimeout < Result < TChannel [ ] > > (
620- timeout ,
621- ldk . listChannels ( ) ,
622- ) ;
623- if ( listChannelsResponse . isOk ( ) ) {
624- channels = listChannelsResponse . value ;
605+ if ( this . currentBlock . hash !== bestBlock ?. hash ) {
606+ this . currentBlock = bestBlock ;
625607 }
626- }
627608
628- // Iterate over watch transactions/outputs and set whether they are confirmed or unconfirmed.
629- const watchTxsRes = await promiseTimeout < Result < string > > (
630- timeout ,
631- this . checkWatchTxs ( this . watchTxs , channels , bestBlock ) ,
632- ) ;
633- if ( watchTxsRes . isErr ( ) ) {
634- return this . retrySyncOrReturnError ( {
635- timeout,
636- retryAttempts,
637- e : watchTxsRes ,
638- } ) ;
639- }
640- const watchOutputsRes = await promiseTimeout < Result < string > > (
641- timeout ,
642- this . checkWatchOutputs ( this . watchOutputs ) ,
643- ) ;
644- if ( watchOutputsRes . isErr ( ) ) {
645- return this . retrySyncOrReturnError ( {
646- timeout,
647- retryAttempts,
648- e : watchOutputsRes ,
649- } ) ;
650- }
651- const unconfirmedTxsRes = await promiseTimeout < Result < string > > (
652- timeout ,
653- this . checkUnconfirmedTransactions ( ) ,
654- ) ;
655- if ( unconfirmedTxsRes . isErr ( ) ) {
656- return this . retrySyncOrReturnError ( {
657- timeout,
658- retryAttempts,
659- e : unconfirmedTxsRes ,
660- } ) ;
661- }
662-
663- this . isSyncing = false ;
609+ let channels : TChannel [ ] = [ ] ;
610+ if ( this . watchTxs . length > 0 ) {
611+ // Get fresh array of channels.
612+ const listChannelsResponse = await promiseTimeout < Result < TChannel [ ] > > (
613+ timeout ,
614+ ldk . listChannels ( ) ,
615+ ) ;
616+ if ( listChannelsResponse . isOk ( ) ) {
617+ channels = listChannelsResponse . value ;
618+ }
619+ }
664620
665- // Handle force sync if needed.
666- if ( this . forceSync ) {
667- return this . handleForceSync ( { timeout, retryAttempts } ) ;
621+ // Iterate over watch transactions/outputs and set whether they are confirmed or unconfirmed.
622+ const promises = [
623+ promiseTimeout < Result < string > > (
624+ timeout ,
625+ this . checkWatchTxs ( this . watchTxs , channels , bestBlock ) ,
626+ ) ,
627+ promiseTimeout < Result < string > > (
628+ timeout ,
629+ this . checkWatchOutputs ( this . watchOutputs ) ,
630+ ) ,
631+ promiseTimeout < Result < string > > (
632+ timeout ,
633+ this . checkUnconfirmedTransactions ( ) ,
634+ ) ,
635+ ] ;
636+ const results = await Promise . all ( promises ) ;
637+ for ( const result of results ) {
638+ if ( result . isErr ( ) ) {
639+ return this . retrySyncOrReturnError ( {
640+ timeout,
641+ retryAttempts,
642+ e : result ,
643+ } ) ;
644+ }
645+ }
646+ } finally {
647+ this . isSyncing = false ;
648+ const result = ok ( `Synced to block ${ this . currentBlock . height } ` ) ;
649+ this . resolveAllPendingSyncPromises ( result ) ;
650+ ldk . nodeStateDump ( ) . catch ( console . error ) ;
651+ return result ;
668652 }
669- const result = ok ( `Synced to block ${ height } ` ) ;
670- this . resolveAllPendingSyncPromises ( result ) ;
671- ldk . nodeStateDump ( ) . catch ( console . error ) ;
672- return result ;
673653 }
674654
675655 /**
@@ -687,27 +667,6 @@ class LightningManager {
687667 }
688668 }
689669
690- /**
691- * Sets forceSync to false and re-runs the sync method.
692- * @private
693- * @param {number } timeout
694- * @param {number } retryAttempts
695- * @returns {Promise<Result<string>> }
696- */
697- private handleForceSync = async ( {
698- timeout,
699- retryAttempts,
700- } : {
701- timeout : number ;
702- retryAttempts : number ;
703- } ) : Promise < Result < string > > => {
704- this . forceSync = false ;
705- return this . syncLdk ( {
706- timeout,
707- retryAttempts,
708- } ) ;
709- } ;
710-
711670 /**
712671 * Attempts to retry the syncLdk method. Otherwise, the error gets passed to handleSyncError.
713672 * @private
@@ -741,14 +700,13 @@ class LightningManager {
741700 } ;
742701
743702 /**
744- * Sets isSyncing & forceSync to false and returns error.
703+ * Sets isSyncing to false and returns error.
745704 * @private
746705 * @param {Err<string> } e
747706 * @returns {Promise<Result<string>> }
748707 */
749708 private handleSyncError = ( e : Err < string > ) : Result < string > => {
750709 this . isSyncing = false ;
751- this . forceSync = false ;
752710 this . resolveAllPendingSyncPromises ( e ) ;
753711 return e ;
754712 } ;
0 commit comments