initial commit
This commit is contained in:
commit
fa44705673
13 changed files with 1908 additions and 0 deletions
81
lnd/lnd.go
Normal file
81
lnd/lnd.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package lnd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/lightninglabs/lndclient"
|
||||
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func convertChannelIDToString(channelID uint64) string {
|
||||
blockHeight := channelID >> 40
|
||||
blockIndex := (channelID >> 16) & ((1 << 24) - 1)
|
||||
outputIndex := channelID & ((1 << 16) - 1)
|
||||
return fmt.Sprintf("%d:%d:%d", blockHeight, blockIndex, outputIndex)
|
||||
}
|
||||
|
||||
func ConnectToLND() (*lndclient.GrpcLndServices, error) {
|
||||
config := lndclient.LndServicesConfig{
|
||||
LndAddress: os.Getenv("LND_ADDRESS"),
|
||||
Network: lndclient.Network(os.Getenv("LND_NETWORK")),
|
||||
CustomMacaroonPath: os.Getenv("LND_MACAROON_PATH"),
|
||||
TLSPath: os.Getenv("LND_TLS_CERT_PATH"),
|
||||
}
|
||||
return lndclient.NewLndServices(&config)
|
||||
}
|
||||
|
||||
func writeNodesToMemgraph(session neo4j.Session, nodes []lndclient.Node) {
|
||||
for _, node := range nodes {
|
||||
query := "MERGE (n:Node {pub_key: $pubKey, alias: $alias})"
|
||||
params := map[string]interface{}{
|
||||
"pubKey": node.PubKey.String(),
|
||||
"alias": node.Alias,
|
||||
}
|
||||
_, err := session.Run(query, params)
|
||||
if err != nil {
|
||||
log.Printf("Failed to execute node query: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeChannelsToMemgraph(session neo4j.Session, edges []lndclient.ChannelEdge) {
|
||||
for _, edge := range edges {
|
||||
chanID := convertChannelIDToString(edge.ChannelID) // Convert uint64 to string format
|
||||
if edge.Node1Policy != nil && !edge.Node1Policy.Disabled {
|
||||
writeChannelPolicyToMemgraph(session, &edge, edge.Node1Policy, edge.Node1.String(), edge.Node2.String(), chanID)
|
||||
}
|
||||
if edge.Node2Policy != nil && !edge.Node2Policy.Disabled {
|
||||
writeChannelPolicyToMemgraph(session, &edge, edge.Node2Policy, edge.Node2.String(), edge.Node1.String(), chanID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeChannelPolicyToMemgraph(session neo4j.Session, edge *lndclient.ChannelEdge, policy *lndclient.RoutingPolicy, node1PubKey, node2PubKey, chanID string) {
|
||||
if policy != nil {
|
||||
query := fmt.Sprintf(`
|
||||
MATCH (a:Node {pub_key: '%s'}), (b:Node {pub_key: '%s'})
|
||||
MERGE (a)-[r:CHANNEL {channel_id: '%s', capacity: %d}]->(b)
|
||||
SET r.fee_base_msat = %d, r.fee_rate_milli_msat = %d, r.time_lock_delta = %d, r.disabled = %v
|
||||
`, node1PubKey, node2PubKey, chanID, edge.Capacity,
|
||||
policy.FeeBaseMsat, policy.FeeRateMilliMsat, policy.TimeLockDelta, policy.Disabled)
|
||||
_, err := session.Run(query, nil)
|
||||
if err != nil {
|
||||
log.Printf("Failed to execute channel policy query: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WriteGraphToMemgraph(lndServices *lndclient.GrpcLndServices, neo4jDriver neo4j.Driver) {
|
||||
session := neo4jDriver.NewSession(neo4j.SessionConfig{})
|
||||
defer session.Close()
|
||||
|
||||
graph, err := lndServices.Client.DescribeGraph(context.Background(), false)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to retrieve graph: %v", err)
|
||||
}
|
||||
|
||||
writeNodesToMemgraph(session, graph.Nodes)
|
||||
writeChannelsToMemgraph(session, graph.Edges)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue