Skip to content

Commit

Permalink
Merge pull request #879 from lightninglabs/assert-payment-invoice-htlcs
Browse files Browse the repository at this point in the history
[custom channels]: in itest assert payment and invoice HTLCs show custom data
  • Loading branch information
guggero authored Oct 30, 2024
2 parents 25d64a1 + 2e30a6d commit 9d2c79d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
47 changes: 47 additions & 0 deletions itest/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/lightninglabs/taproot-assets/tapscript"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lntest/rpc"
"github.com/lightningnetwork/lnd/lntest/wait"
Expand Down Expand Up @@ -924,6 +925,52 @@ func createAssetInvoice(t *testing.T, dstRfqPeer, dst *HarnessNode,
return resp.InvoiceResult
}

// assertInvoiceHtlcAssets makes sure the invoice with the given hash shows the
// individual HTLCs that arrived for it and that they show the correct asset
// amounts for the given ID when decoded.
func assertInvoiceHtlcAssets(t *testing.T, node *HarnessNode,
addedInvoice *lnrpc.AddInvoiceResponse, assetID []byte,
assetAmount uint64) {

ctxb := context.Background()
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()

invoice, err := node.InvoicesClient.LookupInvoiceV2(
ctxt, &invoicesrpc.LookupInvoiceMsg{
InvoiceRef: &invoicesrpc.LookupInvoiceMsg_PaymentAddr{
PaymentAddr: addedInvoice.PaymentAddr,
},
},
)
require.NoError(t, err)
require.NotEmpty(t, invoice.Htlcs)

t.Logf("Asset invoice: %v", toProtoJSON(t, invoice))

targetID := hex.EncodeToString(assetID)

var totalAssetAmount uint64
for _, htlc := range invoice.Htlcs {
require.NotEmpty(t, htlc.CustomChannelData)

jsonHtlc := &rfqmsg.JsonHtlc{}
err := json.Unmarshal(htlc.CustomChannelData, jsonHtlc)
require.NoError(t, err)

for _, balance := range jsonHtlc.Balances {
if balance.AssetID != targetID {
continue
}

totalAssetAmount += balance.Amount
}
}

// Due to rounding we allow up to 1 unit of error.
require.InDelta(t, assetAmount, totalAssetAmount, 1)
}

// assertPaymentHtlcAssets makes sure the payment with the given hash shows the
// individual HTLCs that arrived for it and that they show the correct asset
// amounts for the given ID when decoded.
Expand Down
51 changes: 46 additions & 5 deletions itest/litd_custom_channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ func testCustomChannelsLarge(_ context.Context, net *NetworkHarness,
payInvoiceWithAssets(t.t, charlie, dave, invoiceResp3, assetID, false)
logBalance(t.t, nodes, assetID, "after invoice 3")

// Make sure the invoice on the receiver side and the payment on the
// sender side show the individual HTLCs that arrived for it and that
// they show the correct asset amounts when decoded.
assertInvoiceHtlcAssets(
t.t, dave, invoiceResp3, assetID, largeInvoiceAmount,
)
assertPaymentHtlcAssets(
t.t, charlie, invoiceResp3.RHash, assetID, largeInvoiceAmount,
)

// We keysend the rest, so that all the balance is on Dave's side.
charlieRemainingBalance := charlieFundingAmount - largeInvoiceAmount -
fabiaInvoiceAssetAmount/2
Expand Down Expand Up @@ -422,6 +432,16 @@ func testCustomChannels(_ context.Context, net *NetworkHarness,
payInvoiceWithAssets(t.t, dave, charlie, invoiceResp, assetID, true)
logBalance(t.t, nodes, assetID, "after invoice back")

// Make sure the invoice on the receiver side and the payment on the
// sender side show the individual HTLCs that arrived for it and that
// they show the correct asset amounts when decoded.
assertInvoiceHtlcAssets(
t.t, charlie, invoiceResp, assetID, charlieInvoiceAmount,
)
assertPaymentHtlcAssets(
t.t, dave, invoiceResp.RHash, assetID, charlieInvoiceAmount,
)

charlieAssetBalance += charlieInvoiceAmount
daveAssetBalance -= charlieInvoiceAmount

Expand Down Expand Up @@ -897,6 +917,16 @@ func testCustomChannelsGroupedAsset(_ context.Context, net *NetworkHarness,
payInvoiceWithAssets(t.t, charlie, dave, invoiceResp, assetID, true)
logBalance(t.t, nodes, assetID, "after invoice")

// Make sure the invoice on the receiver side and the payment on the
// sender side show the individual HTLCs that arrived for it and that
// they show the correct asset amounts when decoded.
assertInvoiceHtlcAssets(
t.t, dave, invoiceResp, assetID, daveInvoiceAssetAmount,
)
assertPaymentHtlcAssets(
t.t, charlie, invoiceResp.RHash, assetID, daveInvoiceAssetAmount,
)

charlieAssetBalance -= daveInvoiceAssetAmount
daveAssetBalance += daveInvoiceAssetAmount

Expand Down Expand Up @@ -1876,27 +1906,38 @@ func testCustomChannelsLiquidityEdgeCases(_ context.Context,
"invoice, multi-hop)")

// Edge case: Big asset invoice paid by direct peer with assets.
const bigAssetAmount = 100_000
invoiceResp := createAssetInvoice(
t.t, charlie, dave, 100_000, assetID,
t.t, charlie, dave, bigAssetAmount, assetID,
)

payInvoiceWithAssets(t.t, charlie, dave, invoiceResp, assetID, false)

logBalance(t.t, nodes, assetID, "after big asset payment (asset "+
"invoice, direct)")

// Make sure the invoice on the receiver side and the payment on the
// sender side show the individual HTLCs that arrived for it and that
// they show the correct asset amounts when decoded.
assertInvoiceHtlcAssets(
t.t, dave, invoiceResp, assetID, bigAssetAmount,
)
assertPaymentHtlcAssets(
t.t, charlie, invoiceResp.RHash, assetID, bigAssetAmount,
)

// Edge case: Big normal invoice, paid by direct channel peer with
// assets.
const hugeAssetAmount = 1_000_000
_ = createAndPayNormalInvoice(
t.t, dave, charlie, charlie, 1_000_000, assetID, true,
t.t, dave, charlie, charlie, hugeAssetAmount, assetID, true,
)

logBalance(t.t, nodes, assetID, "after big asset payment (btc "+
"invoice, direct)")

// Dave sends 200k assets and 2k sats to Yara.
sendAssetKeySendPayment(
t.t, dave, yara, 200_000, assetID,
t.t, dave, yara, 2*bigAssetAmount, assetID,
fn.None[int64](), lnrpc.Payment_SUCCEEDED,
fn.None[lnrpc.PaymentFailureReason](),
)
Expand All @@ -1909,7 +1950,7 @@ func testCustomChannelsLiquidityEdgeCases(_ context.Context,
// channels, where the total asset value exceeds the btc capacity of the
// channels.
invoiceResp = createAssetInvoice(
t.t, dave, charlie, 100_000, assetID,
t.t, dave, charlie, bigAssetAmount, assetID,
)

payInvoiceWithAssets(t.t, yara, dave, invoiceResp, assetID, false)
Expand Down

0 comments on commit 9d2c79d

Please sign in to comment.