Skip to content

Commit d157af0

Browse files
committed
itest: speed up test, fix flakes
1 parent 9cd8b38 commit d157af0

File tree

3 files changed

+129
-39
lines changed

3 files changed

+129
-39
lines changed

itest/assets_test.go

+62-13
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ func sendAssetKeySendPayment(t *testing.T, src, dst *HarnessNode, amt uint64,
10331033
DestCustomRecords: customRecords,
10341034
PaymentHash: hash[:],
10351035
TimeoutSeconds: int32(PaymentTimeout.Seconds()),
1036+
MaxParts: cfg.maxShards,
10361037
}
10371038

10381039
request := &tchrpc.SendPaymentRequest{
@@ -1090,7 +1091,7 @@ func sendKeySendPayment(t *testing.T, src, dst *HarnessNode,
10901091
stream, err := src.RouterClient.SendPaymentV2(ctxt, req)
10911092
require.NoError(t, err)
10921093

1093-
result, err := getPaymentResult(stream)
1094+
result, err := getFinalPaymentResult(stream)
10941095
require.NoError(t, err)
10951096
require.Equal(t, lnrpc.Payment_SUCCEEDED, result.Status)
10961097
}
@@ -1135,6 +1136,12 @@ func createAndPayNormalInvoice(t *testing.T, src, rfqPeer, dst *HarnessNode,
11351136
func payInvoiceWithSatoshi(t *testing.T, payer *HarnessNode,
11361137
invoice *lnrpc.AddInvoiceResponse, opts ...payOpt) {
11371138

1139+
payPayReqWithSatoshi(t, payer, invoice.PaymentRequest, opts...)
1140+
}
1141+
1142+
func payPayReqWithSatoshi(t *testing.T, payer *HarnessNode, payReq string,
1143+
opts ...payOpt) {
1144+
11381145
cfg := defaultPayConfig()
11391146
for _, opt := range opts {
11401147
opt(cfg)
@@ -1145,15 +1152,30 @@ func payInvoiceWithSatoshi(t *testing.T, payer *HarnessNode,
11451152
defer cancel()
11461153

11471154
sendReq := &routerrpc.SendPaymentRequest{
1148-
PaymentRequest: invoice.PaymentRequest,
1149-
TimeoutSeconds: int32(PaymentTimeout.Seconds()),
1150-
MaxShardSizeMsat: 80_000_000,
1151-
FeeLimitMsat: 1_000_000,
1155+
PaymentRequest: payReq,
1156+
TimeoutSeconds: int32(PaymentTimeout.Seconds()),
1157+
FeeLimitMsat: 1_000_000,
1158+
MaxParts: cfg.maxShards,
1159+
}
1160+
1161+
if cfg.smallShards {
1162+
sendReq.MaxShardSizeMsat = 80_000_000
11521163
}
1164+
11531165
stream, err := payer.RouterClient.SendPaymentV2(ctxt, sendReq)
11541166
require.NoError(t, err)
11551167

1156-
result, err := getPaymentResult(stream)
1168+
if cfg.payStatus == lnrpc.Payment_IN_FLIGHT {
1169+
t.Logf("Waiting for initial stream response...")
1170+
result, err := stream.Recv()
1171+
require.NoError(t, err)
1172+
1173+
require.Equal(t, cfg.payStatus, result.Status)
1174+
1175+
return
1176+
}
1177+
1178+
result, err := getFinalPaymentResult(stream)
11571179
if cfg.errSubStr != "" {
11581180
require.ErrorContains(t, err, cfg.errSubStr)
11591181
} else {
@@ -1212,6 +1234,7 @@ func payInvoiceWithSatoshiLastHop(t *testing.T, payer *HarnessNode,
12121234

12131235
type payConfig struct {
12141236
smallShards bool
1237+
maxShards uint32
12151238
errSubStr string
12161239
allowOverpay bool
12171240
feeLimit lnwire.MilliSatoshi
@@ -1240,6 +1263,12 @@ func withSmallShards() payOpt {
12401263
}
12411264
}
12421265

1266+
func withMaxShards(maxShards uint32) payOpt {
1267+
return func(c *payConfig) {
1268+
c.maxShards = maxShards
1269+
}
1270+
}
1271+
12431272
func withPayErrSubStr(errSubStr string) payOpt {
12441273
return func(c *payConfig) {
12451274
c.errSubStr = errSubStr
@@ -1310,6 +1339,7 @@ func payInvoiceWithAssets(t *testing.T, payer, rfqPeer *HarnessNode,
13101339
TimeoutSeconds: int32(PaymentTimeout.Seconds()),
13111340
FeeLimitMsat: int64(cfg.feeLimit),
13121341
DestCustomRecords: cfg.destCustomRecords,
1342+
MaxParts: cfg.maxShards,
13131343
}
13141344

13151345
if cfg.smallShards {
@@ -1394,8 +1424,9 @@ func payInvoiceWithAssets(t *testing.T, payer, rfqPeer *HarnessNode,
13941424
}
13951425

13961426
type invoiceConfig struct {
1397-
errSubStr string
1398-
groupKey []byte
1427+
errSubStr string
1428+
groupKey []byte
1429+
routeHints []*lnrpc.RouteHint
13991430
}
14001431

14011432
func defaultInvoiceConfig() *invoiceConfig {
@@ -2323,10 +2354,11 @@ func assertMinNumHtlcs(t *testing.T, node *HarnessNode, expected int) {
23232354
type subscribeEventsClient = routerrpc.Router_SubscribeHtlcEventsClient
23242355

23252356
type htlcEventConfig struct {
2326-
timeout time.Duration
2327-
numEvents int
2328-
withLinkFailure bool
2329-
withFailureDetail routerrpc.FailureDetail
2357+
timeout time.Duration
2358+
numEvents int
2359+
withLinkFailure bool
2360+
withForwardFailure bool
2361+
withFailureDetail routerrpc.FailureDetail
23302362
}
23312363

23322364
func defaultHtlcEventConfig() *htlcEventConfig {
@@ -2356,6 +2388,12 @@ func withLinkFailure(detail routerrpc.FailureDetail) htlcEventOpt {
23562388
}
23572389
}
23582390

2391+
func withForwardFailure() htlcEventOpt {
2392+
return func(config *htlcEventConfig) {
2393+
config.withForwardFailure = true
2394+
}
2395+
}
2396+
23592397
func assertHtlcEvents(t *testing.T, c subscribeEventsClient,
23602398
opts ...htlcEventOpt) {
23612399

@@ -2390,7 +2428,10 @@ func assertHtlcEvents(t *testing.T, c subscribeEventsClient,
23902428

23912429
var numEvents int
23922430
for {
2393-
type linkFailEvent = *routerrpc.HtlcEvent_LinkFailEvent
2431+
type (
2432+
linkFailEvent = *routerrpc.HtlcEvent_LinkFailEvent
2433+
forwardFailEvent = *routerrpc.HtlcEvent_ForwardFailEvent
2434+
)
23942435

23952436
select {
23962437
case evt, ok := <-events:
@@ -2413,6 +2454,14 @@ func assertHtlcEvents(t *testing.T, c subscribeEventsClient,
24132454
}
24142455
}
24152456

2457+
if cfg.withForwardFailure {
2458+
_, ok := evt.Event.(forwardFailEvent)
2459+
if !ok {
2460+
// We only count link failure events.
2461+
continue
2462+
}
2463+
}
2464+
24162465
numEvents++
24172466

24182467
if numEvents == cfg.numEvents {

itest/litd_accounts_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,12 @@ func payNode(invoiceCtx, paymentCtx context.Context, t *harnessTest,
416416
stream, err := from.SendPaymentV2(paymentCtx, sendReq)
417417
require.NoError(t.t, err)
418418

419-
result, err := getPaymentResult(stream)
419+
result, err := getFinalPaymentResult(stream)
420420
require.NoError(t.t, err)
421421
require.Equal(t.t, result.Status, lnrpc.Payment_SUCCEEDED)
422422
}
423423

424-
func getPaymentResult(stream routerrpc.Router_SendPaymentV2Client) (
424+
func getFinalPaymentResult(stream routerrpc.Router_SendPaymentV2Client) (
425425
*lnrpc.Payment, error) {
426426

427427
for {

itest/litd_custom_channels_test.go

+65-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package itest
22

33
import (
4-
"bytes"
54
"context"
5+
"crypto/rand"
66
"fmt"
77
"math"
88
"math/big"
@@ -32,6 +32,7 @@ import (
3232
"github.com/lightningnetwork/lnd/lntest"
3333
"github.com/lightningnetwork/lnd/lntest/node"
3434
"github.com/lightningnetwork/lnd/lntest/port"
35+
"github.com/lightningnetwork/lnd/lntypes"
3536
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
3637
"github.com/lightningnetwork/lnd/lnwire"
3738
"github.com/stretchr/testify/require"
@@ -2720,30 +2721,65 @@ func testCustomChannelsLiquidityEdgeCasesCore(ctx context.Context,
27202721

27212722
// We now manually add the invoice in order to inject the above,
27222723
// manually generated, quote.
2723-
iResp, err := charlie.AddInvoice(ctx, &lnrpc.Invoice{
2724-
Memo: "",
2725-
Value: 200_000,
2726-
RPreimage: bytes.Repeat([]byte{11}, 32),
2727-
CltvExpiry: 60,
2728-
RouteHints: []*lnrpc.RouteHint{{
2729-
HopHints: []*lnrpc.HopHint{{
2730-
NodeId: dave.PubKeyStr,
2731-
ChanId: quote.AcceptedQuote.Scid,
2724+
hint := &lnrpc.HopHint{
2725+
NodeId: dave.PubKeyStr,
2726+
ChanId: quote.AcceptedQuote.Scid,
2727+
CltvExpiryDelta: 80,
2728+
FeeBaseMsat: 1000,
2729+
FeeProportionalMillionths: 1,
2730+
}
2731+
var preimage lntypes.Preimage
2732+
_, _ = rand.Read(preimage[:])
2733+
payHash = preimage.Hash()
2734+
iResp, err := charlie.AddHoldInvoice(
2735+
ctx, &invoicesrpc.AddHoldInvoiceRequest{
2736+
Memo: "",
2737+
Value: 200_000,
2738+
Hash: payHash[:],
2739+
RouteHints: []*lnrpc.RouteHint{{
2740+
HopHints: []*lnrpc.HopHint{hint},
27322741
}},
2733-
}},
2734-
})
2742+
},
2743+
)
2744+
require.NoError(t.t, err)
2745+
2746+
htlcStream, err := dave.RouterClient.SubscribeHtlcEvents(
2747+
ctx, &routerrpc.SubscribeHtlcEventsRequest{},
2748+
)
27352749
require.NoError(t.t, err)
27362750

27372751
// Now Erin tries to pay the invoice. Since rfq quote cannot satisfy the
27382752
// total amount of the invoice this payment will fail.
2739-
payInvoiceWithSatoshi(
2740-
t.t, erin, iResp, withPayErrSubStr("context deadline exceeded"),
2741-
withFailure(lnrpc.Payment_FAILED, failureNone),
2742-
withGroupKey(groupID),
2753+
payPayReqWithSatoshi(
2754+
t.t, erin, iResp.PaymentRequest,
2755+
withFailure(lnrpc.Payment_IN_FLIGHT, failureNone),
2756+
withGroupKey(groupID), withMaxShards(4),
2757+
)
2758+
2759+
t.Logf("Asserting number of HTLCs on each node...")
2760+
assertNumHtlcs(t.t, dave, 2)
2761+
2762+
t.Logf("Asserting HTLC events on Dave...")
2763+
assertHtlcEvents(
2764+
t.t, htlcStream, withNumEvents(1), withForwardFailure(),
2765+
)
2766+
2767+
_, err = charlie.InvoicesClient.CancelInvoice(
2768+
ctx, &invoicesrpc.CancelInvoiceMsg{
2769+
PaymentHash: payHash[:],
2770+
},
27432771
)
2772+
require.NoError(t.t, err)
2773+
2774+
assertNumHtlcs(t.t, dave, 0)
27442775

27452776
logBalance(t.t, nodes, assetID, "after small manual rfq")
27462777

2778+
_ = htlcStream.CloseSend()
2779+
_, _ = erin.RouterClient.ResetMissionControl(
2780+
context.Background(), &routerrpc.ResetMissionControlRequest{},
2781+
)
2782+
27472783
// Edge case: Fabia creates an invoice which Erin cannot satisfy with
27482784
// his side of asset liquidity. This tests that Erin will not try to
27492785
// add an HTLC with more asset units than what his local balance is. To
@@ -2753,7 +2789,7 @@ func testCustomChannelsLiquidityEdgeCasesCore(ctx context.Context,
27532789
// We now create a hodl invoice on Fabia, for 125k assets.
27542790
hodlInv = createAssetHodlInvoice(t.t, erin, fabia, 125_000, assetID)
27552791

2756-
htlcStream, err := erin.RouterClient.SubscribeHtlcEvents(
2792+
htlcStream, err = erin.RouterClient.SubscribeHtlcEvents(
27572793
ctx, &routerrpc.SubscribeHtlcEventsRequest{},
27582794
)
27592795
require.NoError(t.t, err)
@@ -2797,10 +2833,11 @@ func testCustomChannelsLiquidityEdgeCasesCore(ctx context.Context,
27972833

27982834
// Now let's create a smaller invoice and pay it, to validate that the
27992835
// channel is still healthy.
2800-
invoiceResp = createAssetInvoice(
2801-
t.t, erin, fabia, 50_000, assetID,
2802-
)
2836+
invoiceResp = createAssetInvoice(t.t, erin, fabia, 50_000, assetID)
28032837

2838+
_, _ = charlie.RouterClient.ResetMissionControl(
2839+
context.Background(), &routerrpc.ResetMissionControlRequest{},
2840+
)
28042841
payInvoiceWithAssets(
28052842
t.t, charlie, dave, invoiceResp.PaymentRequest, assetID,
28062843
)
@@ -4154,14 +4191,18 @@ func testCustomChannelsForwardBandwidth(ctx context.Context,
41544191

41554192
// We now manually add the invoice in order to inject the above,
41564193
// manually generated, quote.
4194+
hopHint := &lnrpc.HopHint{
4195+
NodeId: erin.PubKeyStr,
4196+
ChanId: quote.AcceptedQuote.Scid,
4197+
CltvExpiryDelta: 80,
4198+
FeeBaseMsat: 1000,
4199+
FeeProportionalMillionths: 1,
4200+
}
41574201
invoiceResp2, err := fabia.AddInvoice(ctx, &lnrpc.Invoice{
41584202
Memo: "too small invoice",
41594203
ValueMsat: int64(oneUnitMilliSat - 1),
41604204
RouteHints: []*lnrpc.RouteHint{{
4161-
HopHints: []*lnrpc.HopHint{{
4162-
NodeId: erin.PubKeyStr,
4163-
ChanId: quote.AcceptedQuote.Scid,
4164-
}},
4205+
HopHints: []*lnrpc.HopHint{hopHint},
41654206
}},
41664207
})
41674208
require.NoError(t.t, err)

0 commit comments

Comments
 (0)