prysm-pulse/beacon-chain/cache/block_test.go

102 lines
2.1 KiB
Go
Raw Normal View History

package cache
import (
"reflect"
"strconv"
"testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)
func TestHeightHeightFn_OK(t *testing.T) {
height := uint64(999)
hash := []byte{'A'}
aInfo := &AncestorInfo{
Height: height,
Hash: hash,
}
key, err := heightKeyFn(aInfo)
if err != nil {
t.Fatal(err)
}
strHeightKey := string(aInfo.Hash) + strconv.Itoa(int(aInfo.Height))
if key != strHeightKey {
t.Errorf("Incorrect hash key: %s, expected %s", key, strHeightKey)
}
}
func TestHeightKeyFn_InvalidObj(t *testing.T) {
_, err := heightKeyFn("bad")
if err != ErrNotAncestorCacheObj {
t.Errorf("Expected error %v, got %v", ErrNotAncestorCacheObj, err)
}
}
func TestAncestorCache_AncestorInfoByHeight(t *testing.T) {
cache := NewBlockAncestorCache()
height := uint64(123)
hash := []byte{'B'}
aInfo := &AncestorInfo{
Height: height,
Hash: hash,
Block: &pb.BeaconBlock{Slot: height},
}
fetchedInfo, err := cache.AncestorBySlot(hash, height)
if err != nil {
t.Fatal(err)
}
if fetchedInfo != nil {
t.Error("Expected ancestor info not to exist in empty cache")
}
if err := cache.AddBlockAncestor(aInfo); err != nil {
t.Fatal(err)
}
fetchedInfo, err = cache.AncestorBySlot(hash, height)
if err != nil {
t.Fatal(err)
}
if fetchedInfo == nil {
t.Error("Expected ancestor info to exist")
}
if fetchedInfo.Height != height {
t.Errorf(
"Expected fetched slot number to be %d, got %d",
aInfo.Height,
fetchedInfo.Height,
)
}
if !reflect.DeepEqual(fetchedInfo.Block, aInfo.Block) {
t.Errorf(
"Expected fetched info committee to be %v, got %v",
aInfo.Block,
fetchedInfo.Block,
)
}
}
func TestBlockAncestor_maxSize(t *testing.T) {
cache := NewBlockAncestorCache()
for i := 0; i < maxCacheSize+10; i++ {
aInfo := &AncestorInfo{
Height: uint64(i),
}
if err := cache.AddBlockAncestor(aInfo); err != nil {
t.Fatal(err)
}
}
if len(cache.ancestorBlockCache.ListKeys()) != maxCacheSize {
t.Errorf(
"Expected hash cache key size to be %d, got %d",
maxCacheSize,
len(cache.ancestorBlockCache.ListKeys()),
)
}
}