chainfee: simplify WebAPIFeeSource interface
This commit removes the interface methods `GenQueryURL` and `ParseResponse` so the `WebAPIEstimator` can care less about the implementation details of this interface.
This commit is contained in:
2 files changed
+13
-48
No files matched your search
@@ -493,16 +493,6 @@ var _ Estimator = (*BitcoindEstimator)(nil)
|
|||||||
// implementation of this interface in order to allow the WebAPIEstimator to
|
// implementation of this interface in order to allow the WebAPIEstimator to
|
||||||
// be fully generic in its logic.
|
// be fully generic in its logic.
|
||||||
type WebAPIFeeSource interface {
|
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)
|
|
||||||
|
|
||||||
// GetFeeMap will query the web API, parse the response and return a
|
// GetFeeMap will query the web API, parse the response and return a
|
||||||
// map of confirmation targets to sat/kw fees.
|
// map of confirmation targets to sat/kw fees.
|
||||||
GetFeeMap() (map[uint32]uint32, error)
|
GetFeeMap() (map[uint32]uint32, error)
|
||||||
@@ -517,21 +507,12 @@ type SparseConfFeeSource struct {
|
|||||||
URL string
|
URL string
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenQueryURL generates the full query URL. The value returned by this
|
// parseResponse attempts to parse the body of the response generated by the
|
||||||
// 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
|
// above query URL. Typically this will be JSON, but the specifics are left to
|
||||||
// the WebAPIFeeSource implementation.
|
// the WebAPIFeeSource implementation.
|
||||||
//
|
func (s SparseConfFeeSource) parseResponse(r io.Reader) (
|
||||||
// NOTE: Part of the WebAPIFeeSource interface.
|
map[uint32]uint32, error) {
|
||||||
func (s SparseConfFeeSource) ParseResponse(r io.Reader) (map[uint32]uint32, error) {
|
|
||||||
type jsonResp struct {
|
type jsonResp struct {
|
||||||
FeeByBlockTarget map[uint32]uint32 `json:"fee_by_block_target"`
|
FeeByBlockTarget map[uint32]uint32 `json:"fee_by_block_target"`
|
||||||
}
|
}
|
||||||
@@ -567,7 +548,7 @@ func (s SparseConfFeeSource) GetFeeMap() (map[uint32]uint32, error) {
|
|||||||
|
|
||||||
// With the client created, we'll query the API source to fetch the URL
|
// With the client created, we'll query the API source to fetch the URL
|
||||||
// that we should use to query for the fee estimation.
|
// that we should use to query for the fee estimation.
|
||||||
targetURL := s.GenQueryURL()
|
targetURL := s.URL
|
||||||
resp, err := netClient.Get(targetURL)
|
resp, err := netClient.Get(targetURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to query web api for fee response: %v",
|
log.Errorf("unable to query web api for fee response: %v",
|
||||||
@@ -578,7 +559,7 @@ func (s SparseConfFeeSource) GetFeeMap() (map[uint32]uint32, error) {
|
|||||||
|
|
||||||
// Once we've obtained the response, we'll instruct the WebAPIFeeSource
|
// Once we've obtained the response, we'll instruct the WebAPIFeeSource
|
||||||
// to parse out the body to obtain our final result.
|
// to parse out the body to obtain our final result.
|
||||||
feesByBlockTarget, err := s.ParseResponse(resp.Body)
|
feesByBlockTarget, err := s.parseResponse(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to parse fee api response: %v", err)
|
log.Errorf("unable to parse fee api response: %v", err)
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package chainfee
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcutil"
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
@@ -16,14 +14,6 @@ type mockSparseConfFeeSource struct {
|
|||||||
fees map[uint32]uint32
|
fees map[uint32]uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e mockSparseConfFeeSource) GenQueryURL() string {
|
|
||||||
return e.url
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e mockSparseConfFeeSource) ParseResponse(r io.Reader) (map[uint32]uint32, error) {
|
|
||||||
return e.fees, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e mockSparseConfFeeSource) GetFeeMap() (map[uint32]uint32, error) {
|
func (e mockSparseConfFeeSource) GetFeeMap() (map[uint32]uint32, error) {
|
||||||
return e.fees, nil
|
return e.fees, nil
|
||||||
}
|
}
|
||||||
@@ -117,10 +107,6 @@ func TestSparseConfFeeSource(t *testing.T) {
|
|||||||
// Test that GenQueryURL returns the URL as is.
|
// Test that GenQueryURL returns the URL as is.
|
||||||
url := "test"
|
url := "test"
|
||||||
feeSource := SparseConfFeeSource{URL: url}
|
feeSource := SparseConfFeeSource{URL: url}
|
||||||
queryURL := feeSource.GenQueryURL()
|
|
||||||
if queryURL != url {
|
|
||||||
t.Fatalf("expected query URL of %v, got %v", url, queryURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test parsing a properly formatted JSON API response.
|
// Test parsing a properly formatted JSON API response.
|
||||||
// First, create the response as a bytes.Reader.
|
// First, create the response as a bytes.Reader.
|
||||||
@@ -129,17 +115,17 @@ func TestSparseConfFeeSource(t *testing.T) {
|
|||||||
2: 42,
|
2: 42,
|
||||||
3: 54321,
|
3: 54321,
|
||||||
}
|
}
|
||||||
testJSON := map[string]map[uint32]uint32{"fee_by_block_target": testFees}
|
testJSON := map[string]map[uint32]uint32{
|
||||||
|
"fee_by_block_target": testFees,
|
||||||
|
}
|
||||||
jsonResp, err := json.Marshal(testJSON)
|
jsonResp, err := json.Marshal(testJSON)
|
||||||
require.NoError(t, err, "unable to marshal JSON API response")
|
require.NoError(t, err, "unable to marshal JSON API response")
|
||||||
reader := bytes.NewReader(jsonResp)
|
reader := bytes.NewReader(jsonResp)
|
||||||
|
|
||||||
// Finally, ensure the expected map is returned without error.
|
// Finally, ensure the expected map is returned without error.
|
||||||
fees, err := feeSource.ParseResponse(reader)
|
fees, err := feeSource.parseResponse(reader)
|
||||||
require.NoError(t, err, "unable to parse API response")
|
require.NoError(t, err, "unable to parse API response")
|
||||||
if !reflect.DeepEqual(fees, testFees) {
|
require.Equal(t, testFees, fees, "unexpected fee map returned")
|
||||||
t.Fatalf("expected %v, got %v", testFees, fees)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test parsing an improperly formatted JSON API response.
|
// Test parsing an improperly formatted JSON API response.
|
||||||
badFees := map[string]uint32{"hi": 12345, "hello": 42, "satoshi": 54321}
|
badFees := map[string]uint32{"hi": 12345, "hello": 42, "satoshi": 54321}
|
||||||
@@ -149,10 +135,8 @@ func TestSparseConfFeeSource(t *testing.T) {
|
|||||||
reader = bytes.NewReader(jsonResp)
|
reader = bytes.NewReader(jsonResp)
|
||||||
|
|
||||||
// Finally, ensure the improperly formatted fees error.
|
// Finally, ensure the improperly formatted fees error.
|
||||||
_, err = feeSource.ParseResponse(reader)
|
_, err = feeSource.parseResponse(reader)
|
||||||
if err == nil {
|
require.Error(t, err, "expected error when parsing bad JSON")
|
||||||
t.Fatalf("expected ParseResponse to fail")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestWebAPIFeeEstimator checks that the WebAPIFeeEstimator returns fee rates
|
// TestWebAPIFeeEstimator checks that the WebAPIFeeEstimator returns fee rates
|
||||||
|
|||||||
Reference in new issue
Block a user