Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

routing: add payment failure reason FailureReasonCancel #8836

Merged

Conversation

hieblmi
Copy link
Collaborator

@hieblmi hieblmi commented Jun 13, 2024

This PR depends on #8734 which introduced a user-cancelable payment loop.

It adds a new failure reason code channeldb.FailureReasonCanceled to distinguish payment-timeout cancels in the db from manual user cancellations.

Copy link
Contributor

coderabbitai bot commented Jun 13, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch from ffe9cf8 to a8a49f2 Compare June 13, 2024 07:53
@hieblmi hieblmi self-assigned this Jun 13, 2024
@hieblmi hieblmi marked this pull request as draft June 13, 2024 07:53
@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch from a8a49f2 to 1b2ec34 Compare June 13, 2024 08:21
@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch 2 times, most recently from d67ffc1 to 3cb50a8 Compare June 17, 2024 11:03
@hieblmi hieblmi marked this pull request as ready for review June 17, 2024 11:04
@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch from 3cb50a8 to bd6c7f4 Compare June 17, 2024 11:29
// TODO(halseth): cancel state.
// FailureReasonCanceled indicates that the payment was canceled by the
// user.
FailureReasonCanceled FailureReason = 5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to update the String method below, also wondering if it's easy to modify one of the itest cases to check this behavior (probably not as we don't have an easy way to cancel context there). Mentioning the test because I think there are other places that also need to be updated, such as lnrpc.PaymentFailureReason.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for noticing this, I've adjusted the String method and complemented the lnrpc part.

For an itest we'd probably have to create harness rpc method like SendPaymentWithContext or so, then pre-cancel it and try sending the payment, which should result in lnrpc.FAILURE_REASON_CANCELED.

Should we do that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sounds feasible to me! So sth like,

	stream := alice.RPC.SendPaymentWithContext(ctx, req)
	ht.AssertPaymentStatusFromStream(stream, lnrpc.Payment_IN_FLIGHT)

	ctx.Cancel()
	ht.AssertPaymentStatusFromStream(stream, lnrpc.Payment_CANCELED)

Copy link
Collaborator Author

@hieblmi hieblmi Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure if we should introduce lnrpc.Payment_CANCELED because adding this state would be a deviation from how we handle other payment FailureReasons.

In the reference below we define state lnrpc.Payment_FAILED as an umbrella state for any FailureReason. So adding a further distinction here would complicate the flow.

paymentFailed = true

I think the proper way to check for a cancel is first to check the payment for lnrpc.Payment_FAILED, then check the payment failure reason for PaymentFailureReason_FAILURE_REASON_INCORRECT_PAYMENT_DETAILSlnrpc. PaymentFailureReason_FAILURE_REASON_CANCELED.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea my bad that code ref is incorrect as we def don't want to add a new status, what I meant is,

	stream := alice.RPC.SendPaymentWithContext(ctx, req)
	ht.AssertPaymentStatusFromStream(stream, lnrpc.Payment_IN_FLIGHT)

	ctx.Cancel()
	payment := ht.AssertPaymentStatusFromStream(stream, lnrpc.Payment_FAILED)

	require.Equal(ht, lnrpc.PaymentFailureReason_FAILURE_REASON_CANCELED, payment.FailureReason)

Copy link
Collaborator Author

@hieblmi hieblmi Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is nothing preventing the payment from settling then the payment status after the ctx.Cancel() should be lnrpc.Payment_SUCCEEDED since the cancellation can't abort the current payment attempt. But the failure reason should be canceled.

If attached an itest.

Edit: I've extended the test to resemble a real world example more closely.
In the itest A tries to pay C's invoice in A - B - C. B intercepts and holds the htlc until A cancels the payment context, then B fails the htlc. The expected result is that no further payment attempts are taken, that the payment is in payment state FAILED and the failure reason is FAILURE_REASON_CANCELED.

@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch 2 times, most recently from 3c1d246 to 4ac08ce Compare July 8, 2024 10:26
@hieblmi hieblmi requested a review from yyforyongyu July 9, 2024 12:55
Copy link
Collaborator

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending an itest, otherwise LGTM!

// TODO(halseth): cancel state.
// FailureReasonCanceled indicates that the payment was canceled by the
// user.
FailureReasonCanceled FailureReason = 5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sounds feasible to me! So sth like,

	stream := alice.RPC.SendPaymentWithContext(ctx, req)
	ht.AssertPaymentStatusFromStream(stream, lnrpc.Payment_IN_FLIGHT)

	ctx.Cancel()
	ht.AssertPaymentStatusFromStream(stream, lnrpc.Payment_CANCELED)

@lightninglabs-deploy
Copy link

@bitromortac: review reminder
@hieblmi, remember to re-request review from reviewers when ready

@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch from 4ac08ce to e95fe0d Compare July 26, 2024 15:19
@hieblmi hieblmi requested a review from yyforyongyu July 26, 2024 15:32
@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch 6 times, most recently from aa68ded to 0dc2414 Compare July 28, 2024 13:11
@hieblmi hieblmi added this to the v0.18.3 milestone Jul 28, 2024
Copy link
Collaborator

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, only a few comments in the itest.

// FAILURE_REASON_CANCELED. This failure reason indicates that the context was
// cancelled manually by the user. It does not interrupt the current payment
// attempt, but will prevent any further payment attempts. The test steps are:
// 1.) Alice pays Carol's invoice through Bob.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice👍

itest/lnd_payment_test.go Show resolved Hide resolved
cancelInterceptor()

// Make sure all goroutines are finished.
select {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unnecessary as we know paymentStream was returned in the above goroutine.

func (h *HarnessRPC) SendPaymentWithContext(context context.Context,
req *routerrpc.SendPaymentRequest) PaymentClient {

require.NotNil(h.T, context, "context must not be nil")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


var preimage lntypes.Preimage
copy(preimage[:], invoice.RPreimage)
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_IN_FLIGHT)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method already returns a payment and we can assert the failure reason directly via payment.FailureReason.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works here, but below we need to call AssertPaymentFailureReason since the failure reason update on the payment doesn't seem to happen immediately.

@@ -1639,6 +1639,19 @@ func (h *HarnessTest) AssertPaymentStatus(hn *node.HarnessNode,
return target
}

// AssertPaymentFailureReason asserts that the given node lists a payment with
// the given preimage which has the expected failure reason.
func (h *HarnessTest) AssertPaymentFailureReason(hn *node.HarnessNode,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this assertion is unnecessary since we always have the payment and can assert the fields on it directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not addressed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the Predicate but for mentioned reason I couldn't remove the assertion.

preimage lntypes.Preimage, reason lnrpc.PaymentFailureReason) {

payHash := preimage.Hash()
err := wait.Predicate(func() bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

future tips: we almost never use Predicate and favor NoError as it gives us more descriptive error - maybe we should remove it someday.

Copy link
Collaborator

@bitromortac bitromortac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, almost ready 👍. Have a suggestion concerning the itest

@@ -772,17 +772,17 @@ func TestResumePaymentFailContextCancel(t *testing.T) {
cancel()

m.control.On(
"FailPayment", p.identifier, channeldb.FailureReasonTimeout,
"FailPayment", p.identifier, channeldb.FailureReasonCanceled,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you squash this with the previous commit, to make the unit test pass there? I think it's important to let every unit test pass in each commit, to make bisecting of commits easier

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡

itest/lnd_payment_test.go Show resolved Hide resolved
@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch from 0dc2414 to 9c97a61 Compare July 30, 2024 18:49
@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch 2 times, most recently from 81589c4 to b55ff63 Compare July 30, 2024 20:21
Copy link
Collaborator

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One final comment, otherwise good to go!

require.NoError(ht, err, "failed to send request")

// Assert that the payment status is as expected.
ht.AssertPaymentStatus(alice, preimage, expectedPaymentStatus)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we can grab the payment here and check its FailureReason below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status on the payment here is not set yet, hence waiting for it in the extra assertion, see comment #8836 (comment)

@@ -1639,6 +1639,19 @@ func (h *HarnessTest) AssertPaymentStatus(hn *node.HarnessNode,
return target
}

// AssertPaymentFailureReason asserts that the given node lists a payment with
// the given preimage which has the expected failure reason.
func (h *HarnessTest) AssertPaymentFailureReason(hn *node.HarnessNode,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not addressed

@hieblmi hieblmi force-pushed the payment-failure-reason-cancel branch from b55ff63 to 808d958 Compare August 1, 2024 08:27
Copy link
Collaborator

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! LGTM🛳️

Copy link
Collaborator

@bitromortac bitromortac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good 🎉, but technically this is a breaking change, should somebody rely on the timeout failure code which now becomes canceled (in some cases). So therefore v0.18.3 may not be the best release for that.

@Roasbeef Roasbeef merged commit c46b1a4 into lightningnetwork:master Aug 1, 2024
32 of 34 checks passed
@hieblmi hieblmi deleted the payment-failure-reason-cancel branch August 3, 2024 06:21
@hieblmi
Copy link
Collaborator Author

hieblmi commented Aug 3, 2024

The PR looks good 🎉, but technically this is a breaking change, should somebody rely on the timeout failure code which now becomes canceled (in some cases). So therefore v0.18.3 may not be the best release for that.

canceled only appears if the new cancelable flag on the payment request is set, so that it doesn't break old clients. Is your concern still valid then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants