@@ -15,6 +15,7 @@ import (
15
15
"github.com/btcsuite/btcd/chaincfg/chainhash"
16
16
"github.com/btcsuite/btcd/wire"
17
17
"github.com/btcsuite/btclog"
18
+ "github.com/lightninglabs/lndclient"
18
19
"github.com/lightninglabs/loop/loopdb"
19
20
"github.com/lightninglabs/loop/test"
20
21
"github.com/lightninglabs/loop/utils"
@@ -401,6 +402,144 @@ func testFeeBumping(t *testing.T, store testStore,
401
402
}
402
403
}
403
404
405
+ // walletKitWrapper wraps a wallet kit and memorizes the label of the most
406
+ // recent published transaction.
407
+ type walletKitWrapper struct {
408
+ lndclient.WalletKitClient
409
+
410
+ lastLabel string
411
+ }
412
+
413
+ // PublishTransaction publishes the transaction and memorizes its label.
414
+ func (w * walletKitWrapper ) PublishTransaction (ctx context.Context ,
415
+ tx * wire.MsgTx , label string ) error {
416
+
417
+ w .lastLabel = label
418
+
419
+ return w .WalletKitClient .PublishTransaction (ctx , tx , label )
420
+ }
421
+
422
+ // testTxLabeler tests transaction labels.
423
+ func testTxLabeler (t * testing.T , store testStore ,
424
+ batcherStore testBatcherStore ) {
425
+
426
+ defer test .Guard (t )()
427
+
428
+ lnd := test .NewMockLnd ()
429
+ ctx , cancel := context .WithCancel (context .Background ())
430
+
431
+ sweepStore , err := NewSweepFetcherFromSwapStore (store , lnd .ChainParams )
432
+ require .NoError (t , err )
433
+
434
+ walletKit := & walletKitWrapper {WalletKitClient : lnd .WalletKit }
435
+
436
+ batcher := NewBatcher (walletKit , lnd .ChainNotifier , lnd .Signer ,
437
+ testMuSig2SignSweep , testVerifySchnorrSig , lnd .ChainParams ,
438
+ batcherStore , sweepStore )
439
+
440
+ var (
441
+ runErr error
442
+ wg sync.WaitGroup
443
+ )
444
+
445
+ wg .Add (1 )
446
+ go func () {
447
+ defer wg .Done ()
448
+ runErr = batcher .Run (ctx )
449
+ }()
450
+
451
+ // Create a sweep request.
452
+ sweepReq1 := SweepRequest {
453
+ SwapHash : lntypes.Hash {1 , 1 , 1 },
454
+ Value : 111 ,
455
+ Outpoint : wire.OutPoint {
456
+ Hash : chainhash.Hash {1 , 1 },
457
+ Index : 1 ,
458
+ },
459
+ Notifier : & dummyNotifier ,
460
+ }
461
+
462
+ swap1 := & loopdb.LoopOutContract {
463
+ SwapContract : loopdb.SwapContract {
464
+ CltvExpiry : 111 ,
465
+ AmountRequested : 111 ,
466
+ ProtocolVersion : loopdb .ProtocolVersionMuSig2 ,
467
+ HtlcKeys : htlcKeys ,
468
+ },
469
+
470
+ DestAddr : destAddr ,
471
+ SwapInvoice : swapInvoice ,
472
+ SweepConfTarget : 111 ,
473
+ }
474
+
475
+ err = store .CreateLoopOut (ctx , sweepReq1 .SwapHash , swap1 )
476
+ require .NoError (t , err )
477
+ store .AssertLoopOutStored ()
478
+
479
+ // Deliver sweep request to batcher.
480
+ require .NoError (t , batcher .AddSweep (& sweepReq1 ))
481
+
482
+ // Eventually request will be consumed and a new batch will spin up.
483
+ require .Eventually (t , func () bool {
484
+ return len (batcher .batches ) == 1
485
+ }, test .Timeout , eventuallyCheckFrequency )
486
+
487
+ // When batch is successfully created it will execute it's first step,
488
+ // which leads to a spend monitor of the primary sweep.
489
+ <- lnd .RegisterSpendChannel
490
+
491
+ // Wait for tx to be published.
492
+ <- lnd .TxPublishChannel
493
+
494
+ // Find the batch and assign it to a local variable for easier access.
495
+ var theBatch * batch
496
+ for _ , btch := range batcher .batches {
497
+ if btch .primarySweepID == sweepReq1 .SwapHash {
498
+ theBatch = btch
499
+ }
500
+ }
501
+
502
+ // Now test the label.
503
+ wantLabel := fmt .Sprintf ("BatchOutSweepSuccess -- %d" , theBatch .id )
504
+ require .Equal (t , wantLabel , walletKit .lastLabel )
505
+
506
+ // Now make the batcher quit by canceling the context.
507
+ cancel ()
508
+ wg .Wait ()
509
+ checkBatcherError (t , runErr )
510
+
511
+ // Define dummy tx labeler, always returning "test".
512
+ txLabeler := func (batchID int32 ) string {
513
+ return "test"
514
+ }
515
+
516
+ // Now try it with option WithTxLabeler.
517
+ batcher = NewBatcher (walletKit , lnd .ChainNotifier , lnd .Signer ,
518
+ testMuSig2SignSweep , testVerifySchnorrSig , lnd .ChainParams ,
519
+ batcherStore , sweepStore , WithTxLabeler (txLabeler ))
520
+
521
+ ctx , cancel = context .WithCancel (context .Background ())
522
+ wg .Add (1 )
523
+ go func () {
524
+ defer wg .Done ()
525
+ runErr = batcher .Run (ctx )
526
+ }()
527
+
528
+ // Expect batch to register for spending.
529
+ <- lnd .RegisterSpendChannel
530
+
531
+ // Wait for tx to be published.
532
+ <- lnd .TxPublishChannel
533
+
534
+ // Now test the label.
535
+ require .Equal (t , "test" , walletKit .lastLabel )
536
+
537
+ // Now make the batcher quit by canceling the context.
538
+ cancel ()
539
+ wg .Wait ()
540
+ checkBatcherError (t , runErr )
541
+ }
542
+
404
543
// testSweepBatcherSimpleLifecycle tests the simple lifecycle of the batches
405
544
// that are created and run by the batcher.
406
545
func testSweepBatcherSimpleLifecycle (t * testing.T , store testStore ,
@@ -2767,6 +2906,11 @@ func TestFeeBumping(t *testing.T) {
2767
2906
})
2768
2907
}
2769
2908
2909
+ // TestTxLabeler tests transaction labels.
2910
+ func TestTxLabeler (t * testing.T ) {
2911
+ runTests (t , testTxLabeler )
2912
+ }
2913
+
2770
2914
// TestSweepBatcherSimpleLifecycle tests the simple lifecycle of the batches
2771
2915
// that are created and run by the batcher.
2772
2916
func TestSweepBatcherSimpleLifecycle (t * testing.T ) {
0 commit comments