Use local mempool api in tests

This commit is contained in:
Jesse de Wit
2023-05-12 10:54:21 +02:00
parent cad6540f42
commit aafb07e5f0
8 changed files with 113 additions and 14 deletions

View File

@@ -42,7 +42,7 @@ type clnLspNodeRuntime struct {
cleanups []*lntest.Cleanup cleanups []*lntest.Cleanup
} }
func NewClnLspdNode(h *lntest.TestHarness, m *lntest.Miner, name string, nodeConfig *config.NodeConfig) LspNode { func NewClnLspdNode(h *lntest.TestHarness, m *lntest.Miner, mem *mempoolApi, name string, nodeConfig *config.NodeConfig) LspNode {
scriptDir := h.GetDirectory("lspd") scriptDir := h.GetDirectory("lspd")
pluginBinary := *clnPluginExec pluginBinary := *clnPluginExec
pluginPort, err := lntest.GetPort() pluginPort, err := lntest.GetPort()
@@ -65,7 +65,7 @@ func NewClnLspdNode(h *lntest.TestHarness, m *lntest.Miner, name string, nodeCon
PluginAddress: pluginAddress, PluginAddress: pluginAddress,
SocketPath: filepath.Join(lightningNode.SocketDir(), lightningNode.SocketFile()), SocketPath: filepath.Join(lightningNode.SocketDir(), lightningNode.SocketFile()),
} }
lspbase, err := newLspd(h, name, nodeConfig, nil, cln) lspbase, err := newLspd(h, mem, name, nodeConfig, nil, cln)
if err != nil { if err != nil {
h.T.Fatalf("failed to initialize lspd") h.T.Fatalf("failed to initialize lspd")
} }

View File

@@ -19,7 +19,10 @@ func TestConfigParameters(t *testing.T) {
m := lntest.NewMiner(h) m := lntest.NewMiner(h)
m.Start() m.Start()
lsp := NewClnLspdNode(h, m, "lsp", nil) mem := NewMempoolApi(h)
mem.Start()
lsp := NewClnLspdNode(h, m, mem, "lsp", nil)
lsp.Start() lsp.Start()
log.Printf("Waiting %v to allow lsp server to activate.", htlcInterceptorDelay) log.Printf("Waiting %v to allow lsp server to activate.", htlcInterceptorDelay)

View File

@@ -38,7 +38,7 @@ type lndLspNodeRuntime struct {
cleanups []*lntest.Cleanup cleanups []*lntest.Cleanup
} }
func NewLndLspdNode(h *lntest.TestHarness, m *lntest.Miner, name string, nodeConfig *config.NodeConfig) LspNode { func NewLndLspdNode(h *lntest.TestHarness, m *lntest.Miner, mem *mempoolApi, name string, nodeConfig *config.NodeConfig) LspNode {
args := []string{ args := []string{
"--protocol.zero-conf", "--protocol.zero-conf",
"--protocol.option-scid-alias", "--protocol.option-scid-alias",
@@ -56,7 +56,7 @@ func NewLndLspdNode(h *lntest.TestHarness, m *lntest.Miner, name string, nodeCon
Cert: string(lightningNode.TlsCert()), Cert: string(lightningNode.TlsCert()),
Macaroon: hex.EncodeToString(lightningNode.Macaroon()), Macaroon: hex.EncodeToString(lightningNode.Macaroon()),
} }
lspBase, err := newLspd(h, name, nodeConfig, lnd, nil) lspBase, err := newLspd(h, mem, name, nodeConfig, lnd, nil)
if err != nil { if err != nil {
h.T.Fatalf("failed to initialize lspd") h.T.Fatalf("failed to initialize lspd")
} }

View File

@@ -60,7 +60,7 @@ type lspBase struct {
postgresBackend *PostgresContainer postgresBackend *PostgresContainer
} }
func newLspd(h *lntest.TestHarness, name string, nodeConfig *config.NodeConfig, lnd *config.LndConfig, cln *config.ClnConfig, envExt ...string) (*lspBase, error) { func newLspd(h *lntest.TestHarness, mem *mempoolApi, name string, nodeConfig *config.NodeConfig, lnd *config.LndConfig, cln *config.ClnConfig, envExt ...string) (*lspBase, error) {
scriptDir := h.GetDirectory(fmt.Sprintf("lspd-%s", name)) scriptDir := h.GetDirectory(fmt.Sprintf("lspd-%s", name))
log.Printf("%s: Creating LSPD in dir %s", name, scriptDir) log.Printf("%s: Creating LSPD in dir %s", name, scriptDir)
@@ -129,7 +129,7 @@ func newLspd(h *lntest.TestHarness, name string, nodeConfig *config.NodeConfig,
nodes, nodes,
fmt.Sprintf("DATABASE_URL=%s", postgresBackend.ConnectionString()), fmt.Sprintf("DATABASE_URL=%s", postgresBackend.ConnectionString()),
fmt.Sprintf("LISTEN_ADDRESS=%s", grpcAddress), fmt.Sprintf("LISTEN_ADDRESS=%s", grpcAddress),
"MEMPOOL_API_BASE_URL=https://mempool.space/api/v1/", fmt.Sprintf("MEMPOOL_API_BASE_URL=%s", mem.Address()),
"MEMPOOL_PRIORITY=economy", "MEMPOOL_PRIORITY=economy",
} }

View File

@@ -18,12 +18,12 @@ func TestLspd(t *testing.T) {
runTests(t, testCases, "CLN-lspd", clnLspFunc, clnClientFunc) runTests(t, testCases, "CLN-lspd", clnLspFunc, clnClientFunc)
} }
func lndLspFunc(h *lntest.TestHarness, m *lntest.Miner, c *config.NodeConfig) LspNode { func lndLspFunc(h *lntest.TestHarness, m *lntest.Miner, mem *mempoolApi, c *config.NodeConfig) LspNode {
return NewLndLspdNode(h, m, "lsp", c) return NewLndLspdNode(h, m, mem, "lsp", c)
} }
func clnLspFunc(h *lntest.TestHarness, m *lntest.Miner, c *config.NodeConfig) LspNode { func clnLspFunc(h *lntest.TestHarness, m *lntest.Miner, mem *mempoolApi, c *config.NodeConfig) LspNode {
return NewClnLspdNode(h, m, "lsp", c) return NewClnLspdNode(h, m, mem, "lsp", c)
} }
func lndClientFunc(h *lntest.TestHarness, m *lntest.Miner) BreezClient { func lndClientFunc(h *lntest.TestHarness, m *lntest.Miner) BreezClient {
@@ -72,10 +72,15 @@ func runTest(
log.Printf("Creating miner") log.Printf("Creating miner")
miner := lntest.NewMiner(h) miner := lntest.NewMiner(h)
miner.Start() miner.Start()
log.Printf("Creating mempool api")
mem := NewMempoolApi(h)
mem.Start()
log.Printf("Creating lsp") log.Printf("Creating lsp")
var lsp LspNode var lsp LspNode
if !testCase.skipCreateLsp { if !testCase.skipCreateLsp {
lsp = lspFunc(h, miner, nil) lsp = lspFunc(h, miner, mem, nil)
lsp.Start() lsp.Start()
} }
c := clientFunc(h, miner) c := clientFunc(h, miner)
@@ -85,6 +90,7 @@ func runTest(
t: t, t: t,
h: h, h: h,
m: miner, m: miner,
mem: mem,
c: c, c: c,
lsp: lsp, lsp: lsp,
lspFunc: lspFunc, lspFunc: lspFunc,

85
itest/mempool_api.go Normal file
View File

@@ -0,0 +1,85 @@
package itest
import (
"encoding/json"
"fmt"
"net"
"net/http"
"github.com/breez/lntest"
)
type RecommendedFeesResponse struct {
FastestFee float64 `json:"fastestFee"`
HalfHourFee float64 `json:"halfHourFee"`
HourFee float64 `json:"hourFee"`
EconomyFee float64 `json:"economyFee"`
MinimumFee float64 `json:"minimumFee"`
}
type mempoolApi struct {
addr string
h *lntest.TestHarness
fees *RecommendedFeesResponse
lis net.Listener
}
func NewMempoolApi(h *lntest.TestHarness) *mempoolApi {
port, err := lntest.GetPort()
if err != nil {
h.T.Fatalf("Failed to get port for mempool api: %v", err)
}
return &mempoolApi{
addr: fmt.Sprintf("127.0.0.1:%d", port),
h: h,
fees: &RecommendedFeesResponse{
MinimumFee: 1,
EconomyFee: 1,
HourFee: 1,
HalfHourFee: 1,
FastestFee: 1,
},
}
}
func (m *mempoolApi) Address() string {
return fmt.Sprintf("http://%s/api/v1/", m.addr)
}
func (m *mempoolApi) SetFees(fees *RecommendedFeesResponse) {
m.fees = fees
}
func (m *mempoolApi) Start() {
mux := http.NewServeMux()
mux.HandleFunc("/api/v1/fees/recommended", func(w http.ResponseWriter, r *http.Request) {
j, err := json.Marshal(m.fees)
if err != nil {
m.h.T.Fatalf("Failed to marshal mempool fees: %v", err)
}
_, err = w.Write(j)
if err != nil {
m.h.T.Fatalf("Failed to write mempool response: %v", err)
}
})
lis, err := net.Listen("tcp", m.addr)
if err != nil {
m.h.T.Fatalf("failed to start mempool api: %v", err)
}
m.lis = lis
m.h.AddStoppable(m)
go http.Serve(lis, mux)
}
func (m *mempoolApi) Stop() error {
lis := m.lis
if lis == nil {
return nil
}
m.lis = nil
return lis.Close()
}

View File

@@ -7,13 +7,14 @@ import (
"github.com/breez/lspd/config" "github.com/breez/lspd/config"
) )
type LspFunc func(h *lntest.TestHarness, m *lntest.Miner, c *config.NodeConfig) LspNode type LspFunc func(h *lntest.TestHarness, m *lntest.Miner, mem *mempoolApi, c *config.NodeConfig) LspNode
type ClientFunc func(h *lntest.TestHarness, m *lntest.Miner) BreezClient type ClientFunc func(h *lntest.TestHarness, m *lntest.Miner) BreezClient
type testParams struct { type testParams struct {
t *testing.T t *testing.T
h *lntest.TestHarness h *lntest.TestHarness
m *lntest.Miner m *lntest.Miner
mem *mempoolApi
c BreezClient c BreezClient
lsp LspNode lsp LspNode
lspFunc LspFunc lspFunc LspFunc
@@ -28,6 +29,10 @@ func (h *testParams) Miner() *lntest.Miner {
return h.m return h.m
} }
func (h *testParams) Mempool() *mempoolApi {
return h.mem
}
func (h *testParams) Lsp() LspNode { func (h *testParams) Lsp() LspNode {
return h.lsp return h.lsp
} }

View File

@@ -16,7 +16,7 @@ func testOpenZeroConfUtxo(p *testParams) {
alice.Fund(10000000) alice.Fund(10000000)
minConfs := uint32(0) minConfs := uint32(0)
lsp := p.lspFunc(p.h, p.m, &config.NodeConfig{MinConfs: &minConfs}) lsp := p.lspFunc(p.h, p.m, p.mem, &config.NodeConfig{MinConfs: &minConfs})
lsp.Start() lsp.Start()
log.Print("Opening channel between Alice and the lsp") log.Print("Opening channel between Alice and the lsp")