Skip to content

Commit 2e30a6d

Browse files
committed
itest: assert invoice and payment HTLCs show custom data
With this commit we sporadically assert in our itests that the outgoing HTLCs for a payment and the incoming HTLCs of an invoice show the custom channel data JSON with the actual asset amounts.
1 parent 25d64a1 commit 2e30a6d

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

itest/assets_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/lightninglabs/taproot-assets/tapscript"
3434
"github.com/lightningnetwork/lnd/fn"
3535
"github.com/lightningnetwork/lnd/lnrpc"
36+
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
3637
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
3738
"github.com/lightningnetwork/lnd/lntest/rpc"
3839
"github.com/lightningnetwork/lnd/lntest/wait"
@@ -924,6 +925,52 @@ func createAssetInvoice(t *testing.T, dstRfqPeer, dst *HarnessNode,
924925
return resp.InvoiceResult
925926
}
926927

928+
// assertInvoiceHtlcAssets makes sure the invoice with the given hash shows the
929+
// individual HTLCs that arrived for it and that they show the correct asset
930+
// amounts for the given ID when decoded.
931+
func assertInvoiceHtlcAssets(t *testing.T, node *HarnessNode,
932+
addedInvoice *lnrpc.AddInvoiceResponse, assetID []byte,
933+
assetAmount uint64) {
934+
935+
ctxb := context.Background()
936+
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
937+
defer cancel()
938+
939+
invoice, err := node.InvoicesClient.LookupInvoiceV2(
940+
ctxt, &invoicesrpc.LookupInvoiceMsg{
941+
InvoiceRef: &invoicesrpc.LookupInvoiceMsg_PaymentAddr{
942+
PaymentAddr: addedInvoice.PaymentAddr,
943+
},
944+
},
945+
)
946+
require.NoError(t, err)
947+
require.NotEmpty(t, invoice.Htlcs)
948+
949+
t.Logf("Asset invoice: %v", toProtoJSON(t, invoice))
950+
951+
targetID := hex.EncodeToString(assetID)
952+
953+
var totalAssetAmount uint64
954+
for _, htlc := range invoice.Htlcs {
955+
require.NotEmpty(t, htlc.CustomChannelData)
956+
957+
jsonHtlc := &rfqmsg.JsonHtlc{}
958+
err := json.Unmarshal(htlc.CustomChannelData, jsonHtlc)
959+
require.NoError(t, err)
960+
961+
for _, balance := range jsonHtlc.Balances {
962+
if balance.AssetID != targetID {
963+
continue
964+
}
965+
966+
totalAssetAmount += balance.Amount
967+
}
968+
}
969+
970+
// Due to rounding we allow up to 1 unit of error.
971+
require.InDelta(t, assetAmount, totalAssetAmount, 1)
972+
}
973+
927974
// assertPaymentHtlcAssets makes sure the payment with the given hash shows the
928975
// individual HTLCs that arrived for it and that they show the correct asset
929976
// amounts for the given ID when decoded.

itest/litd_custom_channels_test.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,16 @@ func testCustomChannelsLarge(_ context.Context, net *NetworkHarness,
243243
payInvoiceWithAssets(t.t, charlie, dave, invoiceResp3, assetID, false)
244244
logBalance(t.t, nodes, assetID, "after invoice 3")
245245

246+
// Make sure the invoice on the receiver side and the payment on the
247+
// sender side show the individual HTLCs that arrived for it and that
248+
// they show the correct asset amounts when decoded.
249+
assertInvoiceHtlcAssets(
250+
t.t, dave, invoiceResp3, assetID, largeInvoiceAmount,
251+
)
252+
assertPaymentHtlcAssets(
253+
t.t, charlie, invoiceResp3.RHash, assetID, largeInvoiceAmount,
254+
)
255+
246256
// We keysend the rest, so that all the balance is on Dave's side.
247257
charlieRemainingBalance := charlieFundingAmount - largeInvoiceAmount -
248258
fabiaInvoiceAssetAmount/2
@@ -422,6 +432,16 @@ func testCustomChannels(_ context.Context, net *NetworkHarness,
422432
payInvoiceWithAssets(t.t, dave, charlie, invoiceResp, assetID, true)
423433
logBalance(t.t, nodes, assetID, "after invoice back")
424434

435+
// Make sure the invoice on the receiver side and the payment on the
436+
// sender side show the individual HTLCs that arrived for it and that
437+
// they show the correct asset amounts when decoded.
438+
assertInvoiceHtlcAssets(
439+
t.t, charlie, invoiceResp, assetID, charlieInvoiceAmount,
440+
)
441+
assertPaymentHtlcAssets(
442+
t.t, dave, invoiceResp.RHash, assetID, charlieInvoiceAmount,
443+
)
444+
425445
charlieAssetBalance += charlieInvoiceAmount
426446
daveAssetBalance -= charlieInvoiceAmount
427447

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

920+
// Make sure the invoice on the receiver side and the payment on the
921+
// sender side show the individual HTLCs that arrived for it and that
922+
// they show the correct asset amounts when decoded.
923+
assertInvoiceHtlcAssets(
924+
t.t, dave, invoiceResp, assetID, daveInvoiceAssetAmount,
925+
)
926+
assertPaymentHtlcAssets(
927+
t.t, charlie, invoiceResp.RHash, assetID, daveInvoiceAssetAmount,
928+
)
929+
900930
charlieAssetBalance -= daveInvoiceAssetAmount
901931
daveAssetBalance += daveInvoiceAssetAmount
902932

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

18781908
// Edge case: Big asset invoice paid by direct peer with assets.
1909+
const bigAssetAmount = 100_000
18791910
invoiceResp := createAssetInvoice(
1880-
t.t, charlie, dave, 100_000, assetID,
1911+
t.t, charlie, dave, bigAssetAmount, assetID,
18811912
)
1882-
18831913
payInvoiceWithAssets(t.t, charlie, dave, invoiceResp, assetID, false)
18841914

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

1918+
// Make sure the invoice on the receiver side and the payment on the
1919+
// sender side show the individual HTLCs that arrived for it and that
1920+
// they show the correct asset amounts when decoded.
1921+
assertInvoiceHtlcAssets(
1922+
t.t, dave, invoiceResp, assetID, bigAssetAmount,
1923+
)
1924+
assertPaymentHtlcAssets(
1925+
t.t, charlie, invoiceResp.RHash, assetID, bigAssetAmount,
1926+
)
1927+
18881928
// Edge case: Big normal invoice, paid by direct channel peer with
18891929
// assets.
1930+
const hugeAssetAmount = 1_000_000
18901931
_ = createAndPayNormalInvoice(
1891-
t.t, dave, charlie, charlie, 1_000_000, assetID, true,
1932+
t.t, dave, charlie, charlie, hugeAssetAmount, assetID, true,
18921933
)
18931934

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

18971938
// Dave sends 200k assets and 2k sats to Yara.
18981939
sendAssetKeySendPayment(
1899-
t.t, dave, yara, 200_000, assetID,
1940+
t.t, dave, yara, 2*bigAssetAmount, assetID,
19001941
fn.None[int64](), lnrpc.Payment_SUCCEEDED,
19011942
fn.None[lnrpc.PaymentFailureReason](),
19021943
)
@@ -1909,7 +1950,7 @@ func testCustomChannelsLiquidityEdgeCases(_ context.Context,
19091950
// channels, where the total asset value exceeds the btc capacity of the
19101951
// channels.
19111952
invoiceResp = createAssetInvoice(
1912-
t.t, dave, charlie, 100_000, assetID,
1953+
t.t, dave, charlie, bigAssetAmount, assetID,
19131954
)
19141955

19151956
payInvoiceWithAssets(t.t, yara, dave, invoiceResp, assetID, false)

0 commit comments

Comments
 (0)