Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.6] server/btc: use external fee rates #2638

Open
wants to merge 1 commit into
base: release-v0.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 116 additions & 1 deletion server/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -167,6 +170,12 @@ type Backend struct {
fee uint64
hash chainhash.Hash
}

feeRateCache struct {
sync.RWMutex
feeRate uint64
stamp time.Time
}
}

// Check that Backend satisfies the Backend interface.
Expand Down Expand Up @@ -404,11 +413,40 @@ func (btc *Backend) Connect(ctx context.Context) (*sync.WaitGroup, error) {
return nil, fmt.Errorf("%s transaction index is not enabled. Please enable txindex in the node config and you might need to re-index when you enable txindex", btc.name)
}

var wg sync.WaitGroup
if btc.cfg.Name == assetName /* "btc" */ {
// Prime the cache.
btc.feeRateCache.feeRate = btc.fetchExternalBitcoinFeeRate(ctx)
if btc.feeRateCache.feeRate == 0 {
btc.shutdown()
return nil, errors.New("failed to retrieve external btc fee rate")
}
btc.feeRateCache.stamp = time.Now()
wg.Add(1)
go func() {
defer wg.Done()
tick := time.NewTicker(time.Minute * 5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe defer tick.Stop()

for {
select {
case <-tick.C:
feeRate := btc.fetchExternalBitcoinFeeRate(ctx)
if feeRate > 0 {
btc.feeRateCache.Lock()
btc.feeRateCache.stamp = time.Now()
btc.feeRateCache.feeRate = feeRate
btc.feeRateCache.Unlock()
}
Comment on lines +431 to +438
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe try again sooner on a failure? In case of a one-off connection issue.

case <-ctx.Done():
return
}
}
}()
}

if _, err = btc.estimateFee(ctx); err != nil {
btc.log.Warnf("Backend started without fee estimation available: %v", err)
}

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
Expand All @@ -417,6 +455,19 @@ func (btc *Backend) Connect(ctx context.Context) (*sync.WaitGroup, error) {
return &wg, nil
}

func (btc *Backend) fetchExternalBitcoinFeeRate(ctx context.Context) uint64 {
if feeRate := getMempoolDotSpaceFeeRate(ctx, btc.log); feeRate > 0 {
btc.log.Tracef("fetched fee rate from mempool.space: %d", feeRate)
return feeRate
}
if feeRate := getBtcDotComFeeRate(ctx, btc.log); feeRate > 0 {
btc.log.Tracef("fetched fee rate from btc.com: %d", feeRate)
return feeRate
}
btc.log.Warnf("failed to fetch an external fee rate")
return 0
}

// Net returns the *chaincfg.Params. This is not part of the asset.Backend
// interface, and is exported as a convenience for embedding types.
func (btc *Backend) Net() *chaincfg.Params {
Expand Down Expand Up @@ -1433,6 +1484,18 @@ out:
// case, an estimate is calculated from the median fees of the previous
// block(s).
func (btc *Backend) estimateFee(ctx context.Context) (satsPerB uint64, err error) {
if btc.cfg.Name == assetName /* "btc" */ {
const feeRateExpiry = time.Minute * 10
btc.feeRateCache.RLock()
stamp, feeRate := btc.feeRateCache.stamp, btc.feeRateCache.feeRate
btc.feeRateCache.RUnlock()
if time.Since(stamp) < feeRateExpiry {
return feeRate, nil
} else {
btc.log.Warnf("external btc fee rate is expired. falling back to estimatesmartfee")
}
}

if btc.cfg.DumbFeeEstimates {
satsPerB, err = btc.node.EstimateFee(btc.feeConfs)
} else {
Expand Down Expand Up @@ -1545,3 +1608,55 @@ func hashTx(tx *wire.MsgTx) *chainhash.Hash {
h := tx.TxHash()
return &h
}

func getMempoolDotSpaceFeeRate(ctx context.Context, log dex.Logger) uint64 {
const uri = "https://mempool.space/api/v1/fees/recommended"
var res struct {
FastestFee uint64 `json:"fastestFee"`
}
if err := getHTTP(ctx, uri, &res); err != nil {
log.Errorf("Error fetching fees from mempool.space: %v", err)
return 0
}
if res.FastestFee == 0 {
log.Errorf("Fee rate of zero reported by mempool.space")
}
return res.FastestFee
}

func getBtcDotComFeeRate(ctx context.Context, log dex.Logger) uint64 {
const uri = "https://btc.com/service/fees/distribution"
var res struct {
RecommendedFees struct {
OneBlockFee uint64 `json:"one_block_fee"`
} `json:"fees_recommended"`
}
if err := getHTTP(ctx, uri, &res); err != nil {
log.Errorf("Error fetching fees from btc.com: %v", err)
return 0
}
if res.RecommendedFees.OneBlockFee == 0 {
log.Errorf("Fee rate of zero reported by btc.com")
}
return res.RecommendedFees.OneBlockFee
}

func getHTTP(ctx context.Context, url string, thing interface{}) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected response, got status code %d", resp.StatusCode)
}

reader := io.LimitReader(resp.Body, 1<<20)
return json.NewDecoder(reader).Decode(thing)
}
2 changes: 1 addition & 1 deletion server/asset/btc/btc_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !btclive
//go:build !btclive && !btcfees

// These tests will not be run if the btclive build tag is set. In that case,
// the live_test.go tests will run.
Expand Down
34 changes: 34 additions & 0 deletions server/asset/btc/btcfees_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build btcfees

package btc

import (
"context"
"testing"
"time"

"decred.org/dcrdex/dex"
)

func TestExternalBTCFeeRates(t *testing.T) {
log := dex.StdOutLogger("T", dex.LevelTrace)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

feeRate := getMempoolDotSpaceFeeRate(ctx, log)
if feeRate == 0 {
t.Fatalf("mempool.space fee rate failed")
}

feeRate = getBtcDotComFeeRate(ctx, log)
if feeRate == 0 {
t.Fatalf("btc.com fee rate failed")
}

time.Sleep(time.Second)

feeRate = (&Backend{log: log}).fetchExternalBitcoinFeeRate(ctx)
if feeRate == 0 {
t.Fatalf("fetchExternalBitcoinFeeRate failed")
}
}