From 4ba09098d1f9f18330875e537c0447552ab20f34 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 12 Oct 2023 18:03:34 +0800 Subject: [PATCH] sweep+lnd: move ticker creation into sweeper --- server.go | 14 ++++++-------- sweep/sweeper.go | 11 ++++++----- sweep/sweeper_test.go | 14 +++++--------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/server.go b/server.go index c47f79757..f3419a446 100644 --- a/server.go +++ b/server.go @@ -1059,14 +1059,12 @@ func newServer(cfg *Config, listenAddrs []net.Addr, } s.sweeper = sweep.New(&sweep.UtxoSweeperConfig{ - FeeEstimator: cc.FeeEstimator, - DetermineFeePerKw: sweep.DetermineFeePerKw, - GenSweepScript: newSweepPkScriptGen(cc.Wallet), - Signer: cc.Wallet.Cfg.Signer, - Wallet: newSweeperWallet(cc.Wallet), - NewBatchTimer: func() <-chan time.Time { - return time.NewTimer(cfg.Sweeper.BatchWindowDuration).C - }, + FeeEstimator: cc.FeeEstimator, + DetermineFeePerKw: sweep.DetermineFeePerKw, + GenSweepScript: newSweepPkScriptGen(cc.Wallet), + Signer: cc.Wallet.Cfg.Signer, + Wallet: newSweeperWallet(cc.Wallet), + TickerDuration: cfg.Sweeper.BatchWindowDuration, Notifier: cc.ChainNotifier, Store: sweeperStore, MaxInputsPerTx: sweep.DefaultMaxInputsPerTx, diff --git a/sweep/sweeper.go b/sweep/sweeper.go index 6a99c9ac5..406cf725f 100644 --- a/sweep/sweeper.go +++ b/sweep/sweeper.go @@ -264,10 +264,11 @@ type UtxoSweeperConfig struct { // Wallet contains the wallet functions that sweeper requires. Wallet Wallet - // NewBatchTimer creates a channel that will be sent on when a certain - // time window has passed. During this time window, new inputs can still - // be added to the sweep tx that is about to be generated. - NewBatchTimer func() <-chan time.Time + // TickerDuration is used to create a channel that will be sent on when + // a certain time window has passed. During this time window, new + // inputs can still be added to the sweep tx that is about to be + // generated. + TickerDuration time.Duration // Notifier is an instance of a chain notifier we'll use to watch for // certain on-chain events. @@ -1019,7 +1020,7 @@ func (s *UtxoSweeper) scheduleSweep(currentHeight int32) error { // Start sweep timer to create opportunity for more inputs to be added // before a tx is constructed. - s.timer = s.cfg.NewBatchTimer() + s.timer = time.NewTicker(s.cfg.TickerDuration).C log.Debugf("Sweep timer started") diff --git a/sweep/sweeper_test.go b/sweep/sweeper_test.go index df935aeff..81171f230 100644 --- a/sweep/sweeper_test.go +++ b/sweep/sweeper_test.go @@ -127,15 +127,11 @@ func createSweeperTestContext(t *testing.T) *sweeperTestContext { } ctx.sweeper = New(&UtxoSweeperConfig{ - Notifier: notifier, - Wallet: backend, - NewBatchTimer: func() <-chan time.Time { - c := make(chan time.Time, 1) - ctx.timeoutChan <- c - return c - }, - Store: store, - Signer: &mock.DummySigner{}, + Notifier: notifier, + Wallet: backend, + TickerDuration: 100 * time.Millisecond, + Store: store, + Signer: &mock.DummySigner{}, GenSweepScript: func() ([]byte, error) { script := make([]byte, input.P2WPKHSize) script[0] = 0