@@ -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 (
@@ -226,44 +227,65 @@ func (m *Manager) recoverWithdrawals(ctx context.Context) error {
226227 }
227228
228229 // Group the deposits by their finalized withdrawal transaction.
229- depositsByWithdrawalTx := make (map [* wire.MsgTx ][]* deposit.Deposit )
230+ depositsByWithdrawalTx := make (map [chainhash.Hash ][]* deposit.Deposit )
231+ hash2tx := make (map [chainhash.Hash ]* wire.MsgTx )
230232 for _ , d := range activeDeposits {
231233 withdrawalTx := d .FinalizedWithdrawalTx
232234 if withdrawalTx == nil {
233235 continue
234236 }
237+ txid := withdrawalTx .TxHash ()
238+ hash2tx [txid ] = withdrawalTx
235239
236- depositsByWithdrawalTx [withdrawalTx ] = append (
237- depositsByWithdrawalTx [withdrawalTx ], d ,
240+ depositsByWithdrawalTx [txid ] = append (
241+ depositsByWithdrawalTx [txid ], d ,
238242 )
239243 }
240244
245+ // Publishing a transaction can take a while in neutrino mode, so
246+ // do it in parallel.
247+ eg := & errgroup.Group {}
248+
241249 // 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- }
250+ for txid , deposits := range depositsByWithdrawalTx {
251+ eg . Go ( func () error {
252+ err : = m .cfg .DepositManager .TransitionDeposits (
253+ ctx , deposits , deposit .OnWithdrawInitiated ,
254+ deposit .Withdrawing ,
255+ )
256+ if err != nil {
257+ return err
258+ }
251259
252- _ , err = m . publishFinalizedWithdrawalTx ( ctx , tx )
253- if err != nil {
254- return err
255- }
260+ tx , ok := hash2tx [ txid ]
261+ if ! ok {
262+ return fmt . Errorf ( "can't find tx %v" , txid )
263+ }
256264
257- err = m .handleWithdrawal (
258- ctx , deposits , tx .TxHash (), tx .TxOut [0 ].PkScript ,
259- )
260- if err != nil {
261- return err
262- }
265+ _ , err = m .publishFinalizedWithdrawalTx (ctx , tx )
266+ if err != nil {
267+ return err
268+ }
263269
264- m .mu .Lock ()
265- m .finalizedWithdrawalTxns [tx .TxHash ()] = tx
266- m .mu .Unlock ()
270+ err = m .handleWithdrawal (
271+ ctx , deposits , tx .TxHash (),
272+ tx .TxOut [0 ].PkScript ,
273+ )
274+ if err != nil {
275+ return err
276+ }
277+
278+ m .mu .Lock ()
279+ m .finalizedWithdrawalTxns [tx .TxHash ()] = tx
280+ m .mu .Unlock ()
281+
282+ return nil
283+ })
284+ }
285+
286+ // Wait for all goroutines to report back.
287+ if err := eg .Wait (); err != nil {
288+ return fmt .Errorf ("error recovering withdrawals: %w" , err )
267289 }
268290
269291 return nil
@@ -558,6 +580,9 @@ func (m *Manager) publishFinalizedWithdrawalTx(ctx context.Context,
558580 "withdrawal tx is nil" )
559581 }
560582
583+ log .Debugf ("Publishing deposit withdrawal with txid: %v ..." ,
584+ tx .TxHash ())
585+
561586 txLabel := fmt .Sprintf ("deposit-withdrawal-%v" , tx .TxHash ())
562587
563588 // Publish the withdrawal sweep transaction.
@@ -577,7 +602,7 @@ func (m *Manager) publishFinalizedWithdrawalTx(ctx context.Context,
577602 return false , nil
578603 }
579604 } else {
580- log .Debugf ("published deposit withdrawal with txid: %v" ,
605+ log .Debugf ("Published deposit withdrawal with txid: %v" ,
581606 tx .TxHash ())
582607 }
583608
0 commit comments