Skip to content

Commit 535aaa0

Browse files
committed
graph/db: version IsPublicNode
1 parent 2a5a4a0 commit 535aaa0

File tree

6 files changed

+46
-20
lines changed

6 files changed

+46
-20
lines changed

graph/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,7 @@ func (b *Builder) IsStaleNode(ctx context.Context, node route.Vertex,
13301330
//
13311331
// NOTE: This method is part of the ChannelGraphSource interface.
13321332
func (b *Builder) IsPublicNode(node route.Vertex) (bool, error) {
1333-
return b.cfg.Graph.IsPublicNode(node)
1333+
return b.v1Graph.IsPublicNode(node)
13341334
}
13351335

13361336
// IsKnownEdge returns true if the graph source already knows of the passed

graph/db/graph.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ func (c *ChannelGraph) HasV1Node(ctx context.Context,
637637

638638
// IsPublicNode determines whether the node is seen as public in the graph.
639639
func (c *ChannelGraph) IsPublicNode(pubKey [33]byte) (bool, error) {
640-
return c.db.IsPublicNode(pubKey)
640+
return c.db.IsPublicNode(lnwire.GossipVersion1, pubKey)
641641
}
642642

643643
// ForEachChannel iterates through all channel edges stored within the graph.
@@ -861,6 +861,11 @@ func (c *VersionedGraph) DeleteChannelEdges(strictZombiePruning,
861861
return err
862862
}
863863

864+
// IsPublicNode determines whether the node is seen as public in the graph.
865+
func (c *VersionedGraph) IsPublicNode(pubKey [33]byte) (bool, error) {
866+
return c.db.IsPublicNode(c.v, pubKey)
867+
}
868+
864869
// MakeTestGraph creates a new instance of the ChannelGraph for testing
865870
// purposes. The backing Store implementation depends on the version of
866871
// NewTestDB included in the current build.

graph/db/graph_test.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ var versionedTests = []versionedTest{
142142
name: "partial node",
143143
test: testPartialNode,
144144
},
145+
{
146+
name: "node is public",
147+
test: testNodeIsPublic,
148+
},
145149
}
146150

147151
// TestVersionedDBs runs various tests against both v1 and v2 versioned
@@ -4078,9 +4082,9 @@ func nextBlockHeight() uint32 {
40784082
return updateBlock
40794083
}
40804084

4081-
// TestNodeIsPublic ensures that we properly detect nodes that are seen as
4085+
// testNodeIsPublic ensures that we properly detect nodes that are seen as
40824086
// public within the network graph.
4083-
func TestNodeIsPublic(t *testing.T) {
4087+
func testNodeIsPublic(t *testing.T, v lnwire.GossipVersion) {
40844088
t.Parallel()
40854089
ctx := t.Context()
40864090

@@ -4092,29 +4096,29 @@ func TestNodeIsPublic(t *testing.T) {
40924096
// We'll need to create a separate database and channel graph for each
40934097
// participant to replicate real-world scenarios (private edges being in
40944098
// some graphs but not others, etc.).
4095-
aliceGraph := MakeTestGraph(t)
4096-
aliceNode := createTestVertex(t, lnwire.GossipVersion1)
4099+
aliceGraph := NewVersionedGraph(MakeTestGraph(t), v)
4100+
aliceNode := createTestVertex(t, v)
40974101
err := aliceGraph.SetSourceNode(ctx, aliceNode)
40984102
require.NoError(t, err, "unable to set source node")
40994103

4100-
bobGraph := MakeTestGraph(t)
4101-
bobNode := createTestVertex(t, lnwire.GossipVersion1)
4104+
bobGraph := NewVersionedGraph(MakeTestGraph(t), v)
4105+
bobNode := createTestVertex(t, v)
41024106
err = bobGraph.SetSourceNode(ctx, bobNode)
41034107
require.NoError(t, err, "unable to set source node")
41044108

4105-
carolGraph := MakeTestGraph(t)
4106-
carolNode := createTestVertex(t, lnwire.GossipVersion1)
4109+
carolGraph := NewVersionedGraph(MakeTestGraph(t), v)
4110+
carolNode := createTestVertex(t, v)
41074111
err = carolGraph.SetSourceNode(ctx, carolNode)
41084112
require.NoError(t, err, "unable to set source node")
41094113

4110-
aliceBobEdge, _ := createEdge(lnwire.GossipVersion1, 10, 0, 0, 0, aliceNode, bobNode)
4111-
bobCarolEdge, _ := createEdge(lnwire.GossipVersion1, 10, 1, 0, 1, bobNode, carolNode)
4114+
aliceBobEdge, _ := createEdge(v, 10, 0, 0, 0, aliceNode, bobNode)
4115+
bobCarolEdge, _ := createEdge(v, 10, 1, 0, 1, bobNode, carolNode)
41124116

41134117
// After creating all of our nodes and edges, we'll add them to each
41144118
// participant's graph.
41154119
nodes := []*models.Node{aliceNode, bobNode, carolNode}
41164120
edges := []*models.ChannelEdgeInfo{aliceBobEdge, bobCarolEdge}
4117-
graphs := []*ChannelGraph{aliceGraph, bobGraph, carolGraph}
4121+
graphs := []*VersionedGraph{aliceGraph, bobGraph, carolGraph}
41184122
for _, graph := range graphs {
41194123
for _, node := range nodes {
41204124
node.LastUpdate = nextUpdateTime()
@@ -4130,7 +4134,7 @@ func TestNodeIsPublic(t *testing.T) {
41304134
// checkNodes is a helper closure that will be used to assert that the
41314135
// given nodes are seen as public/private within the given graphs.
41324136
checkNodes := func(nodes []*models.Node,
4133-
graphs []*ChannelGraph, public bool) {
4137+
graphs []*VersionedGraph, public bool) {
41344138

41354139
t.Helper()
41364140

@@ -4162,7 +4166,7 @@ func TestNodeIsPublic(t *testing.T) {
41624166
}
41634167
checkNodes(
41644168
[]*models.Node{aliceNode},
4165-
[]*ChannelGraph{bobGraph, carolGraph},
4169+
[]*VersionedGraph{bobGraph, carolGraph},
41664170
false,
41674171
)
41684172

@@ -4190,7 +4194,7 @@ func TestNodeIsPublic(t *testing.T) {
41904194
// node from both Alice's and Carol's perspective.
41914195
checkNodes(
41924196
[]*models.Node{bobNode},
4193-
[]*ChannelGraph{aliceGraph, carolGraph},
4197+
[]*VersionedGraph{aliceGraph, carolGraph},
41944198
false,
41954199
)
41964200
}

graph/db/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ type Store interface { //nolint:interfacebloat
146146
// IsPublicNode is a helper method that determines whether the node with
147147
// the given public key is seen as a public node in the graph from the
148148
// graph's source node's point of view.
149-
IsPublicNode(pubKey [33]byte) (bool, error)
149+
IsPublicNode(v lnwire.GossipVersion, pubKey [33]byte) (bool, error)
150150

151151
// GraphSession will provide the call-back with access to a
152152
// NodeTraverser instance which can be used to perform queries against

graph/db/kv_store.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4006,7 +4006,13 @@ func (c *KVStore) FetchChannelEdgesByID(chanID uint64) (
40064006
// IsPublicNode is a helper method that determines whether the node with the
40074007
// given public key is seen as a public node in the graph from the graph's
40084008
// source node's point of view.
4009-
func (c *KVStore) IsPublicNode(pubKey [33]byte) (bool, error) {
4009+
func (c *KVStore) IsPublicNode(v lnwire.GossipVersion, pubKey [33]byte) (bool,
4010+
error) {
4011+
4012+
if v != lnwire.GossipVersion1 {
4013+
return false, ErrVersionNotSupportedForKVDB
4014+
}
4015+
40104016
var nodeIsPublic bool
40114017
err := kvdb.View(c.db, func(tx kvdb.RTx) error {
40124018
nodes := tx.ReadBucket(nodeBucket)

graph/db/sql_store.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type SQLQueries interface {
4949
ListNodesPaginated(ctx context.Context, arg sqlc.ListNodesPaginatedParams) ([]sqlc.GraphNode, error)
5050
ListNodeIDsAndPubKeys(ctx context.Context, arg sqlc.ListNodeIDsAndPubKeysParams) ([]sqlc.ListNodeIDsAndPubKeysRow, error)
5151
IsPublicV1Node(ctx context.Context, pubKey []byte) (bool, error)
52+
IsPublicV2Node(ctx context.Context, pubKey []byte) (bool, error)
5253
DeleteUnconnectedNodes(ctx context.Context) ([][]byte, error)
5354
DeleteNodeByPubKey(ctx context.Context, arg sqlc.DeleteNodeByPubKeyParams) (sql.Result, error)
5455
DeleteNode(ctx context.Context, id int64) error
@@ -2331,13 +2332,23 @@ func (s *SQLStore) ChannelID(chanPoint *wire.OutPoint) (uint64, error) {
23312332
// source node's point of view.
23322333
//
23332334
// NOTE: part of the Store interface.
2334-
func (s *SQLStore) IsPublicNode(pubKey [33]byte) (bool, error) {
2335+
func (s *SQLStore) IsPublicNode(v lnwire.GossipVersion, pubKey [33]byte) (bool,
2336+
error) {
2337+
23352338
ctx := context.TODO()
2339+
if !isKnownGossipVersion(v) {
2340+
return false, fmt.Errorf("unsupported gossip version: %d", v)
2341+
}
23362342

23372343
var isPublic bool
23382344
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
23392345
var err error
2340-
isPublic, err = db.IsPublicV1Node(ctx, pubKey[:])
2346+
switch v {
2347+
case lnwire.GossipVersion1:
2348+
isPublic, err = db.IsPublicV1Node(ctx, pubKey[:])
2349+
case lnwire.GossipVersion2:
2350+
isPublic, err = db.IsPublicV2Node(ctx, pubKey[:])
2351+
}
23412352

23422353
return err
23432354
}, sqldb.NoOpReset)

0 commit comments

Comments
 (0)