From 568b977a1fc659f8f9964555db4af6f6cb925e93 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 15 Feb 2023 03:05:09 +0800 Subject: [PATCH] routing: add new method `registerAttempt` This commit adds a new method `registerAttempt` to take care of creating and saving an htlc attempt to disk. --- routing/payment_lifecycle.go | 54 ++++++++++++++++++++++-------------- routing/router.go | 2 +- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/routing/payment_lifecycle.go b/routing/payment_lifecycle.go index 6251d9c83..facb9168b 100644 --- a/routing/payment_lifecycle.go +++ b/routing/payment_lifecycle.go @@ -273,12 +273,8 @@ lifecycle: log.Tracef("Found route: %s", spew.Sdump(rt.Hops)) - // If this route will consume the last remaining amount to send - // to the receiver, this will be our last shard (for now). - lastShard := rt.ReceiverAmt() == ps.RemainingAmt - // We found a route to try, launch a new shard. - attempt, outcome, err := p.launchShard(rt, lastShard) + attempt, outcome, err := p.launchShard(rt, ps.RemainingAmt) switch { // We may get a terminal error if we've processed a shard with // a terminal state (settled or permanent failure), while we @@ -386,23 +382,10 @@ type attemptResult struct { // non-nil error, it means that the attempt was not sent onto the network, so // no result will be available in the future for it. func (p *paymentLifecycle) launchShard(rt *route.Route, - lastShard bool) (*channeldb.HTLCAttempt, *attemptResult, error) { + remainingAmt lnwire.MilliSatoshi) (*channeldb.HTLCAttempt, + *attemptResult, error) { - // Using the route received from the payment session, create a new - // shard to send. - attempt, err := p.createNewPaymentAttempt(rt, lastShard) - if err != nil { - return nil, nil, err - } - - // Before sending this HTLC to the switch, we checkpoint the fresh - // paymentID and route to the DB. This lets us know on startup the ID - // of the payment that we attempted to send, such that we can query the - // Switch for its whereabouts. The route is needed to handle the result - // when it eventually comes back. - err = p.router.cfg.Control.RegisterAttempt( - p.identifier, &attempt.HTLCAttemptInfo, - ) + attempt, err := p.registerAttempt(rt, remainingAmt) if err != nil { return nil, nil, err } @@ -592,6 +575,35 @@ func (p *paymentLifecycle) collectResult(attempt *channeldb.HTLCAttempt) ( }, nil } +// registerAttempt is responsible for creating and saving an HTLC attempt in db +// by using the route info provided. The `remainingAmt` is used to decide +// whether this is the last attempt. +func (p *paymentLifecycle) registerAttempt(rt *route.Route, + remainingAmt lnwire.MilliSatoshi) (*channeldb.HTLCAttempt, error) { + + // If this route will consume the last remaining amount to send + // to the receiver, this will be our last shard (for now). + isLastAttempt := rt.ReceiverAmt() == remainingAmt + + // Using the route received from the payment session, create a new + // shard to send. + attempt, err := p.createNewPaymentAttempt(rt, isLastAttempt) + if err != nil { + return nil, err + } + + // Before sending this HTLC to the switch, we checkpoint the fresh + // paymentID and route to the DB. This lets us know on startup the ID + // of the payment that we attempted to send, such that we can query the + // Switch for its whereabouts. The route is needed to handle the result + // when it eventually comes back. + err = p.router.cfg.Control.RegisterAttempt( + p.identifier, &attempt.HTLCAttemptInfo, + ) + + return attempt, err +} + // createNewPaymentAttempt creates a new payment attempt from the given route. func (p *paymentLifecycle) createNewPaymentAttempt(rt *route.Route, lastShard bool) (*channeldb.HTLCAttempt, error) { diff --git a/routing/router.go b/routing/router.go index 2e679b4bb..d20932c28 100644 --- a/routing/router.go +++ b/routing/router.go @@ -2506,7 +2506,7 @@ func (r *ChannelRouter) sendToRoute(htlcHash lntypes.Hash, rt *route.Route, ) var shardError error - attempt, outcome, err := p.launchShard(rt, false) + attempt, outcome, err := p.launchShard(rt, 0) // With SendToRoute, it can happen that the route exceeds protocol // constraints. Mark the payment as failed with an internal error.