prysm-pulse/beacon-chain/powchain/block_cache_test.go
Raul Jordan 252f758baa
QSP-9 Prevent Casting to Int if Possible (#6349)
* no cast to int

* fix up significant casting issues

* more casting

* even more casting fixes

* more casts

* fix subnets

* back to ints

* final touches

* broken test fix

* add in blocks test fix

* unskip

* revert bytes fixes

* casting fixes

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/db/kv/blocks.go

* fmt

* slash:

* fix val tests

* fix up conf

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
2020-06-26 11:07:00 -05:00

166 lines
3.5 KiB
Go

package powchain
import (
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
)
func TestHashKeyFn_OK(t *testing.T) {
bInfo := &blockInfo{
Hash: common.HexToHash("0x0123456"),
}
key, err := hashKeyFn(bInfo)
if err != nil {
t.Fatal(err)
}
if key != bInfo.Hash.Hex() {
t.Errorf("Incorrect hash key: %s, expected %s", key, bInfo.Hash.Hex())
}
}
func TestHashKeyFn_InvalidObj(t *testing.T) {
_, err := hashKeyFn("bad")
if err != ErrNotABlockInfo {
t.Errorf("Expected error %v, got %v", ErrNotABlockInfo, err)
}
}
func TestHeightKeyFn_OK(t *testing.T) {
bInfo := &blockInfo{
Number: big.NewInt(555),
}
key, err := heightKeyFn(bInfo)
if err != nil {
t.Fatal(err)
}
if key != bInfo.Number.String() {
t.Errorf("Incorrect height key: %s, expected %s", key, bInfo.Number.String())
}
}
func TestHeightKeyFn_InvalidObj(t *testing.T) {
_, err := heightKeyFn("bad")
if err != ErrNotABlockInfo {
t.Errorf("Expected error %v, got %v", ErrNotABlockInfo, err)
}
}
func TestBlockCache_byHash(t *testing.T) {
cache := newBlockCache()
header := &gethTypes.Header{
ParentHash: common.HexToHash("0x12345"),
Number: big.NewInt(55),
}
exists, _, err := cache.BlockInfoByHash(header.Hash())
if err != nil {
t.Fatal(err)
}
if exists {
t.Error("Expected block info not to exist in empty cache")
}
if err := cache.AddBlock(gethTypes.NewBlockWithHeader(header)); err != nil {
t.Fatal(err)
}
exists, fetchedInfo, err := cache.BlockInfoByHash(header.Hash())
if err != nil {
t.Fatal(err)
}
if !exists {
t.Error("Expected blockInfo to exist")
}
if fetchedInfo.Number.Cmp(header.Number) != 0 {
t.Errorf(
"Expected fetched info number to be %v, got %v",
header.Number,
fetchedInfo.Number,
)
}
if fetchedInfo.Hash != header.Hash() {
t.Errorf(
"Expected fetched info hash to be %v, got %v",
header.Hash(),
fetchedInfo.Hash,
)
}
}
func TestBlockCache_byHeight(t *testing.T) {
cache := newBlockCache()
header := &gethTypes.Header{
ParentHash: common.HexToHash("0x12345"),
Number: big.NewInt(55),
}
exists, _, err := cache.BlockInfoByHeight(header.Number)
if err != nil {
t.Fatal(err)
}
if exists {
t.Error("Expected block info not to exist in empty cache")
}
if err := cache.AddBlock(gethTypes.NewBlockWithHeader(header)); err != nil {
t.Fatal(err)
}
exists, fetchedInfo, err := cache.BlockInfoByHeight(header.Number)
if err != nil {
t.Fatal(err)
}
if !exists {
t.Error("Expected blockInfo to exist")
}
if fetchedInfo.Number.Cmp(header.Number) != 0 {
t.Errorf(
"Expected fetched info number to be %v, got %v",
header.Number,
fetchedInfo.Number,
)
}
if fetchedInfo.Hash != header.Hash() {
t.Errorf(
"Expected fetched info hash to be %v, got %v",
header.Hash(),
fetchedInfo.Hash,
)
}
}
func TestBlockCache_maxSize(t *testing.T) {
cache := newBlockCache()
for i := int64(0); i < int64(maxCacheSize+10); i++ {
header := &gethTypes.Header{
Number: big.NewInt(i),
}
if err := cache.AddBlock(gethTypes.NewBlockWithHeader(header)); err != nil {
t.Fatal(err)
}
}
if uint64(len(cache.hashCache.ListKeys())) != maxCacheSize {
t.Errorf(
"Expected hash cache key size to be %d, got %d",
maxCacheSize,
len(cache.hashCache.ListKeys()),
)
}
if uint64(len(cache.heightCache.ListKeys())) != maxCacheSize {
t.Errorf(
"Expected height cache key size to be %d, got %d",
maxCacheSize,
len(cache.heightCache.ListKeys()),
)
}
}