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

tapchannel: enforce strict forwarding for asset invoices #1144

Merged
merged 5 commits into from
Oct 31, 2024

Conversation

GeorgeTsagk
Copy link
Member

@GeorgeTsagk GeorgeTsagk commented Oct 8, 2024

This PR adds a few extra steps to the AuxInvoiceManager before it settles an invoice. Previously we wouldn't check that we received an asset HTLC for an asset invoice, leading to some undesired behavior.

With these changes we enforce that if the invoice corresponds to an asset invoice then we only settle it if we received assets.

Todo:

  • Tests

Closes #1008

@GeorgeTsagk GeorgeTsagk self-assigned this Oct 8, 2024
@coveralls
Copy link

coveralls commented Oct 8, 2024

Pull Request Test Coverage Report for Build 11614783416

Details

  • 46 of 50 (92.0%) changed or added relevant lines in 2 files are covered.
  • 25 unchanged lines in 7 files lost coverage.
  • Overall coverage increased (+0.3%) to 40.658%

Changes Missing Coverage Covered Lines Changed/Added Lines %
rfq/manager.go 0 4 0.0%
Files with Coverage Reduction New Missed Lines %
commitment/tap.go 1 84.17%
rfq/manager.go 2 0.0%
asset/asset.go 2 81.8%
tapdb/universe.go 4 80.91%
tapgarden/caretaker.go 4 68.5%
tapchannel/aux_leaf_signer.go 5 35.92%
tapdb/multiverse.go 7 60.32%
Totals Coverage Status
Change from base Build 11599236597: 0.3%
Covered Lines: 24595
Relevant Lines: 60493

💛 - Coveralls

@dstadulis
Copy link
Collaborator

Before assigning reviewers, tests will need to be added.

@GeorgeTsagk GeorgeTsagk requested review from guggero and ffranr October 15, 2024 19:15
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Nice approach! And great to have a unit test. Though I wonder what would actually happen on the sender and receiver side with this change? My assumption is that things would just time out (rather than rejecting the HTLC).
So I think we should also have an integration test for this.

tapchannel/aux_invoice_manager.go Outdated Show resolved Hide resolved
sellQuote, isSell := acceptedSellQuotes[rfqmsg.SerialisedScid(scid)]

switch {
case isBuy:
Copy link
Member

Choose a reason for hiding this comment

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

I wonder: Shouldn't an invoice always relate to a buy order only? Even in a direct peer payment, when I create an invoice, there should be a buy quote for it.

Copy link
Member

Choose a reason for hiding this comment

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

Still unaddressed. If there's no good reason why we'd eve want to check a sell order, I think we should change this to only look at buy orders.

Copy link
Member

Choose a reason for hiding this comment

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

Turns out I was wrong here! In case of a direct-peer payment through an asset invoice, it can actually be a sell order SCID. The itest failed here, so will update it.

tapchannel/aux_invoice_manager.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
@GeorgeTsagk
Copy link
Member Author

An itest that covers this behavior has been added in a litd PR

@GeorgeTsagk GeorgeTsagk requested review from guggero and jharveyb and removed request for ffranr October 17, 2024 09:03
@GeorgeTsagk
Copy link
Member Author

Will enhance the HtlcModify interface to explicitly allow cancelling of HTLCs

@jharveyb
Copy link
Contributor

jharveyb commented Oct 21, 2024

A failing case (with the PR as-is, I think this seed is invalid if we change any of the generators):

go test -run TestAuxInvoiceManagerProperty -rapid.seed 11951275822997898772

Which fails here:

https://github.com/GeorgeTsagk/taproot-assets/blob/a1e4157e0b0905bbb76595485cee268402428fd2/tapchannel/aux_invoice_manager_test.go#L161

Discovered after running the test for 100_000 iterations. Given how fast each case is, I would recommend running the prop tests for at least a few CPU-minutes. For me that's at least a few million checks. The default is 100, which won't find much given the complexity of these tests / state space for generation.

To run for more iterations:

cd tapchannel
go test -run TestAuxInvoiceManagerProperty -rapid.checks=100_000

@jharveyb jharveyb self-requested a review October 21, 2024 21:21
Copy link
Contributor

@jharveyb jharveyb left a comment

Choose a reason for hiding this comment

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

Solid start with the property tests! Definitely headed in the right direction.

The actual implementation changes look reasonable, left some notes on test improvements.

tapchannel/aux_invoice_manager_test.go Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
@guggero guggero force-pushed the update-to-lnd-18-4 branch 2 times, most recently from 1bbe907 to d74dd65 Compare October 22, 2024 10:44
@guggero guggero deleted the branch lightninglabs:main October 22, 2024 14:36
@guggero guggero closed this Oct 22, 2024
@guggero guggero reopened this Oct 22, 2024
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Looks very close! Will do final review once dependencies are in.

tapchannel/aux_invoice_manager.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager.go Outdated Show resolved Hide resolved
sellQuote, isSell := acceptedSellQuotes[rfqmsg.SerialisedScid(scid)]

switch {
case isBuy:
Copy link
Member

Choose a reason for hiding this comment

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

Still unaddressed. If there's no good reason why we'd eve want to check a sell order, I think we should change this to only look at buy orders.

tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager_test.go Outdated Show resolved Hide resolved
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

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

LGTM, just a couple of last nits (and one CI failure).

tapchannel/aux_invoice_manager.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager.go Outdated Show resolved Hide resolved
tapchannel/aux_invoice_manager.go Outdated Show resolved Hide resolved
go.sum Show resolved Hide resolved
Copy link
Contributor

@jharveyb jharveyb left a comment

Choose a reason for hiding this comment

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

Resetting my reviewer state.

@guggero guggero requested a review from jharveyb October 31, 2024 10:52
@guggero
Copy link
Member

guggero commented Oct 31, 2024

Okay, I rolled back the isBuy/isSell change since that made the itest fail.

I should've just read the Godoc of the method, it was always right there 😅

// priceFromQuote retrieves the price from the accepted quote for the given RFQ
// ID. We allow the quote to either be a buy or a sell quote, since we don't
// know if this is a direct peer payment or a payment that is routed through the
// multiple hops. If it's a direct peer payment, then the quote will be a sell
// quote, since that's what the peer created to find out how many units to send
// for an invoice denominated in BTC.

Copy link
Contributor

@jharveyb jharveyb left a comment

Choose a reason for hiding this comment

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

LGTM! Generators look a lot better.

I think we can still simplify them more with other rapid built-ins but this definitely works for now.

// TestAuxInvoiceManagerProperty runs property based tests on the
// AuxInvoiceManager.
func TestAuxInvoiceManagerProperty(t *testing.T) {
t.Parallel()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think rapid tests are not thread safe? Not sure if that meant between multiple rapid tests though, or just any other test.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that's possible. But the t.Parallel() only means other tests in this file can run in parallel to the rapid test. And since we don't have any other rapid tests running there, this should be fine.

@jharveyb
Copy link
Contributor

Tested for 2,000,000 iterations, looks good!

@guggero guggero merged commit 588e58b into lightninglabs:main Oct 31, 2024
18 checks passed
@Roasbeef
Copy link
Member

Wanted to provide a bit of drive by review here, re the usage of property based tests.

I think the best way to approach property bsaed tests is from a black-box angle. So you come up with some invariants a given function/method should hold, then generate random inputs, prune that space to be relevant, and assert the output based on input.

A trivial example of this is encoding, we want decode(encode(x)) == x. That just asserts that the functions should do, now how they do it (which internal fields they update, if they use a buffer or not, etc, etc).

Glancing at this PR, I think the property tests are too white box, and too coarse. They use state in between each test to execute assertions, and re-compute a lot of intermediate values to attempt to assert correct behavior. In short, they're written in "normal" white box manner.

If we take a step back and think about what we wanted here, we want an invariant that goes something like:

  • Given an incoming HTLC, and an invoice that may correspond to that HTLC, we only accept the HTLC if the payload in the HTLC matches the asset ID expectation in the invoice.

This is the invariant that would prevent a known payment hash from being accepted with a normal HTLC, rather than an asset HTLC.

If we look at the commit that added this logic, we see this section was changed: ad05718#diff-5761d8e06dd213a5ff31aca2abe70153485d5d7832da64b35d371c0303e42bbdR156-R167. isAssetInvoice is what's meant to enforce that invariant. So that's the smallest minimal unit we need to test here. Instead of trying to mock/test the much alrge system of the invoice manager, we could just focus on proving the input needed for that routine, and assert the output.

Then we can go another step up to also test the htlc wire custom records recognition. To do this, we'd need to break up the handleInvoiceAccept function into smaller components. Then we can write property tests on each of the components, along with the relevant composition of each component.

If we wanted to test the bit that handles summing up the set of HTLCs, then rather than re-implement the logic to do a white box test, we can use another invariant. This might be somewhere along the lines of:

  • Only once a set of HTLCs that sum up to the invoice amount are extended do we accept the HTLC.

@Roasbeef
Copy link
Member

The package also has some support for testing more stateful machines which may be useful in the future, haven't tried it out myself though: https://pkg.go.dev/pgregory.net/rapid#T.Repeat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

tapchannel/[feature]: update AuxInvoiceManager to enforce strict forwarding for invoices
6 participants