-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathlitd_custom_channels_test.go
3989 lines (3432 loc) · 132 KB
/
litd_custom_channels_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
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 itest
import (
"bytes"
"context"
"fmt"
"math"
"math/big"
"slices"
"time"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/itest"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/rfqmath"
"github.com/lightninglabs/taproot-assets/rfqmsg"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
oraclerpc "github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
"github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
"github.com/lightninglabs/taproot-assets/taprpc/tapchannelrpc"
tchrpc "github.com/lightninglabs/taproot-assets/taprpc/tapchannelrpc"
"github.com/lightninglabs/taproot-assets/taprpc/universerpc"
"github.com/lightninglabs/taproot-assets/tapscript"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntest/port"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/stretchr/testify/require"
)
var (
dummyMetaData = &taprpc.AssetMeta{
Data: []byte("some metadata"),
}
itestAsset = &mintrpc.MintAsset{
AssetType: taprpc.AssetType_NORMAL,
Name: "itest-asset-cents",
AssetMeta: dummyMetaData,
Amount: 1_000_000,
}
shortTimeout = time.Second * 5
)
var (
lndArgsTemplate = []string{
"--trickledelay=50",
"--gossip.sub-batch-delay=5ms",
"--caches.rpc-graph-cache-duration=100ms",
"--default-remote-max-htlcs=483",
"--dust-threshold=5000000",
"--rpcmiddleware.enable",
"--protocol.anchors",
"--protocol.option-scid-alias",
"--protocol.zero-conf",
"--protocol.simple-taproot-chans",
"--protocol.simple-taproot-overlay-chans",
"--protocol.custom-message=17",
"--accept-keysend",
"--debuglevel=trace,GRPC=error,BTCN=info",
}
litdArgsTemplateNoOracle = []string{
"--taproot-assets.allow-public-uni-proof-courier",
"--taproot-assets.universe.public-access=rw",
"--taproot-assets.universe.sync-all-assets",
"--taproot-assets.universerpccourier.skipinitdelay",
"--taproot-assets.universerpccourier.backoffresetwait=1s",
"--taproot-assets.universerpccourier.numtries=5",
"--taproot-assets.universerpccourier.initialbackoff=300ms",
"--taproot-assets.universerpccourier.maxbackoff=600ms",
"--taproot-assets.universerpccourier.skipinitdelay",
"--taproot-assets.universerpccourier.backoffresetwait=100ms",
"--taproot-assets.universerpccourier.initialbackoff=300ms",
"--taproot-assets.universerpccourier.maxbackoff=600ms",
"--taproot-assets.custodianproofretrievaldelay=500ms",
}
litdArgsTemplate = append(litdArgsTemplateNoOracle, []string{
"--taproot-assets.experimental.rfq.priceoracleaddress=" +
"use_mock_price_oracle_service_promise_to_" +
"not_use_on_mainnet",
"--taproot-assets.experimental.rfq.mockoracleassetsperbtc=" +
"5820600",
}...)
)
const (
fundingAmount = 50_000
startAmount = fundingAmount * 2
)
// testCustomChannelsLarge tests that we can create a network with custom
// channels and send large asset payments over them.
func testCustomChannelsLarge(_ context.Context, net *NetworkHarness,
t *harnessTest) {
lndArgs := slices.Clone(lndArgsTemplate)
litdArgs := slices.Clone(litdArgsTemplate)
// Explicitly set the proof courier as Zane (now has no other role
// other than proof shuffling), otherwise a hashmail courier will be
// used. For the funding transaction, we're just posting it and don't
// expect a true receiver.
zane, err := net.NewNode(
t.t, "Zane", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
litdArgs = append(litdArgs, fmt.Sprintf(
"--taproot-assets.proofcourieraddr=%s://%s",
proof.UniverseRpcCourierType, zane.Cfg.LitAddr(),
))
// The topology we are going for looks like the following:
//
// Charlie --[assets]--> Dave --[sats]--> Erin --[assets]--> Fabia
// |
// |
// [assets]
// |
// v
// Yara
//
// With [assets] being a custom channel and [sats] being a normal, BTC
// only channel.
// All 5 nodes need to be full litd nodes running in integrated mode
// with tapd included. We also need specific flags to be enabled, so we
// create 5 completely new nodes, ignoring the two default nodes that
// are created by the harness.
charlie, err := net.NewNode(
t.t, "Charlie", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
dave, err := net.NewNode(t.t, "Dave", lndArgs, false, true, litdArgs...)
require.NoError(t.t, err)
erin, err := net.NewNode(t.t, "Erin", lndArgs, false, true, litdArgs...)
require.NoError(t.t, err)
fabia, err := net.NewNode(
t.t, "Fabia", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
yara, err := net.NewNode(
t.t, "Yara", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
nodes := []*HarnessNode{charlie, dave, erin, fabia, yara}
connectAllNodes(t.t, net, nodes)
fundAllNodes(t.t, net, nodes)
// Create the normal channel between Dave and Erin.
t.Logf("Opening normal channel between Dave and Erin...")
channelOp := openChannelAndAssert(
t, net, dave, erin, lntest.OpenChannelParams{
Amt: 10_000_000,
SatPerVByte: 5,
},
)
defer closeChannelAndAssert(t, net, dave, channelOp, false)
// This is the only public channel, we need everyone to be aware of it.
assertChannelKnown(t.t, charlie, channelOp)
assertChannelKnown(t.t, fabia, channelOp)
universeTap := newTapClient(t.t, zane)
charlieTap := newTapClient(t.t, charlie)
daveTap := newTapClient(t.t, dave)
erinTap := newTapClient(t.t, erin)
fabiaTap := newTapClient(t.t, fabia)
yaraTap := newTapClient(t.t, yara)
// Mint an asset on Charlie and sync all nodes to Charlie as the
// universe.
mintedAssets := itest.MintAssetsConfirmBatch(
t.t, t.lndHarness.Miner.Client, charlieTap,
[]*mintrpc.MintAssetRequest{
{
Asset: itestAsset,
},
},
)
cents := mintedAssets[0]
assetID := cents.AssetGenesis.AssetId
t.Logf("Minted %d lightning cents, syncing universes...", cents.Amount)
syncUniverses(t.t, charlieTap, dave, erin, fabia, yara)
t.Logf("Universes synced between all nodes, distributing assets...")
const (
daveFundingAmount = uint64(400_000)
erinFundingAmount = uint64(200_000)
)
charlieFundingAmount := cents.Amount - uint64(2*400_000)
chanPointCD, _, _ := createTestAssetNetwork(
t, net, charlieTap, daveTap, erinTap, fabiaTap, yaraTap,
universeTap, cents, 400_000, charlieFundingAmount,
daveFundingAmount, erinFundingAmount, DefaultPushSat,
)
// Before we start sending out payments, let's make sure each node can
// see the other one in the graph and has all required features.
require.NoError(t.t, t.lndHarness.AssertNodeKnown(charlie, dave))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(dave, charlie))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(dave, yara))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(yara, dave))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(erin, fabia))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(fabia, erin))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(charlie, erin))
// Print initial channel balances.
logBalance(t.t, nodes, assetID, "initial")
// Try larger invoice payments, first from Charlie to Fabia, then half
// of the amount back in the other direction.
const fabiaInvoiceAssetAmount = 20_000
invoiceResp := createAssetInvoice(
t.t, erin, fabia, fabiaInvoiceAssetAmount, assetID,
)
payInvoiceWithAssets(
t.t, charlie, dave, invoiceResp.PaymentRequest, assetID,
)
logBalance(t.t, nodes, assetID, "after invoice")
invoiceResp2 := createAssetInvoice(
t.t, dave, charlie, fabiaInvoiceAssetAmount/2, assetID,
)
// Sleep for a second to make sure the balances fully propagated before
// we make the payment. Otherwise, we'll make an RFQ order with a max
// amount of zero.
time.Sleep(time.Second * 1)
payInvoiceWithAssets(
t.t, fabia, erin, invoiceResp2.PaymentRequest, assetID,
)
logBalance(t.t, nodes, assetID, "after invoice 2")
// Now we send a large invoice from Charlie to Dave.
const largeInvoiceAmount = 100_000
invoiceResp3 := createAssetInvoice(
t.t, charlie, dave, largeInvoiceAmount, assetID,
)
payInvoiceWithAssets(
t.t, charlie, dave, invoiceResp3.PaymentRequest, assetID,
)
logBalance(t.t, nodes, assetID, "after invoice 3")
// Make sure the invoice on the receiver side and the payment on the
// sender side show the individual HTLCs that arrived for it and that
// they show the correct asset amounts when decoded.
assertInvoiceHtlcAssets(
t.t, dave, invoiceResp3, assetID, largeInvoiceAmount,
)
assertPaymentHtlcAssets(
t.t, charlie, invoiceResp3.RHash, assetID, largeInvoiceAmount,
)
// We keysend the rest, so that all the balance is on Dave's side.
charlieRemainingBalance := charlieFundingAmount - largeInvoiceAmount -
fabiaInvoiceAssetAmount/2
sendAssetKeySendPayment(
t.t, charlie, dave, charlieRemainingBalance,
assetID, fn.None[int64](),
)
logBalance(t.t, nodes, assetID, "after keysend")
// And now we close the channel to test how things look if all the
// balance is on the non-initiator (recipient) side.
t.Logf("Closing Charlie -> Dave channel")
closeAssetChannelAndAssert(
t, net, charlie, dave, chanPointCD, assetID, nil,
universeTap, initiatorZeroAssetBalanceCoOpBalanceCheck,
)
}
// testCustomChannels tests that we can create a network with custom channels
// and send asset payments over them.
func testCustomChannels(ctx context.Context, net *NetworkHarness,
t *harnessTest) {
lndArgs := slices.Clone(lndArgsTemplate)
litdArgs := slices.Clone(litdArgsTemplate)
// Explicitly set the proof courier as Zane (now has no other role
// other than proof shuffling), otherwise a hashmail courier will be
// used. For the funding transaction, we're just posting it and don't
// expect a true receiver.
zane, err := net.NewNode(
t.t, "Zane", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
litdArgs = append(litdArgs, fmt.Sprintf(
"--taproot-assets.proofcourieraddr=%s://%s",
proof.UniverseRpcCourierType, zane.Cfg.LitAddr(),
))
// The topology we are going for looks like the following:
//
// Charlie --[assets]--> Dave --[sats]--> Erin --[assets]--> Fabia
// |
// |
// [assets]
// |
// v
// Yara
//
// With [assets] being a custom channel and [sats] being a normal, BTC
// only channel.
// All 5 nodes need to be full litd nodes running in integrated mode
// with tapd included. We also need specific flags to be enabled, so we
// create 5 completely new nodes, ignoring the two default nodes that
// are created by the harness.
charlie, err := net.NewNode(
t.t, "Charlie", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
dave, err := net.NewNode(t.t, "Dave", lndArgs, false, true, litdArgs...)
require.NoError(t.t, err)
erin, err := net.NewNode(t.t, "Erin", lndArgs, false, true, litdArgs...)
require.NoError(t.t, err)
fabia, err := net.NewNode(
t.t, "Fabia", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
yara, err := net.NewNode(
t.t, "Yara", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
nodes := []*HarnessNode{charlie, dave, erin, fabia, yara}
connectAllNodes(t.t, net, nodes)
fundAllNodes(t.t, net, nodes)
// Create the normal channel between Dave and Erin.
t.Logf("Opening normal channel between Dave and Erin...")
channelOp := openChannelAndAssert(
t, net, dave, erin, lntest.OpenChannelParams{
Amt: 5_000_000,
SatPerVByte: 5,
},
)
defer closeChannelAndAssert(t, net, dave, channelOp, false)
// This is the only public channel, we need everyone to be aware of it.
assertChannelKnown(t.t, charlie, channelOp)
assertChannelKnown(t.t, fabia, channelOp)
universeTap := newTapClient(t.t, zane)
charlieTap := newTapClient(t.t, charlie)
daveTap := newTapClient(t.t, dave)
erinTap := newTapClient(t.t, erin)
fabiaTap := newTapClient(t.t, fabia)
yaraTap := newTapClient(t.t, yara)
// Mint an asset on Charlie and sync all nodes to Charlie as the
// universe.
mintedAssets := itest.MintAssetsConfirmBatch(
t.t, t.lndHarness.Miner.Client, charlieTap,
[]*mintrpc.MintAssetRequest{
{
Asset: itestAsset,
},
},
)
cents := mintedAssets[0]
assetID := cents.AssetGenesis.AssetId
fundingScriptTree := tapscript.NewChannelFundingScriptTree()
fundingScriptKey := fundingScriptTree.TaprootKey
fundingScriptTreeBytes := fundingScriptKey.SerializeCompressed()
t.Logf("Minted %d lightning cents, syncing universes...", cents.Amount)
syncUniverses(t.t, charlieTap, dave, erin, fabia, yara)
t.Logf("Universes synced between all nodes, distributing assets...")
const (
daveFundingAmount = uint64(startAmount)
erinFundingAmount = uint64(fundingAmount)
)
charlieFundingAmount := cents.Amount - 2*startAmount
chanPointCD, chanPointDY, chanPointEF := createTestAssetNetwork(
t, net, charlieTap, daveTap, erinTap, fabiaTap, yaraTap,
universeTap, cents, startAmount, charlieFundingAmount,
daveFundingAmount, erinFundingAmount, DefaultPushSat,
)
// We'll be tracking the expected asset balances throughout the test, so
// we can assert it after each action.
charlieAssetBalance := charlieFundingAmount
daveAssetBalance := uint64(startAmount)
erinAssetBalance := uint64(startAmount)
fabiaAssetBalance := uint64(0)
yaraAssetBalance := uint64(0)
// Before we start sending out payments, let's make sure each node can
// see the other one in the graph and has all required features.
require.NoError(t.t, t.lndHarness.AssertNodeKnown(charlie, dave))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(dave, charlie))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(dave, yara))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(yara, dave))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(erin, fabia))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(fabia, erin))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(charlie, erin))
// Print initial channel balances.
logBalance(t.t, nodes, assetID, "initial")
// ------------
// Test case 1: Send a direct keysend payment from Charlie to Dave,
// sending the whole balance.
// ------------
keySendAmount := charlieFundingAmount
sendAssetKeySendPayment(
t.t, charlie, dave, charlieFundingAmount, assetID,
fn.None[int64](),
)
logBalance(t.t, nodes, assetID, "after keysend")
charlieAssetBalance -= keySendAmount
daveAssetBalance += keySendAmount
// We should be able to send 1000 assets back immediately, because
// there is enough on-chain balance on Dave's side to be able to create
// an HTLC. We use an invoice to execute another code path.
const charlieInvoiceAmount = 1_000
invoiceResp := createAssetInvoice(
t.t, dave, charlie, charlieInvoiceAmount, assetID,
)
payInvoiceWithAssets(
t.t, dave, charlie, invoiceResp.PaymentRequest, assetID,
withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after invoice back")
// Make sure the invoice on the receiver side and the payment on the
// sender side show the individual HTLCs that arrived for it and that
// they show the correct asset amounts when decoded.
assertInvoiceHtlcAssets(
t.t, charlie, invoiceResp, assetID, charlieInvoiceAmount,
)
assertPaymentHtlcAssets(
t.t, dave, invoiceResp.RHash, assetID, charlieInvoiceAmount,
)
charlieAssetBalance += charlieInvoiceAmount
daveAssetBalance -= charlieInvoiceAmount
// We should also be able to do a non-asset (BTC only) keysend payment
// from Charlie to Dave. This'll also replenish the BTC balance of
// Dave, making it possible to send another asset HTLC below, sending
// all assets back to Charlie (so we have enough balance for further
// tests).
sendKeySendPayment(t.t, charlie, dave, 2000)
logBalance(t.t, nodes, assetID, "after BTC only keysend")
// Let's keysend the rest of the balance back to Charlie.
sendAssetKeySendPayment(
t.t, dave, charlie, charlieFundingAmount-charlieInvoiceAmount,
assetID, fn.None[int64](),
)
logBalance(t.t, nodes, assetID, "after keysend back")
charlieAssetBalance += charlieFundingAmount - charlieInvoiceAmount
daveAssetBalance -= charlieFundingAmount - charlieInvoiceAmount
// ------------
// Test case 2: Pay a normal invoice from Dave by Charlie, making it
// a direct channel invoice payment with no RFQ SCID present in the
// invoice.
// ------------
createAndPayNormalInvoice(
t.t, charlie, dave, dave, 20_000, assetID, withSmallShards(),
withFailure(lnrpc.Payment_FAILED, failureIncorrectDetails),
)
logBalance(t.t, nodes, assetID, "after invoice")
// We should also be able to do a multi-hop BTC only payment, paying an
// invoice from Erin by Charlie.
createAndPayNormalInvoiceWithBtc(t.t, charlie, erin, 2000)
logBalance(t.t, nodes, assetID, "after BTC only invoice")
// ------------
// Test case 3: Pay an asset invoice from Dave by Charlie, making it
// a direct channel invoice payment with an RFQ SCID present in the
// invoice.
// ------------
const daveInvoiceAssetAmount = 2_000
invoiceResp = createAssetInvoice(
t.t, charlie, dave, daveInvoiceAssetAmount, assetID,
)
payInvoiceWithAssets(
t.t, charlie, dave, invoiceResp.PaymentRequest, assetID,
withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after invoice")
charlieAssetBalance -= daveInvoiceAssetAmount
daveAssetBalance += daveInvoiceAssetAmount
// ------------
// Test case 3.5: Pay an asset invoice from Dave by Charlie with normal
// satoshi payment flow. We expect that payment to fail, since it's a
// direct channel payment and the invoice is for assets, not sats. So
// without a conversion, it is rejected by the receiver.
// ------------
invoiceResp = createAssetInvoice(
t.t, charlie, dave, daveInvoiceAssetAmount, assetID,
)
payInvoiceWithSatoshi(
t.t, charlie, invoiceResp, withFailure(
lnrpc.Payment_FAILED, failureIncorrectDetails,
),
)
logBalance(t.t, nodes, assetID, "after asset invoice paid with sats")
// We don't need to update the asset balances of Charlie and Dave here
// as the invoice payment failed.
// ------------
// Test case 4: Pay a normal invoice from Erin by Charlie.
// ------------
paidAssetAmount := createAndPayNormalInvoice(
t.t, charlie, dave, erin, 20_000, assetID, withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after invoice")
charlieAssetBalance -= paidAssetAmount
daveAssetBalance += paidAssetAmount
// ------------
// Test case 5: Create an asset invoice on Fabia and pay it from
// Charlie.
// ------------
const fabiaInvoiceAssetAmount1 = 1000
invoiceResp = createAssetInvoice(
t.t, erin, fabia, fabiaInvoiceAssetAmount1, assetID,
)
payInvoiceWithAssets(
t.t, charlie, dave, invoiceResp.PaymentRequest, assetID,
withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after invoice")
charlieAssetBalance -= fabiaInvoiceAssetAmount1
daveAssetBalance += fabiaInvoiceAssetAmount1
erinAssetBalance -= fabiaInvoiceAssetAmount1
fabiaAssetBalance += fabiaInvoiceAssetAmount1
// ------------
// Test case 6: Create an asset invoice on Fabia and pay it with just
// BTC from Dave, making sure it ends up being a multipart payment (we
// set the maximum shard size to 80k sat and 15k asset units will be
// more than a single shard).
// ------------
const fabiaInvoiceAssetAmount2 = 15_000
invoiceResp = createAssetInvoice(
t.t, erin, fabia, fabiaInvoiceAssetAmount2, assetID,
)
payInvoiceWithSatoshi(t.t, dave, invoiceResp)
logBalance(t.t, nodes, assetID, "after invoice")
erinAssetBalance -= fabiaInvoiceAssetAmount2
fabiaAssetBalance += fabiaInvoiceAssetAmount2
// ------------
// Test case 7: Create an asset invoice on Fabia and pay it with assets
// from Charlie, making sure it ends up being a multipart payment as
// well, with the high amount of asset units to send and the hard coded
// 80k sat max shard size.
// ------------
const fabiaInvoiceAssetAmount3 = 10_000
invoiceResp = createAssetInvoice(
t.t, erin, fabia, fabiaInvoiceAssetAmount3, assetID,
)
payInvoiceWithAssets(
t.t, charlie, dave, invoiceResp.PaymentRequest, assetID,
withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after invoice")
charlieAssetBalance -= fabiaInvoiceAssetAmount3
daveAssetBalance += fabiaInvoiceAssetAmount3
erinAssetBalance -= fabiaInvoiceAssetAmount3
fabiaAssetBalance += fabiaInvoiceAssetAmount3
// ------------
// Test case 8: An invoice payment over two channels that are both asset
// channels.
// ------------
logBalance(t.t, nodes, assetID, "before asset-to-asset")
const yaraInvoiceAssetAmount1 = 1000
invoiceResp = createAssetInvoice(
t.t, dave, yara, yaraInvoiceAssetAmount1, assetID,
)
payInvoiceWithAssets(
t.t, charlie, dave, invoiceResp.PaymentRequest, assetID,
withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after asset-to-asset")
charlieAssetBalance -= yaraInvoiceAssetAmount1
yaraAssetBalance += yaraInvoiceAssetAmount1
// ------------
// Test case 8: Now we'll close each of the channels, starting with the
// Charlie -> Dave custom channel.
// ------------
t.Logf("Closing Charlie -> Dave channel")
closeAssetChannelAndAssert(
t, net, charlie, dave, chanPointCD, assetID, nil,
universeTap, assertDefaultCoOpCloseBalance(true, true),
)
t.Logf("Closing Dave -> Yara channel, close initiated by Yara")
closeAssetChannelAndAssert(
t, net, yara, dave, chanPointDY, assetID, nil,
universeTap, assertDefaultCoOpCloseBalance(false, true),
)
t.Logf("Closing Erin -> Fabia channel")
closeAssetChannelAndAssert(
t, net, erin, fabia, chanPointEF, assetID, nil,
universeTap, assertDefaultCoOpCloseBalance(true, true),
)
// We've been tracking the off-chain channel balances all this time, so
// now that we have the assets on-chain again, we can assert them. Due
// to rounding errors that happened when sending multiple shards with
// MPP, we need to do some slight adjustments.
charlieAssetBalance += 1
erinAssetBalance += 4
fabiaAssetBalance -= 4
yaraAssetBalance -= 1
assertAssetBalance(t.t, charlieTap, assetID, charlieAssetBalance)
assertAssetBalance(t.t, daveTap, assetID, daveAssetBalance)
assertAssetBalance(t.t, erinTap, assetID, erinAssetBalance)
assertAssetBalance(t.t, fabiaTap, assetID, fabiaAssetBalance)
assertAssetBalance(t.t, yaraTap, assetID, yaraAssetBalance)
// ------------
// Test case 10: We now open a new asset channel and close it again, to
// make sure that a non-existent remote balance is handled correctly.
t.Logf("Opening new asset channel between Charlie and Dave...")
fundRespCD, err := charlieTap.FundChannel(
ctx, &tchrpc.FundChannelRequest{
AssetAmount: fundingAmount,
AssetId: assetID,
PeerPubkey: dave.PubKey[:],
FeeRateSatPerVbyte: 5,
},
)
require.NoError(t.t, err)
t.Logf("Funded second channel between Charlie and Dave: %v", fundRespCD)
mineBlocks(t, net, 6, 1)
// Assert that the proofs for both channels has been uploaded to the
// designated Universe server.
assertUniverseProofExists(
t.t, universeTap, assetID, nil, fundingScriptTreeBytes,
fmt.Sprintf("%v:%v", fundRespCD.Txid, fundRespCD.OutputIndex),
)
assertAssetChan(t.t, charlie, dave, fundingAmount, cents)
// And let's just close the channel again.
chanPointCD = &lnrpc.ChannelPoint{
OutputIndex: uint32(fundRespCD.OutputIndex),
FundingTxid: &lnrpc.ChannelPoint_FundingTxidStr{
FundingTxidStr: fundRespCD.Txid,
},
}
t.Logf("Closing Charlie -> Dave channel")
closeAssetChannelAndAssert(
t, net, charlie, dave, chanPointCD, assetID, nil,
universeTap, assertDefaultCoOpCloseBalance(false, false),
)
// Charlie should still have four asset pieces, two with the same size.
assertNumAssetOutputs(t.t, charlieTap, assetID, 2)
assertAssetExists(
t.t, charlieTap, assetID, charlieAssetBalance-fundingAmount,
nil, true, false, false,
)
assertAssetExists(
t.t, charlieTap, assetID, fundingAmount, nil, true, true,
false,
)
// Dave should have two outputs, one from the initial channel with Yara
// and one from the remaining amount of the channel with Charlie.
assertNumAssetOutputs(t.t, daveTap, assetID, 2)
daveFirstChannelRemainder := daveFundingAmount -
yaraInvoiceAssetAmount1 + 1
assertAssetExists(
t.t, daveTap, assetID, daveFirstChannelRemainder, nil, true,
true, false,
)
assertAssetExists(
t.t, daveTap, assetID,
daveAssetBalance-daveFirstChannelRemainder, nil, true, true,
false,
)
// Fabia and Yara should all have a single output each, just what was
// left over from the initial channel.
assertNumAssetOutputs(t.t, fabiaTap, assetID, 1)
assertAssetExists(
t.t, fabiaTap, assetID, fabiaAssetBalance, nil, true, true,
false,
)
assertNumAssetOutputs(t.t, yaraTap, assetID, 1)
assertAssetExists(
t.t, yaraTap, assetID, yaraAssetBalance, nil, true, true, false,
)
// Erin didn't use all of his assets when opening the channel, so he
// should have two outputs, the change from the channel opening and the
// remaining amount after closing the channel.
assertNumAssetOutputs(t.t, erinTap, assetID, 2)
erinChange := startAmount - erinFundingAmount
assertAssetExists(
t.t, erinTap, assetID, erinAssetBalance-erinChange, nil, true,
true, false,
)
assertAssetExists(
t.t, erinTap, assetID, erinChange, nil, true, false, false,
)
// The asset balances should still remain unchanged.
assertAssetBalance(t.t, charlieTap, assetID, charlieAssetBalance)
assertAssetBalance(t.t, daveTap, assetID, daveAssetBalance)
assertAssetBalance(t.t, erinTap, assetID, erinAssetBalance)
assertAssetBalance(t.t, fabiaTap, assetID, fabiaAssetBalance)
}
// testCustomChannelsGroupedAsset tests that we can create a network with custom
// channels that use grouped assets and send asset payments over them.
func testCustomChannelsGroupedAsset(ctx context.Context, net *NetworkHarness,
t *harnessTest) {
lndArgs := slices.Clone(lndArgsTemplate)
litdArgs := slices.Clone(litdArgsTemplate)
// Explicitly set the proof courier as Zane (now has no other role
// other than proof shuffling), otherwise a hashmail courier will be
// used. For the funding transaction, we're just posting it and don't
// expect a true receiver.
zane, err := net.NewNode(
t.t, "Zane", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
litdArgs = append(litdArgs, fmt.Sprintf(
"--taproot-assets.proofcourieraddr=%s://%s",
proof.UniverseRpcCourierType, zane.Cfg.LitAddr(),
))
// The topology we are going for looks like the following:
//
// Charlie --[assets]--> Dave --[sats]--> Erin --[assets]--> Fabia
// |
// |
// [assets]
// |
// v
// Yara
//
// With [assets] being a custom channel and [sats] being a normal, BTC
// only channel.
// All 5 nodes need to be full litd nodes running in integrated mode
// with tapd included. We also need specific flags to be enabled, so we
// create 5 completely new nodes, ignoring the two default nodes that
// are created by the harness.
charlie, err := net.NewNode(
t.t, "Charlie", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
dave, err := net.NewNode(t.t, "Dave", lndArgs, false, true, litdArgs...)
require.NoError(t.t, err)
erin, err := net.NewNode(t.t, "Erin", lndArgs, false, true, litdArgs...)
require.NoError(t.t, err)
fabia, err := net.NewNode(
t.t, "Fabia", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
yara, err := net.NewNode(
t.t, "Yara", lndArgs, false, true, litdArgs...,
)
require.NoError(t.t, err)
nodes := []*HarnessNode{charlie, dave, erin, fabia, yara}
connectAllNodes(t.t, net, nodes)
fundAllNodes(t.t, net, nodes)
// Create the normal channel between Dave and Erin.
t.Logf("Opening normal channel between Dave and Erin...")
channelOp := openChannelAndAssert(
t, net, dave, erin, lntest.OpenChannelParams{
Amt: 5_000_000,
SatPerVByte: 5,
},
)
defer closeChannelAndAssert(t, net, dave, channelOp, false)
// This is the only public channel, we need everyone to be aware of it.
assertChannelKnown(t.t, charlie, channelOp)
assertChannelKnown(t.t, fabia, channelOp)
universeTap := newTapClient(t.t, zane)
charlieTap := newTapClient(t.t, charlie)
daveTap := newTapClient(t.t, dave)
erinTap := newTapClient(t.t, erin)
fabiaTap := newTapClient(t.t, fabia)
yaraTap := newTapClient(t.t, yara)
groupAssetReq := itest.CopyRequest(&mintrpc.MintAssetRequest{
Asset: itestAsset,
})
groupAssetReq.Asset.NewGroupedAsset = true
// Mint an asset on Charlie and sync all nodes to Charlie as the
// universe.
mintedAssets := itest.MintAssetsConfirmBatch(
t.t, t.lndHarness.Miner.Client, charlieTap,
[]*mintrpc.MintAssetRequest{groupAssetReq},
)
cents := mintedAssets[0]
assetID := cents.AssetGenesis.AssetId
groupID := cents.GetAssetGroup().GetTweakedGroupKey()
fundingScriptTree := tapscript.NewChannelFundingScriptTree()
fundingScriptKey := fundingScriptTree.TaprootKey
fundingScriptTreeBytes := fundingScriptKey.SerializeCompressed()
t.Logf("Minted %d lightning cents, syncing universes...", cents.Amount)
syncUniverses(t.t, charlieTap, dave, erin, fabia, yara)
t.Logf("Universes synced between all nodes, distributing assets...")
const (
daveFundingAmount = uint64(startAmount)
erinFundingAmount = uint64(fundingAmount)
)
charlieFundingAmount := cents.Amount - 2*startAmount
chanPointCD, chanPointDY, chanPointEF := createTestAssetNetwork(
t, net, charlieTap, daveTap, erinTap, fabiaTap, yaraTap,
universeTap, cents, startAmount, charlieFundingAmount,
daveFundingAmount, erinFundingAmount, DefaultPushSat,
)
// We'll be tracking the expected asset balances throughout the test, so
// we can assert it after each action.
charlieAssetBalance := charlieFundingAmount
daveAssetBalance := uint64(startAmount)
erinAssetBalance := uint64(startAmount)
fabiaAssetBalance := uint64(0)
yaraAssetBalance := uint64(0)
// Before we start sending out payments, let's make sure each node can
// see the other one in the graph and has all required features.
require.NoError(t.t, t.lndHarness.AssertNodeKnown(charlie, dave))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(dave, charlie))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(dave, yara))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(yara, dave))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(erin, fabia))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(fabia, erin))
require.NoError(t.t, t.lndHarness.AssertNodeKnown(charlie, erin))
// Print initial channel balances.
logBalance(t.t, nodes, assetID, "initial")
// ------------
// Test case 1: Send a direct keysend payment from Charlie to Dave.
// ------------
const keySendAmount = 100
sendAssetKeySendPayment(
t.t, charlie, dave, keySendAmount, assetID, fn.None[int64](),
)
logBalance(t.t, nodes, assetID, "after keysend")
charlieAssetBalance -= keySendAmount
daveAssetBalance += keySendAmount
// We should be able to send the 100 assets back immediately, because
// there is enough on-chain balance on Dave's side to be able to create
// an HTLC.
sendAssetKeySendPayment(
t.t, dave, charlie, keySendAmount, assetID, fn.None[int64](),
)
logBalance(t.t, nodes, assetID, "after keysend back")
charlieAssetBalance += keySendAmount
daveAssetBalance -= keySendAmount
// We should also be able to do a non-asset (BTC only) keysend payment.
sendKeySendPayment(t.t, charlie, dave, 2000)
logBalance(t.t, nodes, assetID, "after BTC only keysend")
// ------------
// Test case 2: Pay a normal invoice from Dave by Charlie, making it
// a direct channel invoice payment with no RFQ SCID present in the
// invoice.
// ------------
createAndPayNormalInvoice(
t.t, charlie, dave, dave, 20_000, assetID, withSmallShards(),
withFailure(lnrpc.Payment_FAILED, failureIncorrectDetails),
)
logBalance(t.t, nodes, assetID, "after invoice")
// We should also be able to do a multi-hop BTC only payment, paying an
// invoice from Erin by Charlie.
createAndPayNormalInvoiceWithBtc(t.t, charlie, erin, 2000)
logBalance(t.t, nodes, assetID, "after BTC only invoice")
// ------------
// Test case 3: Pay an asset invoice from Dave by Charlie, making it
// a direct channel invoice payment with an RFQ SCID present in the
// invoice.
// ------------
const daveInvoiceAssetAmount = 2_000
invoiceResp := createAssetInvoice(
t.t, charlie, dave, daveInvoiceAssetAmount, assetID,
)
payInvoiceWithAssets(
t.t, charlie, dave, invoiceResp.PaymentRequest, assetID,
withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after invoice")
// Make sure the invoice on the receiver side and the payment on the
// sender side show the individual HTLCs that arrived for it and that
// they show the correct asset amounts when decoded.
assertInvoiceHtlcAssets(
t.t, dave, invoiceResp, assetID, daveInvoiceAssetAmount,
)
assertPaymentHtlcAssets(
t.t, charlie, invoiceResp.RHash, assetID,
daveInvoiceAssetAmount,
)
charlieAssetBalance -= daveInvoiceAssetAmount
daveAssetBalance += daveInvoiceAssetAmount
// ------------
// Test case 4: Pay a normal invoice from Erin by Charlie.
// ------------
paidAssetAmount := createAndPayNormalInvoice(
t.t, charlie, dave, erin, 20_000, assetID, withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after invoice")
charlieAssetBalance -= paidAssetAmount
daveAssetBalance += paidAssetAmount
// ------------
// Test case 5: Create an asset invoice on Fabia and pay it from
// Charlie.
// ------------
const fabiaInvoiceAssetAmount1 = 1000
invoiceResp = createAssetInvoice(
t.t, erin, fabia, fabiaInvoiceAssetAmount1, assetID,
)
payInvoiceWithAssets(
t.t, charlie, dave, invoiceResp.PaymentRequest, assetID,
withSmallShards(),
)
logBalance(t.t, nodes, assetID, "after invoice")
charlieAssetBalance -= fabiaInvoiceAssetAmount1
daveAssetBalance += fabiaInvoiceAssetAmount1
erinAssetBalance -= fabiaInvoiceAssetAmount1
fabiaAssetBalance += fabiaInvoiceAssetAmount1
// ------------
// Test case 6: Create an asset invoice on Fabia and pay it with just
// BTC from Dave, making sure it ends up being a multipart payment (we
// set the maximum shard size to 80k sat and 15k asset units will be
// more than a single shard).
// ------------
const fabiaInvoiceAssetAmount2 = 15_000
invoiceResp = createAssetInvoice(
t.t, erin, fabia, fabiaInvoiceAssetAmount2, assetID,
)
payInvoiceWithSatoshi(t.t, dave, invoiceResp)