Skip to content

Commit 422bcd7

Browse files
committed
sweepbatcher: fast-flag enables zero delay publishing
1 parent 92a8726 commit 422bcd7

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

sweepbatcher/greedy_batch_selection.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ func (b *Batcher) greedyAddSweeps(ctx context.Context, sweeps []*sweep) error {
7575

7676
// Try batches, starting with the best.
7777
for _, batchId := range batchesIds {
78-
// If the best option is to start new batch, do it.
78+
// If the best option is to start a new batch, do it.
7979
if batchId == newBatchSignal {
80-
return b.spinUpNewBatch(ctx, sweeps)
80+
fast := false
81+
return b.spinUpNewBatch(ctx, sweeps, fast)
8182
}
8283

8384
// Locate the batch to add the sweeps to.

sweepbatcher/sweep_batch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,9 @@ func (b *batch) Run(ctx context.Context) error {
900900

901901
skipBeforeUpdated := false
902902
if totalSweptAmt != 0 {
903+
fast := false
903904
initialDelay, err := b.cfg.initialDelayProvider(
904-
ctx, len(b.sweeps), totalSweptAmt,
905+
ctx, len(b.sweeps), totalSweptAmt, fast,
905906
)
906907
if err != nil {
907908
b.Warnf("InitialDelayProvider failed: %v. We "+

sweepbatcher/sweep_batcher.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ type FeeRateProvider func(ctx context.Context, swapHash lntypes.Hash,
210210
// faster. If the function returns an error, no delay is used and the error is
211211
// logged as a warning.
212212
type InitialDelayProvider func(ctx context.Context, numSweeps int,
213-
value btcutil.Amount) (time.Duration, error)
213+
value btcutil.Amount, fast bool) (time.Duration, error)
214214

215215
// zeroInitialDelay returns no delay for any sweeps.
216216
func zeroInitialDelay(_ context.Context, _ int,
217-
_ btcutil.Amount) (time.Duration, error) {
217+
_ btcutil.Amount, _ bool) (time.Duration, error) {
218218

219219
return 0, nil
220220
}
@@ -259,6 +259,10 @@ type SweepRequest struct {
259259
// Notifier is a notifier that is used to notify the requester of this
260260
// sweep that the sweep was successful.
261261
Notifier *SpendNotifier
262+
263+
// Fast is set by the client if the sweep should be published
264+
// immediately.
265+
Fast bool
262266
}
263267

264268
// addSweepsRequest is a request to sweep an outpoint or a group of outpoints
@@ -271,6 +275,9 @@ type addSweepsRequest struct {
271275
// Notifier is a notifier that is used to notify the requester of this
272276
// sweep that the sweep was successful.
273277
notifier *SpendNotifier
278+
279+
// fast indicates sweeps that are part of a fast swap.
280+
fast bool
274281
}
275282

276283
// SpendDetail is a notification that is send to the user of sweepbatcher when
@@ -679,7 +686,9 @@ func (b *Batcher) Run(ctx context.Context) error {
679686
for {
680687
select {
681688
case req := <-b.addSweepsChan:
682-
err = b.handleSweeps(runCtx, req.sweeps, req.notifier)
689+
err = b.handleSweeps(
690+
runCtx, req.sweeps, req.notifier, req.fast,
691+
)
683692
if err != nil {
684693
warnf("handleSweeps failed: %v.", err)
685694

@@ -792,12 +801,15 @@ func (b *Batcher) AddSweep(ctx context.Context, sweepReq *SweepRequest) error {
792801
return fmt.Errorf("failed to get the status of sweep %v: %w",
793802
sweep.outpoint, err)
794803
}
795-
var fullyConfirmed bool
804+
var (
805+
parentBatch *dbBatch
806+
fullyConfirmed bool
807+
)
796808
if completed {
797809
// Verify that the parent batch is confirmed. Note that a batch
798810
// is only considered confirmed after it has received three
799811
// on-chain confirmations to prevent issues caused by reorgs.
800-
parentBatch, err := b.store.GetParentBatch(ctx, sweep.outpoint)
812+
parentBatch, err = b.store.GetParentBatch(ctx, sweep.outpoint)
801813
if err != nil {
802814
return fmt.Errorf("unable to get parent batch for "+
803815
"sweep %x: %w", sweep.swapHash[:6], err)
@@ -835,6 +847,7 @@ func (b *Batcher) AddSweep(ctx context.Context, sweepReq *SweepRequest) error {
835847
req := &addSweepsRequest{
836848
sweeps: sweeps,
837849
notifier: sweepReq.Notifier,
850+
fast: sweepReq.Fast,
838851
}
839852

840853
select {
@@ -879,7 +892,7 @@ func (b *Batcher) testRunInEventLoop(ctx context.Context, handler func()) {
879892
// handleSweeps handles a sweep request by either placing the group of sweeps in
880893
// an existing batch, or by spinning up a new batch for it.
881894
func (b *Batcher) handleSweeps(ctx context.Context, sweeps []*sweep,
882-
notifier *SpendNotifier) error {
895+
notifier *SpendNotifier, fast bool) error {
883896

884897
// Since the whole group is added to the same batch and belongs to
885898
// the same transaction, we use sweeps[0] below where we need any sweep.
@@ -968,6 +981,12 @@ func (b *Batcher) handleSweeps(ctx context.Context, sweeps []*sweep,
968981
}
969982
}
970983

984+
// If fast is set, we spin up a new batch which is published
985+
// immediately.
986+
if fast {
987+
return b.spinUpNewBatch(ctx, sweeps, true)
988+
}
989+
971990
// Try to run the greedy algorithm of batch selection to minimize costs.
972991
err = b.greedyAddSweeps(ctx, sweeps)
973992
if err == nil {
@@ -994,15 +1013,17 @@ func (b *Batcher) handleSweeps(ctx context.Context, sweeps []*sweep,
9941013

9951014
// If no batch is capable of accepting the sweep, we spin up a fresh
9961015
// batch and hand the sweep over to it.
997-
return b.spinUpNewBatch(ctx, sweeps)
1016+
return b.spinUpNewBatch(ctx, sweeps, false)
9981017
}
9991018

10001019
// spinUpNewBatch creates new batch, starts it and adds the sweeps to it. If
10011020
// presigned mode is enabled, the result also depends on outcome of
10021021
// presignedHelper.SignTx.
1003-
func (b *Batcher) spinUpNewBatch(ctx context.Context, sweeps []*sweep) error {
1022+
func (b *Batcher) spinUpNewBatch(ctx context.Context, sweeps []*sweep,
1023+
fast bool) error {
1024+
10041025
// Spin up a fresh batch.
1005-
newBatch, err := b.spinUpBatch(ctx)
1026+
newBatch, err := b.spinUpBatch(ctx, fast)
10061027
if err != nil {
10071028
return err
10081029
}
@@ -1024,7 +1045,7 @@ func (b *Batcher) spinUpNewBatch(ctx context.Context, sweeps []*sweep) error {
10241045
}
10251046

10261047
// spinUpBatch spins up a new batch and returns it.
1027-
func (b *Batcher) spinUpBatch(ctx context.Context) (*batch, error) {
1048+
func (b *Batcher) spinUpBatch(ctx context.Context, fast bool) (*batch, error) {
10281049
cfg := b.newBatchConfig(defaultMaxTimeoutDistance)
10291050

10301051
switch b.chainParams {
@@ -1044,7 +1065,7 @@ func (b *Batcher) spinUpBatch(ctx context.Context) (*batch, error) {
10441065
}
10451066

10461067
cfg.initialDelayProvider = b.initialDelayProvider
1047-
if cfg.initialDelayProvider == nil {
1068+
if cfg.initialDelayProvider == nil || fast {
10481069
cfg.initialDelayProvider = zeroInitialDelay
10491070
}
10501071

sweepbatcher/sweep_batcher_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ func testDelays(t *testing.T, store testStore, batcherStore testBatcherStore) {
13741374
)
13751375

13761376
initialDelayProvider := func(_ context.Context, _ int,
1377-
_ btcutil.Amount) (time.Duration, error) {
1377+
_ btcutil.Amount, fast bool) (time.Duration, error) {
13781378

13791379
return initialDelay, nil
13801380
}
@@ -1652,7 +1652,7 @@ func testDelays(t *testing.T, store testStore, batcherStore testBatcherStore) {
16521652
const largeInitialDelay = 6 * time.Hour
16531653

16541654
largeInitialDelayProvider := func(_ context.Context, _ int,
1655-
_ btcutil.Amount) (time.Duration, error) {
1655+
_ btcutil.Amount, fast bool) (time.Duration, error) {
16561656

16571657
return largeInitialDelay, nil
16581658
}
@@ -1877,7 +1877,7 @@ func testCustomDelays(t *testing.T, store testStore,
18771877

18781878
// initialDelay returns initialDelay depending of batch size (sats).
18791879
initialDelayProvider := func(_ context.Context, numSweeps int,
1880-
value btcutil.Amount) (time.Duration, error) {
1880+
value btcutil.Amount, fast bool) (time.Duration, error) {
18811881

18821882
if value <= swapSize1 {
18831883
// Verify the number of sweeps.

0 commit comments

Comments
 (0)