Skip to content

Commit 19b13cd

Browse files
committed
sweepbatcher: add change to PresignSweepsGroup
The batcher now presigns sweep groups including an optional change output. Change is usually leftover from the client's swap amount which is returned to the client.
1 parent 2964c44 commit 19b13cd

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

sweepbatcher/sweep_batcher.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ type SweepInfo struct {
125125
// value should be stable for a sweep. Currently presigned and
126126
// non-presigned sweeps never appear in the same batch.
127127
IsPresigned bool
128+
129+
// Change is an optional change output of the sweep.
130+
Change *wire.TxOut
128131
}
129132

130133
// SweepFetcher is used to get details of a sweep.
@@ -713,7 +716,8 @@ func (b *Batcher) Run(ctx context.Context) error {
713716
// swap. The order of sweeps is important. The first sweep serves as
714717
// primarySweepID if the group starts a new batch.
715718
func (b *Batcher) PresignSweepsGroup(ctx context.Context, inputs []Input,
716-
sweepTimeout int32, destAddress btcutil.Address) error {
719+
sweepTimeout int32, destAddress btcutil.Address,
720+
changeOutput *wire.TxOut) error {
717721

718722
if len(inputs) == 0 {
719723
return fmt.Errorf("no inputs passed to PresignSweepsGroup")
@@ -742,6 +746,7 @@ func (b *Batcher) PresignSweepsGroup(ctx context.Context, inputs []Input,
742746
outpoint: input.Outpoint,
743747
value: input.Value,
744748
timeout: sweepTimeout,
749+
change: changeOutput,
745750
}
746751
}
747752

@@ -751,7 +756,7 @@ func (b *Batcher) PresignSweepsGroup(ctx context.Context, inputs []Input,
751756

752757
return presign(
753758
ctx, b.presignedHelper, destAddress, primarySweepID, sweeps,
754-
nextBlockFeeRate,
759+
nextBlockFeeRate, b.chainParams,
755760
)
756761
}
757762

@@ -1564,6 +1569,7 @@ func (b *Batcher) loadSweep(ctx context.Context, swapHash lntypes.Hash,
15641569
minFeeRate: minFeeRate,
15651570
nonCoopHint: s.NonCoopHint,
15661571
presigned: s.IsPresigned,
1572+
change: s.Change,
15671573
}, nil
15681574
}
15691575

sweepbatcher/sweep_batcher_presigned_test.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (h *mockPresignedHelper) DestPkScript(ctx context.Context,
113113
}
114114

115115
// SignTx tries to sign the transaction. If all the inputs are online, it signs
116-
// the exact transaction passed and adds it to presignedBatches. Otherwise it
116+
// the exact transaction passed and adds it to presignedBatches. Otherwise, it
117117
// looks for a transaction in presignedBatches satisfying the criteria.
118118
func (h *mockPresignedHelper) SignTx(ctx context.Context,
119119
primarySweepID wire.OutPoint, tx *wire.MsgTx, inputAmt btcutil.Amount,
@@ -273,7 +273,7 @@ func testPresigned_forgotten_presign(t *testing.T,
273273
presignedHelper.SetOutpointOnline(op1, false)
274274
err := batcher.PresignSweepsGroup(
275275
ctx, []Input{{Outpoint: op1, Value: 1_000_000}},
276-
sweepTimeout, destAddr,
276+
sweepTimeout, destAddr, nil,
277277
)
278278
require.Error(t, err)
279279
require.ErrorContains(t, err, "offline")
@@ -350,7 +350,7 @@ func testPresigned_input1_offline_then_input2(t *testing.T,
350350
presignedHelper.SetOutpointOnline(op1, true)
351351
err = batcher.PresignSweepsGroup(
352352
ctx, []Input{{Outpoint: op1, Value: 1_000_000}},
353-
sweepTimeout, destAddr,
353+
sweepTimeout, destAddr, nil,
354354
)
355355
require.NoError(t, err)
356356

@@ -413,7 +413,7 @@ func testPresigned_input1_offline_then_input2(t *testing.T,
413413
presignedHelper.SetOutpointOnline(op2, true)
414414
err = batcher.PresignSweepsGroup(
415415
ctx, []Input{{Outpoint: op2, Value: 2_000_000}},
416-
sweepTimeout, destAddr,
416+
sweepTimeout, destAddr, nil,
417417
)
418418
require.NoError(t, err)
419419

@@ -520,7 +520,7 @@ func testPresigned_min_relay_fee(t *testing.T,
520520
presignedHelper.SetOutpointOnline(op1, true)
521521
err := batcher.PresignSweepsGroup(
522522
ctx, []Input{{Outpoint: op1, Value: inputAmt}},
523-
sweepTimeout, destAddr,
523+
sweepTimeout, destAddr, nil,
524524
)
525525
require.NoError(t, err)
526526

@@ -644,7 +644,7 @@ func testPresigned_two_inputs_one_goes_offline(t *testing.T,
644644
presignedHelper.SetOutpointOnline(op1, true)
645645
err = batcher.PresignSweepsGroup(
646646
ctx, []Input{{Outpoint: op1, Value: 1_000_000}},
647-
sweepTimeout, destAddr,
647+
sweepTimeout, destAddr, nil,
648648
)
649649
require.NoError(t, err)
650650
require.NoError(t, batcher.AddSweep(ctx, &sweepReq1))
@@ -670,7 +670,7 @@ func testPresigned_two_inputs_one_goes_offline(t *testing.T,
670670
presignedHelper.SetOutpointOnline(op2, true)
671671
err = batcher.PresignSweepsGroup(
672672
ctx, []Input{{Outpoint: op2, Value: 2_000_000}},
673-
sweepTimeout, destAddr,
673+
sweepTimeout, destAddr, nil,
674674
)
675675
require.NoError(t, err)
676676
require.NoError(t, batcher.AddSweep(ctx, &sweepReq2))
@@ -780,7 +780,7 @@ func testPresigned_first_publish_fails(t *testing.T,
780780
presignedHelper.SetOutpointOnline(op1, true)
781781
err = batcher.PresignSweepsGroup(
782782
ctx, []Input{{Outpoint: op1, Value: 1_000_000}},
783-
sweepTimeout, destAddr,
783+
sweepTimeout, destAddr, nil,
784784
)
785785
require.NoError(t, err)
786786
presignedHelper.SetOutpointOnline(op1, false)
@@ -903,7 +903,7 @@ func testPresigned_locktime(t *testing.T,
903903
presignedHelper.SetOutpointOnline(op1, true)
904904
err = batcher.PresignSweepsGroup(
905905
ctx, []Input{{Outpoint: op1, Value: 1_000_000}},
906-
sweepTimeout, destAddr,
906+
sweepTimeout, destAddr, nil,
907907
)
908908
require.NoError(t, err)
909909
presignedHelper.SetOutpointOnline(op1, false)
@@ -994,14 +994,18 @@ func testPresigned_presigned_group(t *testing.T,
994994
presignedHelper.SetOutpointOnline(op2, false)
995995

996996
// An attempt to presign must fail.
997-
err = batcher.PresignSweepsGroup(ctx, group1, sweepTimeout, destAddr)
997+
err = batcher.PresignSweepsGroup(
998+
ctx, group1, sweepTimeout, destAddr, nil,
999+
)
9981000
require.ErrorContains(t, err, "some outpoint is offline")
9991001

10001002
// Enable both outpoints.
10011003
presignedHelper.SetOutpointOnline(op2, true)
10021004

10031005
// An attempt to presign must succeed.
1004-
err = batcher.PresignSweepsGroup(ctx, group1, sweepTimeout, destAddr)
1006+
err = batcher.PresignSweepsGroup(
1007+
ctx, group1, sweepTimeout, destAddr, nil,
1008+
)
10051009
require.NoError(t, err)
10061010

10071011
// Add the sweep, triggering the publish attempt.
@@ -1053,7 +1057,9 @@ func testPresigned_presigned_group(t *testing.T,
10531057
presignedHelper.SetOutpointOnline(op4, true)
10541058

10551059
// An attempt to presign must succeed.
1056-
err = batcher.PresignSweepsGroup(ctx, group2, sweepTimeout, destAddr)
1060+
err = batcher.PresignSweepsGroup(
1061+
ctx, group2, sweepTimeout, destAddr, nil,
1062+
)
10571063
require.NoError(t, err)
10581064

10591065
// Add the sweep. It should go to the same batch.
@@ -1107,7 +1113,9 @@ func testPresigned_presigned_group(t *testing.T,
11071113
presignedHelper.SetOutpointOnline(op6, true)
11081114

11091115
// An attempt to presign must succeed.
1110-
err = batcher.PresignSweepsGroup(ctx, group3, sweepTimeout, destAddr)
1116+
err = batcher.PresignSweepsGroup(
1117+
ctx, group3, sweepTimeout, destAddr, nil,
1118+
)
11111119
require.NoError(t, err)
11121120

11131121
// Add the sweep. It should go to the same batch.
@@ -1215,9 +1223,9 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
12151223

12161224
setFeeRate(feeRateLow)
12171225

1218-
/////////////////////////////////////
1226+
// ///////////////////////////////////
12191227
// Create the first regular sweep. //
1220-
/////////////////////////////////////
1228+
// ///////////////////////////////////
12211229
swapHash1 := lntypes.Hash{1, 1, 1}
12221230
op1 := wire.OutPoint{
12231231
Hash: chainhash.Hash{1, 1},
@@ -1264,9 +1272,9 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
12641272
require.Len(t, tx1.TxIn, 1)
12651273
require.Len(t, tx1.TxOut, 1)
12661274

1267-
///////////////////////////////////////
1275+
// /////////////////////////////////////
12681276
// Create the first presigned sweep. //
1269-
///////////////////////////////////////
1277+
// /////////////////////////////////////
12701278
swapHash2 := lntypes.Hash{2, 2, 2}
12711279
op2 := wire.OutPoint{
12721280
Hash: chainhash.Hash{2, 2},
@@ -1304,7 +1312,7 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
13041312
presignedHelper.SetOutpointOnline(op2, true)
13051313
err = batcher.PresignSweepsGroup(
13061314
ctx, []Input{{Outpoint: op2, Value: 2_000_000}},
1307-
sweepTimeout, destAddr,
1315+
sweepTimeout, destAddr, nil,
13081316
)
13091317
require.NoError(t, err)
13101318
require.NoError(t, batcher.AddSweep(ctx, &sweepReq2))
@@ -1319,9 +1327,9 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
13191327
require.Len(t, tx2.TxOut, 1)
13201328
require.Equal(t, op2, tx2.TxIn[0].PreviousOutPoint)
13211329

1322-
//////////////////////////////////////
1330+
// ////////////////////////////////////
13231331
// Create the second regular sweep. //
1324-
//////////////////////////////////////
1332+
// ////////////////////////////////////
13251333
swapHash3 := lntypes.Hash{3, 3, 3}
13261334
op3 := wire.OutPoint{
13271335
Hash: chainhash.Hash{3, 3},
@@ -1359,9 +1367,9 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
13591367
// Deliver sweep request to batcher.
13601368
require.NoError(t, batcher.AddSweep(ctx, &sweepReq3))
13611369

1362-
////////////////////////////////////////
1370+
// //////////////////////////////////////
13631371
// Create the second presigned sweep. //
1364-
////////////////////////////////////////
1372+
// //////////////////////////////////////
13651373
swapHash4 := lntypes.Hash{4, 4, 4}
13661374
op4 := wire.OutPoint{
13671375
Hash: chainhash.Hash{4, 4},
@@ -1399,7 +1407,7 @@ func testPresigned_presigned_and_regular_sweeps(t *testing.T, store testStore,
13991407
presignedHelper.SetOutpointOnline(op4, true)
14001408
err = batcher.PresignSweepsGroup(
14011409
ctx, []Input{{Outpoint: op4, Value: 3_000_000}},
1402-
sweepTimeout, destAddr,
1410+
sweepTimeout, destAddr, nil,
14031411
)
14041412
require.NoError(t, err)
14051413
require.NoError(t, batcher.AddSweep(ctx, &sweepReq4))
@@ -1520,7 +1528,7 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
15201528

15211529
// An attempt to presign must succeed.
15221530
err := batcher.PresignSweepsGroup(
1523-
ctx, group, sweepTimeout, destAddr,
1531+
ctx, group, sweepTimeout, destAddr, nil,
15241532
)
15251533
require.NoError(t, err)
15261534

@@ -1584,11 +1592,11 @@ func testPresigned_purging(t *testing.T, numSwaps, numConfirmedSwaps int,
15841592

15851593
// An attempt to presign must succeed.
15861594
err := batcher.PresignSweepsGroup(
1587-
ctx, group, sweepTimeout, destAddr,
1595+
ctx, group, sweepTimeout, destAddr, nil,
15881596
)
15891597
require.NoError(t, err)
15901598

1591-
// Add the sweep, triggering the publish attempt.
1599+
// Add the sweep, triggering the publishing attempt.
15921600
require.NoError(t, batcher.AddSweep(ctx, &SweepRequest{
15931601
SwapHash: swapHash,
15941602
Inputs: group,

0 commit comments

Comments
 (0)