Skip to content

Commit 3320ab7

Browse files
committed
(to squash) add purging test when sweeps are online
1 parent 1bd22e1 commit 3320ab7

File tree

1 file changed

+105
-16
lines changed

1 file changed

+105
-16
lines changed

sweepbatcher/sweep_batcher_presigned_test.go

Lines changed: 105 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,10 +1330,12 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
13301330
}
13311331

13321332
// 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.
13351337
func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1336-
store testStore, batcherStore testBatcherStore) {
1338+
store testStore, batcherStore testBatcherStore, online bool) {
13371339

13381340
defer test.Guard(t)()
13391341

@@ -1448,6 +1450,67 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
14481450
presignedHelper.SetOutpointOnline(op, false)
14491451
}
14501452

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+
14511514
// Now mine the transaction which includes first numConfirmedSwaps.
14521515
tx := txs[numConfirmedSwaps-1]
14531516

@@ -1477,14 +1540,21 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
14771540
return
14781541
}
14791542

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+
}
14851551

14861552
// Wait to new batch to appear and to have the expected size.
14871553
wantSize := (numSwaps - numConfirmedSwaps) * sweepsPerSwap
1554+
if online {
1555+
// Add opx to the list of expected inputs.
1556+
wantSize++
1557+
}
14881558
require.Eventually(t, func() bool {
14891559
// Wait for a batch with new ID to appear.
14901560
batches := getBatches(ctx, batcher)
@@ -1508,6 +1578,11 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
15081578
)))
15091579
tx2 := <-lnd.TxPublishChannel
15101580
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+
}
15111586
gotOps := make([]wire.OutPoint, 0, len(tx2.TxIn))
15121587
for _, txIn := range tx2.TxIn {
15131588
gotOps = append(gotOps, txIn.PreviousOutPoint)
@@ -1551,27 +1626,41 @@ func TestPresigned(t *testing.T) {
15511626
})
15521627

15531628
t.Run("purging", func(t *testing.T) {
1554-
testPurging := func(numSwaps, numConfirmedSwaps int) {
1629+
testPurging := func(numSwaps, numConfirmedSwaps int,
1630+
online bool) {
1631+
15551632
name := fmt.Sprintf("%d of %d swaps confirmed",
15561633
numConfirmedSwaps, numSwaps)
1634+
if online {
1635+
name += ", sweeps online"
1636+
} else {
1637+
name += ", sweeps offline"
1638+
}
15571639

15581640
t.Run(name, func(t *testing.T) {
15591641
runTests(t, func(t *testing.T, store testStore,
15601642
batcherStore testBatcherStore) {
15611643

15621644
testPresigned_purging(
15631645
t, numSwaps, numConfirmedSwaps,
1564-
store, batcherStore,
1646+
store, batcherStore, online,
15651647
)
15661648
})
15671649
})
15681650
}
15691651

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)
15761665
})
15771666
}

0 commit comments

Comments
 (0)