Skip to content

Commit

Permalink
routing: add new method refreshPayment
Browse files Browse the repository at this point in the history
To further shorten the lifecycle loop.
  • Loading branch information
yyforyongyu committed Oct 11, 2024
1 parent 7860350 commit 6c77f75
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions routing/payment_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,13 @@ func (p *paymentLifecycle) resumePayment(ctx context.Context) ([32]byte,
// critical error during path finding.
lifecycle:
for {
// We update the payment state on every iteration. Since the
// payment state is affected by multiple goroutines (ie,
// collectResultAsync), it is NOT guaranteed that we always
// have the latest state here. This is fine as long as the
// state is consistent as a whole.
payment, err = p.router.cfg.Control.FetchPayment(p.identifier)
// We update the payment state on every iteration.
currentPayment, ps, err := p.refreshPayment()
if err != nil {
return exitWithErr(err)
}

ps := payment.GetState()
remainingFees := p.calcFeeBudget(ps.FeesPaid)

log.Debugf("Payment %v: status=%v, active_shards=%v, "+
"rem_value=%v, fee_limit=%v", p.identifier,
payment.GetStatus(), ps.NumAttemptsInFlight,
ps.RemainingAmt, remainingFees)
payment = currentPayment

// We now proceed our lifecycle with the following tasks in
// order,
Expand Down Expand Up @@ -1084,3 +1074,24 @@ func (p *paymentLifecycle) reloadInflightAttempts() (DBMPPayment, error) {

return payment, nil
}

// refreshPayment returns the latest payment found in the control tower after
// all its attempt results are processed.
func (p *paymentLifecycle) refreshPayment() (DBMPPayment,
*channeldb.MPPaymentState, error) {

// Read the db to get the latest state of the payment.
payment, err := p.router.cfg.Control.FetchPayment(p.identifier)
if err != nil {
return nil, nil, err
}

ps := payment.GetState()
remainingFees := p.calcFeeBudget(ps.FeesPaid)

log.Debugf("Payment %v: status=%v, active_shards=%v, rem_value=%v, ",
"fee_limit=%v", p.identifier, payment.GetStatus(),
ps.NumAttemptsInFlight, ps.RemainingAmt, remainingFees)

return payment, ps, nil
}

0 comments on commit 6c77f75

Please sign in to comment.