2022-07-22 02:41:08 +02:00
|
|
|
package lntemp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2022-08-16 11:31:20 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-07-22 02:41:08 +02:00
|
|
|
)
|
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
// WebFeeService defines an interface that's used to provide fee estimation
|
|
|
|
// service used in the integration tests. It must provide an URL so that a lnd
|
|
|
|
// node can be started with the flag `--feeurl` and uses the customized fee
|
|
|
|
// estimator.
|
|
|
|
type WebFeeService interface {
|
|
|
|
// Start starts the service.
|
|
|
|
Start() error
|
|
|
|
|
|
|
|
// Stop stops the service.
|
|
|
|
Stop() error
|
|
|
|
|
|
|
|
// URL returns the service's endpoint.
|
|
|
|
URL() string
|
|
|
|
|
|
|
|
// SetFeeRate sets the estimated fee rate for a given confirmation
|
|
|
|
// target.
|
|
|
|
SetFeeRate(feeRate chainfee.SatPerKWeight, conf uint32)
|
|
|
|
}
|
|
|
|
|
2022-07-22 02:41:08 +02:00
|
|
|
const (
|
|
|
|
// feeServiceTarget is the confirmation target for which a fee estimate
|
|
|
|
// is returned. Requests for higher confirmation targets will fall back
|
|
|
|
// to this.
|
|
|
|
feeServiceTarget = 1
|
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
// DefaultFeeRateSatPerKw specifies the default fee rate used in the
|
|
|
|
// tests.
|
|
|
|
DefaultFeeRateSatPerKw = 12500
|
|
|
|
)
|
2022-07-22 02:41:08 +02:00
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
// FeeService runs a web service that provides fee estimation information.
|
|
|
|
type FeeService struct {
|
|
|
|
*testing.T
|
2022-07-22 02:41:08 +02:00
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
feeRateMap map[uint32]uint32
|
|
|
|
url string
|
2022-07-22 02:41:08 +02:00
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
srv *http.Server
|
|
|
|
wg sync.WaitGroup
|
2022-07-22 02:41:08 +02:00
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
// Compile-time check for the WebFeeService interface.
|
|
|
|
var _ WebFeeService = (*FeeService)(nil)
|
2022-07-22 02:41:08 +02:00
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
// Start spins up a go-routine to serve fee estimates.
|
|
|
|
func NewFeeService(t *testing.T) *FeeService {
|
2022-07-22 02:41:08 +02:00
|
|
|
port := lntest.NextAvailablePort()
|
2022-08-16 11:31:20 +02:00
|
|
|
f := FeeService{
|
|
|
|
T: t,
|
2022-07-22 02:41:08 +02:00
|
|
|
url: fmt.Sprintf(
|
|
|
|
"http://localhost:%v/fee-estimates.json", port,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize default fee estimate.
|
2022-08-16 11:31:20 +02:00
|
|
|
f.feeRateMap = map[uint32]uint32{
|
|
|
|
feeServiceTarget: DefaultFeeRateSatPerKw,
|
|
|
|
}
|
2022-07-22 02:41:08 +02:00
|
|
|
|
|
|
|
listenAddr := fmt.Sprintf(":%v", port)
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/fee-estimates.json", f.handleRequest)
|
|
|
|
|
|
|
|
f.srv = &http.Server{
|
|
|
|
Addr: listenAddr,
|
|
|
|
Handler: mux,
|
|
|
|
}
|
2022-08-16 11:31:20 +02:00
|
|
|
return &f
|
|
|
|
}
|
2022-07-22 02:41:08 +02:00
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
// Start starts the web server.
|
|
|
|
func (f *FeeService) Start() error {
|
2022-07-22 02:41:08 +02:00
|
|
|
f.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer f.wg.Done()
|
|
|
|
|
|
|
|
if err := f.srv.ListenAndServe(); err != http.ErrServerClosed {
|
2022-08-16 11:31:20 +02:00
|
|
|
require.NoErrorf(f, err, "cannot start fee api")
|
2022-07-22 02:41:08 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
return nil
|
2022-07-22 02:41:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleRequest handles a client request for fee estimates.
|
2022-08-16 11:31:20 +02:00
|
|
|
func (f *FeeService) handleRequest(w http.ResponseWriter, r *http.Request) {
|
2022-07-22 02:41:08 +02:00
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
bytes, err := json.Marshal(
|
|
|
|
struct {
|
|
|
|
Fees map[uint32]uint32 `json:"fee_by_block_target"`
|
|
|
|
}{
|
|
|
|
Fees: f.feeRateMap,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoErrorf(f, err, "cannot serialize estimates")
|
2022-07-22 02:41:08 +02:00
|
|
|
|
|
|
|
_, err = io.WriteString(w, string(bytes))
|
2022-08-16 11:31:20 +02:00
|
|
|
require.NoError(f, err, "cannot send estimates")
|
2022-07-22 02:41:08 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
// Stop stops the web server.
|
|
|
|
func (f *FeeService) Stop() error {
|
|
|
|
err := f.srv.Shutdown(context.Background())
|
|
|
|
require.NoError(f, err, "cannot stop fee api")
|
2022-07-22 02:41:08 +02:00
|
|
|
|
|
|
|
f.wg.Wait()
|
2022-08-16 11:31:20 +02:00
|
|
|
return nil
|
2022-07-22 02:41:08 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
// SetFeeRate sets a fee for the given confirmation target.
|
|
|
|
func (f *FeeService) SetFeeRate(fee chainfee.SatPerKWeight, conf uint32) {
|
2022-07-22 02:41:08 +02:00
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
|
2022-08-16 11:31:20 +02:00
|
|
|
f.feeRateMap[conf] = uint32(fee.FeePerKVByte())
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL returns the service endpoint.
|
|
|
|
func (f *FeeService) URL() string {
|
|
|
|
return f.url
|
2022-07-22 02:41:08 +02:00
|
|
|
}
|