/
nodesSetup.go
403 lines (334 loc) · 12 KB
/
nodesSetup.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package sharding
import (
"bytes"
"fmt"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go/sharding/nodesCoordinator"
)
var _ GenesisNodesSetupHandler = (*NodesSetup)(nil)
var _ nodesCoordinator.GenesisNodeInfoHandler = (*nodeInfo)(nil)
const defaultInitialRating = uint32(5000001)
// InitialNode holds data from json
type InitialNode struct {
PubKey string `json:"pubkey"`
Address string `json:"address"`
InitialRating uint32 `json:"initialRating"`
nodeInfo
}
// nodeInfo holds node info
type nodeInfo struct {
assignedShard uint32
eligible bool
pubKey []byte
address []byte
initialRating uint32
}
// AssignedShard gets the node assigned shard
func (ni *nodeInfo) AssignedShard() uint32 {
return ni.assignedShard
}
// AddressBytes gets the node address as bytes
func (ni *nodeInfo) AddressBytes() []byte {
return ni.address
}
// PubKeyBytes gets the node public key as bytes
func (ni *nodeInfo) PubKeyBytes() []byte {
return ni.pubKey
}
// GetInitialRating gets the initial rating for a node
func (ni *nodeInfo) GetInitialRating() uint32 {
return ni.initialRating
}
// IsInterfaceNil returns true if underlying object is nil
func (ni *nodeInfo) IsInterfaceNil() bool {
return ni == nil
}
// NodesSetup hold data for decoded data from json file
type NodesSetup struct {
StartTime int64 `json:"startTime"`
RoundDuration uint64 `json:"roundDuration"`
ConsensusGroupSize uint32 `json:"consensusGroupSize"`
MinNodesPerShard uint32 `json:"minNodesPerShard"`
MetaChainConsensusGroupSize uint32 `json:"metaChainConsensusGroupSize"`
MetaChainMinNodes uint32 `json:"metaChainMinNodes"`
Hysteresis float32 `json:"hysteresis"`
Adaptivity bool `json:"adaptivity"`
InitialNodes []*InitialNode `json:"initialNodes"`
genesisMaxNumShards uint32
nrOfShards uint32
nrOfNodes uint32
nrOfMetaChainNodes uint32
eligible map[uint32][]nodesCoordinator.GenesisNodeInfoHandler
waiting map[uint32][]nodesCoordinator.GenesisNodeInfoHandler
validatorPubkeyConverter core.PubkeyConverter
addressPubkeyConverter core.PubkeyConverter
}
// NewNodesSetup creates a new decoded nodes structure from json config file
func NewNodesSetup(
nodesFilePath string,
addressPubkeyConverter core.PubkeyConverter,
validatorPubkeyConverter core.PubkeyConverter,
genesisMaxNumShards uint32,
) (*NodesSetup, error) {
if check.IfNil(addressPubkeyConverter) {
return nil, fmt.Errorf("%w for addressPubkeyConverter", ErrNilPubkeyConverter)
}
if check.IfNil(validatorPubkeyConverter) {
return nil, fmt.Errorf("%w for validatorPubkeyConverter", ErrNilPubkeyConverter)
}
if genesisMaxNumShards < 1 {
return nil, fmt.Errorf("%w for genesisMaxNumShards", ErrInvalidMaximumNumberOfShards)
}
nodes := &NodesSetup{
addressPubkeyConverter: addressPubkeyConverter,
validatorPubkeyConverter: validatorPubkeyConverter,
genesisMaxNumShards: genesisMaxNumShards,
}
err := core.LoadJsonFile(nodes, nodesFilePath)
if err != nil {
return nil, err
}
err = nodes.processConfig()
if err != nil {
return nil, err
}
nodes.processMetaChainAssigment()
nodes.processShardAssignment()
nodes.createInitialNodesInfo()
return nodes, nil
}
func (ns *NodesSetup) processConfig() error {
var err error
ns.nrOfNodes = 0
ns.nrOfMetaChainNodes = 0
for i := 0; i < len(ns.InitialNodes); i++ {
pubKey := ns.InitialNodes[i].PubKey
ns.InitialNodes[i].pubKey, err = ns.validatorPubkeyConverter.Decode(pubKey)
if err != nil {
return fmt.Errorf("%w, %s for string %s", ErrCouldNotParsePubKey, err.Error(), pubKey)
}
address := ns.InitialNodes[i].Address
ns.InitialNodes[i].address, err = ns.addressPubkeyConverter.Decode(address)
if err != nil {
return fmt.Errorf("%w, %s for string %s", ErrCouldNotParseAddress, err.Error(), address)
}
// decoder treats empty string as correct, it is not allowed to have empty string as public key
if ns.InitialNodes[i].PubKey == "" {
ns.InitialNodes[i].pubKey = nil
return ErrCouldNotParsePubKey
}
// decoder treats empty string as correct, it is not allowed to have empty string as address
if ns.InitialNodes[i].Address == "" {
ns.InitialNodes[i].address = nil
return ErrCouldNotParseAddress
}
initialRating := ns.InitialNodes[i].InitialRating
if initialRating == uint32(0) {
initialRating = defaultInitialRating
}
ns.InitialNodes[i].initialRating = initialRating
ns.nrOfNodes++
}
if ns.ConsensusGroupSize < 1 {
return ErrNegativeOrZeroConsensusGroupSize
}
if ns.MinNodesPerShard < ns.ConsensusGroupSize {
return ErrMinNodesPerShardSmallerThanConsensusSize
}
if ns.nrOfNodes < ns.MinNodesPerShard {
return ErrNodesSizeSmallerThanMinNoOfNodes
}
if ns.MetaChainConsensusGroupSize < 1 {
return ErrNegativeOrZeroConsensusGroupSize
}
if ns.MetaChainMinNodes < ns.MetaChainConsensusGroupSize {
return ErrMinNodesPerShardSmallerThanConsensusSize
}
totalMinNodes := ns.MetaChainMinNodes + ns.MinNodesPerShard
if ns.nrOfNodes < totalMinNodes {
return ErrNodesSizeSmallerThanMinNoOfNodes
}
return nil
}
func (ns *NodesSetup) processMetaChainAssigment() {
ns.nrOfMetaChainNodes = 0
for id := uint32(0); id < ns.MetaChainMinNodes; id++ {
if ns.InitialNodes[id].pubKey != nil {
ns.InitialNodes[id].assignedShard = core.MetachainShardId
ns.InitialNodes[id].eligible = true
ns.nrOfMetaChainNodes++
}
}
hystMeta := uint32(float32(ns.MetaChainMinNodes) * ns.Hysteresis)
hystShard := uint32(float32(ns.MinNodesPerShard) * ns.Hysteresis)
ns.nrOfShards = (ns.nrOfNodes - ns.nrOfMetaChainNodes - hystMeta) / (ns.MinNodesPerShard + hystShard)
if ns.nrOfShards > ns.genesisMaxNumShards {
ns.nrOfShards = ns.genesisMaxNumShards
}
}
func (ns *NodesSetup) processShardAssignment() {
// initial implementation - as there is no other info than public key, we allocate first nodes in FIFO order to shards
currentShard := uint32(0)
countSetNodes := ns.nrOfMetaChainNodes
for ; currentShard < ns.nrOfShards; currentShard++ {
for id := countSetNodes; id < ns.nrOfMetaChainNodes+(currentShard+1)*ns.MinNodesPerShard; id++ {
// consider only nodes with valid public key
if ns.InitialNodes[id].pubKey != nil {
ns.InitialNodes[id].assignedShard = currentShard
ns.InitialNodes[id].eligible = true
countSetNodes++
}
}
}
// allocate the rest to waiting lists
currentShard = 0
for i := countSetNodes; i < ns.nrOfNodes; i++ {
currentShard = (currentShard + 1) % (ns.nrOfShards + 1)
if currentShard == ns.nrOfShards {
currentShard = core.MetachainShardId
}
if ns.InitialNodes[i].pubKey != nil {
ns.InitialNodes[i].assignedShard = currentShard
ns.InitialNodes[i].eligible = false
}
}
}
func (ns *NodesSetup) createInitialNodesInfo() {
nrOfShardAndMeta := ns.nrOfShards + 1
ns.eligible = make(map[uint32][]nodesCoordinator.GenesisNodeInfoHandler, nrOfShardAndMeta)
ns.waiting = make(map[uint32][]nodesCoordinator.GenesisNodeInfoHandler, nrOfShardAndMeta)
for _, in := range ns.InitialNodes {
if in.pubKey != nil && in.address != nil {
ni := &nodeInfo{
assignedShard: in.assignedShard,
eligible: in.eligible,
pubKey: in.pubKey,
address: in.address,
initialRating: in.initialRating,
}
if in.eligible {
ns.eligible[in.assignedShard] = append(ns.eligible[in.assignedShard], ni)
} else {
ns.waiting[in.assignedShard] = append(ns.waiting[in.assignedShard], ni)
}
}
}
}
// InitialNodesPubKeys - gets initial nodes public keys
func (ns *NodesSetup) InitialNodesPubKeys() map[uint32][]string {
allNodesPubKeys := make(map[uint32][]string)
for shardId, nodesInfo := range ns.eligible {
pubKeys := make([]string, len(nodesInfo))
for i := 0; i < len(nodesInfo); i++ {
pubKeys[i] = string(nodesInfo[i].PubKeyBytes())
}
allNodesPubKeys[shardId] = pubKeys
}
return allNodesPubKeys
}
// InitialNodesInfo - gets initial nodes info
func (ns *NodesSetup) InitialNodesInfo() (map[uint32][]nodesCoordinator.GenesisNodeInfoHandler, map[uint32][]nodesCoordinator.GenesisNodeInfoHandler) {
return ns.eligible, ns.waiting
}
// AllInitialNodes returns all initial nodes loaded
func (ns *NodesSetup) AllInitialNodes() []nodesCoordinator.GenesisNodeInfoHandler {
list := make([]nodesCoordinator.GenesisNodeInfoHandler, len(ns.InitialNodes))
for idx, initialNode := range ns.InitialNodes {
list[idx] = initialNode
}
return list
}
// InitialEligibleNodesPubKeysForShard - gets initial nodes public keys for shard
func (ns *NodesSetup) InitialEligibleNodesPubKeysForShard(shardId uint32) ([]string, error) {
if ns.eligible[shardId] == nil {
return nil, ErrShardIdOutOfRange
}
if len(ns.eligible[shardId]) == 0 {
return nil, ErrNoPubKeys
}
nodesInfo := ns.eligible[shardId]
pubKeys := make([]string, len(nodesInfo))
for i := 0; i < len(nodesInfo); i++ {
pubKeys[i] = string(nodesInfo[i].PubKeyBytes())
}
return pubKeys, nil
}
// InitialNodesInfoForShard - gets initial nodes info for shard
func (ns *NodesSetup) InitialNodesInfoForShard(shardId uint32) ([]nodesCoordinator.GenesisNodeInfoHandler, []nodesCoordinator.GenesisNodeInfoHandler, error) {
if ns.eligible[shardId] == nil {
return nil, nil, ErrShardIdOutOfRange
}
if len(ns.eligible[shardId]) == 0 {
return nil, nil, ErrNoPubKeys
}
return ns.eligible[shardId], ns.waiting[shardId], nil
}
// NumberOfShards returns the calculated number of shards
func (ns *NodesSetup) NumberOfShards() uint32 {
return ns.nrOfShards
}
// MinNumberOfNodes returns the minimum number of nodes
func (ns *NodesSetup) MinNumberOfNodes() uint32 {
return ns.nrOfShards*ns.MinNodesPerShard + ns.MetaChainMinNodes
}
// MinShardHysteresisNodes returns the minimum number of hysteresis nodes per shard
func (ns *NodesSetup) MinShardHysteresisNodes() uint32 {
return uint32(float32(ns.MinNodesPerShard) * ns.Hysteresis)
}
// MinMetaHysteresisNodes returns the minimum number of hysteresis nodes in metachain
func (ns *NodesSetup) MinMetaHysteresisNodes() uint32 {
return uint32(float32(ns.MetaChainMinNodes) * ns.Hysteresis)
}
// MinNumberOfNodesWithHysteresis returns the minimum number of nodes with hysteresis
func (ns *NodesSetup) MinNumberOfNodesWithHysteresis() uint32 {
hystNodesMeta := ns.MinMetaHysteresisNodes()
hystNodesShard := ns.MinShardHysteresisNodes()
minNumberOfNodes := ns.MinNumberOfNodes()
return minNumberOfNodes + hystNodesMeta + ns.nrOfShards*hystNodesShard
}
// MinNumberOfShardNodes returns the minimum number of nodes per shard
func (ns *NodesSetup) MinNumberOfShardNodes() uint32 {
return ns.MinNodesPerShard
}
// MinNumberOfMetaNodes returns the minimum number of nodes in metachain
func (ns *NodesSetup) MinNumberOfMetaNodes() uint32 {
return ns.MetaChainMinNodes
}
// GetHysteresis returns the hysteresis value
func (ns *NodesSetup) GetHysteresis() float32 {
return ns.Hysteresis
}
// GetAdaptivity returns the value of the adaptivity boolean flag
func (ns *NodesSetup) GetAdaptivity() bool {
return ns.Adaptivity
}
// GetShardIDForPubKey returns the allocated shard ID from public key
func (ns *NodesSetup) GetShardIDForPubKey(pubKey []byte) (uint32, error) {
for _, in := range ns.InitialNodes {
if in.pubKey != nil && bytes.Equal(pubKey, in.pubKey) {
return in.assignedShard, nil
}
}
return 0, ErrPublicKeyNotFoundInGenesis
}
// GetStartTime returns the start time
func (ns *NodesSetup) GetStartTime() int64 {
return ns.StartTime
}
// GetRoundDuration returns the round duration
func (ns *NodesSetup) GetRoundDuration() uint64 {
return ns.RoundDuration
}
// GetShardConsensusGroupSize returns the shard consensus group size
func (ns *NodesSetup) GetShardConsensusGroupSize() uint32 {
return ns.ConsensusGroupSize
}
// GetMetaConsensusGroupSize returns the metachain consensus group size
func (ns *NodesSetup) GetMetaConsensusGroupSize() uint32 {
return ns.MetaChainConsensusGroupSize
}
// IsInterfaceNil returns true if underlying object is nil
func (ns *NodesSetup) IsInterfaceNil() bool {
return ns == nil
}