@@ -1341,7 +1341,12 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1341
1341
1342
1342
require .LessOrEqual (t , numConfirmedSwaps , numSwaps )
1343
1343
1344
- const sweepsPerSwap = 2
1344
+ const (
1345
+ sweepsPerSwap = 2
1346
+ feeRate = chainfee .SatPerKWeight (10_000 )
1347
+ swapAmount = 3_000_001
1348
+ )
1349
+ sweepAmounts := []btcutil.Amount {1_000_001 , 2_000_000 }
1345
1350
1346
1351
lnd := test .NewMockLnd ()
1347
1352
@@ -1351,7 +1356,7 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1351
1356
customFeeRate := func (_ context.Context ,
1352
1357
_ lntypes.Hash ) (chainfee.SatPerKWeight , error ) {
1353
1358
1354
- return chainfee . SatPerKWeight ( 10_000 ) , nil
1359
+ return feeRate , nil
1355
1360
}
1356
1361
1357
1362
presignedHelper := newMockPresignedHelper ()
@@ -1369,12 +1374,17 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1369
1374
checkBatcherError (t , err )
1370
1375
}()
1371
1376
1377
+ swapHashes := make ([]lntypes.Hash , numSwaps )
1378
+ groups := make ([][]Input , numSwaps )
1372
1379
txs := make ([]* wire.MsgTx , numSwaps )
1373
1380
allOps := make ([]wire.OutPoint , 0 , numSwaps * sweepsPerSwap )
1381
+ spendChans := make ([]<- chan * SpendDetail , numSwaps )
1382
+ confChans := make ([]<- chan * chainntnfs.TxConfirmation , numSwaps )
1374
1383
1375
1384
for i := range numSwaps {
1376
1385
// Create a swap of sweepsPerSwap sweeps.
1377
1386
swapHash := lntypes.Hash {byte (i + 1 )}
1387
+ swapHashes [i ] = swapHash
1378
1388
ops := make ([]wire.OutPoint , sweepsPerSwap )
1379
1389
group := make ([]Input , sweepsPerSwap )
1380
1390
for j := range sweepsPerSwap {
@@ -1386,15 +1396,16 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1386
1396
1387
1397
group [j ] = Input {
1388
1398
Outpoint : ops [j ],
1389
- Value : btcutil . Amount ( 1_000_000 * ( j + 1 )) ,
1399
+ Value : sweepAmounts [ j ] ,
1390
1400
}
1391
1401
}
1402
+ groups [i ] = group
1392
1403
1393
1404
// Create a swap in DB.
1394
1405
swap := & loopdb.LoopOutContract {
1395
1406
SwapContract : loopdb.SwapContract {
1396
1407
CltvExpiry : 111 ,
1397
- AmountRequested : 3_000_000 ,
1408
+ AmountRequested : swapAmount ,
1398
1409
ProtocolVersion : loopdb .ProtocolVersionMuSig2 ,
1399
1410
HtlcKeys : htlcKeys ,
1400
1411
@@ -1421,11 +1432,24 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1421
1432
)
1422
1433
require .NoError (t , err )
1423
1434
1435
+ // Create a spending notification channel.
1436
+ spendChan := make (chan * SpendDetail , 1 )
1437
+ spendChans [i ] = spendChan
1438
+ confChan := make (chan * chainntnfs.TxConfirmation , 1 )
1439
+ confChans [i ] = confChan
1440
+ notifier := & SpendNotifier {
1441
+ SpendChan : spendChan ,
1442
+ SpendErrChan : make (chan error , 1 ),
1443
+ ConfChan : confChan ,
1444
+ ConfErrChan : make (chan error , 1 ),
1445
+ QuitChan : make (chan bool , 1 ),
1446
+ }
1447
+
1424
1448
// Add the sweep, triggering the publish attempt.
1425
1449
require .NoError (t , batcher .AddSweep (& SweepRequest {
1426
1450
SwapHash : swapHash ,
1427
1451
Inputs : group ,
1428
- Notifier : & dummyNotifier ,
1452
+ Notifier : notifier ,
1429
1453
}))
1430
1454
1431
1455
// For the first group it should register for the sweep's spend
@@ -1463,6 +1487,34 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1463
1487
SpendingHeight : int32 (601 + numSwaps + 1 ),
1464
1488
}
1465
1489
lnd .SpendChannel <- spendDetail
1490
+
1491
+ // Calculate the expected on-chain fee of the swap.
1492
+ wantFee := make ([]btcutil.Amount , numConfirmedSwaps )
1493
+ for i := range numConfirmedSwaps {
1494
+ batchAmount := swapAmount * btcutil .Amount (numConfirmedSwaps )
1495
+ txFee := batchAmount - btcutil .Amount (tx .TxOut [0 ].Value )
1496
+ numConfirmedSweeps := numConfirmedSwaps * sweepsPerSwap
1497
+ feePerSweep := txFee / btcutil .Amount (numConfirmedSweeps )
1498
+ roundingDiff := txFee - feePerSweep * btcutil .Amount (
1499
+ numConfirmedSweeps ,
1500
+ )
1501
+ swapFee := feePerSweep * 2
1502
+
1503
+ // Add rounding difference to the first swap.
1504
+ if i == 0 {
1505
+ swapFee += roundingDiff
1506
+ }
1507
+
1508
+ wantFee [i ] = swapFee
1509
+ }
1510
+
1511
+ // Make sure that notifiers of confirmed sweeps received notifications.
1512
+ for i := range numConfirmedSwaps {
1513
+ spend := <- spendChans [i ]
1514
+ require .Equal (t , txHash , spend .Tx .TxHash ())
1515
+ require .Equal (t , wantFee [i ], spend .OnChainFeePortion )
1516
+ }
1517
+
1466
1518
<- lnd .RegisterConfChannel
1467
1519
require .NoError (t , lnd .NotifyHeight (
1468
1520
int32 (601 + numSwaps + 1 + batchConfHeight ),
@@ -1474,16 +1526,61 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
1474
1526
// CleanupTransactions is called here.
1475
1527
<- presignedHelper .cleanupCalled
1476
1528
1477
- // If all the swaps were confirmed, stop.
1478
- if numConfirmedSwaps == numSwaps {
1479
- return
1529
+ // Make sure that notifiers of confirmed sweeps received notifications.
1530
+ for i := range numConfirmedSwaps {
1531
+ conf := <- confChans [i ]
1532
+ require .Equal (t , txHash , conf .Tx .TxHash ())
1480
1533
}
1481
1534
1482
1535
// Missing sweeps in the confirmed transaction should be re-added to the
1483
1536
// batcher as new batch. The groups are added incrementally, so we need
1484
1537
// to wait until the batch reaches the expected size.
1485
- <- lnd .RegisterSpendChannel
1486
- <- lnd .TxPublishChannel
1538
+ if numConfirmedSwaps != numSwaps {
1539
+ <- lnd .RegisterSpendChannel
1540
+ <- lnd .TxPublishChannel
1541
+ }
1542
+
1543
+ // Now make sure that a correct spenf and conf contification is sent if
1544
+ // AddSweep is called after confirming the sweeps.
1545
+ for i := range numConfirmedSwaps {
1546
+ // Create a spending notification channel.
1547
+ spendChan := make (chan * SpendDetail , 1 )
1548
+ confChan := make (chan * chainntnfs.TxConfirmation )
1549
+ notifier := & SpendNotifier {
1550
+ SpendChan : spendChan ,
1551
+ SpendErrChan : make (chan error , 1 ),
1552
+ ConfChan : confChan ,
1553
+ ConfErrChan : make (chan error , 1 ),
1554
+ QuitChan : make (chan bool , 1 ),
1555
+ }
1556
+
1557
+ // Add the sweep, triggering the publish attempt.
1558
+ require .NoError (t , batcher .AddSweep (& SweepRequest {
1559
+ SwapHash : swapHashes [i ],
1560
+ Inputs : groups [i ],
1561
+ Notifier : notifier ,
1562
+ }))
1563
+
1564
+ spendReg := <- lnd .RegisterSpendChannel
1565
+ spendReg .SpendChannel <- spendDetail
1566
+
1567
+ spend := <- spendChan
1568
+ require .Equal (t , txHash , spend .Tx .TxHash ())
1569
+ require .Equal (t , wantFee [i ], spend .OnChainFeePortion )
1570
+
1571
+ <- lnd .RegisterConfChannel
1572
+ lnd .ConfChannel <- & chainntnfs.TxConfirmation {
1573
+ Tx : tx ,
1574
+ }
1575
+
1576
+ conf := <- confChan
1577
+ require .Equal (t , tx .TxHash (), conf .Tx .TxHash ())
1578
+ }
1579
+
1580
+ // If all the swaps were confirmed, stop.
1581
+ if numConfirmedSwaps == numSwaps {
1582
+ return
1583
+ }
1487
1584
1488
1585
// Wait to new batch to appear and to have the expected size.
1489
1586
wantSize := (numSwaps - numConfirmedSwaps ) * sweepsPerSwap
@@ -1575,5 +1672,6 @@ func TestPresigned(t *testing.T) {
1575
1672
testPurging (3 , 1 )
1576
1673
testPurging (3 , 2 )
1577
1674
testPurging (5 , 2 )
1675
+ testPurging (5 , 3 )
1578
1676
})
1579
1677
}
0 commit comments