From 0bcd521216be675c8da7251c970ebaaba1568603 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Thu, 18 Apr 2019 11:18:32 +0200 Subject: [PATCH] Compare whatTheFee estimates to default --- bchain/coins/btc/whatthefee.go | 40 +++++++++++++++++++++++++----- configs/coins/bitcoin_testnet.json | 5 +--- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/bchain/coins/btc/whatthefee.go b/bchain/coins/btc/whatthefee.go index ae572b9c..ff122ec6 100644 --- a/bchain/coins/btc/whatthefee.go +++ b/bchain/coins/btc/whatthefee.go @@ -4,6 +4,7 @@ import ( "blockbook/bchain" "bytes" "encoding/json" + "fmt" "math" "net/http" "strconv" @@ -67,23 +68,29 @@ func InitWhatTheFee(chain bchain.BlockChain, params string) error { func whatTheFeeDownloader() { period := time.Duration(whatTheFee.params.PeriodSeconds) * time.Second timer := time.NewTimer(period) + counter := 0 for { var data whatTheFeeServiceResult err := whatTheFeeGetData(&data) if err != nil { glog.Error("whatTheFeeGetData ", err) } else { - whatTheFeeProcessData(&data) + if whatTheFeeProcessData(&data) { + if counter%60 == 0 { + whatTheFeeCompareToDefault() + } + counter++ + } } <-timer.C timer.Reset(period) } } -func whatTheFeeProcessData(data *whatTheFeeServiceResult) { +func whatTheFeeProcessData(data *whatTheFeeServiceResult) bool { if len(data.Index) == 0 || len(data.Index) != len(data.Data) || len(data.Columns) == 0 { glog.Errorf("invalid data %+v", data) - return + return false } whatTheFee.mux.Lock() defer whatTheFee.mux.Unlock() @@ -92,7 +99,7 @@ func whatTheFeeProcessData(data *whatTheFeeServiceResult) { for i, blocks := range data.Index { if len(data.Columns) != len(data.Data[i]) { glog.Errorf("invalid data %+v", data) - return + return false } fees := make([]int, len(data.Columns)) for j, l := range data.Data[i] { @@ -105,6 +112,7 @@ func whatTheFeeProcessData(data *whatTheFeeServiceResult) { } whatTheFee.lastSync = time.Now() glog.Infof("%+v", whatTheFee.fees) + return true } func whatTheFeeGetData(res interface{}) error { @@ -120,10 +128,30 @@ func whatTheFeeGetData(res interface{}) error { if err != nil { return err } - // if server returns HTTP error code it might not return json with response - // handle both cases if httpRes.StatusCode != 200 { return errors.New("whatthefee.io returned status " + strconv.Itoa(httpRes.StatusCode)) } return safeDecodeResponse(httpRes.Body, &res) } + +func whatTheFeeCompareToDefault() { + output := "" + for _, fee := range whatTheFee.fees { + output += fmt.Sprint(fee.blocks, ",") + for _, wtf := range fee.feesPerKB { + output += fmt.Sprint(wtf, ",") + } + conservative, err := whatTheFee.chain.EstimateSmartFee(fee.blocks, true) + if err != nil { + glog.Error(err) + return + } + economical, err := whatTheFee.chain.EstimateSmartFee(fee.blocks, false) + if err != nil { + glog.Error(err) + return + } + output += fmt.Sprint(conservative.String(), ",", economical.String(), "\n") + } + glog.Info("whatTheFeeCompareToDefault\n", output) +} diff --git a/configs/coins/bitcoin_testnet.json b/configs/coins/bitcoin_testnet.json index 581f7d7b..a8647f68 100644 --- a/configs/coins/bitcoin_testnet.json +++ b/configs/coins/bitcoin_testnet.json @@ -59,10 +59,7 @@ "xpub_magic_segwit_p2sh": 71979618, "xpub_magic_segwit_native": 73342198, "slip44": 1, - "additional_params": { - "alternativeEstimateFee": "whatthefee", - "alternativeEstimateFeeParams": "{\"url\": \"https://whatthefee.io/data.json\", \"periodSeconds\": 60}" - } + "additional_params": {} } }, "meta": {