Skip to content

Commit 2e3c96f

Browse files
committed
routing: new failure reason for cancelled payments
1 parent d18c4d6 commit 2e3c96f

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

channeldb/payments.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ var (
104104
)
105105

106106
var (
107-
// ErrNoSequenceNumber is returned if we lookup a payment which does
107+
// ErrNoSequenceNumber is returned if we look up a payment which does
108108
// not have a sequence number.
109109
ErrNoSequenceNumber = errors.New("sequence number not found")
110110

@@ -147,18 +147,20 @@ const (
147147
// balance to complete the payment.
148148
FailureReasonInsufficientBalance FailureReason = 4
149149

150-
// TODO(halseth): cancel state.
150+
// FailureReasonCanceled indicates that the payment was canceled by the
151+
// user.
152+
FailureReasonCanceled FailureReason = 5
151153

152154
// TODO(joostjager): Add failure reasons for:
153155
// LocalLiquidityInsufficient, RemoteCapacityInsufficient.
154156
)
155157

156-
// Error returns a human readable error string for the FailureReason.
158+
// Error returns a human-readable error string for the FailureReason.
157159
func (r FailureReason) Error() string {
158160
return r.String()
159161
}
160162

161-
// String returns a human readable FailureReason.
163+
// String returns a human-readable FailureReason.
162164
func (r FailureReason) String() string {
163165
switch r {
164166
case FailureReasonTimeout:
@@ -171,6 +173,8 @@ func (r FailureReason) String() string {
171173
return "incorrect_payment_details"
172174
case FailureReasonInsufficientBalance:
173175
return "insufficient_balance"
176+
case FailureReasonCanceled:
177+
return "canceled"
174178
}
175179

176180
return "unknown"

lnrpc/routerrpc/router_backend.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,9 @@ func marshallPaymentFailureReason(reason *channeldb.FailureReason) (
17471747

17481748
case channeldb.FailureReasonInsufficientBalance:
17491749
return lnrpc.PaymentFailureReason_FAILURE_REASON_INSUFFICIENT_BALANCE, nil
1750+
1751+
case channeldb.FailureReasonCanceled:
1752+
return lnrpc.PaymentFailureReason_FAILURE_REASON_CANCELED, nil
17501753
}
17511754

17521755
return 0, errors.New("unknown failure reason")

routing/payment_lifecycle.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,13 @@ func (p *paymentLifecycle) checkContext(ctx context.Context) error {
322322
// user-provided timeout was reached, or the context was
323323
// canceled, either to a manual cancellation or due to an
324324
// unknown error.
325+
var reason channeldb.FailureReason
325326
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
327+
reason = channeldb.FailureReasonTimeout
326328
log.Warnf("Payment attempt not completed before "+
327329
"timeout, id=%s", p.identifier.String())
328330
} else {
331+
reason = channeldb.FailureReasonCanceled
329332
log.Warnf("Payment attempt context canceled, id=%s",
330333
p.identifier.String())
331334
}
@@ -334,7 +337,6 @@ func (p *paymentLifecycle) checkContext(ctx context.Context) error {
334337
// inflight HTLCs or not, its status will now either be
335338
// `StatusInflight` or `StatusFailed`. In either case, no more
336339
// HTLCs will be attempted.
337-
reason := channeldb.FailureReasonTimeout
338340
err := p.router.cfg.Control.FailPayment(p.identifier, reason)
339341
if err != nil {
340342
return fmt.Errorf("FailPayment got %w", err)

routing/payment_lifecycle_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,17 +772,17 @@ func TestResumePaymentFailContextCancel(t *testing.T) {
772772
cancel()
773773

774774
m.control.On(
775-
"FailPayment", p.identifier, channeldb.FailureReasonTimeout,
775+
"FailPayment", p.identifier, channeldb.FailureReasonCanceled,
776776
).Return(nil).Once()
777777

778-
// 5. decideNextStep now returns stepExit.
778+
// 4. decideNextStep now returns stepExit.
779779
m.payment.On("AllowMoreAttempts").Return(false, nil).Once().
780780
On("NeedWaitAttempts").Return(false, nil).Once()
781781

782-
// 6. Control tower deletes failed attempts.
782+
// 5. Control tower deletes failed attempts.
783783
m.control.On("DeleteFailedAttempts", p.identifier).Return(nil).Once()
784784

785-
// 7. We will observe FailureReasonError if the context was cancelled.
785+
// 6. We will observe FailureReasonError if the context was cancelled.
786786
reason := channeldb.FailureReasonError
787787
m.payment.On("TerminalInfo").Return(nil, &reason)
788788

0 commit comments

Comments
 (0)