routing: improve TestFindBlindedPaths readability

Add a helper so that we can refer to channels by using the aliases of
the nodes that own the channel instead of needing to use the raw channel
ID.
This commit is contained in:
Elle Mouton committed 2025-06-25 12:33:01 +02:00
1 parent da37fe20fc
commit 4bc0aee35b
1 file changed
+43 -7
+43 -7
View File
@@ -538,6 +538,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
aliasMap := make(map[string]route.Vertex) aliasMap := make(map[string]route.Vertex)
privKeyMap := make(map[string]*btcec.PrivateKey) privKeyMap := make(map[string]*btcec.PrivateKey)
channelIDs := make(map[route.Vertex]map[route.Vertex]uint64)
nodeIndex := byte(0) nodeIndex := byte(0)
addNodeWithAlias := func(alias string, features *lnwire.FeatureVector) ( addNodeWithAlias := func(alias string, features *lnwire.FeatureVector) (
@@ -652,6 +653,16 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
node1Vertex, node2Vertex = node2Vertex, node1Vertex node1Vertex, node2Vertex = node2Vertex, node1Vertex
} }
if _, ok := channelIDs[node1Vertex]; !ok {
channelIDs[node1Vertex] = map[route.Vertex]uint64{}
}
channelIDs[node1Vertex][node2Vertex] = channelID
if _, ok := channelIDs[node2Vertex]; !ok {
channelIDs[node2Vertex] = map[route.Vertex]uint64{}
}
channelIDs[node2Vertex][node1Vertex] = channelID
// We first insert the existence of the edge between the two // We first insert the existence of the edge between the two
// nodes. // nodes.
edgeInfo := models.ChannelEdgeInfo{ edgeInfo := models.ChannelEdgeInfo{
@@ -765,6 +776,7 @@ func createTestGraphFromChannels(t *testing.T, useCache bool,
mcBackend: graphBackend, mcBackend: graphBackend,
aliasMap: aliasMap, aliasMap: aliasMap,
privKeyMap: privKeyMap, privKeyMap: privKeyMap,
channelIDs: channelIDs,
links: links, links: links,
}, nil }, nil
} }
@@ -3192,6 +3204,16 @@ func newPathFindingTestContext(t *testing.T, useCache bool,
return ctx return ctx
} }
func (c *pathFindingTestContext) nodePairChannel(alias1, alias2 string) uint64 {
node1 := c.keyFromAlias(alias1)
node2 := c.keyFromAlias(alias2)
channel, ok := c.testGraphInstance.channelIDs[node1][node2]
require.True(c.t, ok)
return channel
}
func (c *pathFindingTestContext) keyFromAlias(alias string) route.Vertex { func (c *pathFindingTestContext) keyFromAlias(alias string) route.Vertex {
return c.testGraphInstance.aliasMap[alias] return c.testGraphInstance.aliasMap[alias]
} }
@@ -3916,7 +3938,9 @@ func TestFindBlindedPaths(t *testing.T) {
paths, err = ctx.findBlindedPaths(&blindedPathRestrictions{ paths, err = ctx.findBlindedPaths(&blindedPathRestrictions{
minNumHops: 1, minNumHops: 1,
maxNumHops: 1, maxNumHops: 1,
incomingChainedChannels: []uint64{2}, incomingChainedChannels: []uint64{
ctx.nodePairChannel("bob", "dave"),
},
}) })
require.NoError(t, err) require.NoError(t, err)
@@ -3930,7 +3954,9 @@ func TestFindBlindedPaths(t *testing.T) {
paths, err = ctx.findBlindedPaths(&blindedPathRestrictions{ paths, err = ctx.findBlindedPaths(&blindedPathRestrictions{
minNumHops: 1, minNumHops: 1,
maxNumHops: 2, maxNumHops: 2,
incomingChainedChannels: []uint64{2}, incomingChainedChannels: []uint64{
ctx.nodePairChannel("bob", "dave"),
},
}) })
require.NoError(t, err) require.NoError(t, err)
@@ -3947,7 +3973,9 @@ func TestFindBlindedPaths(t *testing.T) {
paths, err = ctx.findBlindedPaths(&blindedPathRestrictions{ paths, err = ctx.findBlindedPaths(&blindedPathRestrictions{
minNumHops: 2, minNumHops: 2,
maxNumHops: 3, maxNumHops: 3,
incomingChainedChannels: []uint64{3}, incomingChainedChannels: []uint64{
ctx.nodePairChannel("charlie", "dave"),
},
}) })
require.NoError(t, err) require.NoError(t, err)
@@ -3965,16 +3993,24 @@ func TestFindBlindedPaths(t *testing.T) {
minNumHops: 2, minNumHops: 2,
maxNumHops: 3, maxNumHops: 3,
nodeOmissionSet: fn.NewSet(ctx.keyFromAlias("charlie")), nodeOmissionSet: fn.NewSet(ctx.keyFromAlias("charlie")),
incomingChainedChannels: []uint64{3}, incomingChainedChannels: []uint64{
ctx.nodePairChannel("charlie", "dave"),
},
}) })
require.ErrorContains(t, err, "cannot simultaneously be included in "+ require.ErrorContains(t, err, "cannot simultaneously be included in "+
"the omission set and in the partially specified path") "the omission set and in the partially specified path")
// 11) Test the circular route error. // 11) Assert that an error is returned if a user accidentally tries
// to force a circular path.
_, err = ctx.findBlindedPaths(&blindedPathRestrictions{ _, err = ctx.findBlindedPaths(&blindedPathRestrictions{
minNumHops: 2, minNumHops: 2,
maxNumHops: 3, maxNumHops: 3,
incomingChainedChannels: []uint64{2, 7, 6, 3}, incomingChainedChannels: []uint64{
ctx.nodePairChannel("dave", "alice"),
ctx.nodePairChannel("alice", "frank"),
ctx.nodePairChannel("frank", "bob"),
ctx.nodePairChannel("bob", "dave"),
},
}) })
require.ErrorContains(t, err, "a circular route cannot be specified") require.ErrorContains(t, err, "circular route")
} }