lnd+sweep: move DetermineFeePerKw into config

This commit makes `DetermineFeePerKw` configurable on sweeper so it's
easier to write unit tests for it.
This commit is contained in:
yyforyongyu 2023-07-12 16:22:10 +08:00
parent 939375f1a5
commit 82053970ef
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
3 changed files with 19 additions and 6 deletions

View File

@ -1059,10 +1059,11 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
} }
s.sweeper = sweep.New(&sweep.UtxoSweeperConfig{ s.sweeper = sweep.New(&sweep.UtxoSweeperConfig{
FeeEstimator: cc.FeeEstimator, FeeEstimator: cc.FeeEstimator,
GenSweepScript: newSweepPkScriptGen(cc.Wallet), DetermineFeePerKw: sweep.DetermineFeePerKw,
Signer: cc.Wallet.Cfg.Signer, GenSweepScript: newSweepPkScriptGen(cc.Wallet),
Wallet: newSweeperWallet(cc.Wallet), Signer: cc.Wallet.Cfg.Signer,
Wallet: newSweeperWallet(cc.Wallet),
NewBatchTimer: func() <-chan time.Time { NewBatchTimer: func() <-chan time.Time {
return time.NewTimer(cfg.Sweeper.BatchWindowDuration).C return time.NewTimer(cfg.Sweeper.BatchWindowDuration).C
}, },

View File

@ -237,12 +237,21 @@ type UtxoSweeper struct {
wg sync.WaitGroup wg sync.WaitGroup
} }
// feeDeterminer defines an alias to the function signature of
// `DetermineFeePerKw`.
type feeDeterminer func(chainfee.Estimator,
FeePreference) (chainfee.SatPerKWeight, error)
// UtxoSweeperConfig contains dependencies of UtxoSweeper. // UtxoSweeperConfig contains dependencies of UtxoSweeper.
type UtxoSweeperConfig struct { type UtxoSweeperConfig struct {
// GenSweepScript generates a P2WKH script belonging to the wallet where // GenSweepScript generates a P2WKH script belonging to the wallet where
// funds can be swept. // funds can be swept.
GenSweepScript func() ([]byte, error) GenSweepScript func() ([]byte, error)
// DetermineFeePerKw determines the fee in sat/kw based on the given
// estimator and fee preference.
DetermineFeePerKw feeDeterminer
// FeeEstimator is used when crafting sweep transactions to estimate // FeeEstimator is used when crafting sweep transactions to estimate
// the necessary fee relative to the expected size of the sweep // the necessary fee relative to the expected size of the sweep
// transaction. // transaction.
@ -470,7 +479,9 @@ func (s *UtxoSweeper) feeRateForPreference(
return 0, ErrNoFeePreference return 0, ErrNoFeePreference
} }
feeRate, err := DetermineFeePerKw(s.cfg.FeeEstimator, feePreference) feeRate, err := s.cfg.DetermineFeePerKw(
s.cfg.FeeEstimator, feePreference,
)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -1599,7 +1610,7 @@ func (s *UtxoSweeper) handleUpdateReq(req *updateReq, bestHeight int32) (
func (s *UtxoSweeper) CreateSweepTx(inputs []input.Input, feePref FeePreference, func (s *UtxoSweeper) CreateSweepTx(inputs []input.Input, feePref FeePreference,
currentBlockHeight uint32) (*wire.MsgTx, error) { currentBlockHeight uint32) (*wire.MsgTx, error) {
feePerKw, err := DetermineFeePerKw(s.cfg.FeeEstimator, feePref) feePerKw, err := s.cfg.DetermineFeePerKw(s.cfg.FeeEstimator, feePref)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -149,6 +149,7 @@ func createSweeperTestContext(t *testing.T) *sweeperTestContext {
}, },
MaxFeeRate: DefaultMaxFeeRate, MaxFeeRate: DefaultMaxFeeRate,
FeeRateBucketSize: DefaultFeeRateBucketSize, FeeRateBucketSize: DefaultFeeRateBucketSize,
DetermineFeePerKw: DetermineFeePerKw,
}) })
ctx.sweeper.Start() ctx.sweeper.Start()