@@ -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.
212212type 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.
216216func 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.
881894func (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
0 commit comments