Skip to content

Commit fae7e0c

Browse files
authored
Merge pull request #8848 from ellemouton/graphManager
refactor: move graph responsibilities from routing.ChannelRouter to new graph.Builder
2 parents 09b38aa + b112e10 commit fae7e0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+6341
-4577
lines changed

autopilot/graph.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (d *dbNode) Addrs() []net.Addr {
8989
//
9090
// NOTE: Part of the autopilot.Node interface.
9191
func (d *dbNode) ForEachChannel(cb func(ChannelEdge) error) error {
92-
return d.db.ForEachNodeChannel(d.tx, d.node.PubKeyBytes,
92+
return d.db.ForEachNodeChannelTx(d.tx, d.node.PubKeyBytes,
9393
func(tx kvdb.RTx, ei *models.ChannelEdgeInfo, ep,
9494
_ *models.ChannelEdgePolicy) error {
9595

@@ -105,7 +105,9 @@ func (d *dbNode) ForEachChannel(cb func(ChannelEdge) error) error {
105105
return nil
106106
}
107107

108-
node, err := d.db.FetchLightningNode(tx, ep.ToNode)
108+
node, err := d.db.FetchLightningNodeTx(
109+
tx, ep.ToNode,
110+
)
109111
if err != nil {
110112
return err
111113
}
@@ -164,7 +166,7 @@ func (d *databaseChannelGraph) addRandChannel(node1, node2 *btcec.PublicKey,
164166
return nil, err
165167
}
166168

167-
dbNode, err := d.db.FetchLightningNode(nil, vertex)
169+
dbNode, err := d.db.FetchLightningNode(vertex)
168170
switch {
169171
case err == channeldb.ErrGraphNodeNotFound:
170172
fallthrough

autopilot/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66

77
"github.com/btcsuite/btcd/btcec/v2"
88
"github.com/btcsuite/btcd/wire"
9+
"github.com/lightningnetwork/lnd/graph"
910
"github.com/lightningnetwork/lnd/lnwallet"
1011
"github.com/lightningnetwork/lnd/lnwire"
11-
"github.com/lightningnetwork/lnd/routing"
1212
)
1313

1414
// ManagerCfg houses a set of values and methods that is passed to the Manager
@@ -36,7 +36,7 @@ type ManagerCfg struct {
3636

3737
// SubscribeTopology is used to get a subscription for topology changes
3838
// on the network.
39-
SubscribeTopology func() (*routing.TopologyClient, error)
39+
SubscribeTopology func() (*graph.TopologyClient, error)
4040
}
4141

4242
// Manager is struct that manages an autopilot agent, making it possible to

channeldb/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ func (d *DB) AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr,
13511351
if err != nil {
13521352
return nil, err
13531353
}
1354-
graphNode, err := d.graph.FetchLightningNode(nil, pubKey)
1354+
graphNode, err := d.graph.FetchLightningNode(pubKey)
13551355
if err != nil && err != ErrGraphNodeNotFound {
13561356
return nil, err
13571357
} else if err == ErrGraphNodeNotFound {

channeldb/graph.go

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func (c *ChannelGraph) FetchNodeFeatures(
529529
}
530530

531531
// Fallback that uses the database.
532-
targetNode, err := c.FetchLightningNode(nil, node)
532+
targetNode, err := c.FetchLightningNode(node)
533533
switch err {
534534
// If the node exists and has features, return them directly.
535535
case nil:
@@ -565,7 +565,7 @@ func (c *ChannelGraph) ForEachNodeCached(cb func(node route.Vertex,
565565
return c.ForEachNode(func(tx kvdb.RTx, node *LightningNode) error {
566566
channels := make(map[uint64]*DirectedChannel)
567567

568-
err := c.ForEachNodeChannel(tx, node.PubKeyBytes,
568+
err := c.ForEachNodeChannelTx(tx, node.PubKeyBytes,
569569
func(tx kvdb.RTx, e *models.ChannelEdgeInfo,
570570
p1 *models.ChannelEdgePolicy,
571571
p2 *models.ChannelEdgePolicy) error {
@@ -2374,10 +2374,19 @@ func (c *ChannelGraph) FilterChannelRange(startHeight,
23742374
// skipped and the result will contain only those edges that exist at the time
23752375
// of the query. This can be used to respond to peer queries that are seeking to
23762376
// fill in gaps in their view of the channel graph.
2377+
func (c *ChannelGraph) FetchChanInfos(chanIDs []uint64) ([]ChannelEdge, error) {
2378+
return c.fetchChanInfos(nil, chanIDs)
2379+
}
2380+
2381+
// fetchChanInfos returns the set of channel edges that correspond to the passed
2382+
// channel ID's. If an edge is the query is unknown to the database, it will
2383+
// skipped and the result will contain only those edges that exist at the time
2384+
// of the query. This can be used to respond to peer queries that are seeking to
2385+
// fill in gaps in their view of the channel graph.
23772386
//
23782387
// NOTE: An optional transaction may be provided. If none is provided, then a
23792388
// new one will be created.
2380-
func (c *ChannelGraph) FetchChanInfos(tx kvdb.RTx, chanIDs []uint64) (
2389+
func (c *ChannelGraph) fetchChanInfos(tx kvdb.RTx, chanIDs []uint64) (
23812390
[]ChannelEdge, error) {
23822391
// TODO(roasbeef): sort cids?
23832392

@@ -2922,7 +2931,7 @@ func (c *ChannelGraph) isPublic(tx kvdb.RTx, nodePub route.Vertex,
29222931
// used to terminate the check early.
29232932
nodeIsPublic := false
29242933
errDone := errors.New("done")
2925-
err := c.ForEachNodeChannel(tx, nodePub, func(tx kvdb.RTx,
2934+
err := c.ForEachNodeChannelTx(tx, nodePub, func(tx kvdb.RTx,
29262935
info *models.ChannelEdgeInfo, _ *models.ChannelEdgePolicy,
29272936
_ *models.ChannelEdgePolicy) error {
29282937

@@ -2954,12 +2963,31 @@ func (c *ChannelGraph) isPublic(tx kvdb.RTx, nodePub route.Vertex,
29542963
return nodeIsPublic, nil
29552964
}
29562965

2966+
// FetchLightningNodeTx attempts to look up a target node by its identity
2967+
// public key. If the node isn't found in the database, then
2968+
// ErrGraphNodeNotFound is returned. An optional transaction may be provided.
2969+
// If none is provided, then a new one will be created.
2970+
func (c *ChannelGraph) FetchLightningNodeTx(tx kvdb.RTx, nodePub route.Vertex) (
2971+
*LightningNode, error) {
2972+
2973+
return c.fetchLightningNode(tx, nodePub)
2974+
}
2975+
29572976
// FetchLightningNode attempts to look up a target node by its identity public
29582977
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
2978+
// returned.
2979+
func (c *ChannelGraph) FetchLightningNode(nodePub route.Vertex) (*LightningNode,
2980+
error) {
2981+
2982+
return c.fetchLightningNode(nil, nodePub)
2983+
}
2984+
2985+
// fetchLightningNode attempts to look up a target node by its identity public
2986+
// key. If the node isn't found in the database, then ErrGraphNodeNotFound is
29592987
// returned. An optional transaction may be provided. If none is provided, then
29602988
// a new one will be created.
2961-
func (c *ChannelGraph) FetchLightningNode(tx kvdb.RTx, nodePub route.Vertex) (
2962-
*LightningNode, error) {
2989+
func (c *ChannelGraph) fetchLightningNode(tx kvdb.RTx,
2990+
nodePub route.Vertex) (*LightningNode, error) {
29632991

29642992
var node *LightningNode
29652993
fetch := func(tx kvdb.RTx) error {
@@ -3196,13 +3224,29 @@ func nodeTraversal(tx kvdb.RTx, nodePub []byte, db kvdb.Backend,
31963224
// halted with the error propagated back up to the caller.
31973225
//
31983226
// Unknown policies are passed into the callback as nil values.
3227+
func (c *ChannelGraph) ForEachNodeChannel(nodePub route.Vertex,
3228+
cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3229+
*models.ChannelEdgePolicy) error) error {
3230+
3231+
return nodeTraversal(nil, nodePub[:], c.db, cb)
3232+
}
3233+
3234+
// ForEachNodeChannelTx iterates through all channels of the given node,
3235+
// executing the passed callback with an edge info structure and the policies
3236+
// of each end of the channel. The first edge policy is the outgoing edge *to*
3237+
// the connecting node, while the second is the incoming edge *from* the
3238+
// connecting node. If the callback returns an error, then the iteration is
3239+
// halted with the error propagated back up to the caller.
3240+
//
3241+
// Unknown policies are passed into the callback as nil values.
31993242
//
32003243
// If the caller wishes to re-use an existing boltdb transaction, then it
3201-
// should be passed as the first argument. Otherwise the first argument should
3244+
// should be passed as the first argument. Otherwise, the first argument should
32023245
// be nil and a fresh transaction will be created to execute the graph
32033246
// traversal.
3204-
func (c *ChannelGraph) ForEachNodeChannel(tx kvdb.RTx, nodePub route.Vertex,
3205-
cb func(kvdb.RTx, *models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
3247+
func (c *ChannelGraph) ForEachNodeChannelTx(tx kvdb.RTx,
3248+
nodePub route.Vertex, cb func(kvdb.RTx, *models.ChannelEdgeInfo,
3249+
*models.ChannelEdgePolicy,
32063250
*models.ChannelEdgePolicy) error) error {
32073251

32083252
return nodeTraversal(tx, nodePub[:], c.db, cb)
@@ -3705,7 +3749,7 @@ func (c *ChannelGraph) markEdgeLiveUnsafe(tx kvdb.RwTx, chanID uint64) error {
37053749
// We need to add the channel back into our graph cache, otherwise we
37063750
// won't use it for path finding.
37073751
if c.graphCache != nil {
3708-
edgeInfos, err := c.FetchChanInfos(tx, []uint64{chanID})
3752+
edgeInfos, err := c.fetchChanInfos(tx, []uint64{chanID})
37093753
if err != nil {
37103754
return err
37113755
}

channeldb/graph_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
141141

142142
// Next, fetch the node from the database to ensure everything was
143143
// serialized properly.
144-
dbNode, err := graph.FetchLightningNode(nil, testPub)
144+
dbNode, err := graph.FetchLightningNode(testPub)
145145
require.NoError(t, err, "unable to locate node")
146146

147147
if _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes); err != nil {
@@ -164,7 +164,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
164164

165165
// Finally, attempt to fetch the node again. This should fail as the
166166
// node should have been deleted from the database.
167-
_, err = graph.FetchLightningNode(nil, testPub)
167+
_, err = graph.FetchLightningNode(testPub)
168168
if err != ErrGraphNodeNotFound {
169169
t.Fatalf("fetch after delete should fail!")
170170
}
@@ -192,7 +192,7 @@ func TestPartialNode(t *testing.T) {
192192

193193
// Next, fetch the node from the database to ensure everything was
194194
// serialized properly.
195-
dbNode, err := graph.FetchLightningNode(nil, testPub)
195+
dbNode, err := graph.FetchLightningNode(testPub)
196196
require.NoError(t, err, "unable to locate node")
197197

198198
if _, exists, err := graph.HasLightningNode(dbNode.PubKeyBytes); err != nil {
@@ -222,7 +222,7 @@ func TestPartialNode(t *testing.T) {
222222

223223
// Finally, attempt to fetch the node again. This should fail as the
224224
// node should have been deleted from the database.
225-
_, err = graph.FetchLightningNode(nil, testPub)
225+
_, err = graph.FetchLightningNode(testPub)
226226
if err != ErrGraphNodeNotFound {
227227
t.Fatalf("fetch after delete should fail!")
228228
}
@@ -1055,7 +1055,7 @@ func TestGraphTraversal(t *testing.T) {
10551055
// outgoing channels for a particular node.
10561056
numNodeChans := 0
10571057
firstNode, secondNode := nodeList[0], nodeList[1]
1058-
err = graph.ForEachNodeChannel(nil, firstNode.PubKeyBytes,
1058+
err = graph.ForEachNodeChannel(firstNode.PubKeyBytes,
10591059
func(_ kvdb.RTx, _ *models.ChannelEdgeInfo, outEdge,
10601060
inEdge *models.ChannelEdgePolicy) error {
10611061

@@ -2685,7 +2685,7 @@ func TestFetchChanInfos(t *testing.T) {
26852685
// We'll now attempt to query for the range of channel ID's we just
26862686
// inserted into the database. We should get the exact same set of
26872687
// edges back.
2688-
resp, err := graph.FetchChanInfos(nil, edgeQuery)
2688+
resp, err := graph.FetchChanInfos(edgeQuery)
26892689
require.NoError(t, err, "unable to fetch chan edges")
26902690
if len(resp) != len(edges) {
26912691
t.Fatalf("expected %v edges, instead got %v", len(edges),
@@ -2737,7 +2737,7 @@ func TestIncompleteChannelPolicies(t *testing.T) {
27372737
// Ensure that channel is reported with unknown policies.
27382738
checkPolicies := func(node *LightningNode, expectedIn, expectedOut bool) {
27392739
calls := 0
2740-
err := graph.ForEachNodeChannel(nil, node.PubKeyBytes,
2740+
err := graph.ForEachNodeChannel(node.PubKeyBytes,
27412741
func(_ kvdb.RTx, _ *models.ChannelEdgeInfo, outEdge,
27422742
inEdge *models.ChannelEdgePolicy) error {
27432743

@@ -3014,7 +3014,7 @@ func TestPruneGraphNodes(t *testing.T) {
30143014

30153015
// Finally, we'll ensure that node3, the only fully unconnected node as
30163016
// properly deleted from the graph and not another node in its place.
3017-
_, err = graph.FetchLightningNode(nil, node3.PubKeyBytes)
3017+
_, err = graph.FetchLightningNode(node3.PubKeyBytes)
30183018
if err == nil {
30193019
t.Fatalf("node 3 should have been deleted!")
30203020
}
@@ -3048,13 +3048,13 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
30483048

30493049
// Ensure that node1 was inserted as a full node, while node2 only has
30503050
// a shell node present.
3051-
node1, err = graph.FetchLightningNode(nil, node1.PubKeyBytes)
3051+
node1, err = graph.FetchLightningNode(node1.PubKeyBytes)
30523052
require.NoError(t, err, "unable to fetch node1")
30533053
if !node1.HaveNodeAnnouncement {
30543054
t.Fatalf("have shell announcement for node1, shouldn't")
30553055
}
30563056

3057-
node2, err = graph.FetchLightningNode(nil, node2.PubKeyBytes)
3057+
node2, err = graph.FetchLightningNode(node2.PubKeyBytes)
30583058
require.NoError(t, err, "unable to fetch node2")
30593059
if node2.HaveNodeAnnouncement {
30603060
t.Fatalf("should have shell announcement for node2, but is full")

0 commit comments

Comments
 (0)