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

lightningnetwork / lnd / 13727082033

07 Mar 2025 06:37PM UTC coverage: 58.289% (-10.3%) from 68.615%
13727082033

push

github

web-flow
Merge pull request #9581 from yyforyongyu/fix-TestReconnectSucceed

tor: fix `TestReconnectSucceed`

94454 of 162044 relevant lines covered (58.29%)

1.81 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.
23
func NewSimpleGraph(g ChannelGraph) (*SimpleGraph, error) {
×
24
        nodes := make(map[NodeID]int)
×
25
        adj := make(map[int][]int)
×
26
        nextIndex := 0
×
27

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

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

42
                return nodeIndex
×
43
        }
44

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

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

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

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

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

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

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

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

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

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

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

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

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

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

×
127
        // Mark the root node as seen.
×
128
        seen[node] = level
×
129

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

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

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

×
143
                // We swap the queues for efficient memory management.
×
144
                thisLevel, nextLevel = nextLevel, thisLevel
×
145

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

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

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

167
        return seen
×
168
}
169

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

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

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

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

×
211
        distances := graph.shortestPathLengths(nodeMaxDegree)
×
212
        eccentricityMaxDegreeNode := maxVal(distances)
×
213

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

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

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