-
Notifications
You must be signed in to change notification settings - Fork 922
/
shuffle_test.go
190 lines (171 loc) · 5.15 KB
/
shuffle_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package helpers
import (
"fmt"
"reflect"
"testing"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/sliceutil"
)
func TestShuffleList_InvalidValidatorCount(t *testing.T) {
maxShuffleListSize = 20
list := make([]uint64, 21)
if _, err := ShuffleList(list, [32]byte{123, 125}); err == nil {
t.Error("Shuffle should have failed when validator count exceeds ModuloBias")
maxShuffleListSize = 1 << 40
}
maxShuffleListSize = 1 << 40
}
func TestShuffleList_OK(t *testing.T) {
var list1 []uint64
seed1 := [32]byte{1, 128, 12}
seed2 := [32]byte{2, 128, 12}
for i := 0; i < 10; i++ {
list1 = append(list1, uint64(i))
}
list2 := make([]uint64, len(list1))
copy(list2, list1)
list1, err := ShuffleList(list1, seed1)
if err != nil {
t.Errorf("Shuffle failed with: %v", err)
}
list2, err = ShuffleList(list2, seed2)
if err != nil {
t.Errorf("Shuffle failed with: %v", err)
}
if reflect.DeepEqual(list1, list2) {
t.Errorf("2 shuffled lists shouldn't be equal")
}
if !reflect.DeepEqual(list1, []uint64{0, 7, 8, 6, 3, 9, 4, 5, 2, 1}) {
t.Errorf("list 1 was incorrectly shuffled got: %v", list1)
}
if !reflect.DeepEqual(list2, []uint64{0, 5, 2, 1, 6, 8, 7, 3, 4, 9}) {
t.Errorf("list 2 was incorrectly shuffled got: %v", list2)
}
}
func TestSplitIndices_OK(t *testing.T) {
var l []uint64
numValidators := uint64(64000)
for i := uint64(0); i < numValidators; i++ {
l = append(l, i)
}
split := SplitIndices(l, params.BeaconConfig().SlotsPerEpoch)
if uint64(len(split)) != params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", params.BeaconConfig().SlotsPerEpoch, len(split))
}
for _, s := range split {
if uint64(len(s)) != numValidators/params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", numValidators/params.BeaconConfig().SlotsPerEpoch, len(s))
}
}
}
func TestShuffleList_Vs_ShuffleIndex(t *testing.T) {
list := []uint64{}
listSize := uint64(1000)
seed := [32]byte{123, 42}
for i := uint64(0); i < listSize; i++ {
list = append(list, i)
}
shuffledListByIndex := make([]uint64, listSize)
for i := uint64(0); i < listSize; i++ {
si, err := ShuffledIndex(i, listSize, seed)
if err != nil {
t.Error(err)
}
shuffledListByIndex[si] = i
}
shuffledList, err := ShuffleList(list, seed)
if err != nil {
t.Fatalf("shuffled list error: %v", err)
}
if !reflect.DeepEqual(shuffledList, shuffledListByIndex) {
t.Errorf("shuffled lists ar not equal shuffled list: %v shuffled list by index: %v", shuffledList, shuffledListByIndex)
}
}
func BenchmarkShuffledIndex(b *testing.B) {
listSizes := []uint64{4000000, 40000, 400}
seed := [32]byte{123, 42}
for _, listSize := range listSizes {
b.Run(fmt.Sprintf("ShuffledIndex_%d", listSize), func(ib *testing.B) {
for i := uint64(0); i < uint64(ib.N); i++ {
if _, err := ShuffledIndex(i%listSize, listSize, seed); err != nil {
b.Error(err)
}
}
})
}
}
func BenchmarkIndexComparison(b *testing.B) {
listSizes := []uint64{400000, 40000, 400}
seed := [32]byte{123, 42}
for _, listSize := range listSizes {
b.Run(fmt.Sprintf("Indexwise_ShuffleList_%d", listSize), func(ib *testing.B) {
for i := 0; i < ib.N; i++ {
// Simulate a list-shuffle by running shuffle-index listSize times.
for j := uint64(0); j < listSize; j++ {
if _, err := ShuffledIndex(j, listSize, seed); err != nil {
b.Error(err)
}
}
}
})
}
}
func BenchmarkShuffleList(b *testing.B) {
listSizes := []uint64{400000, 40000, 400}
seed := [32]byte{123, 42}
for _, listSize := range listSizes {
testIndices := make([]uint64, listSize, listSize)
for i := uint64(0); i < listSize; i++ {
testIndices[i] = i
}
b.Run(fmt.Sprintf("ShuffleList_%d", listSize), func(ib *testing.B) {
for i := 0; i < ib.N; i++ {
if _, err := ShuffleList(testIndices, seed); err != nil {
b.Error(err)
}
}
})
}
}
func TestShuffledIndex(t *testing.T) {
list := []uint64{}
listSize := uint64(399)
for i := uint64(0); i < listSize; i++ {
list = append(list, i)
}
shuffledList := make([]uint64, listSize)
unShuffledList := make([]uint64, listSize)
seed := [32]byte{123, 42}
for i := uint64(0); i < listSize; i++ {
si, err := ShuffledIndex(i, listSize, seed)
if err != nil {
t.Error(err)
}
shuffledList[si] = i
}
for i := uint64(0); i < listSize; i++ {
ui, err := UnShuffledIndex(i, listSize, seed)
if err != nil {
t.Error(err)
}
unShuffledList[ui] = shuffledList[i]
}
if !reflect.DeepEqual(unShuffledList, list) {
t.Errorf("Want: %v got: %v", list, unShuffledList)
}
}
func TestSplitIndicesAndOffset_OK(t *testing.T) {
var l []uint64
validators := uint64(64000)
for i := uint64(0); i < validators; i++ {
l = append(l, i)
}
chunks := uint64(6)
split := SplitIndices(l, chunks)
for i := uint64(0); i < chunks; i++ {
if !reflect.DeepEqual(split[i], l[sliceutil.SplitOffset(uint64(len(l)), chunks, i):sliceutil.SplitOffset(uint64(len(l)), chunks, i+1)]) {
t.Errorf("Want: %v got: %v", l[sliceutil.SplitOffset(uint64(len(l)), chunks, i):sliceutil.SplitOffset(uint64(len(l)), chunks, i+1)], split[i])
break
}
}
}