-
Notifications
You must be signed in to change notification settings - Fork 199
/
expandedListSampling.go
75 lines (61 loc) · 1.97 KB
/
expandedListSampling.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
package sharding
import (
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/hashing"
)
var _ RandomSelector = (*selectorExpandedList)(nil)
type selectorExpandedList struct {
expandedList []uint32
hasher hashing.Hasher
uniqueItems uint32
}
const minWeight = 1
// NewSelectorExpandedList creates a new selector initializing selection set to the given lists of validators
// and expanding it according to each validator weight.
func NewSelectorExpandedList(weightList []uint32, hasher hashing.Hasher) (*selectorExpandedList, error) {
if len(weightList) == 0 {
return nil, ErrNilWeights
}
if check.IfNil(hasher) {
return nil, ErrNilHasher
}
uniqueItems := uint32(len(weightList))
selector := &selectorExpandedList{
hasher: hasher,
uniqueItems: uniqueItems,
}
var err error
selector.expandedList, err = selector.expandList(weightList)
if err != nil {
return nil, err
}
return selector, nil
}
// Select selects from the preconfigured selection set sampleSize elements, returning the selected indexes from the set
func (s *selectorExpandedList) Select(randSeed []byte, sampleSize uint32) ([]uint32, error) {
if len(randSeed) == 0 {
return nil, ErrNilRandomness
}
if sampleSize == 0 || sampleSize > s.uniqueItems {
return nil, ErrInvalidSampleSize
}
selectorConsensus := NewSelectionBasedProvider(s.hasher, s.uniqueItems)
return selectorConsensus.Get(randSeed, int64(sampleSize), s.expandedList)
}
func (s *selectorExpandedList) expandList(weightList []uint32) ([]uint32, error) {
minSize := len(weightList)
validatorList := make([]uint32, 0, minSize)
for i := 0; i < len(weightList); i++ {
if weightList[i] < minWeight {
return nil, ErrInvalidWeight
}
for j := uint32(0); j < weightList[i]; j++ {
validatorList = append(validatorList, uint32(i))
}
}
return validatorList, nil
}
// IsInterfaceNil returns true if the receiver is nil
func (s *selectorExpandedList) IsInterfaceNil() bool {
return s == nil
}