github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/cache/committee_test.go (about) 1 package cache 2 3 import ( 4 "math" 5 "sort" 6 "strconv" 7 "testing" 8 9 types "github.com/prysmaticlabs/eth2-types" 10 "github.com/prysmaticlabs/prysm/shared/bytesutil" 11 "github.com/prysmaticlabs/prysm/shared/params" 12 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 13 "github.com/prysmaticlabs/prysm/shared/testutil/require" 14 ) 15 16 func TestCommitteeKeyFn_OK(t *testing.T) { 17 item := &Committees{ 18 CommitteeCount: 1, 19 Seed: [32]byte{'A'}, 20 ShuffledIndices: []types.ValidatorIndex{1, 2, 3, 4, 5}, 21 } 22 23 k, err := committeeKeyFn(item) 24 require.NoError(t, err) 25 assert.Equal(t, key(item.Seed), k) 26 } 27 28 func TestCommitteeKeyFn_InvalidObj(t *testing.T) { 29 _, err := committeeKeyFn("bad") 30 assert.Equal(t, ErrNotCommittee, err) 31 } 32 33 func TestCommitteeCache_CommitteesByEpoch(t *testing.T) { 34 cache := NewCommitteesCache() 35 36 item := &Committees{ 37 ShuffledIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}, 38 Seed: [32]byte{'A'}, 39 CommitteeCount: 3, 40 } 41 42 slot := params.BeaconConfig().SlotsPerEpoch 43 committeeIndex := types.CommitteeIndex(1) 44 indices, err := cache.Committee(slot, item.Seed, committeeIndex) 45 require.NoError(t, err) 46 if indices != nil { 47 t.Error("Expected committee not to exist in empty cache") 48 } 49 require.NoError(t, cache.AddCommitteeShuffledList(item)) 50 51 wantedIndex := types.CommitteeIndex(0) 52 indices, err = cache.Committee(slot, item.Seed, wantedIndex) 53 require.NoError(t, err) 54 55 start, end := startEndIndices(item, uint64(wantedIndex)) 56 assert.DeepEqual(t, item.ShuffledIndices[start:end], indices) 57 } 58 59 func TestCommitteeCache_ActiveIndices(t *testing.T) { 60 cache := NewCommitteesCache() 61 62 item := &Committees{Seed: [32]byte{'A'}, SortedIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}} 63 indices, err := cache.ActiveIndices(item.Seed) 64 require.NoError(t, err) 65 if indices != nil { 66 t.Error("Expected committee not to exist in empty cache") 67 } 68 69 require.NoError(t, cache.AddCommitteeShuffledList(item)) 70 71 indices, err = cache.ActiveIndices(item.Seed) 72 require.NoError(t, err) 73 assert.DeepEqual(t, item.SortedIndices, indices) 74 } 75 76 func TestCommitteeCache_ActiveCount(t *testing.T) { 77 cache := NewCommitteesCache() 78 79 item := &Committees{Seed: [32]byte{'A'}, SortedIndices: []types.ValidatorIndex{1, 2, 3, 4, 5, 6}} 80 count, err := cache.ActiveIndicesCount(item.Seed) 81 require.NoError(t, err) 82 assert.Equal(t, 0, count, "Expected active count not to exist in empty cache") 83 84 require.NoError(t, cache.AddCommitteeShuffledList(item)) 85 86 count, err = cache.ActiveIndicesCount(item.Seed) 87 require.NoError(t, err) 88 assert.Equal(t, len(item.SortedIndices), count) 89 } 90 91 func TestCommitteeCache_CanRotate(t *testing.T) { 92 cache := NewCommitteesCache() 93 94 // Should rotate out all the epochs except 190 through 199. 95 start := 100 96 end := 200 97 for i := start; i < end; i++ { 98 s := []byte(strconv.Itoa(i)) 99 item := &Committees{Seed: bytesutil.ToBytes32(s)} 100 require.NoError(t, cache.AddCommitteeShuffledList(item)) 101 } 102 103 k := cache.CommitteeCache.Keys() 104 assert.Equal(t, maxCommitteesCacheSize, uint64(len(k))) 105 106 sort.Slice(k, func(i, j int) bool { 107 return k[i].(string) < k[j].(string) 108 }) 109 wanted := end - int(maxCommitteesCacheSize) 110 s := bytesutil.ToBytes32([]byte(strconv.Itoa(wanted))) 111 assert.Equal(t, key(s), k[0], "incorrect key received for slot 190") 112 113 s = bytesutil.ToBytes32([]byte(strconv.Itoa(199))) 114 assert.Equal(t, key(s), k[len(k)-1], "incorrect key received for slot 199") 115 } 116 117 func TestCommitteeCacheOutOfRange(t *testing.T) { 118 cache := NewCommitteesCache() 119 seed := bytesutil.ToBytes32([]byte("foo")) 120 comms := &Committees{ 121 CommitteeCount: 1, 122 Seed: seed, 123 ShuffledIndices: []types.ValidatorIndex{0}, 124 SortedIndices: []types.ValidatorIndex{}, 125 } 126 key, err := committeeKeyFn(comms) 127 assert.NoError(t, err) 128 _ = cache.CommitteeCache.Add(key, comms) 129 130 _, err = cache.Committee(0, seed, math.MaxUint64) // Overflow! 131 require.NotNil(t, err, "Did not fail as expected") 132 }