From ad2859c8638d81a3a8c2df109e29b5363ac7f129 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 23 Jun 2021 15:14:48 +0200 Subject: [PATCH] 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. --- lnwallet/chainfee/estimator.go | 56 ++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/lnwallet/chainfee/estimator.go b/lnwallet/chainfee/estimator.go index 3a8a2bfea..7439c528a 100644 --- a/lnwallet/chainfee/estimator.go +++ b/lnwallet/chainfee/estimator.go @@ -125,11 +125,11 @@ type BtcdEstimator struct { // produce fee estimates. fallbackFeePerKW SatPerKWeight - // minFeePerKW is the minimum fee, in sat/kw, that we should enforce. - // This will be used as the default fee rate for a transaction when the - // estimated fee rate is too low to allow the transaction to propagate - // through the network. - minFeePerKW SatPerKWeight + // minFeeManager is used to query the current minimum fee, in sat/kw, + // that we should enforce. This will be used to determine fee rate for + // a transaction when the estimated fee rate is too low to allow the + // transaction to propagate through the network. + minFeeManager *minFeeManager btcdConn *rpcclient.Client } @@ -164,34 +164,36 @@ func (b *BtcdEstimator) Start() error { return err } - // Once the connection to the backend node has been established, we'll - // query it for its minimum relay fee. - info, err := b.btcdConn.GetInfo() + // Once the connection to the backend node has been established, we + // can initialise the minimum relay fee manager which queries the + // chain backend for the minimum relay fee on construction. + minRelayFeeManager, err := newMinFeeManager( + defaultUpdateInterval, b.fetchMinRelayFee, + ) if err != nil { 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) if err != nil { - return err + return 0, err } // The fee rate is expressed in sat/kb, so we'll manually convert it to // our desired sat/kw rate. - minRelayFeePerKw := SatPerKVByte(relayFee).FeePerKWeight() - - // 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 + return SatPerKVByte(relayFee).FeePerKWeight(), nil } // 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. func (b *BtcdEstimator) RelayFeePerKW() SatPerKWeight { - return b.minFeePerKW + return b.minFeeManager.fetchMinFee() } // 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() // 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, "+ "using fee floor of %v sat/kw instead", satPerKw, - b.minFeePerKW) - satPerKw = b.minFeePerKW + b.minFeeManager) + satPerKw = b.minFeeManager.fetchMinFee() } log.Debugf("Returning %v sat/kw for conf target of %v",