mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-18 22:34:22 +01:00
move nodes initialization to main
This commit is contained in:
77
main.go
77
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -22,6 +23,7 @@ import (
|
||||
"github.com/breez/lspd/postgresql"
|
||||
"github.com/breez/lspd/shared"
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
ecies "github.com/ecies/go/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -45,7 +47,12 @@ func main() {
|
||||
log.Fatalf("need at least one node configured in NODES.")
|
||||
}
|
||||
|
||||
nodesService, err := shared.NewNodesService(nodeConfigs)
|
||||
nodes, err := initializeNodes(nodeConfigs)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to initialize nodes: %v", err)
|
||||
}
|
||||
|
||||
nodesService, err := shared.NewNodesService(nodes)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create nodes service: %v", err)
|
||||
}
|
||||
@@ -92,7 +99,6 @@ func main() {
|
||||
|
||||
openingService := shared.NewOpeningService(interceptStore, nodesService)
|
||||
var interceptors []interceptor.HtlcInterceptor
|
||||
nodes := nodesService.GetNodes()
|
||||
for _, node := range nodes {
|
||||
var htlcInterceptor interceptor.HtlcInterceptor
|
||||
if node.NodeConfig.Lnd != nil {
|
||||
@@ -205,3 +211,70 @@ func main() {
|
||||
wg.Wait()
|
||||
log.Printf("lspd exited")
|
||||
}
|
||||
|
||||
func initializeNodes(configs []*config.NodeConfig) ([]*shared.Node, error) {
|
||||
if len(configs) == 0 {
|
||||
return nil, fmt.Errorf("no nodes supplied")
|
||||
}
|
||||
|
||||
nodes := []*shared.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 := &shared.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
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/breez/lspd/cln"
|
||||
"github.com/breez/lspd/config"
|
||||
"github.com/breez/lspd/lightning"
|
||||
"github.com/breez/lspd/lnd"
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
ecies "github.com/ecies/go/v2"
|
||||
"golang.org/x/sync/singleflight"
|
||||
@@ -22,6 +19,7 @@ type Node struct {
|
||||
EciesPrivateKey *ecies.PrivateKey
|
||||
EciesPublicKey *ecies.PublicKey
|
||||
OpenChannelReqGroup singleflight.Group
|
||||
Tokens []string
|
||||
}
|
||||
|
||||
type NodesService interface {
|
||||
@@ -33,69 +31,10 @@ type nodesService struct {
|
||||
nodeLookup map[string]*Node
|
||||
}
|
||||
|
||||
func NewNodesService(configs []*config.NodeConfig) (NodesService, error) {
|
||||
if len(configs) == 0 {
|
||||
return nil, fmt.Errorf("no nodes supplied")
|
||||
}
|
||||
|
||||
nodes := []*Node{}
|
||||
func NewNodesService(nodes []*Node) (NodesService, error) {
|
||||
nodeLookup := make(map[string]*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 := &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
|
||||
}
|
||||
|
||||
nodes = append(nodes, node)
|
||||
for _, token := range config.Tokens {
|
||||
for _, node := range nodes {
|
||||
for _, token := range node.Tokens {
|
||||
_, exists := nodeLookup[token]
|
||||
if exists {
|
||||
return nil, fmt.Errorf("cannot have multiple nodes with the same token")
|
||||
|
||||
Reference in New Issue
Block a user