-
Notifications
You must be signed in to change notification settings - Fork 199
/
indexHashedNodesCoordinatorRegistry.go
290 lines (229 loc) · 7.81 KB
/
indexHashedNodesCoordinatorRegistry.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package sharding
import (
"encoding/json"
"fmt"
"strconv"
"github.com/ElrondNetwork/elrond-go/common"
)
// SerializableValidator holds the minimal data required for marshalling and un-marshalling a validator
type SerializableValidator struct {
PubKey []byte `json:"pubKey"`
Chances uint32 `json:"chances"`
Index uint32 `json:"index"`
}
// EpochValidators holds one epoch configuration for a nodes coordinator
type EpochValidators struct {
EligibleValidators map[string][]*SerializableValidator `json:"eligibleValidators"`
WaitingValidators map[string][]*SerializableValidator `json:"waitingValidators"`
LeavingValidators map[string][]*SerializableValidator `json:"leavingValidators"`
}
// NodesCoordinatorRegistry holds the data that can be used to initialize a nodes coordinator
type NodesCoordinatorRegistry struct {
EpochsConfig map[string]*EpochValidators `json:"epochConfigs"`
CurrentEpoch uint32 `json:"currentEpoch"`
}
// TODO: add proto marshalizer for these package - replace all json marshalizers
// LoadState loads the nodes coordinator state from the used boot storage
func (ihgs *indexHashedNodesCoordinator) LoadState(key []byte) error {
return ihgs.baseLoadState(key)
}
func (ihgs *indexHashedNodesCoordinator) baseLoadState(key []byte) error {
ncInternalkey := append([]byte(common.NodesCoordinatorRegistryKeyPrefix), key...)
log.Debug("getting nodes coordinator config", "key", ncInternalkey)
ihgs.loadingFromDisk.Store(true)
defer ihgs.loadingFromDisk.Store(false)
data, err := ihgs.bootStorer.Get(ncInternalkey)
if err != nil {
return err
}
config := &NodesCoordinatorRegistry{}
err = json.Unmarshal(data, config)
if err != nil {
return err
}
ihgs.mutSavedStateKey.Lock()
ihgs.savedStateKey = key
ihgs.mutSavedStateKey.Unlock()
ihgs.currentEpoch = config.CurrentEpoch
log.Debug("loaded nodes config", "current epoch", config.CurrentEpoch)
nodesConfig, err := ihgs.registryToNodesCoordinator(config)
if err != nil {
return err
}
displayNodesConfigInfo(nodesConfig)
ihgs.mutNodesConfig.Lock()
ihgs.nodesConfig = nodesConfig
ihgs.mutNodesConfig.Unlock()
return nil
}
func displayNodesConfigInfo(config map[uint32]*epochNodesConfig) {
for epoch, cfg := range config {
log.Debug("restored config for",
"epoch", epoch,
"computed shard ID", cfg.shardID,
)
}
}
func (ihgs *indexHashedNodesCoordinator) saveState(key []byte) error {
registry := ihgs.NodesCoordinatorToRegistry()
data, err := json.Marshal(registry)
if err != nil {
return err
}
ncInternalkey := append([]byte(common.NodesCoordinatorRegistryKeyPrefix), key...)
log.Debug("saving nodes coordinator config", "key", ncInternalkey)
return ihgs.bootStorer.Put(ncInternalkey, data)
}
// NodesCoordinatorToRegistry will export the nodesCoordinator data to the registry
func (ihgs *indexHashedNodesCoordinator) NodesCoordinatorToRegistry() *NodesCoordinatorRegistry {
ihgs.mutNodesConfig.RLock()
defer ihgs.mutNodesConfig.RUnlock()
registry := &NodesCoordinatorRegistry{
CurrentEpoch: ihgs.currentEpoch,
EpochsConfig: make(map[string]*EpochValidators),
}
minEpoch := 0
lastEpoch := ihgs.getLastEpochConfig()
if lastEpoch >= nodesCoordinatorStoredEpochs {
minEpoch = int(lastEpoch) - nodesCoordinatorStoredEpochs + 1
}
for epoch := uint32(minEpoch); epoch <= lastEpoch; epoch++ {
epochNodesData, ok := ihgs.nodesConfig[epoch]
if !ok {
continue
}
registry.EpochsConfig[fmt.Sprint(epoch)] = epochNodesConfigToEpochValidators(epochNodesData)
}
return registry
}
func (ihgs *indexHashedNodesCoordinator) getLastEpochConfig() uint32 {
lastEpoch := uint32(0)
for epoch := range ihgs.nodesConfig {
if lastEpoch < epoch {
lastEpoch = epoch
}
}
return lastEpoch
}
func (ihgs *indexHashedNodesCoordinator) registryToNodesCoordinator(
config *NodesCoordinatorRegistry,
) (map[uint32]*epochNodesConfig, error) {
var err error
var epoch int64
result := make(map[uint32]*epochNodesConfig)
for epochStr, epochValidators := range config.EpochsConfig {
epoch, err = strconv.ParseInt(epochStr, 10, 64)
if err != nil {
return nil, err
}
var nodesConfig *epochNodesConfig
nodesConfig, err = epochValidatorsToEpochNodesConfig(epochValidators)
if err != nil {
return nil, err
}
nbShards := uint32(len(nodesConfig.eligibleMap))
if nbShards < 2 {
return nil, ErrInvalidNumberOfShards
}
// shards without metachain shard
nodesConfig.nbShards = nbShards - 1
nodesConfig.shardID, _ = ihgs.computeShardForSelfPublicKey(nodesConfig)
epoch32 := uint32(epoch)
result[epoch32] = nodesConfig
log.Debug("registry to nodes coordinator", "epoch", epoch32)
result[epoch32].selectors, err = ihgs.createSelectors(nodesConfig)
if err != nil {
return nil, err
}
}
return result, nil
}
func epochNodesConfigToEpochValidators(config *epochNodesConfig) *EpochValidators {
result := &EpochValidators{
EligibleValidators: make(map[string][]*SerializableValidator, len(config.eligibleMap)),
WaitingValidators: make(map[string][]*SerializableValidator, len(config.waitingMap)),
LeavingValidators: make(map[string][]*SerializableValidator, len(config.leavingMap)),
}
for k, v := range config.eligibleMap {
result.EligibleValidators[fmt.Sprint(k)] = ValidatorArrayToSerializableValidatorArray(v)
}
for k, v := range config.waitingMap {
result.WaitingValidators[fmt.Sprint(k)] = ValidatorArrayToSerializableValidatorArray(v)
}
for k, v := range config.leavingMap {
result.LeavingValidators[fmt.Sprint(k)] = ValidatorArrayToSerializableValidatorArray(v)
}
return result
}
func epochValidatorsToEpochNodesConfig(config *EpochValidators) (*epochNodesConfig, error) {
result := &epochNodesConfig{}
var err error
result.eligibleMap, err = serializableValidatorsMapToValidatorsMap(config.EligibleValidators)
if err != nil {
return nil, err
}
result.waitingMap, err = serializableValidatorsMapToValidatorsMap(config.WaitingValidators)
if err != nil {
return nil, err
}
result.leavingMap, err = serializableValidatorsMapToValidatorsMap(config.LeavingValidators)
if err != nil {
return nil, err
}
return result, nil
}
func serializableValidatorsMapToValidatorsMap(
sValidators map[string][]*SerializableValidator,
) (map[uint32][]Validator, error) {
result := make(map[uint32][]Validator, len(sValidators))
for k, v := range sValidators {
key, err := strconv.ParseInt(k, 10, 64)
if err != nil {
return nil, err
}
result[uint32(key)], err = serializableValidatorArrayToValidatorArray(v)
if err != nil {
return nil, err
}
}
return result, nil
}
// ValidatorArrayToSerializableValidatorArray -
func ValidatorArrayToSerializableValidatorArray(validators []Validator) []*SerializableValidator {
result := make([]*SerializableValidator, len(validators))
for i, v := range validators {
result[i] = &SerializableValidator{
PubKey: v.PubKey(),
Chances: v.Chances(),
Index: v.Index(),
}
}
return result
}
func serializableValidatorArrayToValidatorArray(sValidators []*SerializableValidator) ([]Validator, error) {
result := make([]Validator, len(sValidators))
var err error
for i, v := range sValidators {
result[i], err = NewValidator(v.PubKey, v.Chances, v.Index)
if err != nil {
return nil, err
}
}
return result, nil
}
// NodesInfoToValidators maps nodeInfo to validator interface
func NodesInfoToValidators(nodesInfo map[uint32][]GenesisNodeInfoHandler) (map[uint32][]Validator, error) {
validatorsMap := make(map[uint32][]Validator)
for shId, nodeInfoList := range nodesInfo {
validators := make([]Validator, 0, len(nodeInfoList))
for index, nodeInfo := range nodeInfoList {
validatorObj, err := NewValidator(nodeInfo.PubKeyBytes(), defaultSelectionChances, uint32(index))
if err != nil {
return nil, err
}
validators = append(validators, validatorObj)
}
validatorsMap[shId] = validators
}
return validatorsMap, nil
}