graph/db: version batched edge and cache node channel tests

Convert testBatchedAddChannelEdge and testGraphCacheForEachNodeChannel
to run against both v1 and v2 gossip versions.
This commit is contained in:
Elle Mouton committed 2026-02-20 09:19:24 +02:00
1 parent fc19a24d07
commit 95d6af12d4
1 file changed
+53 -36
+53 -36
View File
@@ -190,6 +190,14 @@ var versionedTests = []versionedTest{
name: "disabled channel ids", name: "disabled channel ids",
test: testDisabledChannelIDs, test: testDisabledChannelIDs,
}, },
{
name: "batched add channel edge",
test: testBatchedAddChannelEdge,
},
{
name: "graph cache for each node channel",
test: testGraphCacheForEachNodeChannel,
},
} }
// TestVersionedDBs runs various tests against both v1 and v2 versioned // TestVersionedDBs runs various tests against both v1 and v2 versioned
@@ -5010,19 +5018,19 @@ func TestComputeFee(t *testing.T) {
// TestBatchedAddChannelEdge asserts that BatchedAddChannelEdge properly // TestBatchedAddChannelEdge asserts that BatchedAddChannelEdge properly
// executes multiple AddChannelEdge requests in a single txn. // executes multiple AddChannelEdge requests in a single txn.
func TestBatchedAddChannelEdge(t *testing.T) { func testBatchedAddChannelEdge(t *testing.T, v lnwire.GossipVersion) {
t.Parallel() t.Parallel()
ctx := t.Context() ctx := t.Context()
graph := MakeTestGraph(t) graph := NewVersionedGraph(MakeTestGraph(t), v)
sourceNode := createTestVertex(t, lnwire.GossipVersion1) sourceNode := createTestVertex(t, v)
require.Nil(t, graph.SetSourceNode(ctx, sourceNode)) require.Nil(t, graph.SetSourceNode(ctx, sourceNode))
// We'd like to test the insertion/deletion of edges, so we create two // We'd like to test the insertion/deletion of edges, so we create two
// vertexes to connect. // vertexes to connect.
node1 := createTestVertex(t, lnwire.GossipVersion1) node1 := createTestVertex(t, v)
node2 := createTestVertex(t, lnwire.GossipVersion1) node2 := createTestVertex(t, v)
// In addition to the fake vertexes we create some fake channel // In addition to the fake vertexes we create some fake channel
// identifiers. // identifiers.
@@ -5045,23 +5053,20 @@ func TestBatchedAddChannelEdge(t *testing.T) {
// Create an edge which has its block height at 156. // Create an edge which has its block height at 156.
height := uint32(156) height := uint32(156)
edgeInfo, _ := createEdge( edgeInfo, _ := createEdge(v, height, 0, 0, 0, node1, node2)
lnwire.GossipVersion1, height, 0, 0, 0, node1, node2,
)
// Create an edge with block height 157. We give it // Create an edge with block height 157. We give it
// maximum values for tx index and position, to make // maximum values for tx index and position, to make
// sure our database range scan get edges from the // sure our database range scan get edges from the
// entire range. // entire range.
edgeInfo2, _ := createEdge( edgeInfo2, _ := createEdge(
lnwire.GossipVersion1, height+1, v, height+1, math.MaxUint32&0x00ffffff, math.MaxUint16, 1,
math.MaxUint32&0x00ffffff, math.MaxUint16, 1, node1, node1, node2,
node2,
) )
// Create a third edge, this with a block height of 155. // Create a third edge, this with a block height of 155.
edgeInfo3, _ := createEdge( edgeInfo3, _ := createEdge(
lnwire.GossipVersion1, height-1, 0, 0, 2, node1, node2, v, height-1, 0, 0, 2, node1, node2,
) )
edges := []models.ChannelEdgeInfo{*edgeInfo, *edgeInfo2, *edgeInfo3} edges := []models.ChannelEdgeInfo{*edgeInfo, *edgeInfo2, *edgeInfo3}
@@ -5203,25 +5208,24 @@ func BenchmarkForEachChannel(b *testing.B) {
// TestGraphCacheForEachNodeChannel tests that the forEachNodeDirectedChannel // TestGraphCacheForEachNodeChannel tests that the forEachNodeDirectedChannel
// method works as expected, and is able to handle nil self edges. // method works as expected, and is able to handle nil self edges.
func TestGraphCacheForEachNodeChannel(t *testing.T) { func testGraphCacheForEachNodeChannel(t *testing.T,
v lnwire.GossipVersion) {
t.Parallel() t.Parallel()
ctx := t.Context() ctx := t.Context()
graph := MakeTestGraph(t) graph := NewVersionedGraph(MakeTestGraph(t), v)
// Unset the channel graph cache to simulate the user running with the // Unset the channel graph cache to simulate the user running with the
// option turned off. // option turned off.
graph.graphCache = nil graph.graphCache = nil
node1 := createTestVertex(t, lnwire.GossipVersion1) node1 := createTestVertex(t, v)
require.NoError(t, graph.AddNode(ctx, node1)) require.NoError(t, graph.AddNode(ctx, node1))
node2 := createTestVertex(t, lnwire.GossipVersion1) node2 := createTestVertex(t, v)
require.NoError(t, graph.AddNode(ctx, node2)) require.NoError(t, graph.AddNode(ctx, node2))
// Create an edge and add it to the db. // Create an edge and add it to the db.
edgeInfo, e1, e2 := createChannelEdge( edgeInfo, e1, e2 := createChannelEdge(node1, node2, v)
node1, node2, lnwire.GossipVersion1,
)
// Because of lexigraphical sorting and the usage of random node keys in // Because of lexigraphical sorting and the usage of random node keys in
// this test, we need to determine which edge belongs to node 1 at // this test, we need to determine which edge belongs to node 1 at
@@ -5238,8 +5242,8 @@ func TestGraphCacheForEachNodeChannel(t *testing.T) {
getSingleChannel := func() *DirectedChannel { getSingleChannel := func() *DirectedChannel {
var ch *DirectedChannel var ch *DirectedChannel
err := graph.ForEachNodeDirectedChannel( err := graph.db.ForEachNodeDirectedChannel(
node1.PubKeyBytes, v, node1.PubKeyBytes,
func(c *DirectedChannel) error { func(c *DirectedChannel) error {
require.Nil(t, ch) require.Nil(t, ch)
ch = c ch = c
@@ -5265,6 +5269,12 @@ func TestGraphCacheForEachNodeChannel(t *testing.T) {
FeeRate: 20, FeeRate: 20,
} }
edge1.InboundFee = fn.Some(inboundFee) edge1.InboundFee = fn.Some(inboundFee)
switch v {
case lnwire.GossipVersion1:
edge1.LastUpdate = edge1.LastUpdate.Add(time.Second)
case lnwire.GossipVersion2:
edge1.LastBlockHeight = nextBlockHeight()
}
require.NoError(t, graph.UpdateEdgePolicy(ctx, edge1)) require.NoError(t, graph.UpdateEdgePolicy(ctx, edge1))
edge1 = copyEdgePolicy(edge1) // Avoid read/write race conditions. edge1 = copyEdgePolicy(edge1) // Avoid read/write race conditions.
@@ -5272,22 +5282,29 @@ func TestGraphCacheForEachNodeChannel(t *testing.T) {
require.NotNil(t, directedChan) require.NotNil(t, directedChan)
require.Equal(t, inboundFee, directedChan.InboundFee) require.Equal(t, inboundFee, directedChan.InboundFee)
// Set an invalid inbound fee and check that persistence fails. // The below test only applies to v1 since in v2, we would fail TLV
edge1.ExtraOpaqueData = []byte{ // parsing at the lnwire level when parsing bytes from the wire.
253, 217, 3, 8, 0, if v == lnwire.GossipVersion1 {
} // Set an invalid inbound fee and check that persistence fails.
// We need to update the timestamp so that we don't hit the DB conflict edge1.ExtraOpaqueData = []byte{
// error when we try to update the edge policy. 253, 217, 3, 8, 0,
edge1.LastUpdate = edge1.LastUpdate.Add(time.Second) }
require.ErrorIs( // We need to update the timestamp so that we don't hit
t, graph.UpdateEdgePolicy(ctx, edge1), ErrParsingExtraTLVBytes, // the DB conflict error when we try to update the edge
) // policy.
edge1.LastUpdate = edge1.LastUpdate.Add(time.Second)
require.ErrorIs(
t, graph.UpdateEdgePolicy(ctx, edge1),
ErrParsingExtraTLVBytes,
)
// Since persistence of the last update failed, we should still bet // Since persistence of the last update failed, we should
// the previous result when we query the channel again. // still bet the previous result when we query the channel
directedChan = getSingleChannel() // again.
require.NotNil(t, directedChan) directedChan = getSingleChannel()
require.Equal(t, inboundFee, directedChan.InboundFee) require.NotNil(t, directedChan)
require.Equal(t, inboundFee, directedChan.InboundFee)
}
} }
// TestGraphLoading asserts that the cache is properly reconstructed after a // TestGraphLoading asserts that the cache is properly reconstructed after a