@@ -195,6 +195,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
195
195
ProtocolVersion : loopdb .CurrentProtocolVersion (),
196
196
},
197
197
OutgoingChanSet : chanSet ,
198
+ PaymentTimeout : request .PaymentTimeout ,
198
199
}
199
200
200
201
swapKit := newSwapKit (
@@ -610,7 +611,8 @@ func (s *loopOutSwap) payInvoices(ctx context.Context) {
610
611
// Use the recommended routing plugin.
611
612
s .swapPaymentChan = s .payInvoice (
612
613
ctx , s .SwapInvoice , s .MaxSwapRoutingFee ,
613
- s .LoopOutContract .OutgoingChanSet , pluginType , true ,
614
+ s .LoopOutContract .OutgoingChanSet ,
615
+ s .LoopOutContract .PaymentTimeout , pluginType , true ,
614
616
)
615
617
616
618
// Pay the prepay invoice. Won't use the routing plugin here as the
@@ -619,7 +621,8 @@ func (s *loopOutSwap) payInvoices(ctx context.Context) {
619
621
s .log .Infof ("Sending prepayment %v" , s .PrepayInvoice )
620
622
s .prePaymentChan = s .payInvoice (
621
623
ctx , s .PrepayInvoice , s .MaxPrepayRoutingFee ,
622
- s .LoopOutContract .OutgoingChanSet , RoutingPluginNone , false ,
624
+ s .LoopOutContract .OutgoingChanSet ,
625
+ s .LoopOutContract .PaymentTimeout , RoutingPluginNone , false ,
623
626
)
624
627
}
625
628
@@ -647,7 +650,7 @@ func (p paymentResult) failure() error {
647
650
// payInvoice pays a single invoice.
648
651
func (s * loopOutSwap ) payInvoice (ctx context.Context , invoice string ,
649
652
maxFee btcutil.Amount , outgoingChanIds loopdb.ChannelSet ,
650
- pluginType RoutingPluginType ,
653
+ paymentTimeout time. Duration , pluginType RoutingPluginType ,
651
654
reportPluginResult bool ) chan paymentResult {
652
655
653
656
resultChan := make (chan paymentResult )
@@ -662,8 +665,8 @@ func (s *loopOutSwap) payInvoice(ctx context.Context, invoice string,
662
665
var result paymentResult
663
666
664
667
status , err := s .payInvoiceAsync (
665
- ctx , invoice , maxFee , outgoingChanIds , pluginType ,
666
- reportPluginResult ,
668
+ ctx , invoice , maxFee , outgoingChanIds , paymentTimeout ,
669
+ pluginType , reportPluginResult ,
667
670
)
668
671
if err != nil {
669
672
result .err = err
@@ -691,8 +694,9 @@ func (s *loopOutSwap) payInvoice(ctx context.Context, invoice string,
691
694
// payInvoiceAsync is the asynchronously executed part of paying an invoice.
692
695
func (s * loopOutSwap ) payInvoiceAsync (ctx context.Context ,
693
696
invoice string , maxFee btcutil.Amount ,
694
- outgoingChanIds loopdb.ChannelSet , pluginType RoutingPluginType ,
695
- reportPluginResult bool ) (* lndclient.PaymentStatus , error ) {
697
+ outgoingChanIds loopdb.ChannelSet , paymentTimeout time.Duration ,
698
+ pluginType RoutingPluginType , reportPluginResult bool ) (
699
+ * lndclient.PaymentStatus , error ) {
696
700
697
701
// Extract hash from payment request. Unfortunately the request
698
702
// components aren't available directly.
@@ -705,7 +709,7 @@ func (s *loopOutSwap) payInvoiceAsync(ctx context.Context,
705
709
}
706
710
707
711
maxRetries := 1
708
- paymentTimeout := s .executeConfig .totalPaymentTimeout
712
+ totalPaymentTimeout := s .executeConfig .totalPaymentTimeout
709
713
710
714
// Attempt to acquire and initialize the routing plugin.
711
715
routingPlugin , err := AcquireRoutingPlugin (
@@ -720,8 +724,30 @@ func (s *loopOutSwap) payInvoiceAsync(ctx context.Context,
720
724
pluginType , hash .String ())
721
725
722
726
maxRetries = s .executeConfig .maxPaymentRetries
723
- paymentTimeout /= time .Duration (maxRetries )
727
+
728
+ // If not set, default to the per payment timeout to the total
729
+ // payment timeout divied by the configured maximum retries.
730
+ if paymentTimeout == 0 {
731
+ paymentTimeout = totalPaymentTimeout /
732
+ time .Duration (maxRetries )
733
+ }
734
+
735
+ // If the payment timeout is too long, we need to adjust the
736
+ // number of retries to ensure we don't exceed the total
737
+ // payment timeout.
738
+ if paymentTimeout * time .Duration (maxRetries ) >
739
+ totalPaymentTimeout {
740
+
741
+ maxRetries = int (totalPaymentTimeout / paymentTimeout )
742
+ s .log .Infof ("Adjusted max routing plugin retries to " +
743
+ "%v to stay within total payment timeout" ,
744
+ maxRetries )
745
+ }
724
746
defer ReleaseRoutingPlugin (ctx )
747
+ } else if paymentTimeout == 0 {
748
+ // If not set, default the payment timeout to the total payment
749
+ // timeout.
750
+ paymentTimeout = totalPaymentTimeout
725
751
}
726
752
727
753
req := lndclient.SendPaymentRequest {
0 commit comments