chainfee: allow specifying min relay feerate from API source

This commit adds a new expected field, `min_relay_feerate`, in the
response body returned from the API source, allowing the API to specify
a min relay feerate to be used instead of the FeePerKwFloor.

This change is backwards compatible as for an old API source which
doesn't specify the `min_relay_feerate`, it will be interpreted as zero.
This commit is contained in:
yyforyongyu committed 2024-07-24 20:05:00 +08:00
1 parent b10ebb2692
commit ce43e4bab7
5 files changed
+124 -32

No files matched your search

+53 -8
View File
@@ -107,17 +107,20 @@ func TestSparseConfFeeSource(t *testing.T) {
2: 42,
3: 54321,
}
testJSON := map[string]map[uint32]uint32{
"fee_by_block_target": testFees,
testMinRelayFee := SatPerKVByte(1000)
testResp := WebAPIResponse{
MinRelayFeerate: testMinRelayFee,
FeeByBlockTarget: testFees,
}
jsonResp, err := json.Marshal(testJSON)
jsonResp, err := json.Marshal(testResp)
require.NoError(t, err, "unable to marshal JSON API response")
reader := bytes.NewReader(jsonResp)
// Finally, ensure the expected map is returned without error.
fees, err := feeSource.parseResponse(reader)
resp, err := feeSource.parseResponse(reader)
require.NoError(t, err, "unable to parse API response")
require.Equal(t, testFees, fees, "unexpected fee map returned")
require.Equal(t, testResp, resp, "unexpected resp returned")
// Test parsing an improperly formatted JSON API response.
badFees := map[string]uint32{"hi": 12345, "hello": 42, "satoshi": 54321}
@@ -131,6 +134,45 @@ func TestSparseConfFeeSource(t *testing.T) {
require.Error(t, err, "expected error when parsing bad JSON")
}
// TestFeeSourceCompatibility checks that when a fee source doesn't return a
// `min_relay_feerate` field in its response, the floor feerate is used.
//
// NOTE: Field `min_relay_feerate` was added in v0.18.3.
func TestFeeSourceCompatibility(t *testing.T) {
t.Parallel()
// Test that GenQueryURL returns the URL as is.
url := "test"
feeSource := SparseConfFeeSource{URL: url}
// Test parsing a properly formatted JSON API response.
//
// Create the resp without the `min_relay_feerate` field.
testFees := map[uint32]uint32{
1: 12345,
}
testResp := struct {
// FeeByBlockTarget is a map of confirmation targets to sat/kvb
// fees.
FeeByBlockTarget map[uint32]uint32 `json:"fee_by_block_target"`
}{
FeeByBlockTarget: testFees,
}
jsonResp, err := json.Marshal(testResp)
require.NoError(t, err, "unable to marshal JSON API response")
reader := bytes.NewReader(jsonResp)
// Ensure the expected map is returned without error.
resp, err := feeSource.parseResponse(reader)
require.NoError(t, err, "unable to parse API response")
require.Equal(t, testResp.FeeByBlockTarget, resp.FeeByBlockTarget,
"unexpected resp returned")
// Expect the floor feerate to be used.
require.Equal(t, FeePerKwFloor.FeePerKVByte(), resp.MinRelayFeerate)
}
// TestWebAPIFeeEstimator checks that the WebAPIFeeEstimator returns fee rates
// as expected.
func TestWebAPIFeeEstimator(t *testing.T) {
@@ -194,14 +236,17 @@ func TestWebAPIFeeEstimator(t *testing.T) {
// This will create a `feeByBlockTarget` map with the following values,
// - 2: 4000 sat/kb
// - 6: 2000 sat/kb.
feeRateResp := map[uint32]uint32{
feeRates := map[uint32]uint32{
minTarget: maxFeeRate,
maxTarget: minFeeRate,
}
resp := WebAPIResponse{
FeeByBlockTarget: feeRates,
}
// Create a mock fee source and mock its returned map.
feeSource := &mockFeeSource{}
feeSource.On("GetFeeMap").Return(feeRateResp, nil)
feeSource.On("GetFeeInfo").Return(resp, nil)
estimator, _ := NewWebAPIEstimator(
feeSource, false, minFeeUpdateTimeout, maxFeeUpdateTimeout,
@@ -234,7 +279,7 @@ func TestWebAPIFeeEstimator(t *testing.T) {
exp := SatPerKVByte(tc.expectedFeeRate).FeePerKWeight()
require.Equalf(t, exp, est, "target %v failed, fee "+
"map is %v", tc.target, feeRateResp)
"map is %v", tc.target, feeRate)
})
}