-
Notifications
You must be signed in to change notification settings - Fork 200
/
common.go
110 lines (95 loc) · 2.93 KB
/
common.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
package sharding
import (
"encoding/hex"
"strconv"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
)
var log = logger.GetOrCreate("sharding")
func computeStartIndexAndNumAppearancesForValidator(expEligibleList []uint32, idx int64) (int64, int64) {
val := expEligibleList[idx]
startIdx := int64(0)
listLen := int64(len(expEligibleList))
for i := idx - 1; i >= 0; i-- {
if expEligibleList[i] != val {
startIdx = i + 1
break
}
}
endIdx := listLen - 1
for i := idx + 1; i < listLen; i++ {
if expEligibleList[i] != val {
endIdx = i - 1
break
}
}
return startIdx, endIdx - startIdx + 1
}
func displayValidatorsForRandomness(validators []Validator, randomness []byte) {
if log.GetLevel() != logger.LogTrace {
return
}
strValidators := ""
for _, v := range validators {
strValidators += "\n" + hex.EncodeToString(v.PubKey())
}
log.Trace("selectValidators", "randomness", randomness, "validators", strValidators)
}
func displayNodesConfiguration(
eligible map[uint32][]Validator,
waiting map[uint32][]Validator,
leaving map[uint32][]Validator,
actualRemaining map[uint32][]Validator,
nbShards uint32,
) {
for shard := uint32(0); shard <= nbShards; shard++ {
shardID := shard
if shardID == nbShards {
shardID = core.MetachainShardId
}
for _, v := range eligible[shardID] {
pk := v.PubKey()
log.Debug("eligible", "pk", pk, "shardID", shardID)
}
for _, v := range waiting[shardID] {
pk := v.PubKey()
log.Debug("waiting", "pk", pk, "shardID", shardID)
}
for _, v := range leaving[shardID] {
pk := v.PubKey()
log.Debug("leaving", "pk", pk, "shardID", shardID)
}
for _, v := range actualRemaining[shardID] {
pk := v.PubKey()
log.Debug("actually remaining", "pk", pk, "shardID", shardID)
}
}
}
// SerializableValidatorsToValidators creates the validator map from serializable validator map
func SerializableValidatorsToValidators(nodeRegistryValidators map[string][]*SerializableValidator) (map[uint32][]Validator, error) {
validators := make(map[uint32][]Validator)
for shardId, shardValidators := range nodeRegistryValidators {
newValidators, err := SerializableShardValidatorListToValidatorList(shardValidators)
if err != nil {
return nil, err
}
shardIdInt, err := strconv.ParseUint(shardId, 10, 32)
if err != nil {
return nil, err
}
validators[uint32(shardIdInt)] = newValidators
}
return validators, nil
}
// SerializableShardValidatorListToValidatorList creates the validator list from serializable validator list
func SerializableShardValidatorListToValidatorList(shardValidators []*SerializableValidator) ([]Validator, error) {
newValidators := make([]Validator, len(shardValidators))
for i, shardValidator := range shardValidators {
v, err := NewValidator(shardValidator.PubKey, shardValidator.Chances, shardValidator.Index)
if err != nil {
return nil, err
}
newValidators[i] = v
}
return newValidators, nil
}