autopilot: fix loop variables, use require
With this commit we fix a few occurrences of loop variables being accessed in closures. While we touch the code, we also migrate it to using the require library.
This commit is contained in:
2 files changed
+91
-153
No files matched your search
@@ -37,10 +37,11 @@ func TestBetweennessCentralityEmptyGraph(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
for _, chanGraph := range chanGraphs {
|
for _, chanGraph := range chanGraphs {
|
||||||
success := t.Run(chanGraph.name, func(t1 *testing.T) {
|
chanGraph := chanGraph
|
||||||
graph, err := chanGraph.genFunc(t1)
|
graph, err := chanGraph.genFunc(t)
|
||||||
require.NoError(t1, err, "unable to create graph")
|
require.NoError(t, err, "unable to create graph")
|
||||||
|
|
||||||
|
success := t.Run(chanGraph.name, func(t1 *testing.T) {
|
||||||
err = centralityMetric.Refresh(graph)
|
err = centralityMetric.Refresh(graph)
|
||||||
require.NoError(t1, err)
|
require.NoError(t1, err)
|
||||||
|
|
||||||
@@ -76,6 +77,7 @@ func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) {
|
|||||||
|
|
||||||
for _, numWorkers := range workers {
|
for _, numWorkers := range workers {
|
||||||
for _, chanGraph := range chanGraphs {
|
for _, chanGraph := range chanGraphs {
|
||||||
|
chanGraph := chanGraph
|
||||||
numWorkers := numWorkers
|
numWorkers := numWorkers
|
||||||
graph, err := chanGraph.genFunc(t)
|
graph, err := chanGraph.genFunc(t)
|
||||||
require.NoError(t, err, "unable to create graph")
|
require.NoError(t, err, "unable to create graph")
|
||||||
@@ -107,8 +109,8 @@ func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) {
|
|||||||
expected.normalize,
|
expected.normalize,
|
||||||
)
|
)
|
||||||
|
|
||||||
require.Equal(t1,
|
require.Equal(
|
||||||
centralityTestGraph.nodes,
|
t1, centralityTestGraph.nodes,
|
||||||
len(centrality),
|
len(centrality),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+84
-148
@@ -73,29 +73,23 @@ func TestPrefAttachmentSelectEmptyGraph(t *testing.T) {
|
|||||||
NewNodeID(pub): {},
|
NewNodeID(pub): {},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, graph := range chanGraphs {
|
for _, chanGraph := range chanGraphs {
|
||||||
success := t.Run(graph.name, func(t1 *testing.T) {
|
chanGraph := chanGraph
|
||||||
graph, err := graph.genFunc(t1)
|
graph, err := chanGraph.genFunc(t)
|
||||||
if err != nil {
|
require.NoError(t, err, "unable to create graph")
|
||||||
t1.Fatalf("unable to create graph: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
success := t.Run(chanGraph.name, func(t1 *testing.T) {
|
||||||
// With the necessary state initialized, we'll now
|
// With the necessary state initialized, we'll now
|
||||||
// attempt to get the score for this one node.
|
// attempt to get the score for this one node.
|
||||||
const walletFunds = btcutil.SatoshiPerBitcoin
|
const walletFunds = btcutil.SatoshiPerBitcoin
|
||||||
scores, err := prefAttach.NodeScores(graph, nil,
|
scores, err := prefAttach.NodeScores(
|
||||||
walletFunds, nodes)
|
graph, nil, walletFunds, nodes,
|
||||||
if err != nil {
|
)
|
||||||
t1.Fatalf("unable to select attachment "+
|
require.NoError(t1, err)
|
||||||
"directives: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since the graph is empty, we expect the score to be
|
// Since the graph is empty, we expect the score to be
|
||||||
// 0, giving an empty return map.
|
// 0, giving an empty return map.
|
||||||
if len(scores) != 0 {
|
require.Empty(t1, scores)
|
||||||
t1.Fatalf("expected empty score map, "+
|
|
||||||
"instead got %v ", len(scores))
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
if !success {
|
if !success {
|
||||||
break
|
break
|
||||||
@@ -115,59 +109,48 @@ func TestPrefAttachmentSelectTwoVertexes(t *testing.T) {
|
|||||||
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
|
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, graph := range chanGraphs {
|
for _, chanGraph := range chanGraphs {
|
||||||
success := t.Run(graph.name, func(t1 *testing.T) {
|
chanGraph := chanGraph
|
||||||
graph, err := graph.genFunc(t1)
|
graph, err := chanGraph.genFunc(t)
|
||||||
if err != nil {
|
require.NoError(t, err, "unable to create graph")
|
||||||
t1.Fatalf("unable to create graph: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
success := t.Run(chanGraph.name, func(t1 *testing.T) {
|
||||||
prefAttach := NewPrefAttachment()
|
prefAttach := NewPrefAttachment()
|
||||||
|
|
||||||
// For this set, we'll load the memory graph with two
|
// For this set, we'll load the memory graph with two
|
||||||
// nodes, and a random channel connecting them.
|
// nodes, and a random channel connecting them.
|
||||||
const chanCapacity = btcutil.SatoshiPerBitcoin
|
const chanCapacity = btcutil.SatoshiPerBitcoin
|
||||||
edge1, edge2, err := graph.addRandChannel(nil, nil, chanCapacity)
|
edge1, edge2, err := graph.addRandChannel(
|
||||||
if err != nil {
|
nil, nil, chanCapacity,
|
||||||
t1.Fatalf("unable to generate channel: %v", err)
|
)
|
||||||
}
|
require.NoError(t1, err)
|
||||||
|
|
||||||
// We also add a third, non-connected node to the graph.
|
// We also add a third, non-connected node to the graph.
|
||||||
_, err = graph.addRandNode()
|
_, err = graph.addRandNode()
|
||||||
if err != nil {
|
require.NoError(t1, err)
|
||||||
t1.Fatalf("unable to add random node: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the score for all nodes found in the graph at
|
// Get the score for all nodes found in the graph at
|
||||||
// this point.
|
// this point.
|
||||||
nodes := make(map[NodeID]struct{})
|
nodes := make(map[NodeID]struct{})
|
||||||
if err := graph.ForEachNode(func(n Node) error {
|
err = graph.ForEachNode(func(n Node) error {
|
||||||
nodes[n.PubKey()] = struct{}{}
|
nodes[n.PubKey()] = struct{}{}
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
})
|
||||||
t1.Fatalf("unable to traverse graph: %v", err)
|
require.NoError(t1, err)
|
||||||
}
|
|
||||||
|
|
||||||
if len(nodes) != 3 {
|
require.Len(t1, nodes, 3)
|
||||||
t1.Fatalf("expected 2 nodes, found %d", len(nodes))
|
|
||||||
}
|
|
||||||
|
|
||||||
// With the necessary state initialized, we'll now
|
// With the necessary state initialized, we'll now
|
||||||
// attempt to get our candidates channel score given
|
// attempt to get our candidates channel score given
|
||||||
// the current state of the graph.
|
// the current state of the graph.
|
||||||
candidates, err := prefAttach.NodeScores(graph, nil,
|
candidates, err := prefAttach.NodeScores(
|
||||||
maxChanSize, nodes)
|
graph, nil, maxChanSize, nodes,
|
||||||
if err != nil {
|
)
|
||||||
t1.Fatalf("unable to select attachment "+
|
require.NoError(t1, err)
|
||||||
"directives: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We expect two candidates, since one of the nodes
|
// We expect two candidates, since one of the nodes
|
||||||
// doesn't have any channels.
|
// doesn't have any channels.
|
||||||
if len(candidates) != 2 {
|
require.Len(t1, candidates, 2)
|
||||||
t1.Fatalf("2 nodes should be scored, "+
|
|
||||||
"instead %v were", len(candidates))
|
|
||||||
}
|
|
||||||
|
|
||||||
// The candidates should be amongst the two edges
|
// The candidates should be amongst the two edges
|
||||||
// created above.
|
// created above.
|
||||||
@@ -186,12 +169,7 @@ func TestPrefAttachmentSelectTwoVertexes(t *testing.T) {
|
|||||||
// Since each of the nodes has 1 channel, out
|
// Since each of the nodes has 1 channel, out
|
||||||
// of only one channel in the graph, we expect
|
// of only one channel in the graph, we expect
|
||||||
// their score to be 1.0.
|
// their score to be 1.0.
|
||||||
expScore := float64(1.0)
|
require.EqualValues(t1, 1, candidate.Score)
|
||||||
if candidate.Score != expScore {
|
|
||||||
t1.Fatalf("expected candidate score "+
|
|
||||||
"to be %v, instead was %v",
|
|
||||||
expScore, candidate.Score)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if !success {
|
if !success {
|
||||||
@@ -212,42 +190,38 @@ func TestPrefAttachmentSelectGreedyAllocation(t *testing.T) {
|
|||||||
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
|
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, graph := range chanGraphs {
|
for _, chanGraph := range chanGraphs {
|
||||||
success := t.Run(graph.name, func(t1 *testing.T) {
|
chanGraph := chanGraph
|
||||||
graph, err := graph.genFunc(t1)
|
graph, err := chanGraph.genFunc(t)
|
||||||
if err != nil {
|
require.NoError(t, err, "unable to create graph")
|
||||||
t1.Fatalf("unable to create graph: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
success := t.Run(chanGraph.name, func(t1 *testing.T) {
|
||||||
prefAttach := NewPrefAttachment()
|
prefAttach := NewPrefAttachment()
|
||||||
|
|
||||||
const chanCapacity = btcutil.SatoshiPerBitcoin
|
const chanCapacity = btcutil.SatoshiPerBitcoin
|
||||||
|
|
||||||
// Next, we'll add 3 nodes to the graph, creating an
|
// Next, we'll add 3 nodes to the graph, creating an
|
||||||
// "open triangle topology".
|
// "open triangle topology".
|
||||||
edge1, _, err := graph.addRandChannel(nil, nil,
|
edge1, _, err := graph.addRandChannel(
|
||||||
chanCapacity)
|
nil, nil, chanCapacity,
|
||||||
if err != nil {
|
)
|
||||||
t1.Fatalf("unable to create channel: %v", err)
|
require.NoError(t1, err)
|
||||||
}
|
|
||||||
peerPubBytes := edge1.Peer.PubKey()
|
peerPubBytes := edge1.Peer.PubKey()
|
||||||
peerPub, err := btcec.ParsePubKey(peerPubBytes[:])
|
peerPub, err := btcec.ParsePubKey(peerPubBytes[:])
|
||||||
if err != nil {
|
require.NoError(t1, err)
|
||||||
t.Fatalf("unable to parse pubkey: %v", err)
|
|
||||||
}
|
|
||||||
_, _, err = graph.addRandChannel(
|
_, _, err = graph.addRandChannel(
|
||||||
peerPub, nil, chanCapacity,
|
peerPub, nil, chanCapacity,
|
||||||
)
|
)
|
||||||
if err != nil {
|
require.NoError(t1, err)
|
||||||
t1.Fatalf("unable to create channel: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// At this point, there should be three nodes in the
|
// At this point, there should be three nodes in the
|
||||||
// graph, with node node having two edges.
|
// graph, with node having two edges.
|
||||||
numNodes := 0
|
numNodes := 0
|
||||||
twoChans := false
|
twoChans := false
|
||||||
nodes := make(map[NodeID]struct{})
|
nodes := make(map[NodeID]struct{})
|
||||||
if err := graph.ForEachNode(func(n Node) error {
|
err = graph.ForEachNode(func(n Node) error {
|
||||||
numNodes++
|
numNodes++
|
||||||
nodes[n.PubKey()] = struct{}{}
|
nodes[n.PubKey()] = struct{}{}
|
||||||
numChans := 0
|
numChans := 0
|
||||||
@@ -262,65 +236,46 @@ func TestPrefAttachmentSelectGreedyAllocation(t *testing.T) {
|
|||||||
twoChans = twoChans || (numChans == 2)
|
twoChans = twoChans || (numChans == 2)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
})
|
||||||
t1.Fatalf("unable to traverse graph: %v", err)
|
require.NoError(t1, err)
|
||||||
}
|
|
||||||
if numNodes != 3 {
|
require.EqualValues(t1, 3, numNodes)
|
||||||
t1.Fatalf("expected 3 nodes, instead have: %v",
|
require.True(t1, twoChans, "have two chans")
|
||||||
numNodes)
|
|
||||||
}
|
|
||||||
if !twoChans {
|
|
||||||
t1.Fatalf("expected node to have two channels")
|
|
||||||
}
|
|
||||||
|
|
||||||
// We'll now begin our test, modeling the available
|
// We'll now begin our test, modeling the available
|
||||||
// wallet balance to be 5.5 BTC. We're shooting for a
|
// wallet balance to be 5.5 BTC. We're shooting for a
|
||||||
// 50/50 allocation, and have 3 BTC in channels. As a
|
// 50/50 allocation, and have 3 BTC in channels. As a
|
||||||
// result, the heuristic should try to greedily
|
// result, the heuristic should try to greedily
|
||||||
// allocate funds to channels.
|
// allocate funds to channels.
|
||||||
scores, err := prefAttach.NodeScores(graph, nil,
|
scores, err := prefAttach.NodeScores(
|
||||||
maxChanSize, nodes)
|
graph, nil, maxChanSize, nodes,
|
||||||
if err != nil {
|
)
|
||||||
t1.Fatalf("unable to select attachment "+
|
require.NoError(t1, err)
|
||||||
"directives: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(scores) != len(nodes) {
|
require.Equal(t1, len(nodes), len(scores))
|
||||||
t1.Fatalf("all nodes should be scored, "+
|
|
||||||
"instead %v were", len(scores))
|
|
||||||
}
|
|
||||||
|
|
||||||
// The candidates should have a non-zero score, and
|
// The candidates should have a non-zero score, and
|
||||||
// have the max chan size funds recommended channel
|
// have the max chan size funds recommended channel
|
||||||
// size.
|
// size.
|
||||||
for _, candidate := range scores {
|
for _, candidate := range scores {
|
||||||
if candidate.Score == 0 {
|
require.NotZero(t1, candidate.Score)
|
||||||
t1.Fatalf("Expected non-zero score")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Imagine a few channels are being opened, and there's
|
// Imagine a few channels are being opened, and there's
|
||||||
// only 0.5 BTC left. That should leave us with channel
|
// only 0.5 BTC left. That should leave us with channel
|
||||||
// candidates of that size.
|
// candidates of that size.
|
||||||
const remBalance = btcutil.SatoshiPerBitcoin * 0.5
|
const remBalance = btcutil.SatoshiPerBitcoin * 0.5
|
||||||
scores, err = prefAttach.NodeScores(graph, nil,
|
scores, err = prefAttach.NodeScores(
|
||||||
remBalance, nodes)
|
graph, nil, remBalance, nodes,
|
||||||
if err != nil {
|
)
|
||||||
t1.Fatalf("unable to select attachment "+
|
require.NoError(t1, err)
|
||||||
"directives: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(scores) != len(nodes) {
|
require.Equal(t1, len(nodes), len(scores))
|
||||||
t1.Fatalf("all nodes should be scored, "+
|
|
||||||
"instead %v were", len(scores))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that the recommended channel sizes are now the
|
// Check that the recommended channel sizes are now the
|
||||||
// remaining channel balance.
|
// remaining channel balance.
|
||||||
for _, candidate := range scores {
|
for _, candidate := range scores {
|
||||||
if candidate.Score == 0 {
|
require.NotZero(t1, candidate.Score)
|
||||||
t1.Fatalf("Expected non-zero score")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if !success {
|
if !success {
|
||||||
@@ -341,56 +296,42 @@ func TestPrefAttachmentSelectSkipNodes(t *testing.T) {
|
|||||||
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
|
maxChanSize = btcutil.Amount(btcutil.SatoshiPerBitcoin)
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, graph := range chanGraphs {
|
for _, chanGraph := range chanGraphs {
|
||||||
success := t.Run(graph.name, func(t1 *testing.T) {
|
chanGraph := chanGraph
|
||||||
graph, err := graph.genFunc(t1)
|
graph, err := chanGraph.genFunc(t)
|
||||||
if err != nil {
|
require.NoError(t, err, "unable to create graph")
|
||||||
t1.Fatalf("unable to create graph: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
success := t.Run(chanGraph.name, func(t1 *testing.T) {
|
||||||
prefAttach := NewPrefAttachment()
|
prefAttach := NewPrefAttachment()
|
||||||
|
|
||||||
// Next, we'll create a simple topology of two nodes,
|
// Next, we'll create a simple topology of two nodes,
|
||||||
// with a single channel connecting them.
|
// with a single channel connecting them.
|
||||||
const chanCapacity = btcutil.SatoshiPerBitcoin
|
const chanCapacity = btcutil.SatoshiPerBitcoin
|
||||||
_, _, err = graph.addRandChannel(nil, nil,
|
_, _, err = graph.addRandChannel(nil, nil, chanCapacity)
|
||||||
chanCapacity)
|
require.NoError(t1, err)
|
||||||
if err != nil {
|
|
||||||
t1.Fatalf("unable to create channel: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
nodes := make(map[NodeID]struct{})
|
nodes := make(map[NodeID]struct{})
|
||||||
if err := graph.ForEachNode(func(n Node) error {
|
err = graph.ForEachNode(func(n Node) error {
|
||||||
nodes[n.PubKey()] = struct{}{}
|
nodes[n.PubKey()] = struct{}{}
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
})
|
||||||
t1.Fatalf("unable to traverse graph: %v", err)
|
require.NoError(t1, err)
|
||||||
}
|
|
||||||
|
|
||||||
if len(nodes) != 2 {
|
require.Len(t1, nodes, 2)
|
||||||
t1.Fatalf("expected 2 nodes, found %d", len(nodes))
|
|
||||||
}
|
|
||||||
|
|
||||||
// With our graph created, we'll now get the scores for
|
// With our graph created, we'll now get the scores for
|
||||||
// all nodes in the graph.
|
// all nodes in the graph.
|
||||||
scores, err := prefAttach.NodeScores(graph, nil,
|
scores, err := prefAttach.NodeScores(
|
||||||
maxChanSize, nodes)
|
graph, nil, maxChanSize, nodes,
|
||||||
if err != nil {
|
)
|
||||||
t1.Fatalf("unable to select attachment "+
|
require.NoError(t1, err)
|
||||||
"directives: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(scores) != len(nodes) {
|
require.Equal(t1, len(nodes), len(scores))
|
||||||
t1.Fatalf("all nodes should be scored, "+
|
|
||||||
"instead %v were", len(scores))
|
|
||||||
}
|
|
||||||
|
|
||||||
// THey should all have a score, and a maxChanSize
|
// THey should all have a score, and a maxChanSize
|
||||||
// channel size recommendation.
|
// channel size recommendation.
|
||||||
for _, candidate := range scores {
|
for _, candidate := range scores {
|
||||||
if candidate.Score == 0 {
|
require.NotZero(t1, candidate.Score)
|
||||||
t1.Fatalf("Expected non-zero score")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll simulate a channel update by adding the nodes
|
// We'll simulate a channel update by adding the nodes
|
||||||
@@ -408,19 +349,14 @@ func TestPrefAttachmentSelectSkipNodes(t *testing.T) {
|
|||||||
// function, without providing any new information,
|
// function, without providing any new information,
|
||||||
// then all nodes should have a score of zero, since we
|
// then all nodes should have a score of zero, since we
|
||||||
// already got channels to them.
|
// already got channels to them.
|
||||||
scores, err = prefAttach.NodeScores(graph, chans,
|
scores, err = prefAttach.NodeScores(
|
||||||
maxChanSize, nodes)
|
graph, chans, maxChanSize, nodes,
|
||||||
if err != nil {
|
)
|
||||||
t1.Fatalf("unable to select attachment "+
|
require.NoError(t1, err)
|
||||||
"directives: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since all should be given a score of 0, the map
|
// Since all should be given a score of 0, the map
|
||||||
// should be empty.
|
// should be empty.
|
||||||
if len(scores) != 0 {
|
require.Empty(t1, scores)
|
||||||
t1.Fatalf("expected empty score map, "+
|
|
||||||
"instead got %v ", len(scores))
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
if !success {
|
if !success {
|
||||||
break
|
break
|
||||||
|
|||||||
Reference in new issue
Block a user