Skip to content

Commit 33f4ef3

Browse files
committed
itest: add assets to btc self-payment test case
1 parent d3e9d27 commit 33f4ef3

File tree

2 files changed

+112
-14
lines changed

2 files changed

+112
-14
lines changed

itest/assets_test.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,20 @@ func assertChannelAssetBalanceWithDelta(t *testing.T, node *HarnessNode,
12821282
require.InDelta(t, remote, assetBalance.RemoteBalance, delta)
12831283
}
12841284

1285+
func channelAssetBalance(t *testing.T, node *HarnessNode,
1286+
chanPoint *lnrpc.ChannelPoint) (uint64, uint64) {
1287+
1288+
targetChan := fetchChannel(t, node, chanPoint)
1289+
1290+
var assetBalance rfqmsg.JsonAssetChannel
1291+
err := json.Unmarshal(targetChan.CustomChannelData, &assetBalance)
1292+
require.NoError(t, err)
1293+
1294+
require.GreaterOrEqual(t, len(assetBalance.FundingAssets), 1)
1295+
1296+
return assetBalance.LocalBalance, assetBalance.RemoteBalance
1297+
}
1298+
12851299
// addRoutingFee adds the default routing fee (1 part per million fee rate plus
12861300
// 1000 milli-satoshi base fee) to the given milli-satoshi amount.
12871301
func addRoutingFee(amt lnwire.MilliSatoshi) lnwire.MilliSatoshi {
@@ -1321,6 +1335,8 @@ func sendAssetKeySendPayment(t *testing.T, src, dst *HarnessNode, amt uint64,
13211335
PaymentHash: hash[:],
13221336
TimeoutSeconds: int32(PaymentTimeout.Seconds()),
13231337
MaxParts: cfg.maxShards,
1338+
OutgoingChanIds: cfg.outgoingChanIDs,
1339+
AllowSelfPayment: cfg.allowSelfPayment,
13241340
}
13251341

13261342
request := &tchrpc.SendPaymentRequest{
@@ -1402,17 +1418,24 @@ func createAndPayNormalInvoiceWithBtc(t *testing.T, src, dst *HarnessNode,
14021418
}
14031419

14041420
func createNormalInvoice(t *testing.T, dst *HarnessNode,
1405-
amountSat btcutil.Amount) *lnrpc.AddInvoiceResponse {
1421+
amountSat btcutil.Amount,
1422+
opts ...invoiceOpt) *lnrpc.AddInvoiceResponse {
1423+
1424+
cfg := defaultInvoiceConfig()
1425+
for _, opt := range opts {
1426+
opt(cfg)
1427+
}
14061428

14071429
ctxb := context.Background()
14081430
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
14091431
defer cancel()
14101432

14111433
expirySeconds := 10
14121434
invoiceResp, err := dst.AddInvoice(ctxt, &lnrpc.Invoice{
1413-
Value: int64(amountSat),
1414-
Memo: "normal invoice",
1415-
Expiry: int64(expirySeconds),
1435+
Value: int64(amountSat),
1436+
Memo: "normal invoice",
1437+
Expiry: int64(expirySeconds),
1438+
RouteHints: cfg.routeHints,
14161439
})
14171440
require.NoError(t, err)
14181441

@@ -1448,20 +1471,13 @@ func payPayReqWithSatoshi(t *testing.T, payer *HarnessNode, payReq string,
14481471
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
14491472
defer cancel()
14501473

1451-
shardSize := uint64(0)
1452-
1453-
if cfg.smallShards {
1454-
shardSize = 80_000_000
1455-
}
1456-
14571474
sendReq := &routerrpc.SendPaymentRequest{
14581475
PaymentRequest: payReq,
14591476
TimeoutSeconds: int32(PaymentTimeout.Seconds()),
14601477
FeeLimitMsat: 1_000_000,
14611478
MaxParts: cfg.maxShards,
14621479
OutgoingChanIds: cfg.outgoingChanIDs,
14631480
AllowSelfPayment: cfg.allowSelfPayment,
1464-
MaxShardSizeMsat: shardSize,
14651481
}
14661482

14671483
if cfg.smallShards {
@@ -1652,6 +1668,8 @@ func payInvoiceWithAssets(t *testing.T, payer, rfqPeer *HarnessNode,
16521668
FeeLimitMsat: int64(cfg.feeLimit),
16531669
DestCustomRecords: cfg.destCustomRecords,
16541670
MaxParts: cfg.maxShards,
1671+
OutgoingChanIds: cfg.outgoingChanIDs,
1672+
AllowSelfPayment: cfg.allowSelfPayment,
16551673
}
16561674

16571675
if cfg.smallShards {
@@ -1768,6 +1786,12 @@ func withMsatAmount(amt uint64) invoiceOpt {
17681786
}
17691787
}
17701788

1789+
func withRouteHints(hints []*lnrpc.RouteHint) invoiceOpt {
1790+
return func(c *invoiceConfig) {
1791+
c.routeHints = hints
1792+
}
1793+
}
1794+
17711795
func createAssetInvoice(t *testing.T, dstRfqPeer, dst *HarnessNode,
17721796
assetAmount uint64, assetID []byte,
17731797
opts ...invoiceOpt) *lnrpc.AddInvoiceResponse {
@@ -1795,14 +1819,14 @@ func createAssetInvoice(t *testing.T, dstRfqPeer, dst *HarnessNode,
17951819
dstTapd := newTapClient(t, dst)
17961820

17971821
request := &tchrpc.AddInvoiceRequest{
1798-
GroupKey: cfg.groupKey,
17991822
AssetAmount: assetAmount,
18001823
PeerPubkey: peerPubKey,
18011824
InvoiceRequest: &lnrpc.Invoice{
18021825
Memo: fmt.Sprintf("this is an asset invoice for "+
18031826
"%d units", assetAmount),
1804-
Expiry: timeoutSeconds,
1805-
ValueMsat: int64(cfg.msats),
1827+
Expiry: timeoutSeconds,
1828+
ValueMsat: int64(cfg.msats),
1829+
RouteHints: cfg.routeHints,
18061830
},
18071831
}
18081832

itest/litd_custom_channels_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4611,6 +4611,8 @@ func testCustomChannelsSelfPayment(ctx context.Context, net *NetworkHarness,
46114611
)
46124612
defer closeChannelAndAssert(t, net, alice, satChanPoint, false)
46134613

4614+
assetChan := fetchChannel(t.t, alice, assetChanPoint)
4615+
assetChanSCID := assetChan.ChanId
46144616
satChan := fetchChannel(t.t, alice, satChanPoint)
46154617
satChanSCID := satChan.ChanId
46164618

@@ -4624,6 +4626,7 @@ func testCustomChannelsSelfPayment(ctx context.Context, net *NetworkHarness,
46244626
assetKeySendAmount = 15_000
46254627
numInvoicePayments = 10
46264628
assetInvoiceAmount = 1_234
4629+
btcInvoiceAmount = 10_000
46274630
btcKeySendAmount = 200_000
46284631
btcReserveAmount = 2000
46294632
btcHtlcCost = numInvoicePayments * 354
@@ -4695,4 +4698,75 @@ func testCustomChannelsSelfPayment(ctx context.Context, net *NetworkHarness,
46954698
decodedInvoice.NumSatoshis,
46964699
)
46974700
}
4701+
4702+
// We now do the opposite: We create a satoshi invoice on Alice and
4703+
// attempt to pay it with assets.
4704+
aliceAssetBalance, bobAssetBalance = channelAssetBalance(
4705+
t.t, alice, assetChanPoint,
4706+
)
4707+
for i := 0; i < numInvoicePayments; i++ {
4708+
// The BTC balance of Alice before we start the payment. We
4709+
// expect that to go down by at least the invoice amount.
4710+
btcBalanceAliceBefore := fetchChannel(
4711+
t.t, alice, satChanPoint,
4712+
).LocalBalance
4713+
4714+
hopHint := &lnrpc.HopHint{
4715+
NodeId: bob.PubKeyStr,
4716+
ChanId: satChan.PeerScidAlias,
4717+
CltvExpiryDelta: 80,
4718+
FeeBaseMsat: 1000,
4719+
FeeProportionalMillionths: 1,
4720+
}
4721+
invoiceResp := createNormalInvoice(
4722+
t.t, alice, btcInvoiceAmount, withRouteHints(
4723+
[]*lnrpc.RouteHint{{
4724+
HopHints: []*lnrpc.HopHint{hopHint},
4725+
}},
4726+
),
4727+
)
4728+
sentUnits, _ := payInvoiceWithAssets(
4729+
t.t, alice, bob, invoiceResp.PaymentRequest, assetID,
4730+
withAllowSelfPayment(), withOutgoingChanIDs(
4731+
[]uint64{assetChanSCID},
4732+
),
4733+
)
4734+
4735+
logBalance(
4736+
t.t, nodes, assetID,
4737+
"after paying sat invoice "+strconv.Itoa(i),
4738+
)
4739+
4740+
// The accumulated delta from the rounding of multiple sends.
4741+
// We basically allow the balance to be off by one unit for each
4742+
// payment.
4743+
delta := float64(i + 1)
4744+
4745+
// We now expect the channel balance to have increased in the
4746+
// BTC channel and decreased in the assets channel.
4747+
assertChannelAssetBalanceWithDelta(
4748+
t.t, alice, assetChanPoint,
4749+
aliceAssetBalance-sentUnits,
4750+
bobAssetBalance+sentUnits, delta,
4751+
)
4752+
aliceAssetBalance -= sentUnits
4753+
bobAssetBalance += sentUnits
4754+
4755+
btcBalanceAliceAfter := fetchChannel(
4756+
t.t, alice, satChanPoint,
4757+
).LocalBalance
4758+
4759+
// The difference between the two balances should be at least
4760+
// the invoice amount.
4761+
decodedInvoice, err := alice.DecodePayReq(
4762+
context.Background(), &lnrpc.PayReqString{
4763+
PayReq: invoiceResp.PaymentRequest,
4764+
},
4765+
)
4766+
require.NoError(t.t, err)
4767+
require.GreaterOrEqual(
4768+
t.t, btcBalanceAliceAfter-btcBalanceAliceBefore,
4769+
decodedInvoice.NumSatoshis,
4770+
)
4771+
}
46984772
}

0 commit comments

Comments
 (0)