-
Notifications
You must be signed in to change notification settings - Fork 18
/
cln_lspd_node.go
159 lines (136 loc) · 3.99 KB
/
cln_lspd_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package itest
import (
"flag"
"fmt"
"path/filepath"
"sync"
"github.com/breez/lntest"
"github.com/breez/lspd/config"
"github.com/breez/lspd/notifications"
lspd "github.com/breez/lspd/rpc"
"github.com/btcsuite/btcd/btcec/v2"
ecies "github.com/ecies/go/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var clnPluginExec = flag.String(
"clnpluginexec", "", "full path to cln plugin wrapper binary",
)
type ClnLspNode struct {
harness *lntest.TestHarness
lightningNode *lntest.ClnNode
lspBase *lspBase
logFilePath string
runtime *clnLspNodeRuntime
mtx sync.Mutex
pluginBinary string
pluginAddress string
}
type clnLspNodeRuntime struct {
rpc lspd.ChannelOpenerClient
notificationRpc notifications.NotificationsClient
cleanups []*lntest.Cleanup
}
func NewClnLspdNode(h *lntest.TestHarness, m *lntest.Miner, mem *mempoolApi, name string, nodeConfig *config.NodeConfig) LspNode {
scriptDir := h.GetDirectory("lspd")
pluginBinary := *clnPluginExec
pluginPort, err := lntest.GetPort()
if err != nil {
h.T.Fatalf("failed to get port for the htlc interceptor plugin.")
}
pluginAddress := fmt.Sprintf("127.0.0.1:%d", pluginPort)
args := []string{
fmt.Sprintf("--plugin=%s", pluginBinary),
fmt.Sprintf("--lsp-listen=%s", pluginAddress),
fmt.Sprintf("--fee-base=%d", lspBaseFeeMsat),
fmt.Sprintf("--fee-per-satoshi=%d", lspFeeRatePpm),
fmt.Sprintf("--cltv-delta=%d", lspCltvDelta),
"--max-concurrent-htlcs=30",
"--dev-allowdustreserve=true",
"--developer",
}
lightningNode := lntest.NewClnNode(h, m, name, args...)
cln := &config.ClnConfig{
PluginAddress: pluginAddress,
GrpcAddress: fmt.Sprintf("localhost:%d", lightningNode.GrpcPort()),
CaCert: lightningNode.CaCertPath(),
ClientCert: lightningNode.ClientCertPath(),
ClientKey: lightningNode.ClientKeyPath(),
}
lspbase, err := newLspd(h, mem, name, nodeConfig, nil, cln)
if err != nil {
h.T.Fatalf("failed to initialize lspd")
}
logFilePath := filepath.Join(scriptDir, "lspd.log")
h.RegisterLogfile(logFilePath, fmt.Sprintf("lspd-%s", name))
lspNode := &ClnLspNode{
harness: h,
lightningNode: lightningNode,
logFilePath: logFilePath,
lspBase: lspbase,
pluginBinary: pluginBinary,
pluginAddress: pluginAddress,
}
h.AddStoppable(lspNode)
return lspNode
}
func (c *ClnLspNode) Start() {
c.mtx.Lock()
defer c.mtx.Unlock()
var cleanups []*lntest.Cleanup
c.lspBase.Start(c.lightningNode, func(nc *config.NodeConfig) {})
conn, err := grpc.Dial(
c.lspBase.grpcAddress,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithPerRPCCredentials(&token{token: "hello"}),
)
if err != nil {
lntest.PerformCleanup(cleanups)
c.harness.T.Fatalf("failed to create grpc connection: %v", err)
}
cleanups = append(cleanups, &lntest.Cleanup{
Name: fmt.Sprintf("%s: grpc conn", c.lspBase.name),
Fn: conn.Close,
})
client := lspd.NewChannelOpenerClient(conn)
notif := notifications.NewNotificationsClient(conn)
c.runtime = &clnLspNodeRuntime{
rpc: client,
notificationRpc: notif,
cleanups: cleanups,
}
}
func (c *ClnLspNode) Stop() error {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.runtime != nil {
lntest.PerformCleanup(c.runtime.cleanups)
}
c.lspBase.StopRuntime()
c.runtime = nil
return nil
}
func (c *ClnLspNode) Harness() *lntest.TestHarness {
return c.harness
}
func (c *ClnLspNode) PublicKey() *btcec.PublicKey {
return c.lspBase.pubkey
}
func (c *ClnLspNode) EciesPublicKey() *ecies.PublicKey {
return c.lspBase.eciesPubkey
}
func (c *ClnLspNode) Rpc() lspd.ChannelOpenerClient {
return c.runtime.rpc
}
func (c *ClnLspNode) NotificationsRpc() notifications.NotificationsClient {
return c.runtime.notificationRpc
}
func (l *ClnLspNode) NodeId() []byte {
return l.lightningNode.NodeId()
}
func (l *ClnLspNode) LightningNode() lntest.LightningNode {
return l.lightningNode
}
func (l *ClnLspNode) PostgresBackend() *PostgresContainer {
return l.lspBase.postgresBackend
}