Skip to content

Commit 4198cbd

Browse files
committed
(to squash) sweepbatcher: send sweep fee in conf notification
1 parent 6929c7d commit 4198cbd

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,16 @@ func (b *batch) handleConf(ctx context.Context,
21652165
return fmt.Errorf("failed to store confirmed state: %w", err)
21662166
}
21672167

2168+
// Calculate the fee portion that each sweep should pay for the batch.
2169+
// TODO: make sure spendTx matches b.sweeps.
2170+
var totalSweptAmt btcutil.Amount
2171+
for _, s := range b.sweeps {
2172+
totalSweptAmt += s.value
2173+
}
2174+
feePortionPaidPerSweep, roundingDifference := getFeePortionForSweep(
2175+
spendTx, len(b.sweeps), totalSweptAmt,
2176+
)
2177+
21682178
// Send the confirmation to all the notifiers.
21692179
for _, s := range b.sweeps {
21702180
// If the sweep's notifier is empty then this means that
@@ -2174,6 +2184,14 @@ func (b *batch) handleConf(ctx context.Context,
21742184
continue
21752185
}
21762186

2187+
confDetail := &ConfDetail{
2188+
TxConfirmation: conf,
2189+
OnChainFeePortion: getFeePortionPaidBySweep(
2190+
spendTx, feePortionPaidPerSweep,
2191+
roundingDifference, &s,
2192+
),
2193+
}
2194+
21772195
// Notify the caller in a goroutine to avoid possible dead-lock.
21782196
go func(notifier *SpendNotifier) {
21792197
// Note that we don't unblock on ctx, because it will
@@ -2182,7 +2200,7 @@ func (b *batch) handleConf(ctx context.Context,
21822200
select {
21832201
// Try to write the confirmation to the notification
21842202
// channel.
2185-
case notifier.ConfChan <- conf:
2203+
case notifier.ConfChan <- confDetail:
21862204

21872205
// If a quit signal was provided by the swap,
21882206
// continue.

sweepbatcher/sweep_batcher.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ type addSweepsRequest struct {
281281
parentBatch *dbBatch
282282
}
283283

284+
// SpendDetail is a notification that is send to the user of sweepbatcher when
285+
// a batch gets the first confirmation.
284286
type SpendDetail struct {
285287
// Tx is the transaction that spent the outpoint.
286288
Tx *wire.MsgTx
@@ -292,6 +294,19 @@ type SpendDetail struct {
292294
OnChainFeePortion btcutil.Amount
293295
}
294296

297+
// ConfDetail is a notification that is send to the user of sweepbatcher when
298+
// a batch is fully confirmed, i.e. gets batchConfHeight confirmations.
299+
type ConfDetail struct {
300+
// TxConfirmation has data about the confirmation of the transaction.
301+
*chainntnfs.TxConfirmation
302+
303+
// OnChainFeePortion is the fee portion that was paid to get this sweep
304+
// confirmed on chain. This is the difference between the value of the
305+
// outpoint and the value of all sweeps that were included in the batch
306+
// divided by the number of sweeps.
307+
OnChainFeePortion btcutil.Amount
308+
}
309+
295310
// SpendNotifier is a notifier that is used to notify the requester of a sweep
296311
// that the sweep was successful.
297312
type SpendNotifier struct {
@@ -303,7 +318,7 @@ type SpendNotifier struct {
303318

304319
// ConfChan is a channel where the confirmation details are received.
305320
// This channel is optional.
306-
ConfChan chan<- *chainntnfs.TxConfirmation
321+
ConfChan chan<- *ConfDetail
307322

308323
// ConfErrChan is a channel where confirmation errors are received.
309324
// This channel is optional.
@@ -1185,6 +1200,7 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
11851200
case notifier.SpendChan <- spendDetail:
11861201
err := b.monitorConfAndNotify(
11871202
ctx, sweep, notifier, spendTx,
1203+
onChainFeePortion,
11881204
)
11891205
if err != nil {
11901206
b.writeToErrChan(
@@ -1240,7 +1256,8 @@ func (b *Batcher) monitorSpendAndNotify(ctx context.Context, sweep *sweep,
12401256
// is fully confirmed and we just need to deliver the data back to the caller
12411257
// though SpendNotifier.
12421258
func (b *Batcher) monitorConfAndNotify(ctx context.Context, sweep *sweep,
1243-
notifier *SpendNotifier, spendTx *wire.MsgTx) error {
1259+
notifier *SpendNotifier, spendTx *wire.MsgTx,
1260+
onChainFeePortion btcutil.Amount) error {
12441261

12451262
// If confirmation notifications were not requested, stop.
12461263
if notifier.ConfChan == nil && notifier.ConfErrChan == nil {
@@ -1276,8 +1293,13 @@ func (b *Batcher) monitorConfAndNotify(ctx context.Context, sweep *sweep,
12761293
select {
12771294
case conf := <-confChan:
12781295
if notifier.ConfChan != nil {
1296+
confDetail := &ConfDetail{
1297+
TxConfirmation: conf,
1298+
OnChainFeePortion: onChainFeePortion,
1299+
}
1300+
12791301
select {
1280-
case notifier.ConfChan <- conf:
1302+
case notifier.ConfChan <- confDetail:
12811303
case <-notifier.QuitChan:
12821304
case <-ctx.Done():
12831305
}

sweepbatcher/sweep_batcher_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
992992

993993
// Deliver sweep request to batcher.
994994
spendChan = make(chan *SpendDetail, 1)
995-
confChan := make(chan *chainntnfs.TxConfirmation)
995+
confChan := make(chan *ConfDetail)
996996
notifier = &SpendNotifier{
997997
SpendChan: spendChan,
998998
SpendErrChan: make(chan error, 1),
@@ -1059,6 +1059,7 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
10591059
conf := <-confChan
10601060
require.Equal(t, uint32(604), conf.BlockHeight)
10611061
require.Equal(t, spendingTx.TxHash(), conf.Tx.TxHash())
1062+
require.Equal(t, btcutil.Amount(fee), conf.OnChainFeePortion)
10621063

10631064
// Eventually the batch receives the confirmation notification and
10641065
// confirms itself.
@@ -1074,7 +1075,7 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
10741075
// Now emulate adding the sweep again after it was fully confirmed.
10751076
// This triggers another code path (monitorSpendAndNotify).
10761077
spendChan = make(chan *SpendDetail, 1)
1077-
confChan = make(chan *chainntnfs.TxConfirmation)
1078+
confChan = make(chan *ConfDetail)
10781079
notifier = &SpendNotifier{
10791080
SpendChan: spendChan,
10801081
SpendErrChan: make(chan error, 1),
@@ -1106,6 +1107,7 @@ func testSweepBatcherSimpleLifecycle(t *testing.T, store testStore,
11061107
conf = <-confChan
11071108
require.Equal(t, uint32(604), conf.BlockHeight)
11081109
require.Equal(t, spendingTx.TxHash(), conf.Tx.TxHash())
1110+
require.Equal(t, btcutil.Amount(fee), conf.OnChainFeePortion)
11091111

11101112
// Now check what happens in case of a spending error.
11111113
spendErrChan = make(chan error, 1)

0 commit comments

Comments
 (0)