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

lightningnetwork / lnd / 16466354971

23 Jul 2025 09:05AM UTC coverage: 57.54% (-9.7%) from 67.201%
16466354971

Pull #9455

github

web-flow
Merge f914ae23c into 90e211684
Pull Request #9455: discovery+lnwire: add support for DNS host name in NodeAnnouncement msg

151 of 291 new or added lines in 7 files covered. (51.89%)

28441 existing lines in 456 files now uncovered.

98864 of 171817 relevant lines covered (57.54%)

1.79 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.
UNCOV
25
func NewSimpleGraph(ctx context.Context, g ChannelGraph) (*SimpleGraph, error) {
×
UNCOV
26
        nodes := make(map[NodeID]int)
×
UNCOV
27
        adj := make(map[int][]int)
×
UNCOV
28
        nextIndex := 0
×
UNCOV
29

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

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

UNCOV
44
                return nodeIndex
×
45
        }
46

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

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

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

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

×
UNCOV
60
                                return nil
×
UNCOV
61
                        },
×
62
                )
UNCOV
63
        }, func() {
×
UNCOV
64
                clear(adj)
×
UNCOV
65
                clear(nodes)
×
UNCOV
66
                nextIndex = 0
×
UNCOV
67
        })
×
UNCOV
68
        if err != nil {
×
69
                return nil, err
×
70
        }
×
71

UNCOV
72
        graph := &SimpleGraph{
×
UNCOV
73
                Nodes: make([]NodeID, len(nodes)),
×
UNCOV
74
                Adj:   make([][]int, len(nodes)),
×
UNCOV
75
        }
×
UNCOV
76

×
UNCOV
77
        // Fill the adj and the node index to node pubkey mapping.
×
UNCOV
78
        for nodeID, nodeIndex := range nodes {
×
UNCOV
79
                graph.Adj[nodeIndex] = adj[nodeIndex]
×
UNCOV
80
                graph.Nodes[nodeIndex] = nodeID
×
UNCOV
81
        }
×
82

83
        // We prepare to give some debug output about the size of the graph.
UNCOV
84
        totalChannels := 0
×
UNCOV
85
        for _, channels := range graph.Adj {
×
UNCOV
86
                totalChannels += len(channels)
×
UNCOV
87
        }
×
88

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

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

104
// degree determines the number of edges for a node in the graph.
UNCOV
105
func (graph *SimpleGraph) degree(node int) int {
×
UNCOV
106
        return len(graph.Adj[node])
×
UNCOV
107
}
×
108

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

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

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

×
UNCOV
132
        // The root node is put as a starting point for the exploration.
×
UNCOV
133
        nextLevel = append(nextLevel, node)
×
UNCOV
134

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

×
UNCOV
138
        // Mark the root node as seen.
×
UNCOV
139
        seen[node] = level
×
UNCOV
140

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

×
UNCOV
144
        // Abort if we have an empty graph.
×
UNCOV
145
        if len(graph.Adj) == 0 {
×
146
                return seen
×
147
        }
×
148

149
        // We discover other nodes in a ring-like structure as long as we don't
150
        // have more nodes to explore.
UNCOV
151
        for len(nextLevel) > 0 {
×
UNCOV
152
                level++
×
UNCOV
153

×
UNCOV
154
                // We swap the queues for efficient memory management.
×
UNCOV
155
                thisLevel, nextLevel = nextLevel, thisLevel
×
UNCOV
156

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

167
                                // If we have seen all nodes, we return early.
UNCOV
168
                                if len(seen) == graphOrder {
×
UNCOV
169
                                        return seen
×
UNCOV
170
                                }
×
171
                        }
172
                }
173

174
                // Empty the queue to be used in the next level.
UNCOV
175
                thisLevel = thisLevel[:0:cap(thisLevel)]
×
176
        }
177

178
        return seen
×
179
}
180

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

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

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

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

×
UNCOV
222
        distances := graph.shortestPathLengths(nodeMaxDegree)
×
UNCOV
223
        eccentricityMaxDegreeNode := maxVal(distances)
×
UNCOV
224

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

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

×
UNCOV
241
        // Compute the diameter of the remaining nodes.
×
UNCOV
242
        eccentricities := graph.nodeEccentricities(nodes)
×
UNCOV
243
        return maxVal(eccentricities)
×
244
}
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