From 966cfccb94f9ac48ce0baec8e612b5732f73c1fb Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Mon, 30 Sep 2024 09:54:47 +0900 Subject: [PATCH] routing: add new method `reloadInflightAttempts` To shorten the method `resumePayment` and make each step more clear. --- routing/payment_lifecycle.go | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/routing/payment_lifecycle.go b/routing/payment_lifecycle.go index b4d883840..b3008f123 100644 --- a/routing/payment_lifecycle.go +++ b/routing/payment_lifecycle.go @@ -175,20 +175,11 @@ func (p *paymentLifecycle) resumePayment(ctx context.Context) ([32]byte, // If we had any existing attempts outstanding, we'll start by spinning // up goroutines that'll collect their results and deliver them to the // lifecycle loop below. - payment, err := p.router.cfg.Control.FetchPayment(p.identifier) + payment, err := p.reloadInflightAttempts() if err != nil { return [32]byte{}, nil, err } - for _, a := range payment.InFlightHTLCs() { - a := a - - log.Infof("Resuming HTLC attempt %v for payment %v", - a.AttemptID, p.identifier) - - p.resultCollector(&a) - } - // Get the payment status. status := payment.GetStatus() @@ -1084,3 +1075,24 @@ func marshallError(sendError error, time time.Time) *channeldb.HTLCFailInfo { return response } + +// reloadInflightAttempts is called when the payment lifecycle is resumed after +// a restart. It reloads all inflight attempts from the control tower and +// collects the results of the attempts that have been sent before. +func (p *paymentLifecycle) reloadInflightAttempts() (DBMPPayment, error) { + payment, err := p.router.cfg.Control.FetchPayment(p.identifier) + if err != nil { + return nil, err + } + + for _, a := range payment.InFlightHTLCs() { + a := a + + log.Infof("Resuming HTLC attempt %v for payment %v", + a.AttemptID, p.identifier) + + p.resultCollector(&a) + } + + return payment, nil +}