chainfee: fetch fresh relay fee for btcd

This commit adds a function to the BtcdEstimator that fetches the
current min relay fee from the btcd node.
This commit is contained in:
Elle Mouton
2021-06-23 15:14:48 +02:00
parent 55077d9404
commit ad2859c863

View File

@@ -125,11 +125,11 @@ type BtcdEstimator struct {
// produce fee estimates. // produce fee estimates.
fallbackFeePerKW SatPerKWeight fallbackFeePerKW SatPerKWeight
// minFeePerKW is the minimum fee, in sat/kw, that we should enforce. // minFeeManager is used to query the current minimum fee, in sat/kw,
// This will be used as the default fee rate for a transaction when the // that we should enforce. This will be used to determine fee rate for
// estimated fee rate is too low to allow the transaction to propagate // a transaction when the estimated fee rate is too low to allow the
// through the network. // transaction to propagate through the network.
minFeePerKW SatPerKWeight minFeeManager *minFeeManager
btcdConn *rpcclient.Client btcdConn *rpcclient.Client
} }
@@ -164,34 +164,36 @@ func (b *BtcdEstimator) Start() error {
return err return err
} }
// Once the connection to the backend node has been established, we'll // Once the connection to the backend node has been established, we
// query it for its minimum relay fee. // can initialise the minimum relay fee manager which queries the
info, err := b.btcdConn.GetInfo() // chain backend for the minimum relay fee on construction.
minRelayFeeManager, err := newMinFeeManager(
defaultUpdateInterval, b.fetchMinRelayFee,
)
if err != nil { if err != nil {
return err return err
} }
b.minFeeManager = minRelayFeeManager
return nil
}
// fetchMinRelayFee fetches and returns the minimum relay fee in sat/kb from
// the btcd backend.
func (b *BtcdEstimator) fetchMinRelayFee() (SatPerKWeight, error) {
info, err := b.btcdConn.GetInfo()
if err != nil {
return 0, err
}
relayFee, err := btcutil.NewAmount(info.RelayFee) relayFee, err := btcutil.NewAmount(info.RelayFee)
if err != nil { if err != nil {
return err return 0, err
} }
// The fee rate is expressed in sat/kb, so we'll manually convert it to // The fee rate is expressed in sat/kb, so we'll manually convert it to
// our desired sat/kw rate. // our desired sat/kw rate.
minRelayFeePerKw := SatPerKVByte(relayFee).FeePerKWeight() return SatPerKVByte(relayFee).FeePerKWeight(), nil
// By default, we'll use the backend node's minimum relay fee as the
// minimum fee rate we'll propose for transacations. However, if this
// happens to be lower than our fee floor, we'll enforce that instead.
b.minFeePerKW = minRelayFeePerKw
if b.minFeePerKW < FeePerKwFloor {
b.minFeePerKW = FeePerKwFloor
}
log.Debugf("Using minimum fee rate of %v sat/kw",
int64(b.minFeePerKW))
return nil
} }
// Stop stops any spawned goroutines and cleans up the resources used // Stop stops any spawned goroutines and cleans up the resources used
@@ -230,7 +232,7 @@ func (b *BtcdEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error
// //
// NOTE: This method is part of the Estimator interface. // NOTE: This method is part of the Estimator interface.
func (b *BtcdEstimator) RelayFeePerKW() SatPerKWeight { func (b *BtcdEstimator) RelayFeePerKW() SatPerKWeight {
return b.minFeePerKW return b.minFeeManager.fetchMinFee()
} }
// fetchEstimate returns a fee estimate for a transaction to be confirmed in // fetchEstimate returns a fee estimate for a transaction to be confirmed in
@@ -254,11 +256,11 @@ func (b *BtcdEstimator) fetchEstimate(confTarget uint32) (SatPerKWeight, error)
satPerKw := SatPerKVByte(satPerKB).FeePerKWeight() satPerKw := SatPerKVByte(satPerKB).FeePerKWeight()
// Finally, we'll enforce our fee floor. // Finally, we'll enforce our fee floor.
if satPerKw < b.minFeePerKW { if satPerKw < b.minFeeManager.fetchMinFee() {
log.Debugf("Estimated fee rate of %v sat/kw is too low, "+ log.Debugf("Estimated fee rate of %v sat/kw is too low, "+
"using fee floor of %v sat/kw instead", satPerKw, "using fee floor of %v sat/kw instead", satPerKw,
b.minFeePerKW) b.minFeeManager)
satPerKw = b.minFeePerKW satPerKw = b.minFeeManager.fetchMinFee()
} }
log.Debugf("Returning %v sat/kw for conf target of %v", log.Debugf("Returning %v sat/kw for conf target of %v",