forked from breez/lspd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
305 lines (260 loc) · 8.82 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/breez/lspd/chain"
"github.com/breez/lspd/cln"
"github.com/breez/lspd/common"
"github.com/breez/lspd/config"
"github.com/breez/lspd/interceptor"
"github.com/breez/lspd/lnd"
"github.com/breez/lspd/lsps0"
"github.com/breez/lspd/lsps2"
"github.com/breez/lspd/mempool"
"github.com/breez/lspd/notifications"
"github.com/breez/lspd/postgresql"
"github.com/btcsuite/btcd/btcec/v2"
ecies "github.com/ecies/go/v2"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "genkey" {
p, err := btcec.NewPrivateKey()
if err != nil {
log.Fatalf("btcec.NewPrivateKey() error: %v", err)
}
fmt.Printf("LSPD_PRIVATE_KEY=\"%x\"\n", p.Serialize())
return
}
n := os.Getenv("NODES")
var nodeConfigs []*config.NodeConfig
err := json.Unmarshal([]byte(n), &nodeConfigs)
if err != nil {
log.Fatalf("failed to unmarshal NODES env: %v", err)
}
if len(nodeConfigs) == 0 {
log.Fatalf("need at least one node configured in NODES.")
}
nodes, err := initializeNodes(nodeConfigs)
if err != nil {
log.Fatalf("failed to initialize nodes: %v", err)
}
nodesService, err := common.NewNodesService(nodes)
if err != nil {
log.Fatalf("failed to create nodes service: %v", err)
}
mempoolUrl := os.Getenv("MEMPOOL_API_BASE_URL")
if mempoolUrl == "" {
log.Fatalf("No mempool url configured.")
}
feeEstimator, err := mempool.NewMempoolClient(mempoolUrl)
if err != nil {
log.Fatalf("failed to initialize mempool client: %v", err)
}
var feeStrategy chain.FeeStrategy
envFeeStrategy := os.Getenv("MEMPOOL_PRIORITY")
switch strings.ToLower(envFeeStrategy) {
case "minimum":
feeStrategy = chain.FeeStrategyMinimum
case "economy":
feeStrategy = chain.FeeStrategyEconomy
case "hour":
feeStrategy = chain.FeeStrategyHour
case "halfhour":
feeStrategy = chain.FeeStrategyHalfHour
case "fastest":
feeStrategy = chain.FeeStrategyFastest
default:
feeStrategy = chain.FeeStrategyEconomy
}
log.Printf("using mempool api for fee estimation: %v, fee strategy: %v:%v", mempoolUrl, envFeeStrategy, feeStrategy)
databaseUrl := os.Getenv("DATABASE_URL")
pool, err := postgresql.PgConnect(databaseUrl)
if err != nil {
log.Fatalf("pgConnect() error: %v", err)
}
interceptStore := postgresql.NewPostgresInterceptStore(pool)
openingStore := postgresql.NewPostgresOpeningStore(pool)
forwardingStore := postgresql.NewForwardingEventStore(pool)
notificationsStore := postgresql.NewNotificationsStore(pool)
lsps2Store := postgresql.NewLsps2Store(pool)
ctx, cancel := context.WithCancel(context.Background())
notificationService := notifications.NewNotificationService(notificationsStore)
go notificationService.Start(ctx)
openingService := common.NewOpeningService(openingStore, nodesService)
lsps2CleanupService := lsps2.NewCleanupService(lsps2Store)
go lsps2CleanupService.Start(ctx)
notificationCleanupService := notifications.NewCleanupService(notificationsStore)
go notificationCleanupService.Start(ctx)
var interceptors []interceptor.HtlcInterceptor
for _, node := range nodes {
var htlcInterceptor interceptor.HtlcInterceptor
if node.NodeConfig.Lnd != nil {
client, err := lnd.NewLndClient(node.NodeConfig.Lnd)
if err != nil {
log.Fatalf("failed to initialize LND client: %v", err)
}
client.StartListeners()
fwsync := lnd.NewForwardingHistorySync(client, interceptStore, forwardingStore)
interceptor := interceptor.NewInterceptHandler(client, node.NodeConfig, interceptStore, openingService, feeEstimator, feeStrategy, notificationService)
htlcInterceptor, err = lnd.NewLndHtlcInterceptor(node.NodeConfig, client, fwsync, interceptor)
if err != nil {
log.Fatalf("failed to initialize LND interceptor: %v", err)
}
}
if node.NodeConfig.Cln != nil {
client, err := cln.NewClnClient(node.NodeConfig.Cln.SocketPath)
if err != nil {
log.Fatalf("failed to initialize CLN client: %v", err)
}
legacyHandler := interceptor.NewInterceptHandler(client, node.NodeConfig, interceptStore, openingService, feeEstimator, feeStrategy, notificationService)
lsps2Handler := lsps2.NewInterceptHandler(lsps2Store, openingService, client, feeEstimator, &lsps2.InterceptorConfig{
AdditionalChannelCapacitySat: uint64(node.NodeConfig.AdditionalChannelCapacity),
MinConfs: node.NodeConfig.MinConfs,
TargetConf: node.NodeConfig.TargetConf,
FeeStrategy: feeStrategy,
MinPaymentSizeMsat: node.NodeConfig.MinPaymentSizeMsat,
MaxPaymentSizeMsat: node.NodeConfig.MaxPaymentSizeMsat,
TimeLockDelta: node.NodeConfig.TimeLockDelta,
HtlcMinimumMsat: node.NodeConfig.MinHtlcMsat,
MppTimeout: time.Second * 90,
})
go lsps2Handler.Start(ctx)
combinedHandler := common.NewCombinedHandler(lsps2Handler, legacyHandler)
htlcInterceptor, err = cln.NewClnHtlcInterceptor(node.NodeConfig, client, combinedHandler)
if err != nil {
log.Fatalf("failed to initialize CLN interceptor: %v", err)
}
msgClient := cln.NewCustomMsgClient(node.NodeConfig.Cln, client)
go msgClient.Start()
msgServer := lsps0.NewServer()
protocolServer := lsps0.NewProtocolServer([]uint32{2})
lsps2Server := lsps2.NewLsps2Server(openingService, nodesService, node, lsps2Store)
lsps0.RegisterProtocolServer(msgServer, protocolServer)
lsps2.RegisterLsps2Server(msgServer, lsps2Server)
msgClient.WaitStarted()
defer msgClient.Stop()
go msgServer.Serve(msgClient)
}
if htlcInterceptor == nil {
log.Fatalf("node has to be either cln or lnd")
}
interceptors = append(interceptors, htlcInterceptor)
}
address := os.Getenv("LISTEN_ADDRESS")
certMagicDomain := os.Getenv("CERTMAGIC_DOMAIN")
cs := NewChannelOpenerServer(interceptStore, openingService)
ns := notifications.NewNotificationsServer(notificationsStore)
s, err := NewGrpcServer(nodesService, address, certMagicDomain, cs, ns)
if err != nil {
log.Fatalf("failed to initialize grpc server: %v", err)
}
var wg sync.WaitGroup
wg.Add(len(interceptors) + 1)
stopInterceptors := func() {
for _, interceptor := range interceptors {
interceptor.Stop()
}
cancel()
}
for _, interceptor := range interceptors {
i := interceptor
go func() {
err := i.Start()
if err == nil {
log.Printf("Interceptor stopped.")
} else {
log.Printf("FATAL. Interceptor stopped with error: %v", err)
}
wg.Done()
// If any interceptor stops, stop everything, so we're able to restart using systemd.
s.Stop()
stopInterceptors()
}()
}
go func() {
err := s.Start()
if err == nil {
log.Printf("GRPC server stopped.")
} else {
log.Printf("FATAL. GRPC server stopped with error: %v", err)
}
wg.Done()
// If the server stops, stop everything else, so we're able to restart using systemd.
stopInterceptors()
}()
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-c
log.Printf("Received stop signal %v. Stopping.", sig)
// Stop everything gracefully on stop signal
s.Stop()
stopInterceptors()
}()
wg.Wait()
log.Printf("lspd exited")
}
func initializeNodes(configs []*config.NodeConfig) ([]*common.Node, error) {
if len(configs) == 0 {
return nil, fmt.Errorf("no nodes supplied")
}
nodes := []*common.Node{}
for _, config := range configs {
pk, err := hex.DecodeString(config.LspdPrivateKey)
if err != nil {
return nil, fmt.Errorf("hex.DecodeString(config.lspdPrivateKey=%v) error: %v", config.LspdPrivateKey, err)
}
eciesPrivateKey := ecies.NewPrivateKeyFromBytes(pk)
eciesPublicKey := eciesPrivateKey.PublicKey
privateKey, publicKey := btcec.PrivKeyFromBytes(pk)
node := &common.Node{
NodeConfig: config,
PrivateKey: privateKey,
PublicKey: publicKey,
EciesPrivateKey: eciesPrivateKey,
EciesPublicKey: eciesPublicKey,
}
if config.Lnd == nil && config.Cln == nil {
return nil, fmt.Errorf("node has to be either cln or lnd")
}
if config.Lnd != nil && config.Cln != nil {
return nil, fmt.Errorf("node cannot be both cln and lnd")
}
if config.Lnd != nil {
node.Client, err = lnd.NewLndClient(config.Lnd)
if err != nil {
return nil, err
}
}
if config.Cln != nil {
node.Client, err = cln.NewClnClient(config.Cln.SocketPath)
if err != nil {
return nil, err
}
}
// Make sure the nodes is available and set name and pubkey if not set
// in config.
info, err := node.Client.GetInfo()
if err != nil {
return nil, fmt.Errorf("failed to get info from host %s", node.NodeConfig.Host)
}
if node.NodeConfig.Name == "" {
node.NodeConfig.Name = info.Alias
}
if node.NodeConfig.NodePubkey == "" {
node.NodeConfig.NodePubkey = info.Pubkey
}
node.Tokens = config.Tokens
nodes = append(nodes, node)
}
return nodes, nil
}