add local snapshot to repo; add Dockerfile; update docker-compose.yml;
This commit is contained in:
parent
bc74ef9cbd
commit
d55d7832bc
10 changed files with 2471532 additions and 2 deletions
122
lnd/lnd.go
122
lnd/lnd.go
|
@ -2,9 +2,11 @@ package lnd
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/lightninglabs/lndclient"
|
||||
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
@ -28,6 +30,56 @@ func ConnectToLND() (*lndclient.GrpcLndServices, error) {
|
|||
return lndclient.NewLndServices(&config)
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Pub_Key string
|
||||
|
||||
LastUpdate time.Time
|
||||
|
||||
Alias string
|
||||
|
||||
Color string
|
||||
|
||||
Features map[string]interface{}
|
||||
|
||||
Addresses []interface{}
|
||||
}
|
||||
|
||||
type ChannelEdge struct {
|
||||
ChannelId uint64 `json:"channel_id,string"`
|
||||
|
||||
Capacity string `json:"capacity"`
|
||||
|
||||
Node1_Pub string `json:"node1_pub"`
|
||||
|
||||
Node2_Pub string `json:"node2_pub"`
|
||||
|
||||
Node1Policy RoutingPolicy `json:"node1_policy,omitempty"`
|
||||
|
||||
Node2Policy RoutingPolicy `json:"node2_policy,omitempty"`
|
||||
}
|
||||
|
||||
// RoutingPolicy holds the edge routing policy for a channel edge.
|
||||
type RoutingPolicy struct {
|
||||
TimeLockDelta int `json:"time_lock_delta"`
|
||||
MinHtlc string `json:"min_htlc"`
|
||||
FeeBaseMsat string `json:"fee_base_msat"`
|
||||
FeeRateMilliMsat string `json:"fee_rate_milli_msat"`
|
||||
Disabled bool `json:"disabled"`
|
||||
MaxHtlcMsat string `json:"max_htlc_msat"`
|
||||
LastUpdate int `json:"last_update"`
|
||||
CustomRecords struct {
|
||||
} `json:"custom_records"`
|
||||
}
|
||||
|
||||
// Graph describes our view of the graph.
|
||||
type Graph struct {
|
||||
// Nodes is the set of nodes in the channel graph.
|
||||
Nodes []Node
|
||||
|
||||
// Edges is the set of edges in the channel graph.
|
||||
Edges []ChannelEdge
|
||||
}
|
||||
|
||||
func writeNodesToMemgraph(session neo4j.Session, nodes []lndclient.Node) {
|
||||
const batchSize = 100
|
||||
|
||||
|
@ -193,3 +245,73 @@ func WriteGraphToMemgraph(graph *lndclient.Graph, neo4jDriver neo4j.Driver) {
|
|||
writeChannelsToMemgraph(session, graph.Edges)
|
||||
fmt.Println("Finished writing to Memgraph...")
|
||||
}
|
||||
|
||||
func WriteSnapshotToMemgraph(snapshotFilename string, neo4jDriver neo4j.Driver) {
|
||||
var err error
|
||||
session := neo4jDriver.NewSession(neo4j.SessionConfig{})
|
||||
defer session.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to memgraph: %v", err)
|
||||
}
|
||||
|
||||
jsonFile, err := os.Open(snapshotFilename)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open snapshot: %v", err)
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
|
||||
byteValue, _ := io.ReadAll(jsonFile)
|
||||
var graph Graph
|
||||
err = json.Unmarshal(byteValue, &graph)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open snapshot: %v", err)
|
||||
}
|
||||
|
||||
fmt.Println("Writing to Memgraph...")
|
||||
createNodeIndex(session)
|
||||
createIndexForChannels(session)
|
||||
writeSnapshotNodesToMemgraph(session, graph.Nodes)
|
||||
writeSnapshotChannelsToMemgraph(session, graph.Edges)
|
||||
fmt.Println("Finished writing to Memgraph...")
|
||||
}
|
||||
|
||||
func writeSnapshotNodesToMemgraph(session neo4j.Session, nodes []Node) {
|
||||
for _, node := range nodes {
|
||||
_, is_wumbo := node.Features["19"]
|
||||
|
||||
query := "MERGE (n:node {pubkey: $pubKey, alias: $alias, is_wumbo: $is_wumbo})"
|
||||
params := map[string]interface{}{
|
||||
"pubKey": node.Pub_Key,
|
||||
"alias": node.Alias,
|
||||
"is_wumbo": is_wumbo,
|
||||
}
|
||||
_, err := session.Run(query, params)
|
||||
if err != nil {
|
||||
log.Printf("Failed to execute node query: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeSnapshotChannelsToMemgraph(session neo4j.Session, edges []ChannelEdge) {
|
||||
for _, edge := range edges {
|
||||
chanID := convertChannelIDToString(edge.ChannelId) // Convert uint64 to string format
|
||||
writeChannelPolicyToMemgraphSnapshot(session, &edge, edge.Node1Policy, edge.Node1_Pub, edge.Node2_Pub, chanID)
|
||||
writeChannelPolicyToMemgraphSnapshot(session, &edge, edge.Node2Policy, edge.Node2_Pub, edge.Node1_Pub, chanID)
|
||||
}
|
||||
}
|
||||
|
||||
func writeChannelPolicyToMemgraphSnapshot(session neo4j.Session, edge *ChannelEdge, policy RoutingPolicy, node1PubKey, node2PubKey, chanID string) {
|
||||
if policy.MaxHtlcMsat != "" {
|
||||
query := fmt.Sprintf(`
|
||||
MATCH (a:node {pubkey: '%s'}), (b:node {pubkey: '%s'})
|
||||
MERGE (a)-[r:CHANNEL {channel_id: '%s', capacity: %s}]->(b)
|
||||
SET r.fee_base_msat = %s, r.fee_rate_milli_msat = %s, r.time_lock_delta = %d,
|
||||
r.disabled = %v, r.min_htlc_msat = %s, r.max_htlc_msat = %s
|
||||
`, node1PubKey, node2PubKey, chanID, edge.Capacity,
|
||||
policy.FeeBaseMsat, policy.FeeRateMilliMsat, policy.TimeLockDelta, policy.Disabled, policy.MinHtlc, policy.MaxHtlcMsat)
|
||||
_, err := session.Run(query, nil)
|
||||
if err != nil {
|
||||
log.Printf("Failed to execute channel policy query: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue