2018-08-09 18:25:48 +00:00
|
|
|
// Package utils defines utility functions for the beacon-chain.
|
2018-07-22 16:58:14 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
2018-07-28 19:53:02 +00:00
|
|
|
"golang.org/x/crypto/blake2b"
|
2018-07-22 16:58:14 +00:00
|
|
|
)
|
|
|
|
|
2018-07-23 15:43:41 +00:00
|
|
|
// ShuffleIndices returns a list of pseudorandomly sampled
|
2018-07-22 16:58:14 +00:00
|
|
|
// indices. This is used to use to select attesters and proposers.
|
2018-08-25 18:59:46 +00:00
|
|
|
func ShuffleIndices(seed common.Hash, validatorList []uint32) ([]uint32, error) {
|
2018-08-14 00:58:37 +00:00
|
|
|
if len(validatorList) > params.MaxValidators {
|
2018-07-22 16:58:14 +00:00
|
|
|
return nil, errors.New("Validator count has exceeded MaxValidator Count")
|
|
|
|
}
|
|
|
|
|
2018-08-20 00:01:19 +00:00
|
|
|
hashSeed := blake2b.Sum512(seed[:])
|
2018-08-14 00:58:37 +00:00
|
|
|
validatorCount := len(validatorList)
|
2018-07-22 16:58:14 +00:00
|
|
|
|
|
|
|
// shuffle stops at the second to last index.
|
|
|
|
for i := 0; i < validatorCount-1; i++ {
|
|
|
|
// convert every 3 bytes to random number, replace validator index with that number.
|
2018-07-28 19:53:02 +00:00
|
|
|
for j := 0; j+3 < len(hashSeed); j += 3 {
|
|
|
|
swapNum := int(hashSeed[j] + hashSeed[j+1] + hashSeed[j+2])
|
2018-07-22 16:58:14 +00:00
|
|
|
remaining := validatorCount - i
|
|
|
|
swapPos := swapNum%remaining + i
|
|
|
|
validatorList[i], validatorList[swapPos] = validatorList[swapPos], validatorList[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return validatorList, nil
|
|
|
|
}
|
2018-08-02 21:20:54 +00:00
|
|
|
|
2018-08-14 00:58:37 +00:00
|
|
|
// SplitIndices splits a list into n pieces.
|
2018-08-25 18:59:46 +00:00
|
|
|
func SplitIndices(l []uint32, n int) [][]uint32 {
|
|
|
|
var divided [][]uint32
|
2018-08-14 00:58:37 +00:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
start := len(l) * i / n
|
|
|
|
end := len(l) * (i + 1) / n
|
|
|
|
divided = append(divided, l[start:end])
|
2018-08-02 21:20:54 +00:00
|
|
|
}
|
2018-08-14 00:58:37 +00:00
|
|
|
return divided
|
2018-08-02 21:20:54 +00:00
|
|
|
}
|