sweep+lnrpc: add new interface FeePreference

This commit adds a new interface `FeePreference` which makes it easier
to write unit tests and allows more customized implementation in
following commits.
This commit is contained in:
yyforyongyu
2023-11-03 20:05:21 +08:00
parent 530eed92a0
commit 3bcac318eb
6 changed files with 108 additions and 21 deletions

View File

@@ -33,6 +33,24 @@ var (
ErrFeePreferenceConflict = errors.New("fee preference conflict")
)
// FeePreference defines an interface that allows the caller to specify how the
// fee rate should be handled. Depending on the implementation, the fee rate
// can either be specified directly, or via a conf target which relies on the
// chain backend(`bitcoind`) to give a fee estimation, or a customized fee
// function which handles fee calculation based on the specified
// urgency(deadline).
type FeePreference interface {
// String returns a human-readable string of the fee preference.
String() string
// Estimate takes a fee estimator and a max allowed fee rate and
// returns a fee rate for the given fee preference. It ensures that the
// fee rate respects the bounds of the relay fee and the specified max
// fee rates.
Estimate(chainfee.Estimator,
chainfee.SatPerKWeight) (chainfee.SatPerKWeight, error)
}
// FeeEstimateInfo allows callers to express their time value for inclusion of
// a transaction into a block via either a confirmation target, or a fee rate.
type FeeEstimateInfo struct {
@@ -45,12 +63,16 @@ type FeeEstimateInfo struct {
FeeRate chainfee.SatPerKWeight
}
// Compile-time constraint to ensure FeeEstimateInfo implements FeePreference.
var _ FeePreference = (*FeeEstimateInfo)(nil)
// String returns a human-readable string of the fee preference.
func (p FeeEstimateInfo) String() string {
if p.ConfTarget != 0 {
return fmt.Sprintf("%v blocks", p.ConfTarget)
func (f FeeEstimateInfo) String() string {
if f.ConfTarget != 0 {
return fmt.Sprintf("%v blocks", f.ConfTarget)
}
return p.FeeRate.String()
return f.FeeRate.String()
}
// Estimate returns a fee rate for the given fee preference. It ensures that