2022-05-04 06:15:09 +00:00
|
|
|
//go:build !fuzz
|
|
|
|
|
2020-10-07 17:25:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2020-10-07 17:25:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProposerKeyFn_OK(t *testing.T) {
|
|
|
|
item := &ProposerIndices{
|
|
|
|
BlockRoot: [32]byte{'A'},
|
2021-02-23 00:14:50 +00:00
|
|
|
ProposerIndices: []types.ValidatorIndex{1, 2, 3, 4, 5},
|
2020-10-07 17:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
k, err := proposerIndicesKeyFn(item)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, key(item.BlockRoot), k)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProposerKeyFn_InvalidObj(t *testing.T) {
|
|
|
|
_, err := proposerIndicesKeyFn("bad")
|
|
|
|
assert.Equal(t, ErrNotProposerIndices, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProposerCache_AddProposerIndicesList(t *testing.T) {
|
|
|
|
cache := NewProposerIndicesCache()
|
|
|
|
bRoot := [32]byte{'A'}
|
|
|
|
indices, err := cache.ProposerIndices(bRoot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if indices != nil {
|
|
|
|
t.Error("Expected committee count not to exist in empty cache")
|
|
|
|
}
|
2020-12-15 00:09:30 +00:00
|
|
|
has, err := cache.HasProposerIndices(bRoot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, false, has)
|
2020-10-07 17:25:08 +00:00
|
|
|
require.NoError(t, cache.AddProposerIndices(&ProposerIndices{
|
|
|
|
ProposerIndices: indices,
|
|
|
|
BlockRoot: bRoot,
|
|
|
|
}))
|
|
|
|
|
|
|
|
received, err := cache.ProposerIndices(bRoot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, received, indices)
|
2020-12-15 00:09:30 +00:00
|
|
|
has, err = cache.HasProposerIndices(bRoot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, has)
|
2020-10-07 17:25:08 +00:00
|
|
|
|
2021-02-23 00:14:50 +00:00
|
|
|
item := &ProposerIndices{BlockRoot: [32]byte{'B'}, ProposerIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}}
|
2020-10-07 17:25:08 +00:00
|
|
|
require.NoError(t, cache.AddProposerIndices(item))
|
|
|
|
|
|
|
|
received, err = cache.ProposerIndices(item.BlockRoot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, item.ProposerIndices, received)
|
2020-12-15 00:09:30 +00:00
|
|
|
has, err = cache.HasProposerIndices(bRoot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, true, has)
|
|
|
|
|
2020-10-07 17:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProposerCache_CanRotate(t *testing.T) {
|
|
|
|
cache := NewProposerIndicesCache()
|
|
|
|
for i := 0; i < int(maxProposerIndicesCacheSize)+1; i++ {
|
|
|
|
s := []byte(strconv.Itoa(i))
|
|
|
|
item := &ProposerIndices{BlockRoot: bytesutil.ToBytes32(s)}
|
|
|
|
require.NoError(t, cache.AddProposerIndices(item))
|
|
|
|
}
|
2022-05-04 06:15:09 +00:00
|
|
|
assert.Equal(t, int(maxProposerIndicesCacheSize), cache.Len())
|
2020-10-07 17:25:08 +00:00
|
|
|
}
|