2019-10-31 05:43:05 +03:00
|
|
|
package chainfee
|
2017-11-19 01:35:00 +03:00
|
|
|
|
2017-11-23 09:11:02 +03:00
|
|
|
import (
|
2017-11-10 03:30:20 +03:00
|
|
|
"encoding/json"
|
2019-04-10 20:12:12 +03:00
|
|
|
"fmt"
|
2019-04-09 05:31:36 +03:00
|
|
|
"io"
|
2019-04-10 20:12:12 +03:00
|
|
|
prand "math/rand"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2017-11-23 09:11:02 +03:00
|
|
|
)
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
const (
|
2019-04-10 20:12:12 +03:00
|
|
|
// maxBlockTarget is the highest number of blocks confirmations that
|
2019-10-31 05:43:05 +03:00
|
|
|
// a WebAPIEstimator will cache fees for. This number is chosen
|
2019-04-10 20:12:12 +03:00
|
|
|
// because it's the highest number of confs bitcoind will return a fee
|
|
|
|
// estimate for.
|
2021-05-06 14:11:43 +03:00
|
|
|
maxBlockTarget uint32 = 1008
|
2019-04-10 20:12:12 +03:00
|
|
|
|
|
|
|
// minBlockTarget is the lowest number of blocks confirmations that
|
2019-10-31 05:43:05 +03:00
|
|
|
// a WebAPIEstimator will cache fees for. Requesting an estimate for
|
2019-04-10 20:12:12 +03:00
|
|
|
// less than this will result in an error.
|
2021-06-24 14:57:06 +03:00
|
|
|
minBlockTarget uint32 = 1
|
2019-04-10 20:12:12 +03:00
|
|
|
|
|
|
|
// minFeeUpdateTimeout represents the minimum interval in which a
|
2019-10-31 05:43:05 +03:00
|
|
|
// WebAPIEstimator will request fresh fees from its API.
|
2019-04-10 20:12:12 +03:00
|
|
|
minFeeUpdateTimeout = 5 * time.Minute
|
|
|
|
|
|
|
|
// maxFeeUpdateTimeout represents the maximum interval in which a
|
2019-10-31 05:43:05 +03:00
|
|
|
// WebAPIEstimator will request fresh fees from its API.
|
2019-04-10 20:12:12 +03:00
|
|
|
maxFeeUpdateTimeout = 20 * time.Minute
|
2018-07-28 04:37:05 +03:00
|
|
|
)
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// Estimator provides the ability to estimate on-chain transaction fees for
|
2017-11-19 01:35:00 +03:00
|
|
|
// various combinations of transaction sizes and desired confirmation time
|
|
|
|
// (measured by number of blocks).
|
2019-10-31 05:43:05 +03:00
|
|
|
type Estimator interface {
|
2018-07-28 04:37:05 +03:00
|
|
|
// EstimateFeePerKW takes in a target for the number of blocks until an
|
|
|
|
// initial confirmation and returns the estimated fee expressed in
|
|
|
|
// sat/kw.
|
|
|
|
EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error)
|
2017-11-23 09:11:21 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// Start signals the Estimator to start any processes or goroutines
|
2017-11-23 09:11:21 +03:00
|
|
|
// it needs to perform its duty.
|
|
|
|
Start() error
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
Stop() error
|
2018-10-29 11:31:44 +03:00
|
|
|
|
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions
|
|
|
|
// to be relayed. This is also the basis for calculation of the dust
|
|
|
|
// limit.
|
|
|
|
RelayFeePerKW() SatPerKWeight
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// StaticEstimator will return a static value for all fee calculation requests.
|
|
|
|
// It is designed to be replaced by a proper fee calculation implementation.
|
|
|
|
// The fees are not accessible directly, because changing them would not be
|
|
|
|
// thread safe.
|
|
|
|
type StaticEstimator struct {
|
2018-12-18 11:02:27 +03:00
|
|
|
// feePerKW is the static fee rate in satoshis-per-vbyte that will be
|
2018-02-13 16:18:46 +03:00
|
|
|
// returned by this fee estimator.
|
2018-12-18 11:02:27 +03:00
|
|
|
feePerKW SatPerKWeight
|
2018-10-29 11:31:44 +03:00
|
|
|
|
2018-12-18 11:02:27 +03:00
|
|
|
// relayFee is the minimum fee rate required for transactions to be
|
2018-10-29 11:31:44 +03:00
|
|
|
// relayed.
|
2018-12-18 11:02:27 +03:00
|
|
|
relayFee SatPerKWeight
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// NewStaticEstimator returns a new static fee estimator instance.
|
|
|
|
func NewStaticEstimator(feePerKW, relayFee SatPerKWeight) *StaticEstimator {
|
2018-12-18 11:02:27 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
return &StaticEstimator{
|
2018-12-18 11:02:27 +03:00
|
|
|
feePerKW: feePerKW,
|
|
|
|
relayFee: relayFee,
|
|
|
|
}
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// EstimateFeePerKW will return a static value for fee calculations.
|
2017-11-23 09:11:42 +03:00
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (e StaticEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error) {
|
2018-12-18 11:02:27 +03:00
|
|
|
return e.feePerKW, nil
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 11:31:44 +03:00
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions to be
|
|
|
|
// relayed.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (e StaticEstimator) RelayFeePerKW() SatPerKWeight {
|
2018-12-18 11:02:27 +03:00
|
|
|
return e.relayFee
|
2018-10-29 11:31:44 +03:00
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// Start signals the Estimator to start any processes or goroutines
|
2017-11-23 09:11:42 +03:00
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (e StaticEstimator) Start() error {
|
2017-11-23 09:11:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (e StaticEstimator) Stop() error {
|
2017-11-23 09:11:42 +03:00
|
|
|
return nil
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that StaticFeeEstimator implements the
|
2019-10-31 05:43:05 +03:00
|
|
|
// Estimator interface.
|
|
|
|
var _ Estimator = (*StaticEstimator)(nil)
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// BtcdEstimator is an implementation of the Estimator interface backed
|
2017-11-23 09:26:07 +03:00
|
|
|
// by the RPC interface of an active btcd node. This implementation will proxy
|
2017-12-18 05:40:05 +03:00
|
|
|
// any fee estimation requests to btcd's RPC interface.
|
2019-10-31 05:43:05 +03:00
|
|
|
type BtcdEstimator struct {
|
2018-07-28 04:37:05 +03:00
|
|
|
// fallbackFeePerKW is the fall back fee rate in sat/kw that is returned
|
|
|
|
// if the fee estimator does not yet have enough data to actually
|
|
|
|
// produce fee estimates.
|
|
|
|
fallbackFeePerKW SatPerKWeight
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// 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
|
2018-05-14 20:45:39 +03:00
|
|
|
|
2017-11-23 09:26:07 +03:00
|
|
|
btcdConn *rpcclient.Client
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// NewBtcdEstimator creates a new BtcdEstimator given a fully populated
|
2017-11-23 09:26:07 +03:00
|
|
|
// rpc config that is able to successfully connect and authenticate with the
|
|
|
|
// btcd node, and also a fall back fee rate. The fallback fee rate is used in
|
|
|
|
// the occasion that the estimator has insufficient data, or returns zero for a
|
|
|
|
// fee estimate.
|
2019-10-31 05:43:05 +03:00
|
|
|
func NewBtcdEstimator(rpcConfig rpcclient.ConnConfig,
|
|
|
|
fallBackFeeRate SatPerKWeight) (*BtcdEstimator, error) {
|
2017-11-23 09:26:07 +03:00
|
|
|
|
|
|
|
rpcConfig.DisableConnectOnNew = true
|
|
|
|
rpcConfig.DisableAutoReconnect = false
|
|
|
|
chainConn, err := rpcclient.New(&rpcConfig, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
return &BtcdEstimator{
|
2018-07-28 04:37:05 +03:00
|
|
|
fallbackFeePerKW: fallBackFeeRate,
|
|
|
|
btcdConn: chainConn,
|
2017-11-23 09:26:07 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// Start signals the Estimator to start any processes or goroutines
|
2017-11-23 09:26:07 +03:00
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (b *BtcdEstimator) Start() error {
|
2017-11-23 09:26:07 +03:00
|
|
|
if err := b.btcdConn.Connect(20); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:45:39 +03:00
|
|
|
// Once the connection to the backend node has been established, we'll
|
|
|
|
// query it for its minimum relay fee.
|
|
|
|
info, err := b.btcdConn.GetInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
relayFee, err := btcutil.NewAmount(info.RelayFee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Debugf("Using minimum fee rate of %v sat/kw",
|
2018-07-28 04:37:05 +03:00
|
|
|
int64(b.minFeePerKW))
|
2018-05-14 20:45:39 +03:00
|
|
|
|
2017-11-23 09:26:07 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (b *BtcdEstimator) Stop() error {
|
2017-11-23 09:26:07 +03:00
|
|
|
b.btcdConn.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// EstimateFeePerKW takes in a target for the number of blocks until an initial
|
|
|
|
// confirmation and returns the estimated fee expressed in sat/kw.
|
2018-02-13 16:18:46 +03:00
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (b *BtcdEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error) {
|
2018-07-28 04:37:05 +03:00
|
|
|
feeEstimate, err := b.fetchEstimate(numBlocks)
|
2017-11-23 09:26:07 +03:00
|
|
|
switch {
|
|
|
|
// If the estimator doesn't have enough data, or returns an error, then
|
|
|
|
// to return a proper value, then we'll return the default fall back
|
|
|
|
// fee rate.
|
|
|
|
case err != nil:
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Errorf("unable to query estimator: %v", err)
|
2017-11-23 09:26:07 +03:00
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case feeEstimate == 0:
|
2018-07-28 04:37:05 +03:00
|
|
|
return b.fallbackFeePerKW, nil
|
2017-11-23 09:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return feeEstimate, nil
|
|
|
|
}
|
|
|
|
|
2018-10-29 11:31:44 +03:00
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions to be
|
|
|
|
// relayed.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (b *BtcdEstimator) RelayFeePerKW() SatPerKWeight {
|
2018-10-29 11:31:44 +03:00
|
|
|
return b.minFeePerKW
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:03:27 +03:00
|
|
|
// fetchEstimate returns a fee estimate for a transaction to be confirmed in
|
2018-07-28 04:37:05 +03:00
|
|
|
// confTarget blocks. The estimate is returned in sat/kw.
|
2019-10-31 05:43:05 +03:00
|
|
|
func (b *BtcdEstimator) fetchEstimate(confTarget uint32) (SatPerKWeight, error) {
|
2017-11-23 09:26:07 +03:00
|
|
|
// First, we'll fetch the estimate for our confirmation target.
|
|
|
|
btcPerKB, err := b.btcdConn.EstimateFee(int64(confTarget))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll convert the returned value to satoshis, as it's
|
|
|
|
// currently returned in BTC.
|
2017-11-26 22:35:25 +03:00
|
|
|
satPerKB, err := btcutil.NewAmount(btcPerKB)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// Since we use fee rates in sat/kw internally, we'll convert the
|
|
|
|
// estimated fee rate from its sat/kb representation to sat/kw.
|
|
|
|
satPerKw := SatPerKVByte(satPerKB).FeePerKWeight()
|
|
|
|
|
|
|
|
// Finally, we'll enforce our fee floor.
|
|
|
|
if satPerKw < b.minFeePerKW {
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Debugf("Estimated fee rate of %v sat/kw is too low, "+
|
2018-09-21 05:20:01 +03:00
|
|
|
"using fee floor of %v sat/kw instead", satPerKw,
|
|
|
|
b.minFeePerKW)
|
2018-07-28 04:37:05 +03:00
|
|
|
satPerKw = b.minFeePerKW
|
2018-05-14 20:45:39 +03:00
|
|
|
}
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Debugf("Returning %v sat/kw for conf target of %v",
|
2018-07-28 04:37:05 +03:00
|
|
|
int64(satPerKw), confTarget)
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
return satPerKw, nil
|
2017-11-23 09:26:07 +03:00
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// A compile-time assertion to ensure that BtcdEstimator implements the
|
|
|
|
// Estimator interface.
|
|
|
|
var _ Estimator = (*BtcdEstimator)(nil)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// BitcoindEstimator is an implementation of the Estimator interface backed by
|
|
|
|
// the RPC interface of an active bitcoind node. This implementation will proxy
|
|
|
|
// any fee estimation requests to bitcoind's RPC interface.
|
|
|
|
type BitcoindEstimator struct {
|
2018-07-28 04:37:05 +03:00
|
|
|
// fallbackFeePerKW is the fallback fee rate in sat/kw that is returned
|
|
|
|
// if the fee estimator does not yet have enough data to actually
|
|
|
|
// produce fee estimates.
|
|
|
|
fallbackFeePerKW SatPerKWeight
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// 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
|
2018-05-14 20:45:39 +03:00
|
|
|
|
2020-03-13 11:41:08 +03:00
|
|
|
// feeMode is the estimate_mode to use when calling "estimatesmartfee".
|
|
|
|
// It can be either "ECONOMICAL" or "CONSERVATIVE", and it's default
|
|
|
|
// to "CONSERVATIVE".
|
|
|
|
feeMode string
|
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
bitcoindConn *rpcclient.Client
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// NewBitcoindEstimator creates a new BitcoindEstimator given a fully populated
|
|
|
|
// rpc config that is able to successfully connect and authenticate with the
|
|
|
|
// bitcoind node, and also a fall back fee rate. The fallback fee rate is used
|
|
|
|
// in the occasion that the estimator has insufficient data, or returns zero
|
|
|
|
// for a fee estimate.
|
2020-03-13 11:41:08 +03:00
|
|
|
func NewBitcoindEstimator(rpcConfig rpcclient.ConnConfig, feeMode string,
|
2019-10-31 05:43:05 +03:00
|
|
|
fallBackFeeRate SatPerKWeight) (*BitcoindEstimator, error) {
|
2017-11-10 03:30:20 +03:00
|
|
|
|
|
|
|
rpcConfig.DisableConnectOnNew = true
|
|
|
|
rpcConfig.DisableAutoReconnect = false
|
|
|
|
rpcConfig.DisableTLS = true
|
|
|
|
rpcConfig.HTTPPostMode = true
|
|
|
|
chainConn, err := rpcclient.New(&rpcConfig, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
return &BitcoindEstimator{
|
2018-07-28 04:37:05 +03:00
|
|
|
fallbackFeePerKW: fallBackFeeRate,
|
|
|
|
bitcoindConn: chainConn,
|
2020-03-13 11:41:08 +03:00
|
|
|
feeMode: feeMode,
|
2017-11-10 03:30:20 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// Start signals the Estimator to start any processes or goroutines
|
2017-11-10 03:30:20 +03:00
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (b *BitcoindEstimator) Start() error {
|
2018-05-14 20:45:39 +03:00
|
|
|
// Once the connection to the backend node has been established, we'll
|
|
|
|
// query it for its minimum relay fee. Since the `getinfo` RPC has been
|
|
|
|
// deprecated for `bitcoind`, we'll need to send a `getnetworkinfo`
|
|
|
|
// command as a raw request.
|
|
|
|
resp, err := b.bitcoindConn.RawRequest("getnetworkinfo", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response to retrieve the relay fee in sat/KB.
|
|
|
|
info := struct {
|
|
|
|
RelayFee float64 `json:"relayfee"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(resp, &info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
relayFee, err := btcutil.NewAmount(info.RelayFee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Debugf("Using minimum fee rate of %v sat/kw",
|
2018-07-28 04:37:05 +03:00
|
|
|
int64(b.minFeePerKW))
|
2018-05-14 20:45:39 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (b *BitcoindEstimator) Stop() error {
|
2017-11-10 03:30:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// EstimateFeePerKW takes in a target for the number of blocks until an initial
|
|
|
|
// confirmation and returns the estimated fee expressed in sat/kw.
|
2018-02-13 16:18:46 +03:00
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
2021-05-06 14:12:17 +03:00
|
|
|
func (b *BitcoindEstimator) EstimateFeePerKW(
|
|
|
|
numBlocks uint32) (SatPerKWeight, error) {
|
|
|
|
|
|
|
|
if numBlocks > maxBlockTarget {
|
|
|
|
log.Debugf("conf target %d exceeds the max value, "+
|
|
|
|
"use %d instead.", numBlocks, maxBlockTarget,
|
|
|
|
)
|
|
|
|
numBlocks = maxBlockTarget
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
feeEstimate, err := b.fetchEstimate(numBlocks)
|
2017-11-10 03:30:20 +03:00
|
|
|
switch {
|
|
|
|
// If the estimator doesn't have enough data, or returns an error, then
|
|
|
|
// to return a proper value, then we'll return the default fall back
|
|
|
|
// fee rate.
|
|
|
|
case err != nil:
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Errorf("unable to query estimator: %v", err)
|
2017-11-10 03:30:20 +03:00
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case feeEstimate == 0:
|
2018-07-28 04:37:05 +03:00
|
|
|
return b.fallbackFeePerKW, nil
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return feeEstimate, nil
|
|
|
|
}
|
|
|
|
|
2018-10-29 11:31:44 +03:00
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions to be
|
|
|
|
// relayed.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (b *BitcoindEstimator) RelayFeePerKW() SatPerKWeight {
|
2018-10-29 11:31:44 +03:00
|
|
|
return b.minFeePerKW
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// fetchEstimate returns a fee estimate for a transaction to be confirmed in
|
|
|
|
// confTarget blocks. The estimate is returned in sat/kw.
|
2019-10-31 05:43:05 +03:00
|
|
|
func (b *BitcoindEstimator) fetchEstimate(confTarget uint32) (SatPerKWeight, error) {
|
2017-11-10 03:30:20 +03:00
|
|
|
// First, we'll send an "estimatesmartfee" command as a raw request,
|
|
|
|
// since it isn't supported by btcd but is available in bitcoind.
|
|
|
|
target, err := json.Marshal(uint64(confTarget))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-03-13 11:41:08 +03:00
|
|
|
|
|
|
|
// The mode must be either ECONOMICAL or CONSERVATIVE.
|
|
|
|
mode, err := json.Marshal(b.feeMode)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
resp, err := b.bitcoindConn.RawRequest(
|
2020-03-13 11:41:08 +03:00
|
|
|
"estimatesmartfee", []json.RawMessage{target, mode},
|
2018-07-28 04:37:05 +03:00
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll parse the response to get the BTC per KB.
|
|
|
|
feeEstimate := struct {
|
2018-07-28 04:37:05 +03:00
|
|
|
FeeRate float64 `json:"feerate"`
|
2017-11-10 03:30:20 +03:00
|
|
|
}{}
|
|
|
|
err = json.Unmarshal(resp, &feeEstimate)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// Next, we'll convert the returned value to satoshis, as it's currently
|
|
|
|
// returned in BTC.
|
|
|
|
satPerKB, err := btcutil.NewAmount(feeEstimate.FeeRate)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// Since we use fee rates in sat/kw internally, we'll convert the
|
|
|
|
// estimated fee rate from its sat/kb representation to sat/kw.
|
|
|
|
satPerKw := SatPerKVByte(satPerKB).FeePerKWeight()
|
|
|
|
|
|
|
|
// Finally, we'll enforce our fee floor.
|
|
|
|
if satPerKw < b.minFeePerKW {
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Debugf("Estimated fee rate of %v sat/kw is too low, "+
|
2018-08-24 05:36:33 +03:00
|
|
|
"using fee floor of %v sat/kw instead", satPerKw,
|
|
|
|
b.minFeePerKW)
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
satPerKw = b.minFeePerKW
|
2018-05-14 20:45:39 +03:00
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Debugf("Returning %v sat/kw for conf target of %v",
|
2018-07-28 04:37:05 +03:00
|
|
|
int64(satPerKw), confTarget)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
return satPerKw, nil
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// A compile-time assertion to ensure that BitcoindEstimator implements the
|
|
|
|
// Estimator interface.
|
|
|
|
var _ Estimator = (*BitcoindEstimator)(nil)
|
2018-09-10 03:31:34 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// WebAPIFeeSource is an interface allows the WebAPIEstimator to query an
|
2018-09-10 03:31:34 +03:00
|
|
|
// arbitrary HTTP-based fee estimator. Each new set/network will gain an
|
2019-10-31 05:43:05 +03:00
|
|
|
// implementation of this interface in order to allow the WebAPIEstimator to
|
2018-09-10 03:31:34 +03:00
|
|
|
// be fully generic in its logic.
|
|
|
|
type WebAPIFeeSource interface {
|
|
|
|
// GenQueryURL generates the full query URL. The value returned by this
|
|
|
|
// method should be able to be used directly as a path for an HTTP GET
|
|
|
|
// request.
|
|
|
|
GenQueryURL() string
|
|
|
|
|
|
|
|
// ParseResponse attempts to parse the body of the response generated
|
|
|
|
// by the above query URL. Typically this will be JSON, but the
|
|
|
|
// specifics are left to the WebAPIFeeSource implementation.
|
|
|
|
ParseResponse(r io.Reader) (map[uint32]uint32, error)
|
|
|
|
}
|
2019-04-09 05:31:36 +03:00
|
|
|
|
|
|
|
// SparseConfFeeSource is an implementation of the WebAPIFeeSource that utilizes
|
|
|
|
// a user-specified fee estimation API for Bitcoin. It expects the response
|
|
|
|
// to be in the JSON format: `fee_by_block_target: { ... }` where the value maps
|
|
|
|
// block targets to fee estimates (in sat per kilovbyte).
|
|
|
|
type SparseConfFeeSource struct {
|
|
|
|
// URL is the fee estimation API specified by the user.
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenQueryURL generates the full query URL. The value returned by this
|
|
|
|
// method should be able to be used directly as a path for an HTTP GET
|
|
|
|
// request.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the WebAPIFeeSource interface.
|
|
|
|
func (s SparseConfFeeSource) GenQueryURL() string {
|
|
|
|
return s.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseResponse attempts to parse the body of the response generated by the
|
|
|
|
// above query URL. Typically this will be JSON, but the specifics are left to
|
|
|
|
// the WebAPIFeeSource implementation.
|
|
|
|
//
|
|
|
|
// NOTE: Part of the WebAPIFeeSource interface.
|
|
|
|
func (s SparseConfFeeSource) ParseResponse(r io.Reader) (map[uint32]uint32, error) {
|
|
|
|
type jsonResp struct {
|
|
|
|
FeeByBlockTarget map[uint32]uint32 `json:"fee_by_block_target"`
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := jsonResp{
|
|
|
|
FeeByBlockTarget: make(map[uint32]uint32),
|
|
|
|
}
|
|
|
|
jsonReader := json.NewDecoder(r)
|
|
|
|
if err := jsonReader.Decode(&resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.FeeByBlockTarget, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that SparseConfFeeSource implements the
|
|
|
|
// WebAPIFeeSource interface.
|
|
|
|
var _ WebAPIFeeSource = (*SparseConfFeeSource)(nil)
|
2019-04-10 20:12:12 +03:00
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// WebAPIEstimator is an implementation of the Estimator interface that
|
2019-04-10 20:12:12 +03:00
|
|
|
// queries an HTTP-based fee estimation from an existing web API.
|
2019-10-31 05:43:05 +03:00
|
|
|
type WebAPIEstimator struct {
|
2019-04-10 20:12:12 +03:00
|
|
|
started sync.Once
|
|
|
|
stopped sync.Once
|
|
|
|
|
|
|
|
// apiSource is the backing web API source we'll use for our queries.
|
|
|
|
apiSource WebAPIFeeSource
|
|
|
|
|
|
|
|
// updateFeeTicker is the ticker responsible for updating the Estimator's
|
|
|
|
// fee estimates every time it fires.
|
|
|
|
updateFeeTicker *time.Ticker
|
|
|
|
|
|
|
|
// feeByBlockTarget is our cache for fees pulled from the API. When a
|
|
|
|
// fee estimate request comes in, we pull the estimate from this array
|
|
|
|
// rather than re-querying the API, to prevent an inadvertent DoS attack.
|
|
|
|
feesMtx sync.Mutex
|
|
|
|
feeByBlockTarget map[uint32]uint32
|
|
|
|
|
2020-09-10 10:05:54 +03:00
|
|
|
// noCache determines whether the web estimator should cache fee
|
|
|
|
// estimates.
|
|
|
|
noCache bool
|
|
|
|
|
2019-04-10 20:12:12 +03:00
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// NewWebAPIEstimator creates a new WebAPIEstimator from a given URL and a
|
2019-04-10 20:12:12 +03:00
|
|
|
// fallback default fee. The fees are updated whenever a new block is mined.
|
2020-09-10 10:05:54 +03:00
|
|
|
func NewWebAPIEstimator(api WebAPIFeeSource, noCache bool) *WebAPIEstimator {
|
2019-10-31 05:43:05 +03:00
|
|
|
return &WebAPIEstimator{
|
2019-04-10 20:12:12 +03:00
|
|
|
apiSource: api,
|
|
|
|
feeByBlockTarget: make(map[uint32]uint32),
|
2020-09-10 10:05:54 +03:00
|
|
|
noCache: noCache,
|
2019-04-10 20:12:12 +03:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateFeePerKW takes in a target for the number of blocks until an initial
|
|
|
|
// confirmation and returns the estimated fee expressed in sat/kw.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (w *WebAPIEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error) {
|
2019-04-10 20:12:12 +03:00
|
|
|
if numBlocks > maxBlockTarget {
|
|
|
|
numBlocks = maxBlockTarget
|
|
|
|
} else if numBlocks < minBlockTarget {
|
|
|
|
return 0, fmt.Errorf("conf target of %v is too low, minimum "+
|
|
|
|
"accepted is %v", numBlocks, minBlockTarget)
|
|
|
|
}
|
|
|
|
|
2020-09-10 10:05:54 +03:00
|
|
|
// Get fee estimates now if we don't refresh periodically.
|
|
|
|
if w.noCache {
|
|
|
|
w.updateFeeEstimates()
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:12:12 +03:00
|
|
|
feePerKb, err := w.getCachedFee(numBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the result is too low, then we'll clamp it to our current fee
|
|
|
|
// floor.
|
|
|
|
satPerKw := SatPerKVByte(feePerKb).FeePerKWeight()
|
|
|
|
if satPerKw < FeePerKwFloor {
|
|
|
|
satPerKw = FeePerKwFloor
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Debugf("Web API returning %v sat/kw for conf target of %v",
|
2019-04-10 20:12:12 +03:00
|
|
|
int64(satPerKw), numBlocks)
|
|
|
|
|
|
|
|
return satPerKw, nil
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// Start signals the Estimator to start any processes or goroutines it needs
|
2019-04-10 20:12:12 +03:00
|
|
|
// to perform its duty.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (w *WebAPIEstimator) Start() error {
|
2020-09-10 10:05:54 +03:00
|
|
|
// No update loop is needed when we don't cache.
|
|
|
|
if w.noCache {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:12:12 +03:00
|
|
|
var err error
|
|
|
|
w.started.Do(func() {
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Infof("Starting web API fee estimator")
|
2019-04-10 20:12:12 +03:00
|
|
|
|
|
|
|
w.updateFeeTicker = time.NewTicker(w.randomFeeUpdateTimeout())
|
|
|
|
w.updateFeeEstimates()
|
|
|
|
|
|
|
|
w.wg.Add(1)
|
|
|
|
go w.feeUpdateManager()
|
|
|
|
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used by the
|
|
|
|
// fee estimator.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (w *WebAPIEstimator) Stop() error {
|
2020-09-10 10:05:54 +03:00
|
|
|
// Update loop is not running when we don't cache.
|
|
|
|
if w.noCache {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:12:12 +03:00
|
|
|
w.stopped.Do(func() {
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Infof("Stopping web API fee estimator")
|
2019-04-10 20:12:12 +03:00
|
|
|
|
|
|
|
w.updateFeeTicker.Stop()
|
|
|
|
|
|
|
|
close(w.quit)
|
|
|
|
w.wg.Wait()
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions to be
|
|
|
|
// relayed.
|
|
|
|
//
|
2019-10-31 05:43:05 +03:00
|
|
|
// NOTE: This method is part of the Estimator interface.
|
|
|
|
func (w *WebAPIEstimator) RelayFeePerKW() SatPerKWeight {
|
2019-04-10 20:12:12 +03:00
|
|
|
return FeePerKwFloor
|
|
|
|
}
|
|
|
|
|
|
|
|
// randomFeeUpdateTimeout returns a random timeout between minFeeUpdateTimeout
|
|
|
|
// and maxFeeUpdateTimeout that will be used to determine how often the Estimator
|
|
|
|
// should retrieve fresh fees from its API.
|
2019-10-31 05:43:05 +03:00
|
|
|
func (w *WebAPIEstimator) randomFeeUpdateTimeout() time.Duration {
|
2019-04-10 20:12:12 +03:00
|
|
|
lower := int64(minFeeUpdateTimeout)
|
|
|
|
upper := int64(maxFeeUpdateTimeout)
|
|
|
|
return time.Duration(prand.Int63n(upper-lower) + lower)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCachedFee takes in a target for the number of blocks until an initial
|
|
|
|
// confirmation and returns an estimated fee (if one was returned by the API). If
|
|
|
|
// the fee was not previously cached, we cache it here.
|
2019-10-31 05:43:05 +03:00
|
|
|
func (w *WebAPIEstimator) getCachedFee(numBlocks uint32) (uint32, error) {
|
2019-04-10 20:12:12 +03:00
|
|
|
w.feesMtx.Lock()
|
|
|
|
defer w.feesMtx.Unlock()
|
|
|
|
|
|
|
|
// Search our cached fees for the desired block target. If the target is
|
|
|
|
// not cached, then attempt to extrapolate it from the next lowest target
|
|
|
|
// that *is* cached. If we successfully extrapolate, then cache the
|
|
|
|
// target's fee.
|
|
|
|
for target := numBlocks; target >= minBlockTarget; target-- {
|
|
|
|
fee, ok := w.feeByBlockTarget[target]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = w.feeByBlockTarget[numBlocks]
|
|
|
|
if !ok {
|
|
|
|
w.feeByBlockTarget[numBlocks] = fee
|
|
|
|
}
|
|
|
|
return fee, nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("web API does not include a fee estimation for "+
|
|
|
|
"block target of %v", numBlocks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateFeeEstimates re-queries the API for fresh fees and caches them.
|
2019-10-31 05:43:05 +03:00
|
|
|
func (w *WebAPIEstimator) updateFeeEstimates() {
|
2019-04-10 20:12:12 +03:00
|
|
|
// Rather than use the default http.Client, we'll make a custom one
|
|
|
|
// which will allow us to control how long we'll wait to read the
|
|
|
|
// response from the service. This way, if the service is down or
|
|
|
|
// overloaded, we can exit early and use our default fee.
|
|
|
|
netTransport := &http.Transport{
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
netClient := &http.Client{
|
|
|
|
Timeout: time.Second * 10,
|
|
|
|
Transport: netTransport,
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the client created, we'll query the API source to fetch the URL
|
|
|
|
// that we should use to query for the fee estimation.
|
|
|
|
targetURL := w.apiSource.GenQueryURL()
|
|
|
|
resp, err := netClient.Get(targetURL)
|
|
|
|
if err != nil {
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Errorf("unable to query web api for fee response: %v",
|
2019-04-10 20:12:12 +03:00
|
|
|
err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Once we've obtained the response, we'll instruct the WebAPIFeeSource
|
|
|
|
// to parse out the body to obtain our final result.
|
|
|
|
feesByBlockTarget, err := w.apiSource.ParseResponse(resp.Body)
|
|
|
|
if err != nil {
|
2019-10-31 05:43:05 +03:00
|
|
|
log.Errorf("unable to query web api for fee response: %v",
|
2019-04-10 20:12:12 +03:00
|
|
|
err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.feesMtx.Lock()
|
|
|
|
w.feeByBlockTarget = feesByBlockTarget
|
|
|
|
w.feesMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// feeUpdateManager updates the fee estimates whenever a new block comes in.
|
2019-10-31 05:43:05 +03:00
|
|
|
func (w *WebAPIEstimator) feeUpdateManager() {
|
2019-04-10 20:12:12 +03:00
|
|
|
defer w.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-w.updateFeeTicker.C:
|
|
|
|
w.updateFeeEstimates()
|
|
|
|
case <-w.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:43:05 +03:00
|
|
|
// A compile-time assertion to ensure that WebAPIEstimator implements the
|
|
|
|
// Estimator interface.
|
|
|
|
var _ Estimator = (*WebAPIEstimator)(nil)
|