routing: allow nil maps for ignored edges and nodes

This allows removing a lot of empty map initialization code and makes
the code more readable.
This commit is contained in:
Joost Jager
2019-03-05 11:42:25 +01:00
parent b2b28b49b1
commit 4376f3e1bd
3 changed files with 31 additions and 91 deletions

View File

@ -513,6 +513,15 @@ func findPath(g *graphParams, r *RestrictParams,
// mapped to within `next`. // mapped to within `next`.
next := make(map[Vertex]*channeldb.ChannelEdgePolicy) next := make(map[Vertex]*channeldb.ChannelEdgePolicy)
ignoredEdges := r.IgnoredEdges
if ignoredEdges == nil {
ignoredEdges = make(map[EdgeLocator]struct{})
}
ignoredNodes := r.IgnoredNodes
if ignoredNodes == nil {
ignoredNodes = make(map[Vertex]struct{})
}
// processEdge is a helper closure that will be used to make sure edges // processEdge is a helper closure that will be used to make sure edges
// satisfy our specific requirements. // satisfy our specific requirements.
processEdge := func(fromNode *channeldb.LightningNode, processEdge := func(fromNode *channeldb.LightningNode,
@ -544,12 +553,12 @@ func findPath(g *graphParams, r *RestrictParams,
// If this vertex or edge has been black listed, then we'll // If this vertex or edge has been black listed, then we'll
// skip exploring this edge. // skip exploring this edge.
if _, ok := r.IgnoredNodes[fromVertex]; ok { if _, ok := ignoredNodes[fromVertex]; ok {
return return
} }
locator := newEdgeLocator(edge) locator := newEdgeLocator(edge)
if _, ok := r.IgnoredEdges[*locator]; ok { if _, ok := ignoredEdges[*locator]; ok {
return return
} }
@ -784,9 +793,6 @@ func findPaths(tx *bbolt.Tx, graph *channeldb.ChannelGraph,
amt lnwire.MilliSatoshi, feeLimit lnwire.MilliSatoshi, numPaths uint32, amt lnwire.MilliSatoshi, feeLimit lnwire.MilliSatoshi, numPaths uint32,
bandwidthHints map[uint64]lnwire.MilliSatoshi) ([][]*channeldb.ChannelEdgePolicy, error) { bandwidthHints map[uint64]lnwire.MilliSatoshi) ([][]*channeldb.ChannelEdgePolicy, error) {
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
// TODO(roasbeef): modifying ordering within heap to eliminate final // TODO(roasbeef): modifying ordering within heap to eliminate final
// sorting step? // sorting step?
var ( var (
@ -804,9 +810,7 @@ func findPaths(tx *bbolt.Tx, graph *channeldb.ChannelGraph,
bandwidthHints: bandwidthHints, bandwidthHints: bandwidthHints,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: feeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: feeLimit,
}, },
source, target, amt, source, target, amt,
) )
@ -839,8 +843,8 @@ func findPaths(tx *bbolt.Tx, graph *channeldb.ChannelGraph,
// we'll exclude from the next path finding attempt. // we'll exclude from the next path finding attempt.
// These are required to ensure the paths are unique // These are required to ensure the paths are unique
// and loopless. // and loopless.
ignoredEdges = make(map[EdgeLocator]struct{}) ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes = make(map[Vertex]struct{}) ignoredVertexes := make(map[Vertex]struct{})
// Our spur node is the i-th node in the prior shortest // Our spur node is the i-th node in the prior shortest
// path, and our root path will be all nodes in the // path, and our root path will be all nodes in the

View File

@ -598,9 +598,6 @@ func TestFindLowestFeePath(t *testing.T) {
} }
sourceVertex := Vertex(sourceNode.PubKeyBytes) sourceVertex := Vertex(sourceNode.PubKeyBytes)
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
const ( const (
startingHeight = 100 startingHeight = 100
finalHopCLTV = 1 finalHopCLTV = 1
@ -613,9 +610,7 @@ func TestFindLowestFeePath(t *testing.T) {
graph: testGraphInstance.graph, graph: testGraphInstance.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, paymentAmt, sourceNode, target, paymentAmt,
) )
@ -744,9 +739,6 @@ func testBasicGraphPathFindingCase(t *testing.T, graphInstance *testGraphInstanc
} }
sourceVertex := Vertex(sourceNode.PubKeyBytes) sourceVertex := Vertex(sourceNode.PubKeyBytes)
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
const ( const (
startingHeight = 100 startingHeight = 100
finalHopCLTV = 1 finalHopCLTV = 1
@ -759,9 +751,7 @@ func testBasicGraphPathFindingCase(t *testing.T, graphInstance *testGraphInstanc
graph: graphInstance.graph, graph: graphInstance.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: test.feeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: test.feeLimit,
}, },
sourceNode, target, paymentAmt, sourceNode, target, paymentAmt,
) )
@ -1224,9 +1214,6 @@ func TestNewRoutePathTooLong(t *testing.T) {
t.Fatalf("unable to fetch source node: %v", err) t.Fatalf("unable to fetch source node: %v", err)
} }
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
paymentAmt := lnwire.NewMSatFromSatoshis(100) paymentAmt := lnwire.NewMSatFromSatoshis(100)
// We start by confirming that routing a payment 20 hops away is // We start by confirming that routing a payment 20 hops away is
@ -1237,9 +1224,7 @@ func TestNewRoutePathTooLong(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, paymentAmt, sourceNode, target, paymentAmt,
) )
@ -1255,9 +1240,7 @@ func TestNewRoutePathTooLong(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, paymentAmt, sourceNode, target, paymentAmt,
) )
@ -1283,9 +1266,6 @@ func TestPathNotAvailable(t *testing.T) {
t.Fatalf("unable to fetch source node: %v", err) t.Fatalf("unable to fetch source node: %v", err)
} }
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
// With the test graph loaded, we'll test that queries for target that // With the test graph loaded, we'll test that queries for target that
// are either unreachable within the graph, or unknown result in an // are either unreachable within the graph, or unknown result in an
// error. // error.
@ -1304,9 +1284,7 @@ func TestPathNotAvailable(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, unknownNode, 100, sourceNode, unknownNode, 100,
) )
@ -1328,8 +1306,6 @@ func TestPathInsufficientCapacity(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to fetch source node: %v", err) t.Fatalf("unable to fetch source node: %v", err)
} }
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
// Next, test that attempting to find a path in which the current // Next, test that attempting to find a path in which the current
// channel graph cannot support due to insufficient capacity triggers // channel graph cannot support due to insufficient capacity triggers
@ -1347,9 +1323,7 @@ func TestPathInsufficientCapacity(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1373,8 +1347,6 @@ func TestRouteFailMinHTLC(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to fetch source node: %v", err) t.Fatalf("unable to fetch source node: %v", err)
} }
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
// We'll not attempt to route an HTLC of 10 SAT from roasbeef to Son // We'll not attempt to route an HTLC of 10 SAT from roasbeef to Son
// Goku. However, the min HTLC of Son Goku is 1k SAT, as a result, this // Goku. However, the min HTLC of Son Goku is 1k SAT, as a result, this
@ -1386,9 +1358,7 @@ func TestRouteFailMinHTLC(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1438,8 +1408,6 @@ func TestRouteFailMaxHTLC(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to fetch source node: %v", err) t.Fatalf("unable to fetch source node: %v", err)
} }
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
// First, attempt to send a payment greater than the max HTLC we are // First, attempt to send a payment greater than the max HTLC we are
// about to set, which should succeed. // about to set, which should succeed.
@ -1450,9 +1418,7 @@ func TestRouteFailMaxHTLC(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1476,9 +1442,7 @@ func TestRouteFailMaxHTLC(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1505,8 +1469,6 @@ func TestRouteFailDisabledEdge(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to fetch source node: %v", err) t.Fatalf("unable to fetch source node: %v", err)
} }
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
// First, we'll try to route from roasbeef -> sophon. This should // First, we'll try to route from roasbeef -> sophon. This should
// succeed without issue, and return a single path via phamnuwen // succeed without issue, and return a single path via phamnuwen
@ -1517,9 +1479,7 @@ func TestRouteFailDisabledEdge(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1549,9 +1509,7 @@ func TestRouteFailDisabledEdge(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1578,9 +1536,7 @@ func TestRouteFailDisabledEdge(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1605,8 +1561,6 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to fetch source node: %v", err) t.Fatalf("unable to fetch source node: %v", err)
} }
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
// First, we'll try to route from roasbeef -> sophon. This should // First, we'll try to route from roasbeef -> sophon. This should
// succeed without issue, and return a path via songoku, as that's the // succeed without issue, and return a path via songoku, as that's the
@ -1618,9 +1572,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
graph: graph.graph, graph: graph.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1646,9 +1598,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
bandwidthHints: bandwidths, bandwidthHints: bandwidths,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1668,9 +1618,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
bandwidthHints: bandwidths, bandwidthHints: bandwidths,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -1703,9 +1651,7 @@ func TestPathSourceEdgesBandwidth(t *testing.T) {
bandwidthHints: bandwidths, bandwidthHints: bandwidths,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes, FeeLimit: noFeeLimit,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, payAmt, sourceNode, target, payAmt,
) )
@ -2022,9 +1968,6 @@ func TestRestrictOutgoingChannel(t *testing.T) {
} }
sourceVertex := Vertex(sourceNode.PubKeyBytes) sourceVertex := Vertex(sourceNode.PubKeyBytes)
ignoredEdges := make(map[EdgeLocator]struct{})
ignoredVertexes := make(map[Vertex]struct{})
const ( const (
startingHeight = 100 startingHeight = 100
finalHopCLTV = 1 finalHopCLTV = 1
@ -2041,8 +1984,6 @@ func TestRestrictOutgoingChannel(t *testing.T) {
graph: testGraphInstance.graph, graph: testGraphInstance.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoredVertexes,
IgnoredEdges: ignoredEdges,
FeeLimit: noFeeLimit, FeeLimit: noFeeLimit,
OutgoingChannelID: &outgoingChannelID, OutgoingChannelID: &outgoingChannelID,
}, },

View File

@ -1949,9 +1949,6 @@ func TestFindPathFeeWeighting(t *testing.T) {
t.Fatalf("unable to fetch source node: %v", err) t.Fatalf("unable to fetch source node: %v", err)
} }
ignoreVertex := make(map[Vertex]struct{})
ignoreEdge := make(map[EdgeLocator]struct{})
amt := lnwire.MilliSatoshi(100) amt := lnwire.MilliSatoshi(100)
target := ctx.aliases["luoji"] target := ctx.aliases["luoji"]
@ -1967,9 +1964,7 @@ func TestFindPathFeeWeighting(t *testing.T) {
graph: ctx.graph, graph: ctx.graph,
}, },
&RestrictParams{ &RestrictParams{
IgnoredNodes: ignoreVertex, FeeLimit: noFeeLimit,
IgnoredEdges: ignoreEdge,
FeeLimit: noFeeLimit,
}, },
sourceNode, target, amt, sourceNode, target, amt,
) )