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{
FeeEstimator: cc.FeeEstimator,
GenSweepScript: newSweepPkScriptGen(cc.Wallet),
Signer: cc.Wallet.Cfg.Signer,
Wallet: newSweeperWallet(cc.Wallet),
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
},

View File

@ -237,12 +237,21 @@ type UtxoSweeper struct {
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.
type UtxoSweeperConfig struct {
// GenSweepScript generates a P2WKH script belonging to the wallet where
// funds can be swept.
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
// the necessary fee relative to the expected size of the sweep
// transaction.
@ -470,7 +479,9 @@ func (s *UtxoSweeper) feeRateForPreference(
return 0, ErrNoFeePreference
}
feeRate, err := DetermineFeePerKw(s.cfg.FeeEstimator, feePreference)
feeRate, err := s.cfg.DetermineFeePerKw(
s.cfg.FeeEstimator, feePreference,
)
if err != nil {
return 0, err
}
@ -1599,7 +1610,7 @@ func (s *UtxoSweeper) handleUpdateReq(req *updateReq, bestHeight int32) (
func (s *UtxoSweeper) CreateSweepTx(inputs []input.Input, feePref FeePreference,
currentBlockHeight uint32) (*wire.MsgTx, error) {
feePerKw, err := DetermineFeePerKw(s.cfg.FeeEstimator, feePref)
feePerKw, err := s.cfg.DetermineFeePerKw(s.cfg.FeeEstimator, feePref)
if err != nil {
return nil, err
}

View File

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