-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathrouter_backend_test.go
More file actions
1122 lines (1009 loc) · 27.1 KB
/
router_backend_test.go
File metadata and controls
1122 lines (1009 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package routerrpc
import (
"bytes"
"encoding/hex"
"fmt"
"testing"
"time"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/lnmock"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
"github.com/lightningnetwork/lnd/routing"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/stretchr/testify/require"
)
const (
destKey = "0286098b97bc843372b4426d4b276cea9aa2f48f0428d6f5b66ae101befc14f8b4"
ignoreNodeKey = "02f274f48f3c0d590449a6776e3ce8825076ac376e470e992246eebc565ef8bb2a"
hintNodeKey = "0274e7fb33eafd74fe1acb6db7680bb4aa78e9c839a6e954e38abfad680f645ef7"
testMissionControlProb = 0.5
)
var (
sourceKey = route.Vertex{1, 2, 3}
node1 = route.Vertex{10}
node2 = route.Vertex{11}
)
var (
singleChanID = "singleChanID"
multiChanID = "multiChanID"
bothChanIds = "bothChanIds"
)
// TestQueryRoutes asserts that query routes rpc parameters are properly parsed
// and passed onto path finding.
func TestQueryRoutes(t *testing.T) {
t.Run("no mission control", func(t *testing.T) {
testQueryRoutes(t, false, false, true, singleChanID)
})
t.Run("no mission control and msat", func(t *testing.T) {
testQueryRoutes(t, false, true, true, singleChanID)
})
t.Run("with mission control", func(t *testing.T) {
testQueryRoutes(t, true, false, true, singleChanID)
})
t.Run("no mission control bad cltv limit", func(t *testing.T) {
testQueryRoutes(t, false, false, false, singleChanID)
})
t.Run("both outgoing chan id and chan ids", func(t *testing.T) {
testQueryRoutes(t, true, false, true, bothChanIds)
})
t.Run("multiple outgoing chan ids", func(t *testing.T) {
testQueryRoutes(t, false, true, true, multiChanID)
})
}
func testQueryRoutes(t *testing.T, useMissionControl bool, useMsat bool,
setTimelock bool, outgoingChanConfig string) {
ignoreNodeBytes, err := hex.DecodeString(ignoreNodeKey)
if err != nil {
t.Fatal(err)
}
var ignoreNodeVertex route.Vertex
copy(ignoreNodeVertex[:], ignoreNodeBytes)
destNodeBytes, err := hex.DecodeString(destKey)
if err != nil {
t.Fatal(err)
}
var (
lastHop = route.Vertex{64}
outgoingChan = uint64(383322)
outgoingChanIds = []uint64{383322, 383323}
)
hintNode, err := route.NewVertexFromStr(hintNodeKey)
if err != nil {
t.Fatal(err)
}
rpcRouteHints := []*lnrpc.RouteHint{
{
HopHints: []*lnrpc.HopHint{
{
ChanId: 38484,
NodeId: hintNodeKey,
},
},
},
}
request := &lnrpc.QueryRoutesRequest{
PubKey: destKey,
FinalCltvDelta: 100,
IgnoredNodes: [][]byte{ignoreNodeBytes},
IgnoredEdges: []*lnrpc.EdgeLocator{{
ChannelId: 555,
DirectionReverse: true,
}},
IgnoredPairs: []*lnrpc.NodePair{{
From: node1[:],
To: node2[:],
}},
UseMissionControl: useMissionControl,
LastHopPubkey: lastHop[:],
DestFeatures: []lnrpc.FeatureBit{lnrpc.FeatureBit_MPP_OPT},
RouteHints: rpcRouteHints,
}
amtSat := int64(100000)
if useMsat {
request.AmtMsat = amtSat * 1000
request.FeeLimit = &lnrpc.FeeLimit{
Limit: &lnrpc.FeeLimit_FixedMsat{
FixedMsat: 250000,
},
}
} else {
request.Amt = amtSat
request.FeeLimit = &lnrpc.FeeLimit{
Limit: &lnrpc.FeeLimit_Fixed{
Fixed: 250,
},
}
}
switch outgoingChanConfig {
case singleChanID:
request.OutgoingChanId = outgoingChan
case multiChanID:
request.OutgoingChanIds = outgoingChanIds
case bothChanIds:
request.OutgoingChanId = outgoingChan
request.OutgoingChanIds = outgoingChanIds
}
findRoute := func(req *routing.RouteRequest) (*route.Route, float64,
error) {
if int64(req.Amount) != amtSat*1000 {
t.Fatal("unexpected amount")
}
if req.Source != sourceKey {
t.Fatal("unexpected source key")
}
target := req.Target
if !bytes.Equal(target[:], destNodeBytes) {
t.Fatal("unexpected target key")
}
restrictions := req.Restrictions
if restrictions.FeeLimit != 250*1000 {
t.Fatal("unexpected fee limit")
}
if restrictions.ProbabilitySource(route.Vertex{2},
route.Vertex{1}, 0, 0,
) != 0 {
t.Fatal("expecting 0% probability for ignored edge")
}
if restrictions.ProbabilitySource(ignoreNodeVertex,
route.Vertex{6}, 0, 0,
) != 0 {
t.Fatal("expecting 0% probability for ignored node")
}
if restrictions.ProbabilitySource(node1, node2, 0, 0) != 0 {
t.Fatal("expecting 0% probability for ignored pair")
}
if *restrictions.LastHop != lastHop {
t.Fatal("unexpected last hop")
}
switch outgoingChanConfig {
case singleChanID:
require.Equal(
t, restrictions.OutgoingChannelIDs,
[]uint64{outgoingChan},
)
case multiChanID:
require.Equal(
t, restrictions.OutgoingChannelIDs,
outgoingChanIds,
)
}
if !restrictions.DestFeatures.HasFeature(lnwire.MPPOptional) {
t.Fatal("unexpected dest features")
}
routeHints := req.RouteHints
if _, ok := routeHints[hintNode]; !ok {
t.Fatal("expected route hint")
}
expectedProb := 1.0
if useMissionControl {
expectedProb = testMissionControlProb
}
if restrictions.ProbabilitySource(route.Vertex{4},
route.Vertex{5}, 0, 0,
) != expectedProb {
t.Fatal("expecting 100% probability")
}
hops := []*route.Hop{{}}
route, err := route.NewRouteFromHops(
req.Amount, 144, req.Source, hops,
)
return route, expectedProb, err
}
backend := &RouterBackend{
FindRoute: findRoute,
SelfNode: route.Vertex{1, 2, 3},
FetchChannelCapacity: func(chanID uint64) (
btcutil.Amount, error) {
return 1, nil
},
FetchAmountPairCapacity: func(nodeFrom, nodeTo route.Vertex,
amount lnwire.MilliSatoshi) (btcutil.Amount, error) {
return 1, nil
},
MissionControl: &mockMissionControl{},
FetchChannelEndpoints: func(chanID uint64) (route.Vertex,
route.Vertex, error) {
if chanID != 555 {
t.Fatalf("expected endpoints to be fetched for "+
"channel 555, but got %v instead",
chanID)
}
return route.Vertex{1}, route.Vertex{2}, nil
},
}
// If this is set, we'll populate MaxTotalTimelock. If this is not set,
// the test will fail as CltvLimit will be 0.
if setTimelock {
backend.MaxTotalTimelock = 1000
}
resp, err := backend.QueryRoutes(t.Context(), request)
// If we're using both OutgoingChanId and OutgoingChanIds, we should get
// an error.
if outgoingChanConfig == bothChanIds {
require.Error(t, err)
return
}
// If no MaxTotalTimelock was set for the QueryRoutes request, make
// sure an error was returned.
if !setTimelock {
require.NotEmpty(t, err)
return
}
if err != nil {
t.Fatal(err)
}
if len(resp.Routes) != 1 {
t.Fatal("expected a single route response")
}
}
type mockMissionControl struct {
MissionControl
}
func (m *mockMissionControl) GetProbability(fromNode, toNode route.Vertex,
amt lnwire.MilliSatoshi, capacity btcutil.Amount) float64 {
return testMissionControlProb
}
func (m *mockMissionControl) ResetHistory() error {
return nil
}
func (m *mockMissionControl) GetHistorySnapshot() *routing.MissionControlSnapshot {
return nil
}
func (m *mockMissionControl) GetPairHistorySnapshot(fromNode,
toNode route.Vertex) routing.TimedPairResult {
return routing.TimedPairResult{}
}
type recordParseOutcome byte
const (
valid recordParseOutcome = iota
invalid
norecord
)
type unmarshalMPPTest struct {
name string
mpp *lnrpc.MPPRecord
outcome recordParseOutcome
}
// TestUnmarshalMPP checks both positive and negative cases of UnmarshalMPP to
// assert that an MPP record is only returned when both fields are properly
// specified. It also asserts that zero-values for both inputs is also valid,
// but returns a nil record.
func TestUnmarshalMPP(t *testing.T) {
tests := []unmarshalMPPTest{
{
name: "nil record",
mpp: nil,
outcome: norecord,
},
{
name: "invalid total or addr",
mpp: &lnrpc.MPPRecord{
PaymentAddr: nil,
TotalAmtMsat: 0,
},
outcome: invalid,
},
{
name: "valid total only",
mpp: &lnrpc.MPPRecord{
PaymentAddr: nil,
TotalAmtMsat: 8,
},
outcome: invalid,
},
{
name: "valid addr only",
mpp: &lnrpc.MPPRecord{
PaymentAddr: bytes.Repeat([]byte{0x02}, 32),
TotalAmtMsat: 0,
},
outcome: invalid,
},
{
name: "valid total and invalid addr",
mpp: &lnrpc.MPPRecord{
PaymentAddr: []byte{0x02},
TotalAmtMsat: 8,
},
outcome: invalid,
},
{
name: "valid total and valid addr",
mpp: &lnrpc.MPPRecord{
PaymentAddr: bytes.Repeat([]byte{0x02}, 32),
TotalAmtMsat: 8,
},
outcome: valid,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
testUnmarshalMPP(t, test)
})
}
}
func testUnmarshalMPP(t *testing.T, test unmarshalMPPTest) {
mpp, err := UnmarshalMPP(test.mpp)
switch test.outcome {
// Valid arguments should result in no error, a non-nil MPP record, and
// the fields should be set correctly.
case valid:
if err != nil {
t.Fatalf("unable to parse mpp record: %v", err)
}
if mpp == nil {
t.Fatalf("mpp payload should be non-nil")
}
if int64(mpp.TotalMsat()) != test.mpp.TotalAmtMsat {
t.Fatalf("incorrect total msat")
}
addr := mpp.PaymentAddr()
if !bytes.Equal(addr[:], test.mpp.PaymentAddr) {
t.Fatalf("incorrect payment addr")
}
// Invalid arguments should produce a failure and nil MPP record.
case invalid:
if err == nil {
t.Fatalf("expected failure for invalid mpp")
}
if mpp != nil {
t.Fatalf("mpp payload should be nil for failure")
}
// Arguments that produce no MPP field should return no error and no MPP
// record.
case norecord:
if err != nil {
t.Fatalf("failure for args resulting for no-mpp")
}
if mpp != nil {
t.Fatalf("mpp payload should be nil for no-mpp")
}
default:
t.Fatalf("test case has non-standard outcome")
}
}
type unmarshalAMPTest struct {
name string
amp *lnrpc.AMPRecord
outcome recordParseOutcome
}
// TestUnmarshalAMP asserts the behavior of decoding an RPC AMPRecord.
func TestUnmarshalAMP(t *testing.T) {
rootShare := bytes.Repeat([]byte{0x01}, 32)
setID := bytes.Repeat([]byte{0x02}, 32)
// All child indexes are valid.
childIndex := uint32(3)
tests := []unmarshalAMPTest{
{
name: "nil record",
amp: nil,
outcome: norecord,
},
{
name: "invalid root share invalid set id",
amp: &lnrpc.AMPRecord{
RootShare: []byte{0x01},
SetId: []byte{0x02},
ChildIndex: childIndex,
},
outcome: invalid,
},
{
name: "valid root share invalid set id",
amp: &lnrpc.AMPRecord{
RootShare: rootShare,
SetId: []byte{0x02},
ChildIndex: childIndex,
},
outcome: invalid,
},
{
name: "invalid root share valid set id",
amp: &lnrpc.AMPRecord{
RootShare: []byte{0x01},
SetId: setID,
ChildIndex: childIndex,
},
outcome: invalid,
},
{
name: "valid root share valid set id",
amp: &lnrpc.AMPRecord{
RootShare: rootShare,
SetId: setID,
ChildIndex: childIndex,
},
outcome: valid,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
testUnmarshalAMP(t, test)
})
}
}
func testUnmarshalAMP(t *testing.T, test unmarshalAMPTest) {
amp, err := UnmarshalAMP(test.amp)
switch test.outcome {
case valid:
require.NoError(t, err)
require.NotNil(t, amp)
rootShare := amp.RootShare()
setID := amp.SetID()
require.Equal(t, test.amp.RootShare, rootShare[:])
require.Equal(t, test.amp.SetId, setID[:])
require.Equal(t, test.amp.ChildIndex, amp.ChildIndex())
case invalid:
require.Error(t, err)
require.Nil(t, amp)
case norecord:
require.NoError(t, err)
require.Nil(t, amp)
default:
t.Fatalf("test case has non-standard outcome")
}
}
// extractIntentTestCase defines a test case for the
// TestExtractIntentFromSendRequest function. It includes the test name, the
// RouterBackend instance, the SendPaymentRequest to be tested, a boolean
// indicating if the test case is valid, and the expected error message if
// applicable.
type extractIntentTestCase struct {
name string
backend *RouterBackend
sendReq *SendPaymentRequest
valid bool
expectedErrorMsg string
}
// TestExtractIntentFromSendRequest verifies that extractIntentFromSendRequest
// correctly translates a SendPaymentRequest from an RPC client into a
// LightningPayment intent.
func TestExtractIntentFromSendRequest(t *testing.T) {
const paymentAmount = btcutil.Amount(300_000)
const paymentReq = "lnbcrt500u1pnh0xflpp56w08q26t896vg2e9mtdkrem320tp" +
"wws9z9sfr7dw86dx97d90u4sdqqcqzzsxqyz5vqsp5z9945kvfy5g9afmakz" +
"yrur2t4hhn2tr87un8j0r0e6l5m5zm0fus9qxpqysgqk98c6j7qefdpdmzt4" +
"g6aykds4ydvf2x9lpngqcfux3hv8qlraan9v3s9296r5w5eh959yzadgh5ck" +
"gjydgyfxdpumxtuk3p3caugmlqpz5necs"
const paymentReqMissingAddr = "lnbcrt100p1p70xwfzpp5qqqsyqcyq5rqwzqfq" +
"qqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kge" +
"tjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaqnp4q0n326hr8v9zprg8" +
"gsvezcch06gfaqqhde2aj730yg0durunfhv669qypqqqz3uu8wnr7883qzxr" +
"566nuhled49fx6e6q0jn06w6gpgyznwzxwf8xdmye87kpx0y8lqtcgwywsau" +
"0jkm66evelkw7cggwlegp4anv3cq62wusm"
destNodeBytes, err := hex.DecodeString(destKey)
require.NoError(t, err)
target, err := route.NewVertexFromBytes(destNodeBytes)
require.NoError(t, err)
mockClock := &lnmock.MockClock{}
mockClock.On("Now").Return(time.Date(2025, 3, 1, 13, 0, 0, 0, time.UTC))
testCases := []extractIntentTestCase{
{
name: "Time preference out of range",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
TimePref: 2,
},
valid: false,
expectedErrorMsg: "time preference out of range",
},
{
name: "Outgoing channel exclusivity violation",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
OutgoingChanId: 38484,
OutgoingChanIds: []uint64{383322},
},
valid: false,
expectedErrorMsg: "outgoing_chan_id and " +
"outgoing_chan_ids are mutually exclusive",
},
{
name: "Invalid last hop pubkey length",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
LastHopPubkey: []byte{1},
},
valid: false,
expectedErrorMsg: "invalid vertex length",
},
{
name: "total time lock exceeds max allowed",
backend: &RouterBackend{
MaxTotalTimelock: 1000,
},
sendReq: &SendPaymentRequest{
CltvLimit: 1001,
},
valid: false,
expectedErrorMsg: "total time lock of 1001 exceeds " +
"max allowed 1000",
},
{
name: "Max parts exceed allowed limit",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
MaxParts: 1001,
MaxShardSizeMsat: 300_000,
},
valid: false,
expectedErrorMsg: "requested max_parts (1001) exceeds" +
" the allowed upper limit",
},
{
name: "Fee limit conflict, both sat and msat " +
"specified",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
FeeLimitSat: 1000000,
FeeLimitMsat: 1000000000,
},
valid: false,
expectedErrorMsg: "sat and msat arguments are " +
"mutually exclusive",
},
{
name: "Fee limit cannot be negative",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
FeeLimitSat: -1,
},
valid: false,
expectedErrorMsg: "amount cannot be negative",
},
{
name: "Dest custom records with type below minimum" +
" range",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
DestCustomRecords: map[uint64][]byte{
65530: {1, 2},
},
},
valid: false,
expectedErrorMsg: "no custom records with types below",
},
{
name: "MPP params with keysend payments",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
DestCustomRecords: map[uint64][]byte{
record.KeySendType: {1, 2},
},
MaxShardSizeMsat: 300_000,
},
valid: false,
expectedErrorMsg: "MPP not supported with keysend " +
"payments",
},
{
name: "Custom record entry with TLV type below " +
"minimum range",
backend: &RouterBackend{},
sendReq: &SendPaymentRequest{
FirstHopCustomRecords: map[uint64][]byte{
65530: {1, 2},
},
},
valid: false,
expectedErrorMsg: "custom records entry with TLV type",
},
{
name: "Amount conflict, both sat and msat specified",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return true
},
},
sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount),
AmtMsat: int64(paymentAmount) * 1000,
},
valid: false,
expectedErrorMsg: "sat and msat arguments are " +
"mutually exclusive",
},
{
name: "Both dest and payment_request provided",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount),
PaymentRequest: "test",
Dest: destNodeBytes,
},
valid: false,
expectedErrorMsg: "dest and payment_request " +
"cannot appear together",
},
{
name: "Both payment_hash and payment_request provided",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount),
PaymentRequest: "test",
PaymentHash: make([]byte, 32),
},
valid: false,
expectedErrorMsg: "payment_hash and payment_request " +
"cannot appear together",
},
{
name: "Both final_cltv_delta and payment_request " +
"provided",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount),
PaymentRequest: "test",
FinalCltvDelta: 100,
},
valid: false,
expectedErrorMsg: "final_cltv_delta and " +
"payment_request cannot appear together",
},
{
name: "Invalid payment request length",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
ActiveNetParams: &chaincfg.RegressionNetParams,
},
sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount),
PaymentRequest: "test",
},
valid: false,
expectedErrorMsg: "invalid bech32 string length",
},
{
name: "Expired invoice payment request",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
ActiveNetParams: &chaincfg.RegressionNetParams,
Clock: mockClock,
},
sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount),
PaymentRequest: paymentReq,
},
valid: false,
expectedErrorMsg: "invoice expired.",
},
{
name: "Invoice missing payment address",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
ActiveNetParams: &chaincfg.RegressionNetParams,
MaxTotalTimelock: 1000,
Clock: mockClock,
},
sendReq: &SendPaymentRequest{
PaymentRequest: paymentReqMissingAddr,
},
valid: false,
expectedErrorMsg: "payment request must contain " +
"either a payment address or blinded paths",
},
{
name: "Invalid dest vertex length",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Amt: int64(paymentAmount),
Dest: []byte{1},
},
valid: false,
expectedErrorMsg: "invalid vertex length",
},
{
name: "Payment request with missing amount",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Dest: destNodeBytes,
FinalCltvDelta: 100,
},
valid: false,
expectedErrorMsg: "amount must be specified",
},
{
name: "Destination lacks AMP support",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Dest: destNodeBytes,
Amt: int64(paymentAmount),
Amp: true,
DestFeatures: []lnrpc.FeatureBit{},
},
valid: false,
expectedErrorMsg: "destination doesn't " +
"support AMP payments",
},
{
name: "Invalid payment hash length",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Dest: destNodeBytes,
Amt: int64(paymentAmount),
PaymentHash: make([]byte, 1),
},
valid: false,
expectedErrorMsg: "invalid hash length",
},
{
name: "Payment amount exceeds maximum possible amount",
backend: &RouterBackend{
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Dest: destNodeBytes,
Amt: int64(paymentAmount),
PaymentHash: make([]byte, 32),
MaxParts: 10,
MaxShardSizeMsat: 300_000,
},
valid: false,
expectedErrorMsg: "payment amount 300000000 mSAT " +
"exceeds maximum possible amount",
},
{
name: "Reject self-payments if not permitted",
backend: &RouterBackend{
MaxTotalTimelock: 1000,
ShouldSetExpAccountability: func() bool {
return false
},
SelfNode: target,
},
sendReq: &SendPaymentRequest{
Dest: destNodeBytes,
Amt: int64(paymentAmount),
PaymentHash: make([]byte, 32),
},
valid: false,
expectedErrorMsg: "self-payments not allowed",
},
{
name: "Required and optional feature bits set",
backend: &RouterBackend{
MaxTotalTimelock: 1000,
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Dest: destNodeBytes,
Amt: int64(paymentAmount),
PaymentHash: make([]byte, 32),
MaxParts: 10,
MaxShardSizeMsat: 30_000_000,
DestFeatures: []lnrpc.FeatureBit{
lnrpc.FeatureBit_GOSSIP_QUERIES_OPT,
lnrpc.FeatureBit_GOSSIP_QUERIES_REQ,
},
},
valid: true,
},
{
name: "Valid send req parameters, payment settled",
backend: &RouterBackend{
MaxTotalTimelock: 1000,
ShouldSetExpAccountability: func() bool {
return false
},
},
sendReq: &SendPaymentRequest{
Dest: destNodeBytes,
Amt: int64(paymentAmount),
PaymentHash: make([]byte, 32),
MaxParts: 10,
MaxShardSizeMsat: 30_000_000,
},
valid: true,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
_, err := test.backend.
extractIntentFromSendRequest(test.sendReq)
if test.valid {
require.NoError(t, err)
} else {
require.ErrorContains(t, err,
test.expectedErrorMsg)
}
})
}
}
// TestMarshallRouteChanCapacity verifies that MarshallRoute correctly sets the
// ChanCapacity for each hop based on the incoming amount at that hop, not
// the total route amount. This is a regression test to ensure the
// incomingAmt is updated per hop.
func TestMarshallRouteChanCapacity(t *testing.T) {
t.Parallel()
// Build a two-hop route: source -> hop1 -> hop2 -> dest.
//
// TotalAmount (incoming to hop1) = 1000 msat
// hop1.AmtToForward (incoming to hop2) = 900 msat (after fee)
const (
totalAmtMsat = lnwire.MilliSatoshi(1000)
hop1Forward = lnwire.MilliSatoshi(900)
hop2Forward = lnwire.MilliSatoshi(900)
)
hops := []*route.Hop{
{
ChannelID: 1,
AmtToForward: hop1Forward,
PubKeyBytes: node1,
},
{
ChannelID: 2,
AmtToForward: hop2Forward,
PubKeyBytes: node2,
},
}
r, err := route.NewRouteFromHops(totalAmtMsat, 100, sourceKey, hops)
require.NoError(t, err)
backend := &RouterBackend{}
rpcRoute, err := backend.MarshallRoute(r)
require.NoError(t, err)
require.Len(t, rpcRoute.Hops, 2)
// The first hop's capacity should reflect the total incoming amount
// (route.TotalAmount), converted to satoshis.
require.EqualValues(
t, totalAmtMsat.ToSatoshis(), rpcRoute.Hops[0].ChanCapacity,
)
// The second hop's capacity should reflect hop1's forwarded amount, not
// the total route amount. Before the fix, both hops incorrectly used
// the total route amount.
require.EqualValues(
t, hop1Forward.ToSatoshis(), rpcRoute.Hops[1].ChanCapacity,
)
}
// TestUnmarshallRouteSourcePubKey tests that UnmarshallRoute correctly handles
// the source_pub_key field on the Route proto message.
func TestUnmarshallRouteSourcePubKey(t *testing.T) {
t.Parallel()
// Define test vertices. The self node is the default source used
// when source_pub_key is empty.