graph/db: introduce VersionedGraph
For now, all instances will use V1 only so as not to change behaviour yet.
This commit is contained in:
9 files changed
+122
-84
No files matched your search
+56
-39
@@ -293,23 +293,6 @@ func (c *ChannelGraph) AddNode(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteNode starts a new database transaction to remove a vertex/node
|
||||
// from the database according to the node's public key.
|
||||
func (c *ChannelGraph) DeleteNode(ctx context.Context,
|
||||
nodePub route.Vertex) error {
|
||||
|
||||
err := c.db.DeleteNode(ctx, lnwire.GossipVersion1, nodePub)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.graphCache != nil {
|
||||
c.graphCache.RemoveNode(nodePub)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddChannelEdge adds a new (undirected, blank) edge to the graph database. An
|
||||
// undirected edge from the two target nodes are created. The information stored
|
||||
// denotes the static attributes of the channel, such as the channelID, the keys
|
||||
@@ -603,13 +586,6 @@ func (c *ChannelGraph) UpdateEdgePolicy(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddrsForNode returns all known addresses for the target node public key.
|
||||
func (c *ChannelGraph) AddrsForNode(ctx context.Context,
|
||||
nodePub *btcec.PublicKey) (bool, []net.Addr, error) {
|
||||
|
||||
return c.db.AddrsForNode(ctx, lnwire.GossipVersion1, nodePub)
|
||||
}
|
||||
|
||||
// ForEachSourceNodeChannel iterates through all channels of the source node.
|
||||
func (c *ChannelGraph) ForEachSourceNodeChannel(ctx context.Context,
|
||||
cb func(chanPoint wire.OutPoint, havePolicy bool,
|
||||
@@ -657,13 +633,6 @@ func (c *ChannelGraph) NodeUpdatesInHorizon(startTime, endTime time.Time,
|
||||
return c.db.NodeUpdatesInHorizon(startTime, endTime, opts...)
|
||||
}
|
||||
|
||||
// FetchNode attempts to look up a target node by its identity public key.
|
||||
func (c *ChannelGraph) FetchNode(ctx context.Context,
|
||||
nodePub route.Vertex) (*models.Node, error) {
|
||||
|
||||
return c.db.FetchNode(ctx, lnwire.GossipVersion1, nodePub)
|
||||
}
|
||||
|
||||
// HasV1Node determines if the graph has a vertex identified by the target node
|
||||
// in the V1 graph.
|
||||
func (c *ChannelGraph) HasV1Node(ctx context.Context,
|
||||
@@ -672,14 +641,6 @@ func (c *ChannelGraph) HasV1Node(ctx context.Context,
|
||||
return c.db.HasV1Node(ctx, nodePub)
|
||||
}
|
||||
|
||||
// HasNode determines if the graph has a vertex identified by the target node
|
||||
// in the V1 graph.
|
||||
func (c *ChannelGraph) HasNode(ctx context.Context, nodePub [33]byte) (bool,
|
||||
error) {
|
||||
|
||||
return c.db.HasNode(ctx, lnwire.GossipVersion1, nodePub)
|
||||
}
|
||||
|
||||
// IsPublicNode determines whether the node is seen as public in the graph.
|
||||
func (c *ChannelGraph) IsPublicNode(pubKey [33]byte) (bool, error) {
|
||||
return c.db.IsPublicNode(pubKey)
|
||||
@@ -811,6 +772,62 @@ func (c *ChannelGraph) PruneTip() (*chainhash.Hash, uint32, error) {
|
||||
return c.db.PruneTip()
|
||||
}
|
||||
|
||||
// VersionedGraph is a wrapper around ChannelGraph that will call underlying
|
||||
// Store methods with a specific gossip version.
|
||||
type VersionedGraph struct {
|
||||
*ChannelGraph
|
||||
v lnwire.GossipVersion
|
||||
}
|
||||
|
||||
// NewVersionedGraph creates a new VersionedGraph.
|
||||
func NewVersionedGraph(c *ChannelGraph,
|
||||
v lnwire.GossipVersion) *VersionedGraph {
|
||||
|
||||
return &VersionedGraph{
|
||||
ChannelGraph: c,
|
||||
v: v,
|
||||
}
|
||||
}
|
||||
|
||||
// FetchNode attempts to look up a target node by its identity public key.
|
||||
func (c *VersionedGraph) FetchNode(ctx context.Context,
|
||||
nodePub route.Vertex) (*models.Node, error) {
|
||||
|
||||
return c.db.FetchNode(ctx, c.v, nodePub)
|
||||
}
|
||||
|
||||
// AddrsForNode returns all known addresses for the target node public key.
|
||||
func (c *VersionedGraph) AddrsForNode(ctx context.Context,
|
||||
nodePub *btcec.PublicKey) (bool, []net.Addr, error) {
|
||||
|
||||
return c.db.AddrsForNode(ctx, c.v, nodePub)
|
||||
}
|
||||
|
||||
// DeleteNode starts a new database transaction to remove a vertex/node
|
||||
// from the database according to the node's public key.
|
||||
func (c *VersionedGraph) DeleteNode(ctx context.Context,
|
||||
nodePub route.Vertex) error {
|
||||
|
||||
err := c.db.DeleteNode(ctx, c.v, nodePub)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if c.graphCache != nil {
|
||||
c.graphCache.RemoveNode(nodePub)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasNode determines if the graph has a vertex identified by the target node
|
||||
// in the V1 graph.
|
||||
func (c *VersionedGraph) HasNode(ctx context.Context, nodePub [33]byte) (bool,
|
||||
error) {
|
||||
|
||||
return c.db.HasNode(ctx, c.v, nodePub)
|
||||
}
|
||||
|
||||
// MakeTestGraph creates a new instance of the ChannelGraph for testing
|
||||
// purposes. The backing Store implementation depends on the version of
|
||||
// NewTestDB included in the current build.
|
||||
|
||||
+13
-15
@@ -98,7 +98,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := t.Context()
|
||||
|
||||
graph := MakeTestGraph(t)
|
||||
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
|
||||
|
||||
// We'd like to test basic insertion/deletion for vertexes from the
|
||||
// graph, so we'll create a test vertex to start with.
|
||||
@@ -123,7 +123,7 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
||||
// without any errors.
|
||||
node := nodeWithAddrs(testAddrs)
|
||||
require.NoError(t, graph.AddNode(ctx, node))
|
||||
assertNodeInCache(t, graph, node, testFeatures)
|
||||
assertNodeInCache(t, graph.ChannelGraph, node, testFeatures)
|
||||
|
||||
// Our AddNode implementation uses the batcher meaning that it is
|
||||
// possible that two updates for the same node announcement may be
|
||||
@@ -153,16 +153,14 @@ func TestNodeInsertionAndDeletion(t *testing.T) {
|
||||
|
||||
// Check that the node's features are fetched correctly. This check
|
||||
// will check the database directly.
|
||||
features, err = graph.db.FetchNodeFeatures(
|
||||
lnwire.GossipVersion1, node.PubKeyBytes,
|
||||
)
|
||||
features, err = graph.FetchNodeFeatures(node.PubKeyBytes)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, testFeatures, features)
|
||||
|
||||
// Next, delete the node from the graph, this should purge all data
|
||||
// related to the node.
|
||||
require.NoError(t, graph.DeleteNode(ctx, testPub))
|
||||
assertNodeNotInCache(t, graph, testPub)
|
||||
assertNodeNotInCache(t, graph.ChannelGraph, testPub)
|
||||
|
||||
// Attempting to delete the node again should return an error since
|
||||
// the node is no longer known.
|
||||
@@ -287,7 +285,7 @@ func TestPartialNode(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := t.Context()
|
||||
|
||||
graph := MakeTestGraph(t)
|
||||
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
|
||||
|
||||
// To insert a partial node, we need to add a channel edge that has
|
||||
// node keys for nodes we are not yet aware
|
||||
@@ -301,8 +299,8 @@ func TestPartialNode(t *testing.T) {
|
||||
|
||||
// Both of the nodes should now be in both the graph (as partial/shell)
|
||||
// nodes _and_ the cache should also have an awareness of both nodes.
|
||||
assertNodeInCache(t, graph, &node1, nil)
|
||||
assertNodeInCache(t, graph, &node2, nil)
|
||||
assertNodeInCache(t, graph.ChannelGraph, &node1, nil)
|
||||
assertNodeInCache(t, graph.ChannelGraph, &node2, nil)
|
||||
|
||||
// Next, fetch the node2 from the database to ensure everything was
|
||||
// serialized properly.
|
||||
@@ -332,7 +330,7 @@ func TestPartialNode(t *testing.T) {
|
||||
// Next, delete the node from the graph, this should purge all data
|
||||
// related to the node.
|
||||
require.NoError(t, graph.DeleteNode(ctx, pubKey1))
|
||||
assertNodeNotInCache(t, graph, testPub)
|
||||
assertNodeNotInCache(t, graph.ChannelGraph, testPub)
|
||||
|
||||
// Finally, attempt to fetch the node again. This should fail as the
|
||||
// node should have been deleted from the database.
|
||||
@@ -3750,7 +3748,7 @@ func TestPruneGraphNodes(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := t.Context()
|
||||
|
||||
graph := MakeTestGraph(t)
|
||||
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
|
||||
|
||||
// We'll start off by inserting our source node, to ensure that it's
|
||||
// the only node left after we prune the graph.
|
||||
@@ -3801,7 +3799,7 @@ func TestPruneGraphNodes(t *testing.T) {
|
||||
// source node (which can't be pruned), and node 1+2. Nodes 1 and two
|
||||
// should still be left in the graph as there's half of an advertised
|
||||
// edge between them.
|
||||
assertNumNodes(t, graph, 3)
|
||||
assertNumNodes(t, graph.ChannelGraph, 3)
|
||||
|
||||
// Finally, we'll ensure that node3, the only fully unconnected node as
|
||||
// properly deleted from the graph and not another node in its place.
|
||||
@@ -3816,7 +3814,7 @@ func TestAddChannelEdgeShellNodes(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := t.Context()
|
||||
|
||||
graph := MakeTestGraph(t)
|
||||
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
|
||||
|
||||
// To start, we'll create two nodes, and only add one of them to the
|
||||
// channel graph.
|
||||
@@ -3855,7 +3853,7 @@ func TestNodePruningUpdateIndexDeletion(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := t.Context()
|
||||
|
||||
graph := MakeTestGraph(t)
|
||||
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
|
||||
|
||||
// We'll first populate our graph with a single node that will be
|
||||
// removed shortly.
|
||||
@@ -4836,7 +4834,7 @@ func TestLightningNodePersistence(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
|
||||
// Create a new test graph instance.
|
||||
graph := MakeTestGraph(t)
|
||||
graph := NewVersionedGraph(MakeTestGraph(t), lnwire.GossipVersion1)
|
||||
|
||||
nodeAnnBytes, err := hex.DecodeString(testNodeAnn)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in new issue
Block a user