routing: add new method registerAttempt

This commit adds a new method `registerAttempt` to take care of creating
and saving an htlc attempt to disk.
This commit is contained in:
yyforyongyu
2023-02-15 03:05:09 +08:00
parent 071d05e0e3
commit 568b977a1f
2 changed files with 34 additions and 22 deletions

View File

@@ -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) {

View File

@@ -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.