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

lightningnetwork / lnd / 9915780197

13 Jul 2024 12:30AM UTC coverage: 49.268% (-9.1%) from 58.413%
9915780197

push

github

web-flow
Merge pull request #8653 from ProofOfKeags/fn-prim

DynComms [0/n]: `fn` package additions

92837 of 188433 relevant lines covered (49.27%)

1.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.0
/routing/graph.go
1
package routing
2

3
import (
4
        "fmt"
5

6
        "github.com/btcsuite/btcd/btcutil"
7
        "github.com/lightningnetwork/lnd/channeldb"
8
        "github.com/lightningnetwork/lnd/kvdb"
9
        "github.com/lightningnetwork/lnd/lnwire"
10
        "github.com/lightningnetwork/lnd/routing/route"
11
)
12

13
// routingGraph is an abstract interface that provides information about nodes
14
// and edges to pathfinding.
15
type routingGraph interface {
16
        // forEachNodeChannel calls the callback for every channel of the given
17
        // node.
18
        forEachNodeChannel(nodePub route.Vertex,
19
                cb func(channel *channeldb.DirectedChannel) error) error
20

21
        // sourceNode returns the source node of the graph.
22
        sourceNode() route.Vertex
23

24
        // fetchNodeFeatures returns the features of the given node.
25
        fetchNodeFeatures(nodePub route.Vertex) (*lnwire.FeatureVector, error)
26

27
        // FetchAmountPairCapacity determines the maximal capacity between two
28
        // pairs of nodes.
29
        FetchAmountPairCapacity(nodeFrom, nodeTo route.Vertex,
30
                amount lnwire.MilliSatoshi) (btcutil.Amount, error)
31
}
32

33
// CachedGraph is a routingGraph implementation that retrieves from the
34
// database.
35
type CachedGraph struct {
36
        graph  *channeldb.ChannelGraph
37
        tx     kvdb.RTx
38
        source route.Vertex
39
}
40

41
// A compile time assertion to make sure CachedGraph implements the routingGraph
42
// interface.
43
var _ routingGraph = (*CachedGraph)(nil)
44

45
// NewCachedGraph instantiates a new db-connected routing graph. It implicitly
46
// instantiates a new read transaction.
47
func NewCachedGraph(sourceNode *channeldb.LightningNode,
48
        graph *channeldb.ChannelGraph) (*CachedGraph, error) {
3✔
49

3✔
50
        tx, err := graph.NewPathFindTx()
3✔
51
        if err != nil {
3✔
52
                return nil, err
×
53
        }
×
54

55
        return &CachedGraph{
3✔
56
                graph:  graph,
3✔
57
                tx:     tx,
3✔
58
                source: sourceNode.PubKeyBytes,
3✔
59
        }, nil
3✔
60
}
61

62
// Close attempts to close the underlying db transaction. This is a no-op in
63
// case the underlying graph uses an in-memory cache.
64
func (g *CachedGraph) Close() error {
3✔
65
        if g.tx == nil {
6✔
66
                return nil
3✔
67
        }
3✔
68

69
        return g.tx.Rollback()
×
70
}
71

72
// forEachNodeChannel calls the callback for every channel of the given node.
73
//
74
// NOTE: Part of the routingGraph interface.
75
func (g *CachedGraph) forEachNodeChannel(nodePub route.Vertex,
76
        cb func(channel *channeldb.DirectedChannel) error) error {
3✔
77

3✔
78
        return g.graph.ForEachNodeDirectedChannel(g.tx, nodePub, cb)
3✔
79
}
3✔
80

81
// sourceNode returns the source node of the graph.
82
//
83
// NOTE: Part of the routingGraph interface.
84
func (g *CachedGraph) sourceNode() route.Vertex {
3✔
85
        return g.source
3✔
86
}
3✔
87

88
// fetchNodeFeatures returns the features of the given node. If the node is
89
// unknown, assume no additional features are supported.
90
//
91
// NOTE: Part of the routingGraph interface.
92
func (g *CachedGraph) fetchNodeFeatures(nodePub route.Vertex) (
93
        *lnwire.FeatureVector, error) {
3✔
94

3✔
95
        return g.graph.FetchNodeFeatures(nodePub)
3✔
96
}
3✔
97

98
// FetchAmountPairCapacity determines the maximal public capacity between two
99
// nodes depending on the amount we try to send.
100
//
101
// NOTE: Part of the routingGraph interface.
102
func (g *CachedGraph) FetchAmountPairCapacity(nodeFrom, nodeTo route.Vertex,
103
        amount lnwire.MilliSatoshi) (btcutil.Amount, error) {
3✔
104

3✔
105
        // Create unified edges for all incoming connections.
3✔
106
        //
3✔
107
        // Note: Inbound fees are not used here because this method is only used
3✔
108
        // by a deprecated router rpc.
3✔
109
        u := newNodeEdgeUnifier(g.sourceNode(), nodeTo, false, nil)
3✔
110

3✔
111
        err := u.addGraphPolicies(g)
3✔
112
        if err != nil {
3✔
113
                return 0, err
×
114
        }
×
115

116
        edgeUnifier, ok := u.edgeUnifiers[nodeFrom]
3✔
117
        if !ok {
6✔
118
                return 0, fmt.Errorf("no edge info for node pair %v -> %v",
3✔
119
                        nodeFrom, nodeTo)
3✔
120
        }
3✔
121

122
        edge := edgeUnifier.getEdgeNetwork(amount, 0)
3✔
123
        if edge == nil {
6✔
124
                return 0, fmt.Errorf("no edge for node pair %v -> %v "+
3✔
125
                        "(amount %v)", nodeFrom, nodeTo, amount)
3✔
126
        }
3✔
127

128
        return edge.capacity, nil
×
129
}
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