• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15426258570

03 Jun 2025 07:36PM UTC coverage: 58.587%. First build
15426258570

Pull #9893

github

web-flow
Merge 4f69df485 into c52a6ddeb
Pull Request #9893: fix memory leak cherry pick

138 of 250 new or added lines in 20 files covered. (55.2%)

97459 of 166348 relevant lines covered (58.59%)

1.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/autopilot/simple_graph.go
1
package autopilot
2

3
import "context"
4

5
// diameterCutoff is used to discard nodes in the diameter calculation.
6
// It is the multiplier for the eccentricity of the highest-degree node,
7
// serving as a cutoff to discard all nodes with a smaller hop distance. This
8
// number should not be set close to 1 and is a tradeoff for computation cost,
9
// where 0 is maximally costly.
10
const diameterCutoff = 0.75
11

12
// SimpleGraph stores a simplified adj graph of a channel graph to speed
13
// up graph processing by eliminating all unnecessary hashing and map access.
14
type SimpleGraph struct {
15
        // Nodes is a map from node index to NodeID.
16
        Nodes []NodeID
17

18
        // Adj stores nodes and neighbors in an adjacency list.
19
        Adj [][]int
20
}
21

22
// NewSimpleGraph creates a simplified graph from the current channel graph.
23
// Returns an error if the channel graph iteration fails due to underlying
24
// failure.
NEW
25
func NewSimpleGraph(ctx context.Context, g ChannelGraph) (*SimpleGraph, error) {
×
26
        nodes := make(map[NodeID]int)
×
27
        adj := make(map[int][]int)
×
28
        nextIndex := 0
×
29

×
30
        // getNodeIndex returns the integer index of the passed node.
×
31
        // The returned index is then used to create a simplified adjacency list
×
32
        // where each node is identified by its index instead of its pubkey, and
×
33
        // also to create a mapping from node index to node pubkey.
×
34
        getNodeIndex := func(node Node) int {
×
35
                key := NodeID(node.PubKey())
×
36
                nodeIndex, ok := nodes[key]
×
37

×
38
                if !ok {
×
39
                        nodes[key] = nextIndex
×
40
                        nodeIndex = nextIndex
×
41
                        nextIndex++
×
42
                }
×
43

44
                return nodeIndex
×
45
        }
46

47
        // Iterate over each node and each channel and update the adj and the
48
        // node index.
NEW
49
        err := g.ForEachNode(ctx, func(ctx context.Context, node Node) error {
×
50
                u := getNodeIndex(node)
×
51

×
NEW
52
                return node.ForEachChannel(
×
NEW
53
                        ctx, func(_ context.Context,
×
NEW
54
                                edge ChannelEdge) error {
×
NEW
55

×
NEW
56
                                v := getNodeIndex(edge.Peer)
×
NEW
57

×
NEW
58
                                adj[u] = append(adj[u], v)
×
59

×
NEW
60
                                return nil
×
NEW
61
                        },
×
62
                )
63
        })
64
        if err != nil {
×
65
                return nil, err
×
66
        }
×
67

68
        graph := &SimpleGraph{
×
69
                Nodes: make([]NodeID, len(nodes)),
×
70
                Adj:   make([][]int, len(nodes)),
×
71
        }
×
72

×
73
        // Fill the adj and the node index to node pubkey mapping.
×
74
        for nodeID, nodeIndex := range nodes {
×
75
                graph.Adj[nodeIndex] = adj[nodeIndex]
×
76
                graph.Nodes[nodeIndex] = nodeID
×
77
        }
×
78

79
        // We prepare to give some debug output about the size of the graph.
80
        totalChannels := 0
×
81
        for _, channels := range graph.Adj {
×
82
                totalChannels += len(channels)
×
83
        }
×
84

85
        // The number of channels is double counted, so divide by two.
86
        log.Debugf("Initialized simple graph with %d nodes and %d "+
×
87
                "channels", len(graph.Adj), totalChannels/2)
×
88
        return graph, nil
×
89
}
90

91
// maxVal is a helper function to get the maximal value of all values of a map.
92
func maxVal(mapping map[int]uint32) uint32 {
×
93
        maxValue := uint32(0)
×
94
        for _, value := range mapping {
×
95
                maxValue = max(maxValue, value)
×
96
        }
×
97
        return maxValue
×
98
}
99

100
// degree determines the number of edges for a node in the graph.
101
func (graph *SimpleGraph) degree(node int) int {
×
102
        return len(graph.Adj[node])
×
103
}
×
104

105
// nodeMaxDegree determines the node with the max degree and its degree.
106
func (graph *SimpleGraph) nodeMaxDegree() (int, int) {
×
107
        var maxNode, maxDegree int
×
108
        for node := range graph.Adj {
×
109
                degree := graph.degree(node)
×
110
                if degree > maxDegree {
×
111
                        maxNode = node
×
112
                        maxDegree = degree
×
113
                }
×
114
        }
115
        return maxNode, maxDegree
×
116
}
117

118
// shortestPathLengths performs a breadth-first-search from a node to all other
119
// nodes, returning the lengths of the paths.
120
func (graph *SimpleGraph) shortestPathLengths(node int) map[int]uint32 {
×
121
        // level indicates the shell of the search around the root node.
×
122
        var level uint32
×
123
        graphOrder := len(graph.Adj)
×
124

×
125
        // nextLevel tracks which nodes should be visited in the next round.
×
126
        nextLevel := make([]int, 0, graphOrder)
×
127

×
128
        // The root node is put as a starting point for the exploration.
×
129
        nextLevel = append(nextLevel, node)
×
130

×
131
        // Seen tracks already visited nodes and tracks how far away they are.
×
132
        seen := make(map[int]uint32, graphOrder)
×
133

×
134
        // Mark the root node as seen.
×
135
        seen[node] = level
×
136

×
137
        // thisLevel contains the nodes that are explored in the round.
×
138
        thisLevel := make([]int, 0, graphOrder)
×
139

×
140
        // Abort if we have an empty graph.
×
141
        if len(graph.Adj) == 0 {
×
142
                return seen
×
143
        }
×
144

145
        // We discover other nodes in a ring-like structure as long as we don't
146
        // have more nodes to explore.
147
        for len(nextLevel) > 0 {
×
148
                level++
×
149

×
150
                // We swap the queues for efficient memory management.
×
151
                thisLevel, nextLevel = nextLevel, thisLevel
×
152

×
153
                // Visit all neighboring nodes of the level and mark them as
×
154
                // seen if they were not discovered before.
×
155
                for _, thisNode := range thisLevel {
×
156
                        for _, neighbor := range graph.Adj[thisNode] {
×
157
                                _, ok := seen[neighbor]
×
158
                                if !ok {
×
159
                                        nextLevel = append(nextLevel, neighbor)
×
160
                                        seen[neighbor] = level
×
161
                                }
×
162

163
                                // If we have seen all nodes, we return early.
164
                                if len(seen) == graphOrder {
×
165
                                        return seen
×
166
                                }
×
167
                        }
168
                }
169

170
                // Empty the queue to be used in the next level.
171
                thisLevel = thisLevel[:0:cap(thisLevel)]
×
172
        }
173

174
        return seen
×
175
}
176

177
// nodeEccentricity calculates the eccentricity (longest shortest path to all
178
// other nodes) of a node.
179
func (graph *SimpleGraph) nodeEccentricity(node int) uint32 {
×
180
        pathLengths := graph.shortestPathLengths(node)
×
181
        return maxVal(pathLengths)
×
182
}
×
183

184
// nodeEccentricities calculates the eccentricities for the given nodes.
185
func (graph *SimpleGraph) nodeEccentricities(nodes []int) map[int]uint32 {
×
186
        eccentricities := make(map[int]uint32, len(graph.Adj))
×
187
        for _, node := range nodes {
×
188
                eccentricities[node] = graph.nodeEccentricity(node)
×
189
        }
×
190
        return eccentricities
×
191
}
192

193
// Diameter returns the maximal eccentricity (longest shortest path
194
// between any node pair) in the graph.
195
//
196
// Note: This method is exact but expensive, use DiameterRadialCutoff instead.
197
func (graph *SimpleGraph) Diameter() uint32 {
×
198
        nodes := make([]int, len(graph.Adj))
×
199
        for a := range nodes {
×
200
                nodes[a] = a
×
201
        }
×
202
        eccentricities := graph.nodeEccentricities(nodes)
×
203
        return maxVal(eccentricities)
×
204
}
205

206
// DiameterRadialCutoff is a method to efficiently evaluate the diameter of a
207
// graph. The highest-degree node is usually central in the graph. We can
208
// determine its eccentricity (shortest-longest path length to any other node)
209
// and use it as an approximation for the radius of the network. We then
210
// use this radius to compute a cutoff. All the nodes within a distance of the
211
// cutoff are discarded, as they represent the inside of the graph. We then
212
// loop over all outer nodes and determine their eccentricities, from which we
213
// get the diameter.
214
func (graph *SimpleGraph) DiameterRadialCutoff() uint32 {
×
215
        // Determine the reference node as the node with the highest degree.
×
216
        nodeMaxDegree, _ := graph.nodeMaxDegree()
×
217

×
218
        distances := graph.shortestPathLengths(nodeMaxDegree)
×
219
        eccentricityMaxDegreeNode := maxVal(distances)
×
220

×
221
        // We use the eccentricity to define a cutoff for the interior of the
×
222
        // graph from the reference node.
×
223
        cutoff := uint32(float32(eccentricityMaxDegreeNode) * diameterCutoff)
×
224
        log.Debugf("Cutoff radius is %d hops (max-degree node's "+
×
225
                "eccentricity is %d)", cutoff, eccentricityMaxDegreeNode)
×
226

×
227
        // Remove the nodes that are close to the reference node.
×
228
        var nodes []int
×
229
        for node, distance := range distances {
×
230
                if distance > cutoff {
×
231
                        nodes = append(nodes, node)
×
232
                }
×
233
        }
234
        log.Debugf("Evaluated nodes: %d, discarded nodes %d",
×
235
                len(nodes), len(graph.Adj)-len(nodes))
×
236

×
237
        // Compute the diameter of the remaining nodes.
×
238
        eccentricities := graph.nodeEccentricities(nodes)
×
239
        return maxVal(eccentricities)
×
240
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc