lntemp: add utils and fee service
This commit adds several helper functions and a fee service. Note that the fee service is identical to what's inside `lntest`. Once the migration is done, the old file will be removed.
This commit is contained in:
2 files changed
+146
No files matched your search
@@ -0,0 +1,109 @@
|
|||||||
|
package lntemp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/lntest"
|
||||||
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
// feeService runs a web service that provides fee estimation information.
|
||||||
|
type feeService struct {
|
||||||
|
feeEstimates
|
||||||
|
|
||||||
|
t *testing.T
|
||||||
|
|
||||||
|
srv *http.Server
|
||||||
|
wg sync.WaitGroup
|
||||||
|
|
||||||
|
url string
|
||||||
|
|
||||||
|
lock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// feeEstimates contains the current fee estimates.
|
||||||
|
type feeEstimates struct {
|
||||||
|
Fees map[uint32]uint32 `json:"fee_by_block_target"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// startFeeService spins up a go-routine to serve fee estimates.
|
||||||
|
func startFeeService(t *testing.T) *feeService {
|
||||||
|
port := lntest.NextAvailablePort()
|
||||||
|
f := feeService{
|
||||||
|
t: t,
|
||||||
|
url: fmt.Sprintf(
|
||||||
|
"http://localhost:%v/fee-estimates.json", port,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize default fee estimate.
|
||||||
|
f.Fees = map[uint32]uint32{feeServiceTarget: 50000}
|
||||||
|
|
||||||
|
listenAddr := fmt.Sprintf(":%v", port)
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/fee-estimates.json", f.handleRequest)
|
||||||
|
|
||||||
|
f.srv = &http.Server{
|
||||||
|
Addr: listenAddr,
|
||||||
|
Handler: mux,
|
||||||
|
}
|
||||||
|
|
||||||
|
f.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer f.wg.Done()
|
||||||
|
|
||||||
|
if err := f.srv.ListenAndServe(); err != http.ErrServerClosed {
|
||||||
|
f.t.Errorf("error: cannot start fee api: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return &f
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleRequest handles a client request for fee estimates.
|
||||||
|
func (f *feeService) handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
|
f.lock.Lock()
|
||||||
|
defer f.lock.Unlock()
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(f.feeEstimates)
|
||||||
|
if err != nil {
|
||||||
|
f.t.Errorf("error: cannot serialize estimates: %v", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.WriteString(w, string(bytes))
|
||||||
|
if err != nil {
|
||||||
|
f.t.Errorf("error: cannot send estimates: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// stop stops the web server.
|
||||||
|
func (f *feeService) stop() {
|
||||||
|
if err := f.srv.Shutdown(context.Background()); err != nil {
|
||||||
|
f.t.Errorf("error: cannot stop fee api: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// setFee changes the current fee estimate for the fixed confirmation target.
|
||||||
|
func (f *feeService) setFee(fee chainfee.SatPerKWeight) {
|
||||||
|
f.lock.Lock()
|
||||||
|
defer f.lock.Unlock()
|
||||||
|
|
||||||
|
f.Fees[feeServiceTarget] = uint32(fee.FeePerKVByte())
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package lntemp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/lntest"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// NeutrinoBackendName is the name of the neutrino backend.
|
||||||
|
NeutrinoBackendName = "neutrino"
|
||||||
|
|
||||||
|
// TODO(yy): delete.
|
||||||
|
DefaultTimeout = lntest.DefaultTimeout
|
||||||
|
)
|
||||||
|
|
||||||
|
// CopyFile copies the file src to dest.
|
||||||
|
func CopyFile(dest, src string) error {
|
||||||
|
s, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
d, err := os.Create(dest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(d, s); err != nil {
|
||||||
|
d.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.Close()
|
||||||
|
}
|
||||||
Reference in new issue
Block a user