mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-07-28 13:52:55 +02:00
sweep: replace feeRateForPreference
with Estimate
This commit refactors the sweeper so the method `feeRateForPreference` is now moved to `FeePreference`, which makes our following refactor easier to handle.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package sweep
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime/pprof"
|
||||
@@ -151,7 +150,6 @@ func createSweeperTestContext(t *testing.T) *sweeperTestContext {
|
||||
},
|
||||
MaxFeeRate: DefaultMaxFeeRate,
|
||||
FeeRateBucketSize: DefaultFeeRateBucketSize,
|
||||
DetermineFeePerKw: DetermineFeePerKw,
|
||||
})
|
||||
|
||||
ctx.sweeper.Start()
|
||||
@@ -2137,124 +2135,6 @@ func TestSweeperShutdownHandling(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
// TestFeeRateForPreference checks `feeRateForPreference` works as expected.
|
||||
func TestFeeRateForPreference(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dummyErr := errors.New("dummy")
|
||||
|
||||
// Create a test sweeper.
|
||||
s := New(&UtxoSweeperConfig{})
|
||||
|
||||
// errFeeFunc is a mock over DetermineFeePerKw that always return the
|
||||
// above dummy error.
|
||||
errFeeFunc := func(_ chainfee.Estimator, _ FeePreference) (
|
||||
chainfee.SatPerKWeight, error) {
|
||||
|
||||
return 0, dummyErr
|
||||
}
|
||||
|
||||
// Set the relay fee rate to be 1 sat/kw.
|
||||
s.relayFeeRate = 1
|
||||
|
||||
// smallFeeFunc is a mock over DetermineFeePerKw that always return a
|
||||
// fee rate that's below the relayFeeRate.
|
||||
smallFeeFunc := func(_ chainfee.Estimator, _ FeePreference) (
|
||||
chainfee.SatPerKWeight, error) {
|
||||
|
||||
return s.relayFeeRate - 1, nil
|
||||
}
|
||||
|
||||
// Set the max fee rate to be 1000 sat/vb.
|
||||
s.cfg.MaxFeeRate = 1000
|
||||
|
||||
// largeFeeFunc is a mock over DetermineFeePerKw that always return a
|
||||
// fee rate that's larger than the MaxFeeRate.
|
||||
largeFeeFunc := func(_ chainfee.Estimator, _ FeePreference) (
|
||||
chainfee.SatPerKWeight, error) {
|
||||
|
||||
return s.cfg.MaxFeeRate.FeePerKWeight() + 1, nil
|
||||
}
|
||||
|
||||
// validFeeRate is used to test the success case.
|
||||
validFeeRate := (s.cfg.MaxFeeRate.FeePerKWeight() + s.relayFeeRate) / 2
|
||||
|
||||
// normalFeeFunc is a mock over DetermineFeePerKw that always return a
|
||||
// fee rate that's within the range.
|
||||
normalFeeFunc := func(_ chainfee.Estimator, _ FeePreference) (
|
||||
chainfee.SatPerKWeight, error) {
|
||||
|
||||
return validFeeRate, nil
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
feePref FeePreference
|
||||
determineFeePerKw feeDeterminer
|
||||
expectedFeeRate chainfee.SatPerKWeight
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
// When the fee preference is empty, we should see an
|
||||
// error.
|
||||
name: "empty fee preference",
|
||||
feePref: FeePreference{},
|
||||
expectedErr: ErrNoFeePreference,
|
||||
},
|
||||
{
|
||||
// When an error is returned from the fee determiner,
|
||||
// we should return it.
|
||||
name: "error from DetermineFeePerKw",
|
||||
feePref: FeePreference{FeeRate: 1},
|
||||
determineFeePerKw: errFeeFunc,
|
||||
expectedErr: dummyErr,
|
||||
},
|
||||
{
|
||||
// When DetermineFeePerKw gives a too small value, we
|
||||
// should return an error.
|
||||
name: "fee rate below relay fee rate",
|
||||
feePref: FeePreference{FeeRate: 1},
|
||||
determineFeePerKw: smallFeeFunc,
|
||||
expectedErr: ErrFeePreferenceTooLow,
|
||||
},
|
||||
{
|
||||
// When DetermineFeePerKw gives a too large value, we
|
||||
// should cap it at the max fee rate.
|
||||
name: "fee rate above max fee rate",
|
||||
feePref: FeePreference{FeeRate: 1},
|
||||
determineFeePerKw: largeFeeFunc,
|
||||
expectedFeeRate: s.cfg.MaxFeeRate.FeePerKWeight(),
|
||||
},
|
||||
{
|
||||
// When DetermineFeePerKw gives a sane fee rate, we
|
||||
// should return it without any error.
|
||||
name: "success",
|
||||
feePref: FeePreference{FeeRate: 1},
|
||||
determineFeePerKw: normalFeeFunc,
|
||||
expectedFeeRate: validFeeRate,
|
||||
},
|
||||
}
|
||||
|
||||
//nolint:paralleltest
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Attach the mocked method.
|
||||
s.cfg.DetermineFeePerKw = tc.determineFeePerKw
|
||||
|
||||
// Call the function under test.
|
||||
feerate, err := s.feeRateForPreference(tc.feePref)
|
||||
|
||||
// Assert the expected feerate.
|
||||
require.Equal(t, tc.expectedFeeRate, feerate)
|
||||
|
||||
// Assert the expected error.
|
||||
require.ErrorIs(t, err, tc.expectedErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestClusterByLockTime tests the method clusterByLockTime works as expected.
|
||||
func TestClusterByLockTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -2344,13 +2224,7 @@ func TestClusterByLockTime(t *testing.T) {
|
||||
// DetermineFeePerKw that always return the testing fee rate. This
|
||||
// mocked method is then attached to the sweeper.
|
||||
applyFeeRate := func(feeRate chainfee.SatPerKWeight) {
|
||||
mockFeeFunc := func(_ chainfee.Estimator, _ FeePreference) (
|
||||
chainfee.SatPerKWeight, error) {
|
||||
|
||||
return feeRate, nil
|
||||
}
|
||||
|
||||
s.cfg.DetermineFeePerKw = mockFeeFunc
|
||||
// TODO(yy): fix the test here.
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
|
Reference in New Issue
Block a user