@@ -1341,7 +1341,12 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
13411341
13421342 require .LessOrEqual (t , numConfirmedSwaps , numSwaps )
13431343
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 }
13451350
13461351 lnd := test .NewMockLnd ()
13471352
@@ -1351,7 +1356,7 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
13511356 customFeeRate := func (_ context.Context ,
13521357 _ lntypes.Hash ) (chainfee.SatPerKWeight , error ) {
13531358
1354- return chainfee . SatPerKWeight ( 10_000 ) , nil
1359+ return feeRate , nil
13551360 }
13561361
13571362 presignedHelper := newMockPresignedHelper ()
@@ -1369,12 +1374,17 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
13691374 checkBatcherError (t , err )
13701375 }()
13711376
1377+ swapHashes := make ([]lntypes.Hash , numSwaps )
1378+ groups := make ([][]Input , numSwaps )
13721379 txs := make ([]* wire.MsgTx , numSwaps )
13731380 allOps := make ([]wire.OutPoint , 0 , numSwaps * sweepsPerSwap )
1381+ spendChans := make ([]<- chan * SpendDetail , numSwaps )
1382+ confChans := make ([]<- chan * chainntnfs.TxConfirmation , numSwaps )
13741383
13751384 for i := range numSwaps {
13761385 // Create a swap of sweepsPerSwap sweeps.
13771386 swapHash := lntypes.Hash {byte (i + 1 )}
1387+ swapHashes [i ] = swapHash
13781388 ops := make ([]wire.OutPoint , sweepsPerSwap )
13791389 group := make ([]Input , sweepsPerSwap )
13801390 for j := range sweepsPerSwap {
@@ -1386,15 +1396,16 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
13861396
13871397 group [j ] = Input {
13881398 Outpoint : ops [j ],
1389- Value : btcutil . Amount ( 1_000_000 * ( j + 1 )) ,
1399+ Value : sweepAmounts [ j ] ,
13901400 }
13911401 }
1402+ groups [i ] = group
13921403
13931404 // Create a swap in DB.
13941405 swap := & loopdb.LoopOutContract {
13951406 SwapContract : loopdb.SwapContract {
13961407 CltvExpiry : 111 ,
1397- AmountRequested : 3_000_000 ,
1408+ AmountRequested : swapAmount ,
13981409 ProtocolVersion : loopdb .ProtocolVersionMuSig2 ,
13991410 HtlcKeys : htlcKeys ,
14001411
@@ -1421,11 +1432,24 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
14211432 )
14221433 require .NoError (t , err )
14231434
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+
14241448 // Add the sweep, triggering the publish attempt.
14251449 require .NoError (t , batcher .AddSweep (& SweepRequest {
14261450 SwapHash : swapHash ,
14271451 Inputs : group ,
1428- Notifier : & dummyNotifier ,
1452+ Notifier : notifier ,
14291453 }))
14301454
14311455 // 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,
14631487 SpendingHeight : int32 (601 + numSwaps + 1 ),
14641488 }
14651489 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+
14661518 <- lnd .RegisterConfChannel
14671519 require .NoError (t , lnd .NotifyHeight (
14681520 int32 (601 + numSwaps + 1 + batchConfHeight ),
@@ -1474,16 +1526,61 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
14741526 // CleanupTransactions is called here.
14751527 <- presignedHelper .cleanupCalled
14761528
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 ())
14801533 }
14811534
14821535 // Missing sweeps in the confirmed transaction should be re-added to the
14831536 // batcher as new batch. The groups are added incrementally, so we need
14841537 // 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+ }
14871584
14881585 // Wait to new batch to appear and to have the expected size.
14891586 wantSize := (numSwaps - numConfirmedSwaps ) * sweepsPerSwap
@@ -1575,5 +1672,6 @@ func TestPresigned(t *testing.T) {
15751672 testPurging (3 , 1 )
15761673 testPurging (3 , 2 )
15771674 testPurging (5 , 2 )
1675+ testPurging (5 , 3 )
15781676 })
15791677}
0 commit comments