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

router: add new interface MPPayment #7391

Merged

Conversation

yyforyongyu
Copy link
Member

@yyforyongyu yyforyongyu commented Feb 8, 2023

Depends on,

And prepares for.

This PR continues refactoring the payment lifecycle to prepare the coming fixes in the above PR. It moves payment state management into MPPayment, and further makes MPPayment into an interface so it's easier to write unit tests for the payment lifecycle.

@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch 7 times, most recently from 13251be to 1436be6 Compare February 9, 2023 05:33
@yyforyongyu yyforyongyu added this to the v0.16.0 milestone Feb 9, 2023
@yyforyongyu yyforyongyu added routing payments Related to invoices/payments refactoring labels Feb 9, 2023
@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch from 1436be6 to 7475af9 Compare February 14, 2023 08:08
@saubyk saubyk modified the milestones: v0.16.0, v0.16.1 Feb 14, 2023
@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch from 7475af9 to c4f416c Compare February 21, 2023 18:13
@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch 5 times, most recently from d271cf5 to fc51882 Compare March 6, 2023 09:10
@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch from fc51882 to 8a55a1c Compare March 10, 2023 09:33
@saubyk saubyk modified the milestones: v0.16.1, v0.17.0 Mar 16, 2023
Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

Commits are really smooth to digest very clean work 👍 . Left some questions to deepen my understanding. Just reviewing this PR I had a bit of a hard time bringing it all into the greater picture.

Did a first pass-through for the review club today will come back and for a second review :)

channeldb/mp_payment.go Outdated Show resolved Hide resolved
routing/payment_lifecycle.go Outdated Show resolved Hide resolved
routing/payment_lifecycle.go Outdated Show resolved Hide resolved
channeldb/mp_payment.go Outdated Show resolved Hide resolved
channeldb/mp_payment_test.go Outdated Show resolved Hide resolved
channeldb/mp_payment.go Show resolved Hide resolved
channeldb/mp_payment.go Show resolved Hide resolved
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 simplifications 💪, first pass done. I have peeked inside the next PR as well, but need another round to see the whole picture more clearly (the end result of the payment lifecycle and its tests look awesome). Perhaps you could update the PR description as well to guide through the most important refactors.

routing/payment_lifecycle.go Outdated Show resolved Hide resolved
routing/payment_lifecycle.go Outdated Show resolved Hide resolved
routing/payment_lifecycle.go Outdated Show resolved Hide resolved
routing/payment_lifecycle.go Show resolved Hide resolved
channeldb/mp_payment.go Outdated Show resolved Hide resolved
channeldb/mp_payment.go Show resolved Hide resolved
routing/payment_lifecycle.go Outdated Show resolved Hide resolved
routing/router_test.go Show resolved Hide resolved
channeldb/mp_payment.go Show resolved Hide resolved
routing/payment_lifecycle_test.go Show resolved Hide resolved
@yyforyongyu yyforyongyu requested review from ziggie1984 and bitromortac and removed request for bhandras and ziggie1984 September 10, 2023 00:53
@yyforyongyu
Copy link
Member Author

Thank you for the thorough reviews @ziggie1984 @bitromortac 🙏 Think all comments are addressed, let me know if anything is missing!

@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch from 33d5863 to 0bc9fee Compare September 10, 2023 01:24
Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

After reviewing the follow-up PR, the payment lifecycle looks really clean now 👌 LGTM

@lightninglabs-deploy
Copy link

@bitromortac: review reminder

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.

Looks good, just a final question about an edge case


// Update the payment state and status.
m.State = &MPPaymentState{
NumAttemptsInFlight: len(m.InFlightHTLCs()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I just thought about removing the setState methods and inline them in fetchPayment, it seems they are not used for mocking, but not blocking


// If the payment is already in a terminal state, no need to wait.
case StatusSucceeded:
case StatusFailed:
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the remaining amount is zero, it means that there are either HTLCs inflight or that all HTLCs are settled, so can the payment then be failed? It seems like we should error here, looks like we can handle that case together with StatusInitiated

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch! Yeah indeed this is an error state and we should return an error, updated!

This commit moves the creations of hop and htlcAdd message from
`createNewPaymentAttempt` to `sendPaymentAttempt` to clean up the code
and further pave the way to decomposite the lifecycle.
This commit renames the `handleSendError` to be `handleSwitchErr` to
explicitly express that it's handling the error from htlcswitch.
This commit moves the struct `paymentState` used in `routing` into
`channeldb` and replaces it with `MPPaymentState`. In the following
commit we'd see the benefit, that we don't need to pass variables back
and forth between the two packages. More importantly, this state is put
closer to its origin, and is strictly updated whenever a payment is read
from disk. This approach is less error-prone comparing to the previous
one, which both the `payment` and `paymentState` need to be updated at
the same time to make sure the data stay consistant in a parallel
environment.
@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch from 0bc9fee to 9531f6f Compare September 21, 2023 14:24
@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch from 9531f6f to 47a1952 Compare September 21, 2023 15:14
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.

LGTM 🎉

@guggero
Copy link
Collaborator

guggero commented Sep 22, 2023

Linter complains about a newline. Can merge after that's fixed.

This commit adds a new method, `NeedWaitAttempts`, to properly decide
whether we need to wait for the outcome of htlc attempts based on the
payment's current state.
This commit turns `MPPayment` into an interface inside `routing`. Having
this interface gives us the benefit to write more granular unit tests
inside payment lifecycle. As seen from the modified unit tests, several
hacky ways of testing the `SendPayment` method is now replaced by a mock
over `MPPayment`.
This commit adds a new method to properly init a payment lifecycle so we
can easily see the default values used here.
This commit refactors the params used in lifecycle to prefer
`HTLCAttempt` over `HTLCAttemptInfo`. This change is needed as
`HTLCAttempt` also wraps settled and failure info, which is useful in
the following commits.
@yyforyongyu yyforyongyu force-pushed the refactor-payment-lifecycle branch from 47a1952 to 4a7f88e Compare September 25, 2023 13:42
@guggero guggero merged commit 4b69391 into lightningnetwork:0-18-staging Sep 25, 2023
@yyforyongyu yyforyongyu deleted the refactor-payment-lifecycle branch September 25, 2023 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants