2019-03-01 20:31:38 +00:00
|
|
|
package powchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
2020-12-17 12:33:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain/types"
|
2020-07-25 06:20:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-03-01 20:31:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHashKeyFn_OK(t *testing.T) {
|
2020-12-17 12:33:34 +00:00
|
|
|
hInfo := &types.HeaderInfo{
|
2019-03-01 20:31:38 +00:00
|
|
|
Hash: common.HexToHash("0x0123456"),
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:27:08 +00:00
|
|
|
key, err := hashKeyFn(hInfo)
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 16:27:08 +00:00
|
|
|
assert.Equal(t, hInfo.Hash.Hex(), key)
|
2019-03-01 20:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashKeyFn_InvalidObj(t *testing.T) {
|
|
|
|
_, err := hashKeyFn("bad")
|
2020-08-27 16:27:08 +00:00
|
|
|
assert.Equal(t, ErrNotAHeaderInfo, err)
|
2019-03-01 20:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeightKeyFn_OK(t *testing.T) {
|
2020-12-17 12:33:34 +00:00
|
|
|
hInfo := &types.HeaderInfo{
|
2019-03-01 20:31:38 +00:00
|
|
|
Number: big.NewInt(555),
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:27:08 +00:00
|
|
|
key, err := heightKeyFn(hInfo)
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 16:27:08 +00:00
|
|
|
assert.Equal(t, hInfo.Number.String(), key)
|
2019-03-01 20:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeightKeyFn_InvalidObj(t *testing.T) {
|
|
|
|
_, err := heightKeyFn("bad")
|
2020-08-27 16:27:08 +00:00
|
|
|
assert.Equal(t, ErrNotAHeaderInfo, err)
|
2019-03-01 20:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockCache_byHash(t *testing.T) {
|
2020-08-27 16:27:08 +00:00
|
|
|
cache := newHeaderCache()
|
2019-03-01 20:31:38 +00:00
|
|
|
|
|
|
|
header := &gethTypes.Header{
|
|
|
|
ParentHash: common.HexToHash("0x12345"),
|
|
|
|
Number: big.NewInt(55),
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:27:08 +00:00
|
|
|
exists, _, err := cache.HeaderInfoByHash(header.Hash())
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exists, "Expected block info not to exist in empty cache")
|
2019-03-01 20:31:38 +00:00
|
|
|
|
2020-08-27 16:27:08 +00:00
|
|
|
err = cache.AddHeader(header)
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 20:31:38 +00:00
|
|
|
|
2020-08-27 16:27:08 +00:00
|
|
|
exists, fetchedInfo, err := cache.HeaderInfoByHash(header.Hash())
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 16:27:08 +00:00
|
|
|
assert.Equal(t, true, exists, "Expected headerInfo to exist")
|
2020-07-25 06:20:21 +00:00
|
|
|
assert.Equal(t, 0, fetchedInfo.Number.Cmp(header.Number), "Expected fetched info number to be equal")
|
|
|
|
assert.Equal(t, header.Hash(), fetchedInfo.Hash, "Expected hash to be equal")
|
|
|
|
|
2019-03-01 20:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockCache_byHeight(t *testing.T) {
|
2020-08-27 16:27:08 +00:00
|
|
|
cache := newHeaderCache()
|
2019-03-01 20:31:38 +00:00
|
|
|
|
|
|
|
header := &gethTypes.Header{
|
|
|
|
ParentHash: common.HexToHash("0x12345"),
|
|
|
|
Number: big.NewInt(55),
|
|
|
|
}
|
|
|
|
|
2020-08-27 16:27:08 +00:00
|
|
|
exists, _, err := cache.HeaderInfoByHeight(header.Number)
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exists, "Expected block info not to exist in empty cache")
|
2019-03-01 20:31:38 +00:00
|
|
|
|
2020-08-27 16:27:08 +00:00
|
|
|
err = cache.AddHeader(header)
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 20:31:38 +00:00
|
|
|
|
2020-08-27 16:27:08 +00:00
|
|
|
exists, fetchedInfo, err := cache.HeaderInfoByHeight(header.Number)
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-27 16:27:08 +00:00
|
|
|
assert.Equal(t, true, exists, "Expected headerInfo to exist")
|
2020-07-25 06:20:21 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 0, fetchedInfo.Number.Cmp(header.Number), "Expected fetched info number to be equal")
|
|
|
|
assert.Equal(t, header.Hash(), fetchedInfo.Hash, "Expected hash to be equal")
|
|
|
|
|
2019-03-01 20:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockCache_maxSize(t *testing.T) {
|
2020-08-27 16:27:08 +00:00
|
|
|
cache := newHeaderCache()
|
2019-03-01 20:31:38 +00:00
|
|
|
|
|
|
|
for i := int64(0); i < int64(maxCacheSize+10); i++ {
|
|
|
|
header := &gethTypes.Header{
|
|
|
|
Number: big.NewInt(i),
|
|
|
|
}
|
2020-08-27 16:27:08 +00:00
|
|
|
err := cache.AddHeader(header)
|
2020-07-25 06:20:21 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 20:31:38 +00:00
|
|
|
|
|
|
|
}
|
2020-07-25 06:20:21 +00:00
|
|
|
|
|
|
|
assert.Equal(t, int(maxCacheSize), len(cache.hashCache.ListKeys()))
|
|
|
|
assert.Equal(t, int(maxCacheSize), len(cache.heightCache.ListKeys()))
|
2019-03-01 20:31:38 +00:00
|
|
|
}
|