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

lightningnetwork / lnd / 15736109134

18 Jun 2025 02:46PM UTC coverage: 58.197% (-10.1%) from 68.248%
15736109134

Pull #9752

github

web-flow
Merge d2634a68c into 31c74f20f
Pull Request #9752: routerrpc: reject payment to invoice that don't have payment secret or blinded paths

6 of 13 new or added lines in 2 files covered. (46.15%)

28331 existing lines in 455 files now uncovered.

97860 of 168153 relevant lines covered (58.2%)

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
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
                )
63
        })
UNCOV
64
        if err != nil {
×
65
                return nil, err
×
66
        }
×
67

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

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

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

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

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

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

105
// nodeMaxDegree determines the node with the max degree and its degree.
UNCOV
106
func (graph *SimpleGraph) nodeMaxDegree() (int, int) {
×
UNCOV
107
        var maxNode, maxDegree int
×
UNCOV
108
        for node := range graph.Adj {
×
UNCOV
109
                degree := graph.degree(node)
×
UNCOV
110
                if degree > maxDegree {
×
UNCOV
111
                        maxNode = node
×
UNCOV
112
                        maxDegree = degree
×
UNCOV
113
                }
×
114
        }
UNCOV
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.
UNCOV
120
func (graph *SimpleGraph) shortestPathLengths(node int) map[int]uint32 {
×
UNCOV
121
        // level indicates the shell of the search around the root node.
×
UNCOV
122
        var level uint32
×
UNCOV
123
        graphOrder := len(graph.Adj)
×
UNCOV
124

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

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

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

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

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

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

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

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

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

170
                // Empty the queue to be used in the next level.
UNCOV
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.
UNCOV
179
func (graph *SimpleGraph) nodeEccentricity(node int) uint32 {
×
UNCOV
180
        pathLengths := graph.shortestPathLengths(node)
×
UNCOV
181
        return maxVal(pathLengths)
×
UNCOV
182
}
×
183

184
// nodeEccentricities calculates the eccentricities for the given nodes.
UNCOV
185
func (graph *SimpleGraph) nodeEccentricities(nodes []int) map[int]uint32 {
×
UNCOV
186
        eccentricities := make(map[int]uint32, len(graph.Adj))
×
UNCOV
187
        for _, node := range nodes {
×
UNCOV
188
                eccentricities[node] = graph.nodeEccentricity(node)
×
UNCOV
189
        }
×
UNCOV
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.
UNCOV
197
func (graph *SimpleGraph) Diameter() uint32 {
×
UNCOV
198
        nodes := make([]int, len(graph.Adj))
×
UNCOV
199
        for a := range nodes {
×
UNCOV
200
                nodes[a] = a
×
UNCOV
201
        }
×
UNCOV
202
        eccentricities := graph.nodeEccentricities(nodes)
×
UNCOV
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.
UNCOV
214
func (graph *SimpleGraph) DiameterRadialCutoff() uint32 {
×
UNCOV
215
        // Determine the reference node as the node with the highest degree.
×
UNCOV
216
        nodeMaxDegree, _ := graph.nodeMaxDegree()
×
UNCOV
217

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

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

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

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