@@ -26,6 +26,7 @@ import (
2626 "github.com/lightningnetwork/lnd/lntypes"
2727 "github.com/lightningnetwork/lnd/lnwallet"
2828 "github.com/lightningnetwork/lnd/lnwallet/chainfee"
29+ "golang.org/x/sync/errgroup"
2930)
3031
3132var (
@@ -238,32 +239,45 @@ func (m *Manager) recoverWithdrawals(ctx context.Context) error {
238239 )
239240 }
240241
242+ // Publishing a transaction can take a while in neutrino mode, so
243+ // do it in parallel.
244+ eg := & errgroup.Group {}
245+
241246 // We can now reinstate each cluster of deposits for a withdrawal.
242- for finalizedWithdrawalTx , deposits := range depositsByWithdrawalTx {
243- tx := finalizedWithdrawalTx
244- err = m .cfg .DepositManager .TransitionDeposits (
245- ctx , deposits , deposit .OnWithdrawInitiated ,
246- deposit .Withdrawing ,
247- )
248- if err != nil {
249- return err
250- }
247+ for tx , deposits := range depositsByWithdrawalTx {
248+ eg . Go ( func () error {
249+ err : = m .cfg .DepositManager .TransitionDeposits (
250+ ctx , deposits , deposit .OnWithdrawInitiated ,
251+ deposit .Withdrawing ,
252+ )
253+ if err != nil {
254+ return err
255+ }
251256
252- _ , err = m .publishFinalizedWithdrawalTx (ctx , tx )
253- if err != nil {
254- return err
255- }
257+ _ , err = m .publishFinalizedWithdrawalTx (ctx , tx )
258+ if err != nil {
259+ return err
260+ }
256261
257- err = m .handleWithdrawal (
258- ctx , deposits , tx .TxHash (), tx .TxOut [0 ].PkScript ,
259- )
260- if err != nil {
261- return err
262- }
262+ err = m .handleWithdrawal (
263+ ctx , deposits , tx .TxHash (),
264+ tx .TxOut [0 ].PkScript ,
265+ )
266+ if err != nil {
267+ return err
268+ }
263269
264- m .mu .Lock ()
265- m .finalizedWithdrawalTxns [tx .TxHash ()] = tx
266- m .mu .Unlock ()
270+ m .mu .Lock ()
271+ m .finalizedWithdrawalTxns [tx .TxHash ()] = tx
272+ m .mu .Unlock ()
273+
274+ return nil
275+ })
276+ }
277+
278+ // Wait for all goroutines to report back.
279+ if err := eg .Wait (); err != nil {
280+ return fmt .Errorf ("error recovering withdrawals: %w" , err )
267281 }
268282
269283 return nil
0 commit comments