mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-27 06:28:20 +01:00
routing: add index map to distanceHeap
This commit adds the pubkeyIndices map to the distanceHeap to avoid duplicate entries on the heap. This happened in the earlier iteration of the findPath algorithm and would cause the driving loop to evaluate already evaluated entries when there was no need.
This commit is contained in:
@@ -6,7 +6,8 @@ import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lightningnetwork/lnd/routing/route"
|
||||
)
|
||||
|
||||
// TestHeapOrdering ensures that the items inserted into the heap are properly
|
||||
@@ -16,20 +17,33 @@ func TestHeapOrdering(t *testing.T) {
|
||||
|
||||
// First, create a blank heap, we'll use this to push on randomly
|
||||
// generated items.
|
||||
var nodeHeap distanceHeap
|
||||
nodeHeap := newDistanceHeap()
|
||||
|
||||
prand.Seed(time.Now().Unix())
|
||||
prand.Seed(1)
|
||||
|
||||
// Create 100 random entries adding them to the heap created above, but
|
||||
// also a list that we'll sort with the entries.
|
||||
const numEntries = 100
|
||||
sortedEntries := make([]nodeWithDist, 0, numEntries)
|
||||
for i := 0; i < numEntries; i++ {
|
||||
var pubKey [33]byte
|
||||
prand.Read(pubKey[:])
|
||||
|
||||
entry := nodeWithDist{
|
||||
node: route.Vertex(pubKey),
|
||||
dist: prand.Int63(),
|
||||
}
|
||||
|
||||
heap.Push(&nodeHeap, entry)
|
||||
// Use the PushOrFix method for the initial push to test the scenario
|
||||
// where entry doesn't exist on the heap.
|
||||
nodeHeap.PushOrFix(entry)
|
||||
|
||||
// Re-generate this entry's dist field
|
||||
entry.dist = prand.Int63()
|
||||
|
||||
// Reorder the heap with a PushOrFix call.
|
||||
nodeHeap.PushOrFix(entry)
|
||||
|
||||
sortedEntries = append(sortedEntries, entry)
|
||||
}
|
||||
|
||||
@@ -47,6 +61,13 @@ func TestHeapOrdering(t *testing.T) {
|
||||
poppedEntries = append(poppedEntries, e)
|
||||
}
|
||||
|
||||
// Assert that the pubkeyIndices map is empty after popping all of the
|
||||
// items off of it.
|
||||
if len(nodeHeap.pubkeyIndices) != 0 {
|
||||
t.Fatalf("there are still %d pubkeys in the pubkeyIndices map",
|
||||
len(nodeHeap.pubkeyIndices))
|
||||
}
|
||||
|
||||
// Finally, ensure that the items popped from the heap and the items we
|
||||
// sorted are identical at this rate.
|
||||
if !reflect.DeepEqual(poppedEntries, sortedEntries) {
|
||||
|
||||
Reference in New Issue
Block a user