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

lightningnetwork / lnd / 13423774416

19 Feb 2025 10:39PM UTC coverage: 49.338% (-9.5%) from 58.794%
13423774416

Pull #9484

github

Abdulkbk
lnutils: add createdir util function

This utility function replaces repetitive logic patterns
throughout LND.
Pull Request #9484: lnutils: add createDir util function

0 of 13 new or added lines in 1 file covered. (0.0%)

27149 existing lines in 431 files now uncovered.

100732 of 204168 relevant lines covered (49.34%)

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

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

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

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

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

×
UNCOV
36
                if !ok {
×
UNCOV
37
                        nodes[key] = nextIndex
×
UNCOV
38
                        nodeIndex = nextIndex
×
UNCOV
39
                        nextIndex++
×
UNCOV
40
                }
×
41

UNCOV
42
                return nodeIndex
×
43
        }
44

45
        // Iterate over each node and each channel and update the adj and the node
46
        // index.
UNCOV
47
        err := g.ForEachNode(func(node Node) error {
×
UNCOV
48
                u := getNodeIndex(node)
×
UNCOV
49

×
UNCOV
50
                return node.ForEachChannel(func(edge ChannelEdge) error {
×
UNCOV
51
                        v := getNodeIndex(edge.Peer)
×
UNCOV
52

×
UNCOV
53
                        adj[u] = append(adj[u], v)
×
UNCOV
54
                        return nil
×
UNCOV
55
                })
×
56
        })
UNCOV
57
        if err != nil {
×
58
                return nil, err
×
59
        }
×
60

UNCOV
61
        graph := &SimpleGraph{
×
UNCOV
62
                Nodes: make([]NodeID, len(nodes)),
×
UNCOV
63
                Adj:   make([][]int, len(nodes)),
×
UNCOV
64
        }
×
UNCOV
65

×
UNCOV
66
        // Fill the adj and the node index to node pubkey mapping.
×
UNCOV
67
        for nodeID, nodeIndex := range nodes {
×
UNCOV
68
                graph.Adj[nodeIndex] = adj[nodeIndex]
×
UNCOV
69
                graph.Nodes[nodeIndex] = nodeID
×
UNCOV
70
        }
×
71

72
        // We prepare to give some debug output about the size of the graph.
UNCOV
73
        totalChannels := 0
×
UNCOV
74
        for _, channels := range graph.Adj {
×
UNCOV
75
                totalChannels += len(channels)
×
UNCOV
76
        }
×
77

78
        // The number of channels is double counted, so divide by two.
UNCOV
79
        log.Debugf("Initialized simple graph with %d nodes and %d "+
×
UNCOV
80
                "channels", len(graph.Adj), totalChannels/2)
×
UNCOV
81
        return graph, nil
×
82
}
83

84
// maxVal is a helper function to get the maximal value of all values of a map.
UNCOV
85
func maxVal(mapping map[int]uint32) uint32 {
×
UNCOV
86
        maxValue := uint32(0)
×
UNCOV
87
        for _, value := range mapping {
×
UNCOV
88
                if maxValue < value {
×
UNCOV
89
                        maxValue = value
×
UNCOV
90
                }
×
91
        }
92
        return maxValue
93
}
UNCOV
94

×
UNCOV
95
// degree determines the number of edges for a node in the graph.
×
UNCOV
96
func (graph *SimpleGraph) degree(node int) int {
×
97
        return len(graph.Adj[node])
98
}
UNCOV
99

×
UNCOV
100
// nodeMaxDegree determines the node with the max degree and its degree.
×
UNCOV
101
func (graph *SimpleGraph) nodeMaxDegree() (int, int) {
×
UNCOV
102
        var maxNode, maxDegree int
×
UNCOV
103
        for node := range graph.Adj {
×
UNCOV
104
                degree := graph.degree(node)
×
UNCOV
105
                if degree > maxDegree {
×
UNCOV
106
                        maxNode = node
×
107
                        maxDegree = degree
UNCOV
108
                }
×
109
        }
110
        return maxNode, maxDegree
111
}
112

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

×
UNCOV
120
        // nextLevel tracks which nodes should be visited in the next round.
×
UNCOV
121
        nextLevel := make([]int, 0, graphOrder)
×
UNCOV
122

×
UNCOV
123
        // The root node is put as a starting point for the exploration.
×
UNCOV
124
        nextLevel = append(nextLevel, node)
×
UNCOV
125

×
UNCOV
126
        // Seen tracks already visited nodes and tracks how far away they are.
×
UNCOV
127
        seen := make(map[int]uint32, graphOrder)
×
UNCOV
128

×
UNCOV
129
        // Mark the root node as seen.
×
UNCOV
130
        seen[node] = level
×
UNCOV
131

×
UNCOV
132
        // thisLevel contains the nodes that are explored in the round.
×
UNCOV
133
        thisLevel := make([]int, 0, graphOrder)
×
UNCOV
134

×
135
        // Abort if we have an empty graph.
×
136
        if len(graph.Adj) == 0 {
×
137
                return seen
138
        }
139

UNCOV
140
        // We discover other nodes in a ring-like structure as long as we don't
×
UNCOV
141
        // have more nodes to explore.
×
UNCOV
142
        for len(nextLevel) > 0 {
×
UNCOV
143
                level++
×
UNCOV
144

×
UNCOV
145
                // We swap the queues for efficient memory management.
×
UNCOV
146
                thisLevel, nextLevel = nextLevel, thisLevel
×
UNCOV
147

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

×
UNCOV
158
                                // If we have seen all nodes, we return early.
×
UNCOV
159
                                if len(seen) == graphOrder {
×
160
                                        return seen
161
                                }
162
                        }
163
                }
UNCOV
164

×
165
                // Empty the queue to be used in the next level.
166
                thisLevel = thisLevel[:0:cap(thisLevel)]
167
        }
×
168

169
        return seen
170
}
171

UNCOV
172
// nodeEccentricity calculates the eccentricity (longest shortest path to all
×
UNCOV
173
// other nodes) of a node.
×
UNCOV
174
func (graph *SimpleGraph) nodeEccentricity(node int) uint32 {
×
UNCOV
175
        pathLengths := graph.shortestPathLengths(node)
×
176
        return maxVal(pathLengths)
177
}
UNCOV
178

×
UNCOV
179
// nodeEccentricities calculates the eccentricities for the given nodes.
×
UNCOV
180
func (graph *SimpleGraph) nodeEccentricities(nodes []int) map[int]uint32 {
×
UNCOV
181
        eccentricities := make(map[int]uint32, len(graph.Adj))
×
UNCOV
182
        for _, node := range nodes {
×
UNCOV
183
                eccentricities[node] = graph.nodeEccentricity(node)
×
184
        }
185
        return eccentricities
186
}
187

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

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

×
UNCOV
213
        distances := graph.shortestPathLengths(nodeMaxDegree)
×
UNCOV
214
        eccentricityMaxDegreeNode := maxVal(distances)
×
UNCOV
215

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

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

×
UNCOV
232
        // Compute the diameter of the remaining nodes.
×
233
        eccentricities := graph.nodeEccentricities(nodes)
234
        return maxVal(eccentricities)
235
}
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