2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-09 19:10:47 +00:00

sweepbatcher: use lnd/clock for timers

Added option: WithClock. Use it for publishDelay timer.
It will be used for testing.
This commit is contained in:
Boris Nagaev 2024-08-03 23:18:55 -03:00
parent d7fa4ab94d
commit 429eb85e14
No known key found for this signature in database
2 changed files with 30 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import (
"github.com/lightninglabs/loop/swap"
sweeppkg "github.com/lightninglabs/loop/sweep"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
@ -134,6 +135,9 @@ type batchConfig struct {
// batchConfTarget is the confirmation target of the batch transaction.
batchConfTarget int32
// clock provides methods to work with time and timers.
clock clock.Clock
// batchPublishDelay is the delay between receiving a new block and
// publishing the batch transaction.
batchPublishDelay time.Duration
@ -527,6 +531,9 @@ func (b *batch) Run(ctx context.Context) error {
return fmt.Errorf("both musig2 signers provided")
}
// Cache clock variable.
clock := b.cfg.clock
blockChan, blockErrChan, err :=
b.chainNotifier.RegisterBlockEpochNtfn(runCtx)
if err != nil {
@ -562,7 +569,7 @@ func (b *batch) Run(ctx context.Context) error {
// Set the timer to publish the batch transaction after
// the configured delay.
timerChan = time.After(b.cfg.batchPublishDelay)
timerChan = clock.TickAfter(b.cfg.batchPublishDelay)
b.currentHeight = height
case <-timerChan:

View File

@ -16,6 +16,7 @@ import (
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/utils"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@ -253,6 +254,9 @@ type Batcher struct {
// exit.
wg sync.WaitGroup
// clock provides methods to work with time and timers.
clock clock.Clock
// customFeeRate provides custom min fee rate per swap. The batch uses
// max of the fee rates of its swaps. In this mode confTarget is
// ignored and fee bumping by sweepbatcher is disabled.
@ -267,6 +271,9 @@ type Batcher struct {
// BatcherConfig holds batcher configuration.
type BatcherConfig struct {
// clock provides methods to work with time and timers.
clock clock.Clock
// customFeeRate provides custom min fee rate per swap. The batch uses
// max of the fee rates of its swaps. In this mode confTarget is
// ignored and fee bumping by sweepbatcher is disabled.
@ -282,6 +289,14 @@ type BatcherConfig struct {
// BatcherOption configures batcher behaviour.
type BatcherOption func(*BatcherConfig)
// WithClock sets the clock used by sweepbatcher and its batches. It is needed
// to manipulate time in tests.
func WithClock(clock clock.Clock) BatcherOption {
return func(cfg *BatcherConfig) {
cfg.clock = clock
}
}
// WithCustomFeeRate instructs sweepbatcher not to fee bump itself and rely on
// external source of fee rates (FeeRateProvider). To apply a fee rate change,
// the caller should re-add the sweep by calling AddSweep.
@ -315,6 +330,11 @@ func NewBatcher(wallet lndclient.WalletKitClient,
opt(&cfg)
}
// If WithClock was not provided, use default clock.
if cfg.clock == nil {
cfg.clock = clock.NewDefaultClock()
}
if cfg.customMuSig2Signer != nil && musig2ServerSigner != nil {
panic("customMuSig2Signer must not be used with " +
"musig2ServerSigner")
@ -334,6 +354,7 @@ func NewBatcher(wallet lndclient.WalletKitClient,
chainParams: chainparams,
store: store,
sweepStore: sweepStore,
clock: cfg.clock,
customFeeRate: cfg.customFeeRate,
customMuSig2Signer: cfg.customMuSig2Signer,
}
@ -934,6 +955,7 @@ func (b *Batcher) newBatchConfig(maxTimeoutDistance int32) batchConfig {
maxTimeoutDistance: maxTimeoutDistance,
noBumping: b.customFeeRate != nil,
customMuSig2Signer: b.customMuSig2Signer,
clock: b.clock,
}
}