@@ -165,6 +165,21 @@ type VerifySchnorrSig func(pubKey *btcec.PublicKey, hash, sig []byte) error
165165type FeeRateProvider func (ctx context.Context ,
166166 swapHash lntypes.Hash ) (chainfee.SatPerKWeight , error )
167167
168+ // InitialDelayProvider returns the duration after new batch creation before it
169+ // is first published. It allows to customize the duration based on total value
170+ // of the batch. There is a trade-off between better grouping and getting funds
171+ // faster. If the function returns an error, no delay is used and the error is
172+ // logged as a warning.
173+ type InitialDelayProvider func (ctx context.Context , numSweeps int ,
174+ value btcutil.Amount ) (time.Duration , error )
175+
176+ // zeroInitialDelay returns no delay for any sweeps.
177+ func zeroInitialDelay (_ context.Context , _ int ,
178+ _ btcutil.Amount ) (time.Duration , error ) {
179+
180+ return 0 , nil
181+ }
182+
168183// PublishErrorHandler is a function that handles transaction publishing error.
169184type PublishErrorHandler func (err error , errMsg string , log btclog.Logger )
170185
@@ -306,13 +321,13 @@ type Batcher struct {
306321 // clock provides methods to work with time and timers.
307322 clock clock.Clock
308323
309- // initialDelay is the delay of first batch publishing after creation.
310- // It only affects newly created batches, not batches loaded from DB,
311- // so publishing does happen in case of a daemon restart (especially
312- // important in case of a crashloop). If a sweep is about to expire
313- // (time until timeout is less that 2x initialDelay), then waiting is
314- // skipped.
315- initialDelay time. Duration
324+ // initialDelayProvider provides the delay of first batch publishing
325+ // after creation. It only affects newly created batches, not batches
326+ // loaded from DB, so publishing does happen in case of a daemon restart
327+ // (especially important in case of a crashloop). If a sweep is about to
328+ // expire (time until timeout is less that 2x initialDelay), then
329+ // waiting is skipped.
330+ initialDelayProvider InitialDelayProvider
316331
317332 // publishDelay is the delay of batch publishing that is applied in the
318333 // beginning, after the appearance of a new block in the network or
@@ -346,13 +361,13 @@ type BatcherConfig struct {
346361 // clock provides methods to work with time and timers.
347362 clock clock.Clock
348363
349- // initialDelay is the delay of first batch publishing after creation.
350- // It only affects newly created batches, not batches loaded from DB,
351- // so publishing does happen in case of a daemon restart (especially
352- // important in case of a crashloop). If a sweep is about to expire
353- // (time until timeout is less that 2x initialDelay), then waiting is
354- // skipped.
355- initialDelay time. Duration
364+ // initialDelayProvider provides the delay of first batch publishing
365+ // after creation. It only affects newly created batches, not batches
366+ // loaded from DB, so publishing does happen in case of a daemon restart
367+ // (especially important in case of a crashloop). If a sweep is about to
368+ // expire (time until timeout is less that 2x initialDelay), then
369+ // waiting is skipped.
370+ initialDelayProvider InitialDelayProvider
356371
357372 // publishDelay is the delay of batch publishing that is applied in the
358373 // beginning, after the appearance of a new block in the network or
@@ -397,9 +412,9 @@ func WithClock(clock clock.Clock) BatcherOption {
397412// better grouping. Defaults to 0s (no initial delay). If a sweep is about
398413// to expire (time until timeout is less that 2x initialDelay), then waiting
399414// is skipped.
400- func WithInitialDelay (initialDelay time. Duration ) BatcherOption {
415+ func WithInitialDelay (provider InitialDelayProvider ) BatcherOption {
401416 return func (cfg * BatcherConfig ) {
402- cfg .initialDelay = initialDelay
417+ cfg .initialDelayProvider = provider
403418 }
404419}
405420
@@ -485,27 +500,27 @@ func NewBatcher(wallet lndclient.WalletKitClient,
485500 }
486501
487502 return & Batcher {
488- batches : make (map [int32 ]* batch ),
489- sweepReqs : make (chan SweepRequest ),
490- testReqs : make (chan * testRequest ),
491- errChan : make (chan error , 1 ),
492- quit : make (chan struct {}),
493- initDone : make (chan struct {}),
494- wallet : wallet ,
495- chainNotifier : chainNotifier ,
496- signerClient : signerClient ,
497- musig2ServerSign : musig2ServerSigner ,
498- VerifySchnorrSig : verifySchnorrSig ,
499- chainParams : chainparams ,
500- store : store ,
501- sweepStore : sweepStore ,
502- clock : cfg .clock ,
503- initialDelay : cfg .initialDelay ,
504- publishDelay : cfg .publishDelay ,
505- customFeeRate : cfg .customFeeRate ,
506- txLabeler : cfg .txLabeler ,
507- customMuSig2Signer : cfg .customMuSig2Signer ,
508- publishErrorHandler : cfg .publishErrorHandler ,
503+ batches : make (map [int32 ]* batch ),
504+ sweepReqs : make (chan SweepRequest ),
505+ testReqs : make (chan * testRequest ),
506+ errChan : make (chan error , 1 ),
507+ quit : make (chan struct {}),
508+ initDone : make (chan struct {}),
509+ wallet : wallet ,
510+ chainNotifier : chainNotifier ,
511+ signerClient : signerClient ,
512+ musig2ServerSign : musig2ServerSigner ,
513+ VerifySchnorrSig : verifySchnorrSig ,
514+ chainParams : chainparams ,
515+ store : store ,
516+ sweepStore : sweepStore ,
517+ clock : cfg .clock ,
518+ initialDelayProvider : cfg .initialDelayProvider ,
519+ publishDelay : cfg .publishDelay ,
520+ customFeeRate : cfg .customFeeRate ,
521+ txLabeler : cfg .txLabeler ,
522+ customMuSig2Signer : cfg .customMuSig2Signer ,
523+ publishErrorHandler : cfg .publishErrorHandler ,
509524 }
510525}
511526
@@ -764,11 +779,10 @@ func (b *Batcher) spinUpBatch(ctx context.Context) (*batch, error) {
764779 cfg .batchPublishDelay = b .publishDelay
765780 }
766781
767- if b . initialDelay < 0 {
768- return nil , fmt . Errorf ( "negative initialDelay: %v" ,
769- b . initialDelay )
782+ cfg . initialDelayProvider = b . initialDelayProvider
783+ if cfg . initialDelayProvider == nil {
784+ cfg . initialDelayProvider = zeroInitialDelay
770785 }
771- cfg .initialDelay = b .initialDelay
772786
773787 batchKit := b .newBatchKit ()
774788
@@ -862,6 +876,8 @@ func (b *Batcher) spinUpBatchFromDB(ctx context.Context, batch *batch) error {
862876 // Note that initialDelay and batchPublishDelay are 0 for batches
863877 // recovered from DB so publishing happen in case of a daemon restart
864878 // (especially important in case of a crashloop).
879+ cfg .initialDelayProvider = zeroInitialDelay
880+
865881 newBatch , err := NewBatchFromDB (cfg , batchKit )
866882 if err != nil {
867883 return fmt .Errorf ("failed in NewBatchFromDB: %w" , err )
0 commit comments