2018-07-22 16:58:14 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFaultyShuffleIndices(t *testing.T) {
|
2018-08-25 18:59:46 +00:00
|
|
|
var list []uint32
|
2018-08-14 00:58:37 +00:00
|
|
|
|
|
|
|
for i := 0; i < params.MaxValidators+1; i++ {
|
2018-08-25 18:59:46 +00:00
|
|
|
list = append(list, uint32(i))
|
2018-08-14 00:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ShuffleIndices(common.Hash{'a'}, list); err == nil {
|
2018-07-22 16:58:14 +00:00
|
|
|
t.Error("Shuffle should have failed when validator count exceeds MaxValidators")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShuffleIndices(t *testing.T) {
|
|
|
|
hash1 := common.BytesToHash([]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g'})
|
|
|
|
hash2 := common.BytesToHash([]byte{'1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7'})
|
2018-08-25 18:59:46 +00:00
|
|
|
var list1 []uint32
|
2018-08-14 00:58:37 +00:00
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
2018-08-25 18:59:46 +00:00
|
|
|
list1 = append(list1, uint32(i))
|
2018-08-14 00:58:37 +00:00
|
|
|
}
|
2018-07-22 16:58:14 +00:00
|
|
|
|
2018-08-25 18:59:46 +00:00
|
|
|
list2 := make([]uint32, len(list1))
|
2018-08-14 00:58:37 +00:00
|
|
|
copy(list2, list1)
|
|
|
|
|
|
|
|
list1, err := ShuffleIndices(hash1, list1)
|
2018-07-22 16:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shuffle failed with: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-14 00:58:37 +00:00
|
|
|
list2, err = ShuffleIndices(hash2, list2)
|
2018-07-22 16:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Shuffle failed with: %v", err)
|
|
|
|
}
|
2018-08-14 00:58:37 +00:00
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
if reflect.DeepEqual(list1, list2) {
|
|
|
|
t.Errorf("2 shuffled lists shouldn't be equal")
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 21:20:54 +00:00
|
|
|
|
2018-08-14 00:58:37 +00:00
|
|
|
func TestSplitIndices(t *testing.T) {
|
2018-08-25 18:59:46 +00:00
|
|
|
var l []uint32
|
2018-08-14 00:58:37 +00:00
|
|
|
validators := 64000
|
|
|
|
for i := 0; i < validators; i++ {
|
2018-08-25 18:59:46 +00:00
|
|
|
l = append(l, uint32(i))
|
2018-08-02 21:20:54 +00:00
|
|
|
}
|
2018-08-14 00:58:37 +00:00
|
|
|
split := SplitIndices(l, params.CycleLength)
|
|
|
|
if len(split) != params.CycleLength {
|
|
|
|
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", params.CycleLength, len(split))
|
2018-08-02 21:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 00:58:37 +00:00
|
|
|
for _, s := range split {
|
|
|
|
if len(s) != validators/params.CycleLength {
|
|
|
|
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", validators/params.CycleLength, len(s))
|
2018-08-02 21:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|