Skip to content

Commit 57b7fd6

Browse files
committed
graph/db: introduce VersionedGraph
For now, all instances will use V1 only so as not to change behaviour yet.
1 parent 6e9b448 commit 57b7fd6

File tree

9 files changed

+121
-83
lines changed

9 files changed

+121
-83
lines changed

autopilot/prefattach_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ type testGraph interface {
3131
}
3232

3333
type testDBGraph struct {
34-
db *graphdb.ChannelGraph
34+
db *graphdb.VersionedGraph
3535
databaseChannelGraph
3636
}
3737

3838
func newDiskChanGraph(t *testing.T) (testGraph, error) {
39-
graphDB := graphdb.MakeTestGraph(t)
39+
graphDB := graphdb.NewVersionedGraph(
40+
graphdb.MakeTestGraph(t), lnwire.GossipVersion1,
41+
)
4042
require.NoError(t, graphDB.Start())
4143
t.Cleanup(func() {
4244
require.NoError(t, graphDB.Stop())

graph/builder.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ type Builder struct {
111111

112112
bestHeight atomic.Uint32
113113

114-
cfg *Config
114+
cfg *Config
115+
v1Graph *graphdb.VersionedGraph
115116

116117
// newBlocks is a channel in which new blocks connected to the end of
117118
// the main chain are sent over, and blocks updated after a call to
@@ -146,7 +147,11 @@ var _ ChannelGraphSource = (*Builder)(nil)
146147
// NewBuilder constructs a new Builder.
147148
func NewBuilder(cfg *Config) (*Builder, error) {
148149
return &Builder{
149-
cfg: cfg,
150+
cfg: cfg,
151+
// For now, we'll just use V1 graph reader.
152+
v1Graph: graphdb.NewVersionedGraph(
153+
cfg.Graph, lnwire.GossipVersion1,
154+
),
150155
channelEdgeMtx: multimutex.NewMutex[uint64](),
151156
statTicker: ticker.New(defaultStatInterval),
152157
stats: new(builderStats),
@@ -1266,7 +1271,7 @@ func (b *Builder) GetChannelByID(chanID lnwire.ShortChannelID) (
12661271
func (b *Builder) FetchNode(ctx context.Context,
12671272
node route.Vertex) (*models.Node, error) {
12681273

1269-
return b.cfg.Graph.FetchNode(ctx, node)
1274+
return b.v1Graph.FetchNode(ctx, node)
12701275
}
12711276

12721277
// ForAllOutgoingChannels is used to iterate over all outgoing channels owned by

graph/builder_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
359359
// Create new router with same graph database.
360360
router, err := NewBuilder(&Config{
361361
SelfNode: selfNode.PubKeyBytes,
362-
Graph: ctx.graph,
362+
Graph: ctx.graph.ChannelGraph,
363363
Chain: ctx.chain,
364364
ChainView: ctx.chainView,
365365
ChannelPruneExpiry: time.Hour * 24,
@@ -1595,7 +1595,9 @@ func parseTestGraph(t *testing.T, useCache bool, path string) (
15951595
}
15961596

15971597
return &testGraphInstance{
1598-
graph: graph,
1598+
graph: graphdb.NewVersionedGraph(
1599+
graph, lnwire.GossipVersion1,
1600+
),
15991601
aliasMap: aliasMap,
16001602
privKeyMap: privKeyMap,
16011603
channelIDs: channelIDs,
@@ -1690,7 +1692,7 @@ func asymmetricTestChannel(alias1, alias2 string, capacity btcutil.Amount,
16901692

16911693
// assertChannelsPruned ensures that only the given channels are pruned from the
16921694
// graph out of the set of all channels.
1693-
func assertChannelsPruned(t *testing.T, graph *graphdb.ChannelGraph,
1695+
func assertChannelsPruned(t *testing.T, graph *graphdb.VersionedGraph,
16941696
channels []*testChannel, prunedChanIDs ...uint64) {
16951697

16961698
t.Helper()
@@ -1980,7 +1982,9 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
19801982
}
19811983

19821984
return &testGraphInstance{
1983-
graph: graph,
1985+
graph: graphdb.NewVersionedGraph(
1986+
graph, lnwire.GossipVersion1,
1987+
),
19841988
aliasMap: aliasMap,
19851989
privKeyMap: privKeyMap,
19861990
links: links,

graph/db/graph.go

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -293,23 +293,6 @@ func (c *ChannelGraph) AddNode(ctx context.Context,
293293
return nil
294294
}
295295

296-
// DeleteNode starts a new database transaction to remove a vertex/node
297-
// from the database according to the node's public key.
298-
func (c *ChannelGraph) DeleteNode(ctx context.Context,
299-
nodePub route.Vertex) error {
300-
301-
err := c.db.DeleteNode(ctx, lnwire.GossipVersion1, nodePub)
302-
if err != nil {
303-
return err
304-
}
305-
306-
if c.graphCache != nil {
307-
c.graphCache.RemoveNode(nodePub)
308-
}
309-
310-
return nil
311-
}
312-
313296
// AddChannelEdge adds a new (undirected, blank) edge to the graph database. An
314297
// undirected edge from the two target nodes are created. The information stored
315298
// denotes the static attributes of the channel, such as the channelID, the keys
@@ -603,13 +586,6 @@ func (c *ChannelGraph) UpdateEdgePolicy(ctx context.Context,
603586
return nil
604587
}
605588

606-
// AddrsForNode returns all known addresses for the target node public key.
607-
func (c *ChannelGraph) AddrsForNode(ctx context.Context,
608-
nodePub *btcec.PublicKey) (bool, []net.Addr, error) {
609-
610-
return c.db.AddrsForNode(ctx, lnwire.GossipVersion1, nodePub)
611-
}
612-
613589
// ForEachSourceNodeChannel iterates through all channels of the source node.
614590
func (c *ChannelGraph) ForEachSourceNodeChannel(ctx context.Context,
615591
cb func(chanPoint wire.OutPoint, havePolicy bool,
@@ -657,13 +633,6 @@ func (c *ChannelGraph) NodeUpdatesInHorizon(startTime, endTime time.Time,
657633
return c.db.NodeUpdatesInHorizon(startTime, endTime, opts...)
658634
}
659635

660-
// FetchNode attempts to look up a target node by its identity public key.
661-
func (c *ChannelGraph) FetchNode(ctx context.Context,
662-
nodePub route.Vertex) (*models.Node, error) {
663-
664-
return c.db.FetchNode(ctx, lnwire.GossipVersion1, nodePub)
665-
}
666-
667636
// HasV1Node determines if the graph has a vertex identified by the target node
668637
// in the V1 graph.
669638
func (c *ChannelGraph) HasV1Node(ctx context.Context,
@@ -672,14 +641,6 @@ func (c *ChannelGraph) HasV1Node(ctx context.Context,
672641
return c.db.HasV1Node(ctx, nodePub)
673642
}
674643

675-
// HasNode determines if the graph has a vertex identified by the target node
676-
// in the V1 graph.
677-
func (c *ChannelGraph) HasNode(ctx context.Context, nodePub [33]byte) (bool,
678-
error) {
679-
680-
return c.db.HasNode(ctx, lnwire.GossipVersion1, nodePub)
681-
}
682-
683644
// IsPublicNode determines whether the node is seen as public in the graph.
684645
func (c *ChannelGraph) IsPublicNode(pubKey [33]byte) (bool, error) {
685646
return c.db.IsPublicNode(pubKey)
@@ -811,6 +772,62 @@ func (c *ChannelGraph) PruneTip() (*chainhash.Hash, uint32, error) {
811772
return c.db.PruneTip()
812773
}
813774

775+
// VersionedGraph is a wrapper around ChannelGraph that will call underlying
776+
// Store methods with a specific gossip version.
777+
type VersionedGraph struct {
778+
*ChannelGraph
779+
v lnwire.GossipVersion
780+
}
781+
782+
// NewVersionedGraph creates a new VersionedGraph.
783+
func NewVersionedGraph(c *ChannelGraph,
784+
v lnwire.GossipVersion) *VersionedGraph {
785+
786+
return &VersionedGraph{
787+
ChannelGraph: c,
788+
v: v,
789+
}
790+
}
791+
792+
// FetchNode attempts to look up a target node by its identity public key.
793+
func (c *VersionedGraph) FetchNode(ctx context.Context,
794+
nodePub route.Vertex) (*models.Node, error) {
795+
796+
return c.db.FetchNode(ctx, c.v, nodePub)
797+
}
798+
799+
// AddrsForNode returns all known addresses for the target node public key.
800+
func (c *VersionedGraph) AddrsForNode(ctx context.Context,
801+
nodePub *btcec.PublicKey) (bool, []net.Addr, error) {
802+
803+
return c.db.AddrsForNode(ctx, c.v, nodePub)
804+
}
805+
806+
// DeleteNode starts a new database transaction to remove a vertex/node
807+
// from the database according to the node's public key.
808+
func (c *VersionedGraph) DeleteNode(ctx context.Context,
809+
nodePub route.Vertex) error {
810+
811+
err := c.db.DeleteNode(ctx, c.v, nodePub)
812+
if err != nil {
813+
return err
814+
}
815+
816+
if c.graphCache != nil {
817+
c.graphCache.RemoveNode(nodePub)
818+
}
819+
820+
return nil
821+
}
822+
823+
// HasNode determines if the graph has a vertex identified by the target node
824+
// in the V1 graph.
825+
func (c *VersionedGraph) HasNode(ctx context.Context, nodePub [33]byte) (bool,
826+
error) {
827+
828+
return c.db.HasNode(ctx, c.v, nodePub)
829+
}
830+
814831
// MakeTestGraph creates a new instance of the ChannelGraph for testing
815832
// purposes. The backing Store implementation depends on the version of
816833
// NewTestDB included in the current build.

graph/db/graph_test.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
9898
t.Parallel()
9999
ctx := t.Context()
100100

101-
graph := MakeTestGraph(t)
101+
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
102102

103103
// We'd like to test basic insertion/deletion for vertexes from the
104104
// graph, so we'll create a test vertex to start with.
@@ -123,7 +123,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
123123
// without any errors.
124124
node := nodeWithAddrs(testAddrs)
125125
require.NoError(t, graph.AddNode(ctx, node))
126-
assertNodeInCache(t, graph, node, testFeatures)
126+
assertNodeInCache(t, graph.ChannelGraph, node, testFeatures)
127127

128128
// Our AddNode implementation uses the batcher meaning that it is
129129
// possible that two updates for the same node announcement may be
@@ -153,16 +153,14 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
153153

154154
// Check that the node's features are fetched correctly. This check
155155
// will check the database directly.
156-
features, err = graph.db.FetchNodeFeatures(
157-
lnwire.GossipVersion1, node.PubKeyBytes,
158-
)
156+
features, err = graph.FetchNodeFeatures(node.PubKeyBytes)
159157
require.NoError(t, err)
160158
require.Equal(t, testFeatures, features)
161159

162160
// Next, delete the node from the graph, this should purge all data
163161
// related to the node.
164162
require.NoError(t, graph.DeleteNode(ctx, testPub))
165-
assertNodeNotInCache(t, graph, testPub)
163+
assertNodeNotInCache(t, graph.ChannelGraph, testPub)
166164

167165
// Attempting to delete the node again should return an error since
168166
// the node is no longer known.
@@ -287,7 +285,7 @@ func TestPartialNode(t *testing.T) {
287285
t.Parallel()
288286
ctx := t.Context()
289287

290-
graph := MakeTestGraph(t)
288+
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
291289

292290
// To insert a partial node, we need to add a channel edge that has
293291
// node keys for nodes we are not yet aware
@@ -301,8 +299,8 @@ func TestPartialNode(t *testing.T) {
301299

302300
// Both of the nodes should now be in both the graph (as partial/shell)
303301
// nodes _and_ the cache should also have an awareness of both nodes.
304-
assertNodeInCache(t, graph, &node1, nil)
305-
assertNodeInCache(t, graph, &node2, nil)
302+
assertNodeInCache(t, graph.ChannelGraph, &node1, nil)
303+
assertNodeInCache(t, graph.ChannelGraph, &node2, nil)
306304

307305
// Next, fetch the node2 from the database to ensure everything was
308306
// serialized properly.
@@ -332,7 +330,7 @@ func TestPartialNode(t *testing.T) {
332330
// Next, delete the node from the graph, this should purge all data
333331
// related to the node.
334332
require.NoError(t, graph.DeleteNode(ctx, pubKey1))
335-
assertNodeNotInCache(t, graph, testPub)
333+
assertNodeNotInCache(t, graph.ChannelGraph, testPub)
336334

337335
// Finally, attempt to fetch the node again. This should fail as the
338336
// node should have been deleted from the database.
@@ -3691,7 +3689,7 @@ func TestPruneGraphNodes(t *testing.T) {
36913689
t.Parallel()
36923690
ctx := t.Context()
36933691

3694-
graph := MakeTestGraph(t)
3692+
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
36953693

36963694
// We'll start off by inserting our source node, to ensure that it's
36973695
// the only node left after we prune the graph.
@@ -3742,7 +3740,7 @@ func TestPruneGraphNodes(t *testing.T) {
37423740
// source node (which can't be pruned), and node 1+2. Nodes 1 and two
37433741
// should still be left in the graph as there's half of an advertised
37443742
// edge between them.
3745-
assertNumNodes(t, graph, 3)
3743+
assertNumNodes(t, graph.ChannelGraph, 3)
37463744

37473745
// Finally, we'll ensure that node3, the only fully unconnected node as
37483746
// properly deleted from the graph and not another node in its place.
@@ -3757,7 +3755,7 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
37573755
t.Parallel()
37583756
ctx := t.Context()
37593757

3760-
graph := MakeTestGraph(t)
3758+
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
37613759

37623760
// To start, we'll create two nodes, and only add one of them to the
37633761
// channel graph.
@@ -3796,7 +3794,7 @@ func TestNodePruningUpdateIndexDeletion(t *testing.T) {
37963794
t.Parallel()
37973795
ctx := t.Context()
37983796

3799-
graph := MakeTestGraph(t)
3797+
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
38003798

38013799
// We'll first populate our graph with a single node that will be
38023800
// removed shortly.
@@ -4755,7 +4753,7 @@ func TestLightningNodePersistence(t *testing.T) {
47554753
ctx := t.Context()
47564754

47574755
// Create a new test graph instance.
4758-
graph := MakeTestGraph(t)
4756+
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
47594757

47604758
nodeAnnBytes, err := hex.DecodeString(testNodeAnn)
47614759
require.NoError(t, err)

graph/notifications_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ func TestEncodeHexColor(t *testing.T) {
10421042
type testCtx struct {
10431043
builder *Builder
10441044

1045-
graph *graphdb.ChannelGraph
1045+
graph *graphdb.VersionedGraph
10461046

10471047
aliases map[string]route.Vertex
10481048

@@ -1059,7 +1059,9 @@ type testCtx struct {
10591059
func createTestCtxSingleNode(t *testing.T,
10601060
startingHeight uint32) *testCtx {
10611061

1062-
graph := graphdb.MakeTestGraph(t)
1062+
graph := graphdb.NewVersionedGraph(
1063+
graphdb.MakeTestGraph(t), lnwire.GossipVersion1,
1064+
)
10631065
sourceNode := createTestNode(t)
10641066

10651067
require.NoError(t,
@@ -1086,7 +1088,7 @@ func (c *testCtx) RestartBuilder(t *testing.T) {
10861088
// start it.
10871089
builder, err := NewBuilder(&Config{
10881090
SelfNode: selfNode.PubKeyBytes,
1089-
Graph: c.graph,
1091+
Graph: c.graph.ChannelGraph,
10901092
Chain: c.chain,
10911093
ChainView: c.chainView,
10921094
Notifier: c.builder.cfg.Notifier,
@@ -1108,7 +1110,7 @@ func (c *testCtx) RestartBuilder(t *testing.T) {
11081110
}
11091111

11101112
type testGraphInstance struct {
1111-
graph *graphdb.ChannelGraph
1113+
graph *graphdb.VersionedGraph
11121114

11131115
// aliasMap is a map from a node's alias to its public key. This type is
11141116
// provided in order to allow easily look up from the human memorable
@@ -1157,7 +1159,7 @@ func createTestCtxFromGraphInstanceAssumeValid(t *testing.T,
11571159

11581160
graphBuilder, err := NewBuilder(&Config{
11591161
SelfNode: selfnode.PubKeyBytes,
1160-
Graph: graphInstance.graph,
1162+
Graph: graphInstance.graph.ChannelGraph,
11611163
Chain: chain,
11621164
ChainView: chainView,
11631165
Notifier: notifier,

0 commit comments

Comments
 (0)