Skip to content

Commit 429eb85

Browse files
committed
sweepbatcher: use lnd/clock for timers
Added option: WithClock. Use it for publishDelay timer. It will be used for testing.
1 parent d7fa4ab commit 429eb85

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/lightninglabs/loop/swap"
2525
sweeppkg "github.com/lightninglabs/loop/sweep"
2626
"github.com/lightningnetwork/lnd/chainntnfs"
27+
"github.com/lightningnetwork/lnd/clock"
2728
"github.com/lightningnetwork/lnd/input"
2829
"github.com/lightningnetwork/lnd/keychain"
2930
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
@@ -134,6 +135,9 @@ type batchConfig struct {
134135
// batchConfTarget is the confirmation target of the batch transaction.
135136
batchConfTarget int32
136137

138+
// clock provides methods to work with time and timers.
139+
clock clock.Clock
140+
137141
// batchPublishDelay is the delay between receiving a new block and
138142
// publishing the batch transaction.
139143
batchPublishDelay time.Duration
@@ -527,6 +531,9 @@ func (b *batch) Run(ctx context.Context) error {
527531
return fmt.Errorf("both musig2 signers provided")
528532
}
529533

534+
// Cache clock variable.
535+
clock := b.cfg.clock
536+
530537
blockChan, blockErrChan, err :=
531538
b.chainNotifier.RegisterBlockEpochNtfn(runCtx)
532539
if err != nil {
@@ -562,7 +569,7 @@ func (b *batch) Run(ctx context.Context) error {
562569

563570
// Set the timer to publish the batch transaction after
564571
// the configured delay.
565-
timerChan = time.After(b.cfg.batchPublishDelay)
572+
timerChan = clock.TickAfter(b.cfg.batchPublishDelay)
566573
b.currentHeight = height
567574

568575
case <-timerChan:

sweepbatcher/sweep_batcher.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/lightninglabs/loop/loopdb"
1717
"github.com/lightninglabs/loop/swap"
1818
"github.com/lightninglabs/loop/utils"
19+
"github.com/lightningnetwork/lnd/clock"
1920
"github.com/lightningnetwork/lnd/input"
2021
"github.com/lightningnetwork/lnd/lntypes"
2122
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -253,6 +254,9 @@ type Batcher struct {
253254
// exit.
254255
wg sync.WaitGroup
255256

257+
// clock provides methods to work with time and timers.
258+
clock clock.Clock
259+
256260
// customFeeRate provides custom min fee rate per swap. The batch uses
257261
// max of the fee rates of its swaps. In this mode confTarget is
258262
// ignored and fee bumping by sweepbatcher is disabled.
@@ -267,6 +271,9 @@ type Batcher struct {
267271

268272
// BatcherConfig holds batcher configuration.
269273
type BatcherConfig struct {
274+
// clock provides methods to work with time and timers.
275+
clock clock.Clock
276+
270277
// customFeeRate provides custom min fee rate per swap. The batch uses
271278
// max of the fee rates of its swaps. In this mode confTarget is
272279
// ignored and fee bumping by sweepbatcher is disabled.
@@ -282,6 +289,14 @@ type BatcherConfig struct {
282289
// BatcherOption configures batcher behaviour.
283290
type BatcherOption func(*BatcherConfig)
284291

292+
// WithClock sets the clock used by sweepbatcher and its batches. It is needed
293+
// to manipulate time in tests.
294+
func WithClock(clock clock.Clock) BatcherOption {
295+
return func(cfg *BatcherConfig) {
296+
cfg.clock = clock
297+
}
298+
}
299+
285300
// WithCustomFeeRate instructs sweepbatcher not to fee bump itself and rely on
286301
// external source of fee rates (FeeRateProvider). To apply a fee rate change,
287302
// the caller should re-add the sweep by calling AddSweep.
@@ -315,6 +330,11 @@ func NewBatcher(wallet lndclient.WalletKitClient,
315330
opt(&cfg)
316331
}
317332

333+
// If WithClock was not provided, use default clock.
334+
if cfg.clock == nil {
335+
cfg.clock = clock.NewDefaultClock()
336+
}
337+
318338
if cfg.customMuSig2Signer != nil && musig2ServerSigner != nil {
319339
panic("customMuSig2Signer must not be used with " +
320340
"musig2ServerSigner")
@@ -334,6 +354,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
334354
chainParams: chainparams,
335355
store: store,
336356
sweepStore: sweepStore,
357+
clock: cfg.clock,
337358
customFeeRate: cfg.customFeeRate,
338359
customMuSig2Signer: cfg.customMuSig2Signer,
339360
}
@@ -934,6 +955,7 @@ func (b *Batcher) newBatchConfig(maxTimeoutDistance int32) batchConfig {
934955
maxTimeoutDistance: maxTimeoutDistance,
935956
noBumping: b.customFeeRate != nil,
936957
customMuSig2Signer: b.customMuSig2Signer,
958+
clock: b.clock,
937959
}
938960
}
939961

0 commit comments

Comments
 (0)