@@ -1330,10 +1330,12 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
1330
1330
}
1331
1331
1332
1332
// testPresigned_purging tests what happens if a non-final version of the batch
1333
- // is confirmed. Missing sweeps must are added to new batch(es) having valid
1334
- // presigned transactions even if the sweeps are offline at that moment.
1333
+ // is confirmed. Missing sweeps may be online or offline at that moment, which
1334
+ // depends on the last argument of the function. In online case they are added
1335
+ // to another online batch. In offline case they must are added to a new batch
1336
+ // having valid presigned transactions.
1335
1337
func testPresigned_purging (t * testing.T , numSwaps , numConfirmedSwaps int ,
1336
- store testStore , batcherStore testBatcherStore ) {
1338
+ store testStore , batcherStore testBatcherStore , online bool ) {
1337
1339
1338
1340
defer test .Guard (t )()
1339
1341
@@ -1448,6 +1450,67 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1448
1450
presignedHelper .SetOutpointOnline (op , false )
1449
1451
}
1450
1452
1453
+ // In case we are testing the addition of the remaining sweeps to a
1454
+ // batch in online state, we need to create that batch now.
1455
+ opx := wire.OutPoint {Hash : chainhash.Hash {3 , 2 , 1 }, Index : 1 }
1456
+ if online && numConfirmedSwaps < numSwaps {
1457
+ swapHash := lntypes.Hash {1 , 2 , 3 }
1458
+ const amount = 1_234_567
1459
+ group := []Input {
1460
+ {
1461
+ Outpoint : opx ,
1462
+ Value : amount ,
1463
+ },
1464
+ }
1465
+
1466
+ // Create a swap in DB.
1467
+ swap := & loopdb.LoopOutContract {
1468
+ SwapContract : loopdb.SwapContract {
1469
+ CltvExpiry : 111 ,
1470
+ AmountRequested : amount ,
1471
+ ProtocolVersion : loopdb .ProtocolVersionMuSig2 ,
1472
+ HtlcKeys : htlcKeys ,
1473
+
1474
+ // Make preimage unique to pass SQL constraints.
1475
+ Preimage : lntypes.Preimage {1 , 2 , 3 },
1476
+ },
1477
+
1478
+ DestAddr : destAddr ,
1479
+ SwapInvoice : swapInvoice ,
1480
+ SweepConfTarget : 111 ,
1481
+ }
1482
+ err := store .CreateLoopOut (ctx , swapHash , swap )
1483
+ require .NoError (t , err )
1484
+ store .AssertLoopOutStored ()
1485
+
1486
+ // Enable the sweep.
1487
+ presignedHelper .SetOutpointOnline (opx , true )
1488
+
1489
+ // An attempt to presign must succeed.
1490
+ err = batcher .PresignSweepsGroup (
1491
+ ctx , group , sweepTimeout , destAddr ,
1492
+ )
1493
+ require .NoError (t , err )
1494
+
1495
+ // Add the sweep, triggering the publish attempt.
1496
+ require .NoError (t , batcher .AddSweep (ctx , & SweepRequest {
1497
+ SwapHash : swapHash ,
1498
+ Inputs : group ,
1499
+ Notifier : & dummyNotifier ,
1500
+ }))
1501
+
1502
+ <- lnd .RegisterSpendChannel
1503
+ tx := <- lnd .TxPublishChannel
1504
+ require .Len (t , tx .TxIn , 1 )
1505
+ require .Equal (t , opx , tx .TxIn [0 ].PreviousOutPoint )
1506
+
1507
+ // Now enable our main sweeps again so the remaining ones are
1508
+ // added to this new batch.
1509
+ for _ , op := range allOps {
1510
+ presignedHelper .SetOutpointOnline (op , true )
1511
+ }
1512
+ }
1513
+
1451
1514
// Now mine the transaction which includes first numConfirmedSwaps.
1452
1515
tx := txs [numConfirmedSwaps - 1 ]
1453
1516
@@ -1477,14 +1540,21 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1477
1540
return
1478
1541
}
1479
1542
1480
- // Missing sweeps in the confirmed transaction should be re-added to the
1481
- // batcher as new batch. The groups are added incrementally, so we need
1482
- // to wait until the batch reaches the expected size.
1483
- <- lnd .RegisterSpendChannel
1484
- <- lnd .TxPublishChannel
1543
+ if ! online {
1544
+ // If the sweeps are offline, the missing sweeps in the
1545
+ // confirmed transaction should be re-added to the batcher as
1546
+ // new batch. The groups are added incrementally, so we need
1547
+ // to wait until the batch reaches the expected size.
1548
+ <- lnd .RegisterSpendChannel
1549
+ <- lnd .TxPublishChannel
1550
+ }
1485
1551
1486
1552
// Wait to new batch to appear and to have the expected size.
1487
1553
wantSize := (numSwaps - numConfirmedSwaps ) * sweepsPerSwap
1554
+ if online {
1555
+ // Add opx to the list of expected inputs.
1556
+ wantSize ++
1557
+ }
1488
1558
require .Eventually (t , func () bool {
1489
1559
// Wait for a batch with new ID to appear.
1490
1560
batches := getBatches (ctx , batcher )
@@ -1508,6 +1578,11 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1508
1578
)))
1509
1579
tx2 := <- lnd .TxPublishChannel
1510
1580
wantOps := allOps [numConfirmedSwaps * sweepsPerSwap :]
1581
+ if online {
1582
+ // Deep copy wantOps to unlink from allOps.
1583
+ wantOps = append ([]wire.OutPoint {}, wantOps ... )
1584
+ wantOps = append (wantOps , opx )
1585
+ }
1511
1586
gotOps := make ([]wire.OutPoint , 0 , len (tx2 .TxIn ))
1512
1587
for _ , txIn := range tx2 .TxIn {
1513
1588
gotOps = append (gotOps , txIn .PreviousOutPoint )
@@ -1551,27 +1626,41 @@ func TestPresigned(t *testing.T) {
1551
1626
})
1552
1627
1553
1628
t .Run ("purging" , func (t * testing.T ) {
1554
- testPurging := func (numSwaps , numConfirmedSwaps int ) {
1629
+ testPurging := func (numSwaps , numConfirmedSwaps int ,
1630
+ online bool ) {
1631
+
1555
1632
name := fmt .Sprintf ("%d of %d swaps confirmed" ,
1556
1633
numConfirmedSwaps , numSwaps )
1634
+ if online {
1635
+ name += ", sweeps online"
1636
+ } else {
1637
+ name += ", sweeps offline"
1638
+ }
1557
1639
1558
1640
t .Run (name , func (t * testing.T ) {
1559
1641
runTests (t , func (t * testing.T , store testStore ,
1560
1642
batcherStore testBatcherStore ) {
1561
1643
1562
1644
testPresigned_purging (
1563
1645
t , numSwaps , numConfirmedSwaps ,
1564
- store , batcherStore ,
1646
+ store , batcherStore , online ,
1565
1647
)
1566
1648
})
1567
1649
})
1568
1650
}
1569
1651
1570
- testPurging (1 , 1 )
1571
- testPurging (2 , 1 )
1572
- testPurging (2 , 2 )
1573
- testPurging (3 , 1 )
1574
- testPurging (3 , 2 )
1575
- testPurging (5 , 2 )
1652
+ // Test cases in which the sweeps are offline.
1653
+ testPurging (1 , 1 , false )
1654
+ testPurging (2 , 1 , false )
1655
+ testPurging (2 , 2 , false )
1656
+ testPurging (3 , 1 , false )
1657
+ testPurging (3 , 2 , false )
1658
+ testPurging (5 , 2 , false )
1659
+
1660
+ // Test cases in which the sweeps are online.
1661
+ testPurging (2 , 1 , true )
1662
+ testPurging (3 , 1 , true )
1663
+ testPurging (3 , 2 , true )
1664
+ testPurging (5 , 2 , true )
1576
1665
})
1577
1666
}
0 commit comments