2019-09-27 02:51:39 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2020-03-19 04:41:05 +00:00
|
|
|
"math"
|
2019-12-16 18:14:21 +00:00
|
|
|
"sort"
|
2019-09-27 02:51:39 +00:00
|
|
|
"strconv"
|
|
|
|
"testing"
|
2019-11-11 22:03:44 +00:00
|
|
|
|
2021-02-18 20:11:20 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2019-12-16 18:14:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2019-11-11 22:03:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-07-16 19:34:08 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-09-27 02:51:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommitteeKeyFn_OK(t *testing.T) {
|
2019-12-16 18:14:21 +00:00
|
|
|
item := &Committees{
|
|
|
|
CommitteeCount: 1,
|
|
|
|
Seed: [32]byte{'A'},
|
2021-02-23 00:14:50 +00:00
|
|
|
ShuffledIndices: []types.ValidatorIndex{1, 2, 3, 4, 5},
|
2019-09-27 02:51:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
k, err := committeeKeyFn(item)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, key(item.Seed), k)
|
2019-09-27 02:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommitteeKeyFn_InvalidObj(t *testing.T) {
|
|
|
|
_, err := committeeKeyFn("bad")
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.Equal(t, ErrNotCommittee, err)
|
2019-09-27 02:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommitteeCache_CommitteesByEpoch(t *testing.T) {
|
2019-12-16 18:14:21 +00:00
|
|
|
cache := NewCommitteesCache()
|
2019-09-27 02:51:39 +00:00
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
item := &Committees{
|
2021-02-23 00:14:50 +00:00
|
|
|
ShuffledIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6},
|
2019-12-16 18:14:21 +00:00
|
|
|
Seed: [32]byte{'A'},
|
|
|
|
CommitteeCount: 3,
|
2019-09-27 02:51:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
slot := params.BeaconConfig().SlotsPerEpoch
|
2021-02-18 20:11:20 +00:00
|
|
|
committeeIndex := types.CommitteeIndex(1)
|
2019-12-16 18:14:21 +00:00
|
|
|
indices, err := cache.Committee(slot, item.Seed, committeeIndex)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-27 02:51:39 +00:00
|
|
|
if indices != nil {
|
|
|
|
t.Error("Expected committee not to exist in empty cache")
|
|
|
|
}
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, cache.AddCommitteeShuffledList(item))
|
2019-09-27 02:51:39 +00:00
|
|
|
|
2021-02-18 20:11:20 +00:00
|
|
|
wantedIndex := types.CommitteeIndex(0)
|
2019-12-16 18:14:21 +00:00
|
|
|
indices, err = cache.Committee(slot, item.Seed, wantedIndex)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-11 22:03:44 +00:00
|
|
|
|
2021-02-18 20:11:20 +00:00
|
|
|
start, end := startEndIndices(item, uint64(wantedIndex))
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.DeepEqual(t, item.ShuffledIndices[start:end], indices)
|
2019-09-27 02:51:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
func TestCommitteeCache_ActiveIndices(t *testing.T) {
|
|
|
|
cache := NewCommitteesCache()
|
2019-09-27 02:51:39 +00:00
|
|
|
|
2021-02-23 00:14:50 +00:00
|
|
|
item := &Committees{Seed: [32]byte{'A'}, SortedIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}}
|
2019-12-16 18:14:21 +00:00
|
|
|
indices, err := cache.ActiveIndices(item.Seed)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-16 18:14:21 +00:00
|
|
|
if indices != nil {
|
2020-07-16 19:34:08 +00:00
|
|
|
t.Error("Expected committee not to exist in empty cache")
|
2019-09-27 02:51:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, cache.AddCommitteeShuffledList(item))
|
2019-09-27 02:51:39 +00:00
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
indices, err = cache.ActiveIndices(item.Seed)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, item.SortedIndices, indices)
|
2019-09-27 02:51:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 22:24:49 +00:00
|
|
|
func TestCommitteeCache_ActiveCount(t *testing.T) {
|
|
|
|
cache := NewCommitteesCache()
|
|
|
|
|
2021-02-23 00:14:50 +00:00
|
|
|
item := &Committees{Seed: [32]byte{'A'}, SortedIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}}
|
2020-06-14 22:24:49 +00:00
|
|
|
count, err := cache.ActiveIndicesCount(item.Seed)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, count, "Expected active count not to exist in empty cache")
|
2020-06-14 22:24:49 +00:00
|
|
|
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, cache.AddCommitteeShuffledList(item))
|
2020-06-14 22:24:49 +00:00
|
|
|
|
|
|
|
count, err = cache.ActiveIndicesCount(item.Seed)
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, len(item.SortedIndices), count)
|
2020-06-14 22:24:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
func TestCommitteeCache_CanRotate(t *testing.T) {
|
|
|
|
cache := NewCommitteesCache()
|
2019-09-29 19:10:11 +00:00
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
// Should rotate out all the epochs except 190 through 199.
|
2020-08-17 03:17:46 +00:00
|
|
|
start := 100
|
|
|
|
end := 200
|
|
|
|
for i := start; i < end; i++ {
|
2019-12-16 18:14:21 +00:00
|
|
|
s := []byte(strconv.Itoa(i))
|
|
|
|
item := &Committees{Seed: bytesutil.ToBytes32(s)}
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, cache.AddCommitteeShuffledList(item))
|
2019-09-29 19:10:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
k := cache.CommitteeCache.ListKeys()
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.Equal(t, maxCommitteesCacheSize, uint64(len(k)))
|
2019-09-29 19:10:11 +00:00
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
sort.Slice(k, func(i, j int) bool {
|
|
|
|
return k[i] < k[j]
|
|
|
|
})
|
2020-08-17 03:17:46 +00:00
|
|
|
wanted := end - int(maxCommitteesCacheSize)
|
|
|
|
s := bytesutil.ToBytes32([]byte(strconv.Itoa(wanted)))
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.Equal(t, key(s), k[0], "incorrect key received for slot 190")
|
|
|
|
|
2019-12-16 18:14:21 +00:00
|
|
|
s = bytesutil.ToBytes32([]byte(strconv.Itoa(199)))
|
2020-07-16 19:34:08 +00:00
|
|
|
assert.Equal(t, key(s), k[len(k)-1], "incorrect key received for slot 199")
|
2019-09-27 02:51:39 +00:00
|
|
|
}
|
2020-03-19 04:41:05 +00:00
|
|
|
|
|
|
|
func TestCommitteeCacheOutOfRange(t *testing.T) {
|
|
|
|
cache := NewCommitteesCache()
|
|
|
|
seed := bytesutil.ToBytes32([]byte("foo"))
|
2020-04-14 16:41:09 +00:00
|
|
|
err := cache.CommitteeCache.Add(&Committees{
|
2020-03-19 04:41:05 +00:00
|
|
|
CommitteeCount: 1,
|
|
|
|
Seed: seed,
|
2021-02-23 00:14:50 +00:00
|
|
|
ShuffledIndices: []types.ValidatorIndex{0},
|
|
|
|
SortedIndices: []types.ValidatorIndex{},
|
2020-03-19 04:41:05 +00:00
|
|
|
})
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-14 16:41:09 +00:00
|
|
|
_, err = cache.Committee(0, seed, math.MaxUint64) // Overflow!
|
2020-07-16 19:34:08 +00:00
|
|
|
require.NotNil(t, err, "Did not fail as expected")
|
2020-03-19 04:41:05 +00:00
|
|
|
}
|