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

lightningnetwork / lnd / 13523316608

25 Feb 2025 02:12PM UTC coverage: 49.351% (-9.5%) from 58.835%
13523316608

Pull #9549

github

yyforyongyu
routing/chainview: refactor `TestFilteredChainView`

So each test has its own miner and chainView.
Pull Request #9549: Fix unit test flake `TestHistoricalConfDetailsTxIndex`

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

27196 existing lines in 434 files now uncovered.

100945 of 204543 relevant lines covered (49.35%)

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
                maxValue = max(maxValue, value)
×
UNCOV
89
        }
×
UNCOV
90
        return maxValue
×
91
}
92

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

98
// nodeMaxDegree determines the node with the max degree and its degree.
UNCOV
99
func (graph *SimpleGraph) nodeMaxDegree() (int, int) {
×
UNCOV
100
        var maxNode, maxDegree int
×
UNCOV
101
        for node := range graph.Adj {
×
UNCOV
102
                degree := graph.degree(node)
×
UNCOV
103
                if degree > maxDegree {
×
UNCOV
104
                        maxNode = node
×
UNCOV
105
                        maxDegree = degree
×
UNCOV
106
                }
×
107
        }
UNCOV
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.
UNCOV
113
func (graph *SimpleGraph) shortestPathLengths(node int) map[int]uint32 {
×
UNCOV
114
        // level indicates the shell of the search around the root node.
×
UNCOV
115
        var level uint32
×
UNCOV
116
        graphOrder := len(graph.Adj)
×
UNCOV
117

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

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

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

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

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

×
UNCOV
133
        // Abort if we have an empty graph.
×
UNCOV
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.
UNCOV
140
        for len(nextLevel) > 0 {
×
UNCOV
141
                level++
×
UNCOV
142

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

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

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

163
                // Empty the queue to be used in the next level.
UNCOV
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.
UNCOV
172
func (graph *SimpleGraph) nodeEccentricity(node int) uint32 {
×
UNCOV
173
        pathLengths := graph.shortestPathLengths(node)
×
UNCOV
174
        return maxVal(pathLengths)
×
UNCOV
175
}
×
176

177
// nodeEccentricities calculates the eccentricities for the given nodes.
UNCOV
178
func (graph *SimpleGraph) nodeEccentricities(nodes []int) map[int]uint32 {
×
UNCOV
179
        eccentricities := make(map[int]uint32, len(graph.Adj))
×
UNCOV
180
        for _, node := range nodes {
×
UNCOV
181
                eccentricities[node] = graph.nodeEccentricity(node)
×
UNCOV
182
        }
×
UNCOV
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.
UNCOV
190
func (graph *SimpleGraph) Diameter() uint32 {
×
UNCOV
191
        nodes := make([]int, len(graph.Adj))
×
UNCOV
192
        for a := range nodes {
×
UNCOV
193
                nodes[a] = a
×
UNCOV
194
        }
×
UNCOV
195
        eccentricities := graph.nodeEccentricities(nodes)
×
UNCOV
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.
UNCOV
207
func (graph *SimpleGraph) DiameterRadialCutoff() uint32 {
×
UNCOV
208
        // Determine the reference node as the node with the highest degree.
×
UNCOV
209
        nodeMaxDegree, _ := graph.nodeMaxDegree()
×
UNCOV
210

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

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

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

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