forked from lightninglabs/taproot-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaux_invoice_manager_test.go
841 lines (707 loc) · 20.2 KB
/
aux_invoice_manager_test.go
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
package tapchannel
import (
"context"
"crypto/sha256"
"fmt"
"math/big"
"testing"
"time"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/rfq"
"github.com/lightninglabs/taproot-assets/rfqmath"
"github.com/lightninglabs/taproot-assets/rfqmsg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"
)
const (
// maxRandomInvoiceValueMSat is the maximum invoice value in mSAT to be
// generated by the property based tests.
maxRandomInvoiceValueMSat = 100_000_000_000
// minRouteHints is the minimum length of route hints to be included in
// an invoice.
minRouteHints = 0
// maxRouteHints is the maximum length of route hints to be included in
// an invoice.
maxRouteHints = 4
// minHopHints is the minimum length of hop hints to be included in a
// route hint.
minHopHints = 0
// maxHopHints is the maximum length of hop hints to be included in a
// route hint.
maxHopHints = 4
)
var (
// The node ID to be used for the RFQ peer.
testNodeID = route.Vertex{1, 2, 3}
// The asset rate value to use across tests.
assetRate = big.NewInt(100_000)
// The asset rate struct based on the assetRate value.
testAssetRate = rfqmath.FixedPoint[rfqmath.BigInt]{
Coefficient: rfqmath.NewBigInt(assetRate),
Scale: 0,
}
// The test RFQ ID to use across tests.
testRfqID = dummyRfqID(31)
// The test RFQ SCID that is derived from testRfqID.
testScid = testRfqID.Scid()
)
// mockRfqManager mocks the interface of the rfq manager required by the aux
// invoice manager. It also holds some internal state to return the desired
// quotes.
type mockRfqManager struct {
peerBuyQuotes rfq.BuyAcceptMap
localSellQuotes rfq.SellAcceptMap
}
func (m *mockRfqManager) PeerAcceptedBuyQuotes() rfq.BuyAcceptMap {
return m.peerBuyQuotes
}
func (m *mockRfqManager) LocalAcceptedSellQuotes() rfq.SellAcceptMap {
return m.localSellQuotes
}
// mockHtlcModifier mocks the HtlcModifier interface that is required by the
// AuxInvoiceManager.
type mockHtlcModifier struct {
requestQue []lndclient.InvoiceHtlcModifyRequest
expectedResQue []lndclient.InvoiceHtlcModifyResponse
done chan bool
t *testing.T
}
// HtlcModifier handles the invoice htlc modification requests que, then checks
// the returned error and response against the expected values.
func (m *mockHtlcModifier) HtlcModifier(ctx context.Context,
handler lndclient.InvoiceHtlcModifyHandler) error {
// Process the requests that are provided by the test case.
for i, r := range m.requestQue {
res, err := handler(ctx, r)
if err != nil {
return err
}
if m.expectedResQue[i].CancelSet {
if !res.CancelSet {
return fmt.Errorf("expected cancel set flag")
}
continue
}
// Check if there's a match with the expected outcome.
if res.AmtPaid != m.expectedResQue[i].AmtPaid {
return fmt.Errorf("invoice paid amount does not match "+
"expected amount, %v != %v", res.AmtPaid,
m.expectedResQue[i].AmtPaid)
}
}
// Signal that the htlc modifications are completed.
close(m.done)
return nil
}
// mockHtlcModifierProperty mocks the HtlcModifier interface that is required
// by the AuxHtlcModifier. This mock is specific to the property based tests,
// as some more info are needed to run more in-depth checks.
type mockHtlcModifierProperty struct {
requestQue []lndclient.InvoiceHtlcModifyRequest
rfqMap rfq.BuyAcceptMap
done chan bool
t *rapid.T
}
// RfqPeerFromScid retrieves the peer associated with the RFQ id that is mapped
// to the provided scid, if it exists.
func (m *mockHtlcModifierProperty) RfqPeerFromScid(
scid uint64) (route.Vertex, error) {
buyQuote, ok := m.rfqMap[rfqmsg.SerialisedScid(scid)]
if !ok {
return route.Vertex{},
fmt.Errorf("no peer found for RFQ SCID %d", scid)
}
return buyQuote.Peer, nil
}
// HtlcModifier is the version of the HtlcModifier used by the property based
// tests. It handles a que of htlc modification requests, then depending on the
// request and the context it checks the results against the expected behavior.
func (m *mockHtlcModifierProperty) HtlcModifier(ctx context.Context,
handler lndclient.InvoiceHtlcModifyHandler) error {
// Process the requests that are provided by the test case.
for _, r := range m.requestQue {
res, err := handler(ctx, r)
if err != nil {
if r.Invoice == nil {
if !assert.ErrorContains(
m.t, err, "cannot handle empty invoice",
) {
m.t.Errorf("expected empty invoice err")
}
} else {
if !assert.ErrorContains(
m.t, err, "price from quote",
) {
m.t.Errorf("expected quote price err")
}
}
continue
}
if len(r.WireCustomRecords) == 0 {
if isAssetInvoice(r.Invoice, m) {
if !res.CancelSet {
m.t.Errorf("expected cancel set flag")
}
continue
}
if r.ExitHtlcAmt != res.AmtPaid {
m.t.Errorf("AmtPaid != ExitHtlcAmt")
}
} else if !isAssetInvoice(r.Invoice, m) {
if !res.CancelSet {
m.t.Errorf("expected cancel set flag")
}
continue
}
htlcBlob, err := r.WireCustomRecords.Serialize()
require.NoError(m.t, err)
htlc, err := rfqmsg.DecodeHtlc(htlcBlob)
require.NoError(m.t, err)
if htlc.RfqID.ValOpt().IsNone() {
if r.ExitHtlcAmt != res.AmtPaid ||
r.CircuitKey != res.CircuitKey {
m.t.Errorf("exit amt and circuit key mismatch")
}
continue
}
rfqID := htlc.RfqID.ValOpt().UnsafeFromSome()
quote, ok := m.rfqMap[rfqID.Scid()]
if !ok {
m.t.Errorf("no rfq quote found")
}
assetRate := lnwire.MilliSatoshi(
quote.AssetRate.Rate.ToUint64(),
)
msatPerBtc := float64(btcutil.SatoshiPerBitcoin * 1000)
unitValue := msatPerBtc / float64(assetRate)
assetUnits := lnwire.MilliSatoshi(htlc.Amounts.Val.Sum())
floatValue := float64(assetUnits) * unitValue
assetValueMsat := lnwire.MilliSatoshi(floatValue)
acceptedMsat := lnwire.MilliSatoshi(0)
for _, htlc := range r.Invoice.Htlcs {
acceptedMsat += lnwire.MilliSatoshi(htlc.AmtMsat)
}
marginHtlcs := len(r.Invoice.Htlcs) + 1
marginMsat := lnwire.MilliSatoshi(
float64(marginHtlcs) * unitValue,
)
totalMsatIn := marginMsat + assetValueMsat + acceptedMsat + 1
invoiceValue := lnwire.MilliSatoshi(r.Invoice.ValueMsat)
if totalMsatIn >= invoiceValue {
if (invoiceValue - acceptedMsat) != res.AmtPaid {
m.t.Errorf("amt + accepted != invoice amt")
}
} else {
if assetValueMsat != res.AmtPaid {
m.t.Errorf("unexpected final asset value")
}
}
}
// Signal that the htlc modifications are completed.
close(m.done)
return nil
}
// TestAuxInvoiceManager tests that the htlc modifications of the aux invoice
// manager align with our expectations.
func TestAuxInvoiceManager(t *testing.T) {
testCases := []struct {
name string
buyQuotes rfq.BuyAcceptMap
sellQuotes rfq.SellAcceptMap
requests []lndclient.InvoiceHtlcModifyRequest
responses []lndclient.InvoiceHtlcModifyResponse
containedErrStr string
}{
{
name: "non asset invoice",
requests: []lndclient.InvoiceHtlcModifyRequest{
{
Invoice: &lnrpc.Invoice{},
ExitHtlcAmt: 1234,
},
},
responses: []lndclient.InvoiceHtlcModifyResponse{
{
AmtPaid: 1234,
},
},
},
{
name: "non asset routing hints",
requests: []lndclient.InvoiceHtlcModifyRequest{
{
Invoice: &lnrpc.Invoice{
RouteHints: testNonAssetHints(),
ValueMsat: 1_000_000,
},
ExitHtlcAmt: 1234,
},
},
responses: []lndclient.InvoiceHtlcModifyResponse{
{
AmtPaid: 1234,
},
},
buyQuotes: map[rfq.SerialisedScid]rfqmsg.BuyAccept{
testScid: {
Peer: testNodeID,
},
},
},
{
name: "asset invoice, no custom records",
requests: []lndclient.InvoiceHtlcModifyRequest{
{
Invoice: &lnrpc.Invoice{
RouteHints: testRouteHints(),
PaymentAddr: []byte{1, 1, 1},
},
ExitHtlcAmt: 1234,
},
},
responses: []lndclient.InvoiceHtlcModifyResponse{
{
CancelSet: true,
},
},
buyQuotes: map[rfq.SerialisedScid]rfqmsg.BuyAccept{
testScid: {
Peer: testNodeID,
},
},
},
{
name: "asset invoice, custom records",
requests: []lndclient.InvoiceHtlcModifyRequest{
{
Invoice: &lnrpc.Invoice{
RouteHints: testRouteHints(),
ValueMsat: 3_000_000,
PaymentAddr: []byte{1, 1, 1},
},
WireCustomRecords: newWireCustomRecords(
t, []*rfqmsg.AssetBalance{
rfqmsg.NewAssetBalance(
dummyAssetID(1),
3,
),
}, fn.Some(testRfqID),
),
},
},
responses: []lndclient.InvoiceHtlcModifyResponse{
{
AmtPaid: 3_000_000,
},
},
buyQuotes: rfq.BuyAcceptMap{
testScid: {
Peer: testNodeID,
AssetRate: rfqmsg.NewAssetRate(
testAssetRate, time.Now(),
),
},
},
},
{
name: "asset invoice, not enough amt",
requests: []lndclient.InvoiceHtlcModifyRequest{
{
Invoice: &lnrpc.Invoice{
RouteHints: testRouteHints(),
ValueMsat: 10_000_000,
PaymentAddr: []byte{1, 1, 1},
},
WireCustomRecords: newWireCustomRecords(
t, []*rfqmsg.AssetBalance{
rfqmsg.NewAssetBalance(
dummyAssetID(1),
4,
),
}, fn.Some(testRfqID),
),
ExitHtlcAmt: 1234,
},
},
responses: []lndclient.InvoiceHtlcModifyResponse{
{
AmtPaid: 4_000_000,
},
},
buyQuotes: rfq.BuyAcceptMap{
testScid: {
Peer: testNodeID,
AssetRate: rfqmsg.NewAssetRate(
testAssetRate, time.Now(),
),
},
},
},
{
name: "btc invoice, custom records",
requests: []lndclient.InvoiceHtlcModifyRequest{
{
Invoice: &lnrpc.Invoice{
ValueMsat: 10_000_000,
PaymentAddr: []byte{1, 1, 1},
},
WireCustomRecords: newWireCustomRecords(
t, []*rfqmsg.AssetBalance{
rfqmsg.NewAssetBalance(
dummyAssetID(1),
4,
),
}, fn.Some(testRfqID),
),
ExitHtlcAmt: 1234,
},
},
responses: []lndclient.InvoiceHtlcModifyResponse{
{
CancelSet: true,
},
},
},
}
for _, testCase := range testCases {
testCase := testCase
t.Logf("Running AuxInvoiceManager test case: %v", testCase.name)
// Instantiate mock rfq manager.
mockRfq := &mockRfqManager{
peerBuyQuotes: testCase.buyQuotes,
localSellQuotes: testCase.sellQuotes,
}
done := make(chan bool)
// Instantiate mock htlc modifier.
mockModifier := &mockHtlcModifier{
requestQue: testCase.requests,
expectedResQue: testCase.responses,
done: done,
t: t,
}
// Create the manager.
manager := NewAuxInvoiceManager(
&InvoiceManagerConfig{
ChainParams: testChainParams,
InvoiceHtlcModifier: mockModifier,
RfqManager: mockRfq,
},
)
err := manager.Start()
require.NoError(t, err)
// If the manager is not done processing the htlc modification
// requests within the specified timeout, assume this is a
// failure.
select {
case <-done:
case <-time.After(testTimeout):
t.Fail()
}
}
}
// genRandomRfqID generates a random rfqmsg.ID value.
func genRandomRfqID(t *rapid.T) rfqmsg.ID {
return rapid.Make[[32]byte]().Draw(t, "rfq_id")
}
// genInvoice generates an invoice that may have a random amount, and may have
// routing hints.
func genInvoice(t *rapid.T, rfqID rfqmsg.ID) *lnrpc.Invoice {
// Introduce a chance of a null invoice.
if !rapid.Bool().Draw(t, "inv_exists") {
return nil
}
res := &lnrpc.Invoice{}
// Generate a random invoice value.
res.ValueMsat = rapid.Int64Range(
1, maxRandomInvoiceValueMSat,
).Draw(t, "invoice_value_msat")
res.RouteHints = genRouteHints(t, rfqID)
return res
}
// genRouteHints generates route hints for an invoice. Given an rfqID, it may
// contain a hop hint that references that rfqID.
func genRouteHints(t *rapid.T, rfqID rfqmsg.ID) []*lnrpc.RouteHint {
res := make([]*lnrpc.RouteHint, 0)
rhLen := rapid.IntRange(
minRouteHints, maxRouteHints,
).Draw(t, "route_hints_len")
for range rhLen {
hh := genHopHints(t, rfqID)
res = append(res, &lnrpc.RouteHint{HopHints: hh})
}
return res
}
// genHopHints generated random hop hints to be included as part of a route
// hint. They may have incorrect details.
func genHopHints(t *rapid.T, rfqID rfqmsg.ID) []*lnrpc.HopHint {
res := make([]*lnrpc.HopHint, 0)
hhLen := rapid.IntRange(
minHopHints, maxHopHints,
).Draw(t, "hop_hints_len")
for range hhLen {
hop := &lnrpc.HopHint{}
// Introduce a chance of a bad SCID in the hop hint.
if rapid.Bool().Draw(t, "hop_hint_bad_scid") {
hop.ChanId = 314
} else {
hop.ChanId = uint64(rfqID.Scid())
}
// Introduce a chance of a bad node ID in the hop hint.
if rapid.Bool().Draw(t, "incorrect_peer") {
hop.NodeId = "random"
} else {
hop.NodeId = testNodeID.String()
}
res = append(res, hop)
}
return res
}
// genCustomRecords generates custom records that have a random amount of random
// asset units, and may have an SCID as routing hint.
func genCustomRecords(t *rapid.T, amtMsat int64,
rfqID rfqmsg.ID) (lnwire.CustomRecords, uint64) {
// Introduce a chance of no wire custom records.
if rapid.Bool().Draw(t, "no_wire_custom_records") {
return nil, 0
}
// Pick a random number of asset units. The amount of units may be as
// small as 1/100th of the invoice mSats, or as big as 1000x the amount
// of the invoice mSats.
assetUnits := rapid.Uint64Range(
uint64(amtMsat/100)+1,
uint64(amtMsat*1000)+1,
).Draw(t, "asset_units")
balance := []*rfqmsg.AssetBalance{
rfqmsg.NewAssetBalance(
dummyAssetID(rapid.Byte().Draw(t, "asset_id")),
assetUnits,
),
}
htlc := genHtlc(t, balance, rfqID)
customRecords, err := lnwire.ParseCustomRecords(htlc.Bytes())
require.NoError(t, err)
return customRecords, assetUnits
}
// genHtlc generates an instance of rfqmsg.Htlc with the provided asset amounts
// and rfqID.
func genHtlc(t *rapid.T, balance []*rfqmsg.AssetBalance,
rfqID rfqmsg.ID) *rfqmsg.Htlc {
// Introduce a chance of no rfqID in this htlc.
if rapid.Bool().Draw(t, "has_rfqid") {
return rfqmsg.NewHtlc(balance, fn.None[rfqmsg.ID]())
}
// Introduce a chance of a mismatch in the expected and actual htlc
// rfqID.
if rapid.Bool().Draw(t, "rfqid_match") {
return rfqmsg.NewHtlc(balance, fn.Some(dummyRfqID(
rapid.IntRange(0, 255).Draw(t, "scid"),
)))
}
return rfqmsg.NewHtlc(balance, fn.Some(rfqID))
}
// genRequest generates an InvoiceHtlcModifyRequest with random values. This
// method also returns the assetUnits and the rfqID used by the htlc.
func genRequest(t *rapid.T) (lndclient.InvoiceHtlcModifyRequest, uint64,
rfqmsg.ID) {
request := lndclient.InvoiceHtlcModifyRequest{}
rfqID := genRandomRfqID(t)
request.Invoice = genInvoice(t, rfqID)
recordsAmt := int64(0)
if request.Invoice != nil {
recordsAmt = request.Invoice.ValueMsat
}
wireRecords, assetUnits := genCustomRecords(
t, recordsAmt, rfqID,
)
request.WireCustomRecords = wireRecords
request.ExitHtlcAmt = lnwire.MilliSatoshi(recordsAmt)
return request, assetUnits, rfqID
}
// genRequests generates a random array of requests to be processed by the
// AuxInvoiceManager. It also returns the rfq map with the related rfq quotes.
func genRequests(t *rapid.T) ([]lndclient.InvoiceHtlcModifyRequest,
rfq.BuyAcceptMap) {
rfqMap := rfq.BuyAcceptMap{}
numRequests := rapid.IntRange(1, 5).Draw(t, "requestsLen")
requests := make([]lndclient.InvoiceHtlcModifyRequest, 0)
for range numRequests {
req, numAssets, scid := genRequest(t)
requests = append(requests, req)
quoteAmt := uint64(0)
if req.Invoice != nil {
quoteAmt = uint64(req.Invoice.ValueMsat)
}
genBuyQuotes(t, rfqMap, numAssets, quoteAmt, scid)
}
return requests, rfqMap
}
// genRandomVertex generates a route.Vertex instance filled with random bytes.
func genRandomVertex(t *rapid.T) route.Vertex {
var vertex route.Vertex
for i := 0; i < len(vertex); i++ {
vertex[i] = rapid.Byte().Draw(t, "vertex_byte")
}
return vertex
}
// genBuyQuotes populates the provided map of rfq quotes with the desired values
// for a specific
func genBuyQuotes(t *rapid.T, rfqMap rfq.BuyAcceptMap, units, amtMsat uint64,
scid rfqmsg.ID) {
// If the passed asset units is set to 0 this means that no wire custom
// records were set. To avoid a division by zero in the lines below we
// just set it to 1, this data is not relevant anymore.
if units == 0 {
units = 1
}
var peer route.Vertex
var assetRate *big.Int
// Introduce a chance that the quote's peerID is not correct.
if rapid.Bool().Draw(t, "nodeID_mismatch") {
peer = genRandomVertex(t)
} else {
peer = testNodeID
}
rfqScid := scid
// Introduce a chance that the quote's peerID is not correct.
if rapid.Bool().Draw(t, "scid_mismatch") {
rfqScid = rfqmsg.ID{2, 3, 4}
}
// Introduce a chance that the askPrice of this asset will result in a
// random total asset value.
if rapid.Bool().Draw(t, "no_asset_value_match") {
msatPerBtc := int64(btcutil.SatoshiPerBitcoin * 1000)
msatPerUnit := (float64(amtMsat) / float64(units)) + 1
assetRateInt := int64(float64(msatPerBtc)/msatPerUnit) + 1
// For a random asset unit price, draw a random value between
// 1/50th and double the expected price.
assetRate = big.NewInt(rapid.Int64Range(
assetRateInt/2, assetRateInt*50,
).Draw(t, "asset_msat_value"))
} else {
msatPerBtc := int64(btcutil.SatoshiPerBitcoin * 1000)
msatPerUnit := float64(amtMsat) / float64(units)
assetRate = big.NewInt(
int64(float64(msatPerBtc) / msatPerUnit),
)
}
rateFp := rfqmath.FixedPoint[rfqmath.BigInt]{
Coefficient: rfqmath.NewBigInt(assetRate),
Scale: 0,
}
rfqMap[rfqScid.Scid()] = rfqmsg.BuyAccept{
Peer: peer,
AssetRate: rfqmsg.NewAssetRate(rateFp, time.Now()),
}
}
// testInvoiceManager creates an array of requests to be processed by the
// AuxInvoiceManager. Uses the enhanced HtlcModifierMockProperty instance.
func testInvoiceManager(t *rapid.T) {
requests, rfqMap := genRequests(t)
mockRfq := &mockRfqManager{
peerBuyQuotes: rfqMap,
}
done := make(chan bool)
mockModifier := &mockHtlcModifierProperty{
requestQue: requests,
rfqMap: rfqMap,
done: done,
t: t,
}
manager := NewAuxInvoiceManager(
&InvoiceManagerConfig{
ChainParams: testChainParams,
InvoiceHtlcModifier: mockModifier,
RfqManager: mockRfq,
},
)
err := manager.Start()
require.NoError(t, err)
select {
case <-done:
case <-time.After(testTimeout):
t.Fail()
}
}
// TestAuxInvoiceManagerProperty runs property based tests on the
// AuxInvoiceManager.
func TestAuxInvoiceManagerProperty(t *testing.T) {
t.Parallel()
t.Run("invoice_manager", rapid.MakeCheck(testInvoiceManager))
}
func newHash(i []byte) []byte {
h := sha256.New()
_, _ = h.Write(i)
return h.Sum(nil)
}
func dummyAssetID(i byte) asset.ID {
return asset.ID(newHash([]byte{i}))
}
func dummyRfqID(value int) rfqmsg.ID {
return rfqmsg.ID(newHash([]byte{byte(value)}))
}
func testRouteHints() []*lnrpc.RouteHint {
return []*lnrpc.RouteHint{
{
HopHints: []*lnrpc.HopHint{
{
ChanId: 1111,
NodeId: route.Vertex{1, 1, 1}.String(),
},
{
ChanId: 1111,
NodeId: route.Vertex{1, 1, 1}.String(),
},
},
},
{
HopHints: []*lnrpc.HopHint{
{
ChanId: 1233,
NodeId: route.Vertex{1, 1, 1}.String(),
},
{
ChanId: uint64(testScid),
NodeId: testNodeID.String(),
},
},
},
}
}
func testNonAssetHints() []*lnrpc.RouteHint {
return []*lnrpc.RouteHint{
{
HopHints: []*lnrpc.HopHint{
{
ChanId: 1234,
NodeId: route.Vertex{1, 1, 1}.String(),
},
{
ChanId: 1234,
NodeId: route.Vertex{1, 1, 1}.String(),
},
},
},
{
HopHints: []*lnrpc.HopHint{
{
ChanId: 1234,
NodeId: route.Vertex{2, 2, 2}.String(),
},
},
},
}
}
func newWireCustomRecords(t *testing.T, amounts []*rfqmsg.AssetBalance,
rfqID fn.Option[rfqmsg.ID]) lnwire.CustomRecords {
htlc := rfqmsg.NewHtlc(amounts, rfqID)
customRecords, err := lnwire.ParseCustomRecords(htlc.Bytes())
require.NoError(t, err)
return customRecords
}