2017-11-19 01:35:00 +03:00
|
|
|
package lnwallet
|
|
|
|
|
2017-11-23 09:11:02 +03:00
|
|
|
import (
|
2017-11-10 03:30:20 +03:00
|
|
|
"encoding/json"
|
|
|
|
|
2017-11-23 09:11:21 +03:00
|
|
|
"github.com/roasbeef/btcd/blockchain"
|
|
|
|
"github.com/roasbeef/btcd/rpcclient"
|
2017-11-23 09:11:02 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
)
|
|
|
|
|
2018-02-13 16:18:46 +03:00
|
|
|
// SatPerVByte represents a fee rate in satoshis per vbyte.
|
|
|
|
type SatPerVByte btcutil.Amount
|
|
|
|
|
|
|
|
// FeeForVSize calculates the fee resulting from this fee rate and
|
|
|
|
// the given vsize in vbytes.
|
|
|
|
func (s SatPerVByte) FeeForVSize(vbytes int64) btcutil.Amount {
|
|
|
|
return btcutil.Amount(s) * btcutil.Amount(vbytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FeePerKWeight converts the fee rate into SatPerKWeight.
|
|
|
|
func (s SatPerVByte) FeePerKWeight() SatPerKWeight {
|
|
|
|
return SatPerKWeight(s * 1000 / blockchain.WitnessScaleFactor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SatPerKWeight represents a fee rate in satoshis per kilo weight unit.
|
|
|
|
type SatPerKWeight btcutil.Amount
|
|
|
|
|
|
|
|
// FeeForWeight calculates the fee resulting from this fee rate and the
|
|
|
|
// given weight in weight units (wu).
|
|
|
|
func (s SatPerKWeight) FeeForWeight(wu int64) btcutil.Amount {
|
|
|
|
// The resulting fee is rounded down, as specified in BOLT#03.
|
|
|
|
return btcutil.Amount(s) * btcutil.Amount(wu) / 1000
|
|
|
|
}
|
|
|
|
|
2017-11-19 01:35:00 +03:00
|
|
|
// FeeEstimator provides the ability to estimate on-chain transaction fees for
|
|
|
|
// various combinations of transaction sizes and desired confirmation time
|
|
|
|
// (measured by number of blocks).
|
|
|
|
type FeeEstimator interface {
|
2018-02-13 16:18:46 +03:00
|
|
|
// EstimateFeePerVSize takes in a target for the number of blocks until
|
2017-11-19 01:35:00 +03:00
|
|
|
// an initial confirmation and returns the estimated fee expressed in
|
2018-02-13 16:18:46 +03:00
|
|
|
// satoshis/vbyte.
|
|
|
|
EstimateFeePerVSize(numBlocks uint32) (SatPerVByte, error)
|
2017-11-23 09:11:21 +03:00
|
|
|
|
|
|
|
// Start signals the FeeEstimator to start any processes or goroutines
|
|
|
|
// 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
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// StaticFeeEstimator will return a static value for all fee calculation
|
|
|
|
// requests. It is designed to be replaced by a proper fee calculation
|
|
|
|
// implementation.
|
|
|
|
type StaticFeeEstimator struct {
|
2018-02-13 16:18:46 +03:00
|
|
|
// FeeRate is the static fee rate in satoshis-per-vbyte that will be
|
|
|
|
// returned by this fee estimator.
|
|
|
|
FeeRate SatPerVByte
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 16:18:46 +03:00
|
|
|
// EstimateFeePerVSize will return a static value for fee calculations.
|
2017-11-23 09:11:42 +03:00
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
2018-02-13 16:18:46 +03:00
|
|
|
func (e StaticFeeEstimator) EstimateFeePerVSize(numBlocks uint32) (SatPerVByte, error) {
|
2017-11-23 09:11:42 +03:00
|
|
|
return e.FeeRate, nil
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 09:11:42 +03:00
|
|
|
// Start signals the FeeEstimator to start any processes or goroutines
|
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (e StaticFeeEstimator) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (e StaticFeeEstimator) Stop() error {
|
|
|
|
return nil
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that StaticFeeEstimator implements the
|
|
|
|
// FeeEstimator interface.
|
|
|
|
var _ FeeEstimator = (*StaticFeeEstimator)(nil)
|
2017-11-23 09:26:07 +03:00
|
|
|
|
|
|
|
// BtcdFeeEstimator is an implementation of the FeeEstimator interface backed
|
|
|
|
// 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.
|
2017-11-23 09:26:07 +03:00
|
|
|
type BtcdFeeEstimator struct {
|
2018-02-13 16:18:46 +03:00
|
|
|
// fallBackFeeRate is the fall back fee rate in satoshis per vbyte that
|
2017-11-23 09:26:07 +03:00
|
|
|
// is returned if the fee estimator does not yet have enough data to
|
|
|
|
// actually produce fee estimates.
|
2018-02-13 16:18:46 +03:00
|
|
|
fallBackFeeRate SatPerVByte
|
2017-11-23 09:26:07 +03:00
|
|
|
|
|
|
|
btcdConn *rpcclient.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBtcdFeeEstimator creates a new BtcdFeeEstimator given a fully populated
|
|
|
|
// 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.
|
|
|
|
func NewBtcdFeeEstimator(rpcConfig rpcclient.ConnConfig,
|
2018-02-13 16:18:46 +03:00
|
|
|
fallBackFeeRate SatPerVByte) (*BtcdFeeEstimator, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BtcdFeeEstimator{
|
|
|
|
fallBackFeeRate: fallBackFeeRate,
|
|
|
|
btcdConn: chainConn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start signals the FeeEstimator to start any processes or goroutines
|
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BtcdFeeEstimator) Start() error {
|
|
|
|
if err := b.btcdConn.Connect(20); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BtcdFeeEstimator) Stop() error {
|
|
|
|
b.btcdConn.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:18:46 +03:00
|
|
|
// EstimateFeePerVSize takes in a target for the number of blocks until an
|
2017-11-23 09:26:07 +03:00
|
|
|
// initial confirmation and returns the estimated fee expressed in
|
2018-02-13 16:18:46 +03:00
|
|
|
// satoshis/vbyte.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BtcdFeeEstimator) EstimateFeePerVSize(numBlocks uint32) (SatPerVByte, error) {
|
|
|
|
feeEstimate, err := b.fetchEstimatePerVSize(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:
|
|
|
|
walletLog.Errorf("unable to query estimator: %v", err)
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case feeEstimate == 0:
|
|
|
|
return b.fallBackFeeRate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return feeEstimate, nil
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:03:27 +03:00
|
|
|
// fetchEstimate returns a fee estimate for a transaction to be confirmed in
|
2018-02-13 16:18:46 +03:00
|
|
|
// confTarget blocks. The estimate is returned in sat/vbyte.
|
|
|
|
func (b *BtcdFeeEstimator) fetchEstimatePerVSize(
|
|
|
|
confTarget uint32) (SatPerVByte, 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
|
|
|
|
|
|
|
// The value returned is expressed in fees per KB, while we want
|
2018-02-13 16:18:46 +03:00
|
|
|
// fee-per-byte, so we'll divide by 1000 to map to satoshis-per-byte
|
2017-11-23 09:26:07 +03:00
|
|
|
// before returning the estimate.
|
2018-02-13 16:18:46 +03:00
|
|
|
satPerByte := satPerKB / 1000
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2018-02-13 16:18:46 +03:00
|
|
|
walletLog.Debugf("Returning %v sat/vbyte for conf target of %v",
|
2017-11-23 09:26:07 +03:00
|
|
|
int64(satPerByte), confTarget)
|
|
|
|
|
2018-02-13 16:18:46 +03:00
|
|
|
return SatPerVByte(satPerByte), nil
|
2017-11-23 09:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that BtcdFeeEstimator implements the
|
|
|
|
// FeeEstimator interface.
|
|
|
|
var _ FeeEstimator = (*BtcdFeeEstimator)(nil)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
|
|
|
// BitcoindFeeEstimator is an implementation of the FeeEstimator interface
|
|
|
|
// backed by the RPC interface of an active bitcoind node. This implementation
|
2018-02-07 06:11:11 +03:00
|
|
|
// will proxy any fee estimation requests to bitcoind's RPC interface.
|
2017-11-10 03:30:20 +03:00
|
|
|
type BitcoindFeeEstimator struct {
|
2018-02-13 16:18:46 +03:00
|
|
|
// fallBackFeeRate is the fall back fee rate in satoshis per vbyte that
|
2017-11-10 03:30:20 +03:00
|
|
|
// is returned if the fee estimator does not yet have enough data to
|
|
|
|
// actually produce fee estimates.
|
2018-02-13 16:18:46 +03:00
|
|
|
fallBackFeeRate SatPerVByte
|
2017-11-10 03:30:20 +03:00
|
|
|
|
|
|
|
bitcoindConn *rpcclient.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBitcoindFeeEstimator creates a new BitcoindFeeEstimator 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.
|
|
|
|
func NewBitcoindFeeEstimator(rpcConfig rpcclient.ConnConfig,
|
2018-02-13 16:18:46 +03:00
|
|
|
fallBackFeeRate SatPerVByte) (*BitcoindFeeEstimator, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BitcoindFeeEstimator{
|
|
|
|
fallBackFeeRate: fallBackFeeRate,
|
|
|
|
bitcoindConn: chainConn,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start signals the FeeEstimator to start any processes or goroutines
|
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BitcoindFeeEstimator) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BitcoindFeeEstimator) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:18:46 +03:00
|
|
|
// EstimateFeePerVSize takes in a target for the number of blocks until an
|
2017-11-10 03:30:20 +03:00
|
|
|
// initial confirmation and returns the estimated fee expressed in
|
2018-02-13 16:18:46 +03:00
|
|
|
// satoshis/vbyte.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BitcoindFeeEstimator) EstimateFeePerVSize(numBlocks uint32) (SatPerVByte, error) {
|
|
|
|
feeEstimate, err := b.fetchEstimatePerVSize(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:
|
|
|
|
walletLog.Errorf("unable to query estimator: %v", err)
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case feeEstimate == 0:
|
|
|
|
return b.fallBackFeeRate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return feeEstimate, nil
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:03:27 +03:00
|
|
|
// fetchEstimatePerVSize returns a fee estimate for a transaction to be confirmed in
|
2018-02-13 16:18:46 +03:00
|
|
|
// confTarget blocks. The estimate is returned in sat/vbyte.
|
|
|
|
func (b *BitcoindFeeEstimator) fetchEstimatePerVSize(
|
|
|
|
confTarget uint32) (SatPerVByte, 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
|
|
|
|
}
|
|
|
|
// TODO: Allow selection of economical/conservative modifiers.
|
|
|
|
resp, err := b.bitcoindConn.RawRequest("estimatesmartfee",
|
|
|
|
[]json.RawMessage{target})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll parse the response to get the BTC per KB.
|
|
|
|
feeEstimate := struct {
|
|
|
|
Feerate float64 `json:"feerate"`
|
|
|
|
}{}
|
|
|
|
err = json.Unmarshal(resp, &feeEstimate)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll convert the returned value to satoshis, as it's
|
|
|
|
// currently returned in BTC.
|
|
|
|
satPerKB, err := btcutil.NewAmount(feeEstimate.Feerate)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The value returned is expressed in fees per KB, while we want
|
2018-01-29 01:53:53 +03:00
|
|
|
// fee-per-byte, so we'll divide by 1000 to map to satoshis-per-byte
|
2017-11-10 03:30:20 +03:00
|
|
|
// before returning the estimate.
|
2018-01-29 01:53:53 +03:00
|
|
|
satPerByte := satPerKB / 1000
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-02-13 16:18:46 +03:00
|
|
|
walletLog.Debugf("Returning %v sat/vbyte for conf target of %v",
|
2017-11-10 03:30:20 +03:00
|
|
|
int64(satPerByte), confTarget)
|
|
|
|
|
2018-02-13 16:18:46 +03:00
|
|
|
return SatPerVByte(satPerByte), nil
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that BitcoindFeeEstimator implements the
|
|
|
|
// FeeEstimator interface.
|
|
|
|
var _ FeeEstimator = (*BitcoindFeeEstimator)(nil)
|