lnd.xprv/lnwallet/fee_estimator_test.go
Valentine Wallace 4944eb3e54 lnwallet: add implementation of WebApiFeeSource for external APIs
This enables users to specify an external API for fee estimation.
The API is expected to return fees in the JSON format:
`{
	fee_by_block_target: {
	   a: x,
	   b: y,
	   ...
	   c: z
        }
 }`
 where a, b, c are block targets and x, y, z are fees in sat/kb.
 Note that a, b, c need not be contiguous.
2019-04-16 13:50:40 -07:00

147 lines
4.2 KiB
Go

package lnwallet_test
import (
"bytes"
"encoding/json"
"reflect"
"testing"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lnwallet"
)
// TestFeeRateTypes checks that converting fee rates between the
// different types that represent fee rates and calculating fees
// work as expected.
func TestFeeRateTypes(t *testing.T) {
t.Parallel()
// We'll be calculating the transaction fees for the given measurements
// using different fee rates and expecting them to match.
const vsize = 300
const weight = vsize * 4
// Test the conversion from sat/kw to sat/kb.
for feePerKw := lnwallet.SatPerKWeight(250); feePerKw < 10000; feePerKw += 50 {
feePerKB := feePerKw.FeePerKVByte()
if feePerKB != lnwallet.SatPerKVByte(feePerKw*4) {
t.Fatalf("expected %d sat/kb, got %d sat/kb when "+
"converting from %d sat/kw", feePerKw*4,
feePerKB, feePerKw)
}
// The resulting transaction fee should be the same when using
// both rates.
expectedFee := btcutil.Amount(feePerKw * weight / 1000)
fee1 := feePerKw.FeeForWeight(weight)
if fee1 != expectedFee {
t.Fatalf("expected fee of %d sats, got %d sats",
expectedFee, fee1)
}
fee2 := feePerKB.FeeForVSize(vsize)
if fee2 != expectedFee {
t.Fatalf("expected fee of %d sats, got %d sats",
expectedFee, fee2)
}
}
// Test the conversion from sat/kb to sat/kw.
for feePerKB := lnwallet.SatPerKVByte(1000); feePerKB < 40000; feePerKB += 1000 {
feePerKw := feePerKB.FeePerKWeight()
if feePerKw != lnwallet.SatPerKWeight(feePerKB/4) {
t.Fatalf("expected %d sat/kw, got %d sat/kw when "+
"converting from %d sat/kb", feePerKB/4,
feePerKw, feePerKB)
}
// The resulting transaction fee should be the same when using
// both rates.
expectedFee := btcutil.Amount(feePerKB * vsize / 1000)
fee1 := feePerKB.FeeForVSize(vsize)
if fee1 != expectedFee {
t.Fatalf("expected fee of %d sats, got %d sats",
expectedFee, fee1)
}
fee2 := feePerKw.FeeForWeight(weight)
if fee2 != expectedFee {
t.Fatalf("expected fee of %d sats, got %d sats",
expectedFee, fee2)
}
}
}
// TestStaticFeeEstimator checks that the StaticFeeEstimator returns the
// expected fee rate.
func TestStaticFeeEstimator(t *testing.T) {
t.Parallel()
const feePerKw = lnwallet.FeePerKwFloor
feeEstimator := lnwallet.NewStaticFeeEstimator(feePerKw, 0)
if err := feeEstimator.Start(); err != nil {
t.Fatalf("unable to start fee estimator: %v", err)
}
defer feeEstimator.Stop()
feeRate, err := feeEstimator.EstimateFeePerKW(6)
if err != nil {
t.Fatalf("unable to get fee rate: %v", err)
}
if feeRate != feePerKw {
t.Fatalf("expected fee rate %v, got %v", feePerKw, feeRate)
}
}
// TestSparseConfFeeSource checks that SparseConfFeeSource generates URLs and
// parses API responses as expected.
func TestSparseConfFeeSource(t *testing.T) {
t.Parallel()
// Test that GenQueryURL returns the URL as is.
url := "test"
feeSource := lnwallet.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.
// First, create the response as a bytes.Reader.
testFees := map[uint32]uint32{
1: 12345,
2: 42,
3: 54321,
}
testJSON := map[string]map[uint32]uint32{"fee_by_block_target": testFees}
jsonResp, err := json.Marshal(testJSON)
if err != nil {
t.Fatalf("unable to marshal JSON API response: %v", err)
}
reader := bytes.NewReader(jsonResp)
// Finally, ensure the expected map is returned without error.
fees, err := feeSource.ParseResponse(reader)
if err != nil {
t.Fatalf("unable to parse API response: %v", err)
}
if !reflect.DeepEqual(fees, testFees) {
t.Fatalf("expected %v, got %v", testFees, fees)
}
// Test parsing an improperly formatted JSON API response.
badFees := map[string]uint32{"hi": 12345, "hello": 42, "satoshi": 54321}
badJSON := map[string]map[string]uint32{"fee_by_block_target": badFees}
jsonResp, err = json.Marshal(badJSON)
if err != nil {
t.Fatalf("unable to marshal JSON API response: %v", err)
}
reader = bytes.NewReader(jsonResp)
// Finally, ensure the improperly formatted fees error.
_, err = feeSource.ParseResponse(reader)
if err == nil {
t.Fatalf("expected ParseResponse to fail")
}
}