channeldb/graph_test: properly check entries within edge update index
Due to entries within the edge update index having a nil value, the tests need to be modified to account for this. Previously, we'd assume that if we were unable to retrieve a value for a certain key that the entry was non-existent, which is why the improper pruning bug was not caught. Instead, we'll assert the number of entries to be the expected value and populate a lookup map to determine whether the correct entries exist within it.
This commit is contained in:
parent
2f22e6c35f
commit
8dec659e10
@ -2130,44 +2130,54 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
|||||||
t.Fatalf("unable to update edge: %v", err)
|
t.Fatalf("unable to update edge: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that both edges have been updated, if we manually check the
|
// checkIndexTimestamps is a helper function that checks the edge update
|
||||||
// update index, we should have an entry for both edges.
|
// index only includes the given timestamps.
|
||||||
if err := db.View(func(tx *bolt.Tx) error {
|
checkIndexTimestamps := func(timestamps ...uint64) {
|
||||||
edges := tx.Bucket(edgeBucket)
|
timestampSet := make(map[uint64]struct{})
|
||||||
if edges == nil {
|
for _, t := range timestamps {
|
||||||
return ErrGraphNoEdgesFound
|
timestampSet[t] = struct{}{}
|
||||||
}
|
|
||||||
edgeIndex := edges.Bucket(edgeIndexBucket)
|
|
||||||
if edgeIndex == nil {
|
|
||||||
return ErrGraphNoEdgesFound
|
|
||||||
}
|
|
||||||
edgeUpdateIndex := edges.Bucket(edgeUpdateIndexBucket)
|
|
||||||
if edgeUpdateIndex == nil {
|
|
||||||
return ErrGraphNoEdgesFound
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var edgeKey [8 + 8]byte
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
|
edges := tx.Bucket(edgeBucket)
|
||||||
|
if edges == nil {
|
||||||
|
return ErrGraphNoEdgesFound
|
||||||
|
}
|
||||||
|
edgeUpdateIndex := edges.Bucket(edgeUpdateIndexBucket)
|
||||||
|
if edgeUpdateIndex == nil {
|
||||||
|
return ErrGraphNoEdgesFound
|
||||||
|
}
|
||||||
|
|
||||||
byteOrder.PutUint64(edgeKey[:8], uint64(edge1.LastUpdate.Unix()))
|
numEntries := edgeUpdateIndex.Stats().KeyN
|
||||||
byteOrder.PutUint64(edgeKey[8:], edge1.ChannelID)
|
expectedEntries := len(timestampSet)
|
||||||
|
if numEntries != expectedEntries {
|
||||||
|
return fmt.Errorf("expected %v entries in the "+
|
||||||
|
"update index, got %v", expectedEntries,
|
||||||
|
numEntries)
|
||||||
|
}
|
||||||
|
|
||||||
if edgeUpdateIndex.Get(edgeKey[:]) == nil {
|
return edgeUpdateIndex.ForEach(func(k, _ []byte) error {
|
||||||
return fmt.Errorf("first edge not found in update " +
|
t := byteOrder.Uint64(k[:8])
|
||||||
"index")
|
if _, ok := timestampSet[t]; !ok {
|
||||||
|
return fmt.Errorf("found unexpected "+
|
||||||
|
"timestamp "+"%d", t)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
byteOrder.PutUint64(edgeKey[:8], uint64(edge2.LastUpdate.Unix()))
|
|
||||||
byteOrder.PutUint64(edgeKey[8:], edge2.ChannelID)
|
|
||||||
if edgeUpdateIndex.Get(edgeKey[:]) == nil {
|
|
||||||
return fmt.Errorf("second edge not found in update " +
|
|
||||||
"index")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatalf("unable to read update index: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// With both edges policies added, we'll make sure to check they exist
|
||||||
|
// within the edge update index.
|
||||||
|
checkIndexTimestamps(
|
||||||
|
uint64(edge1.LastUpdate.Unix()),
|
||||||
|
uint64(edge2.LastUpdate.Unix()),
|
||||||
|
)
|
||||||
|
|
||||||
// Now we'll prune the graph, removing the edges, and also the update
|
// Now we'll prune the graph, removing the edges, and also the update
|
||||||
// index entries from the database all together.
|
// index entries from the database all together.
|
||||||
var blockHash chainhash.Hash
|
var blockHash chainhash.Hash
|
||||||
@ -2179,43 +2189,10 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
|||||||
t.Fatalf("unable to prune graph: %v", err)
|
t.Fatalf("unable to prune graph: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll now check the database state again, at this point, we should
|
// Finally, we'll check the database state one last time to conclude
|
||||||
// no longer be able to locate the entries within the edge update
|
// that we should no longer be able to locate _any_ entries within the
|
||||||
// index.
|
// edge update index.
|
||||||
if err := db.View(func(tx *bolt.Tx) error {
|
checkIndexTimestamps()
|
||||||
edges := tx.Bucket(edgeBucket)
|
|
||||||
if edges == nil {
|
|
||||||
return ErrGraphNoEdgesFound
|
|
||||||
}
|
|
||||||
edgeIndex := edges.Bucket(edgeIndexBucket)
|
|
||||||
if edgeIndex == nil {
|
|
||||||
return ErrGraphNoEdgesFound
|
|
||||||
}
|
|
||||||
edgeUpdateIndex := edges.Bucket(edgeUpdateIndexBucket)
|
|
||||||
if edgeUpdateIndex == nil {
|
|
||||||
return ErrGraphNoEdgesFound
|
|
||||||
}
|
|
||||||
|
|
||||||
var edgeKey [8 + 8]byte
|
|
||||||
|
|
||||||
byteOrder.PutUint64(edgeKey[:8], uint64(edge1.LastUpdate.Unix()))
|
|
||||||
byteOrder.PutUint64(edgeKey[8:], edge1.ChannelID)
|
|
||||||
if edgeUpdateIndex.Get(edgeKey[:]) != nil {
|
|
||||||
return fmt.Errorf("first edge still found in update " +
|
|
||||||
"index")
|
|
||||||
}
|
|
||||||
|
|
||||||
byteOrder.PutUint64(edgeKey[:8], uint64(edge2.LastUpdate.Unix()))
|
|
||||||
byteOrder.PutUint64(edgeKey[8:], edge2.ChannelID)
|
|
||||||
if edgeUpdateIndex.Get(edgeKey[:]) != nil {
|
|
||||||
return fmt.Errorf("second edge still found in update " +
|
|
||||||
"index")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
t.Fatalf("unable to read update index: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPruneGraphNodes tests that unconnected vertexes are pruned via the
|
// TestPruneGraphNodes tests that unconnected vertexes are pruned via the
|
||||||
|
Loading…
Reference in New Issue
Block a user