-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathsubroundStartRound.go
More file actions
356 lines (287 loc) · 9.85 KB
/
Copy pathsubroundStartRound.go
File metadata and controls
356 lines (287 loc) · 9.85 KB
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
package bls
import (
"context"
"encoding/hex"
"fmt"
"sync"
"time"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
outportcore "github.com/multiversx/mx-chain-core-go/data/outport"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/consensus/spos"
"github.com/multiversx/mx-chain-go/outport"
"github.com/multiversx/mx-chain-go/outport/disabled"
)
// subroundStartRound defines the data needed by the subround StartRound
type subroundStartRound struct {
outportMutex sync.RWMutex
*spos.Subround
processingThresholdPercentage int
executeStoredMessages func()
resetConsensusMessages func()
outportHandler outport.OutportHandler
}
// NewSubroundStartRound creates a subroundStartRound object
func NewSubroundStartRound(
baseSubround *spos.Subround,
extend func(subroundId int),
processingThresholdPercentage int,
executeStoredMessages func(),
resetConsensusMessages func(),
) (*subroundStartRound, error) {
err := checkNewSubroundStartRoundParams(
baseSubround,
)
if err != nil {
return nil, err
}
srStartRound := subroundStartRound{
Subround: baseSubround,
processingThresholdPercentage: processingThresholdPercentage,
executeStoredMessages: executeStoredMessages,
resetConsensusMessages: resetConsensusMessages,
outportHandler: disabled.NewDisabledOutport(),
outportMutex: sync.RWMutex{},
}
srStartRound.Job = srStartRound.doStartRoundJob
srStartRound.Check = srStartRound.doStartRoundConsensusCheck
srStartRound.Extend = extend
baseSubround.EpochStartRegistrationHandler().RegisterHandler(&srStartRound)
return &srStartRound, nil
}
func checkNewSubroundStartRoundParams(
baseSubround *spos.Subround,
) error {
if baseSubround == nil {
return spos.ErrNilSubround
}
if baseSubround.ConsensusState == nil {
return spos.ErrNilConsensusState
}
err := spos.ValidateConsensusCore(baseSubround.ConsensusCoreHandler)
return err
}
// SetOutportHandler method sets outport handler
func (sr *subroundStartRound) SetOutportHandler(outportHandler outport.OutportHandler) error {
if check.IfNil(outportHandler) {
return outport.ErrNilDriver
}
sr.outportMutex.Lock()
sr.outportHandler = outportHandler
sr.outportMutex.Unlock()
return nil
}
// doStartRoundJob method does the job of the subround StartRound
func (sr *subroundStartRound) doStartRoundJob(_ context.Context) bool {
sr.ResetConsensusState()
sr.RoundIndex = sr.RoundHandler().Index()
sr.RoundTimeStamp = sr.RoundHandler().TimeStamp()
topic := spos.GetConsensusTopicID(sr.ShardCoordinator())
sr.GetAntiFloodHandler().ResetForTopic(topic)
sr.resetConsensusMessages()
return true
}
// doStartRoundConsensusCheck method checks if the consensus is achieved in the subround StartRound
func (sr *subroundStartRound) doStartRoundConsensusCheck() bool {
if sr.RoundCanceled {
return false
}
if sr.IsSubroundFinished(sr.Current()) {
return true
}
if sr.initCurrentRound() {
return true
}
return false
}
func (sr *subroundStartRound) initCurrentRound() bool {
nodeState := sr.BootStrapper().GetNodeState()
if nodeState != common.NsSynchronized { // if node is not synchronized yet, it has to continue the bootstrapping mechanism
return false
}
sr.AppStatusHandler().SetStringValue(common.MetricConsensusRoundState, "")
err := sr.generateNextConsensusGroup(sr.RoundHandler().Index())
if err != nil {
log.Debug("initCurrentRound.generateNextConsensusGroup",
"round index", sr.RoundHandler().Index(),
"error", err.Error())
sr.RoundCanceled = true
return false
}
if sr.NodeRedundancyHandler().IsRedundancyNode() {
sr.NodeRedundancyHandler().AdjustInactivityIfNeeded(
sr.SelfPubKey(),
sr.ConsensusGroup(),
sr.RoundHandler().Index(),
)
if sr.NodeRedundancyHandler().IsMainMachineActive() {
return false
}
}
leader, err := sr.GetLeader()
if err != nil {
log.Debug("initCurrentRound.GetLeader", "error", err.Error())
sr.RoundCanceled = true
return false
}
msg := ""
if sr.IsKeyManagedByCurrentNode([]byte(leader)) {
msg = " (my turn in multi-key)"
}
if leader == sr.SelfPubKey() {
sr.AppStatusHandler().Increment(common.MetricCountLeader)
sr.AppStatusHandler().SetStringValue(common.MetricConsensusRoundState, "proposed")
sr.AppStatusHandler().SetStringValue(common.MetricConsensusState, "proposer")
msg = " (my turn)"
}
log.Debug("step 0: preparing the round",
"leader", core.GetTrimmedPk(hex.EncodeToString([]byte(leader))),
"messsage", msg)
pubKeys := sr.ConsensusGroup()
numMultiKeysInConsensusGroup := sr.computeNumManagedKeysInConsensusGroup(pubKeys)
sr.indexRoundIfNeeded(pubKeys)
_, err = sr.SelfConsensusGroupIndex()
if err != nil {
if numMultiKeysInConsensusGroup == 0 {
log.Debug("not in consensus group")
}
sr.AppStatusHandler().SetStringValue(common.MetricConsensusState, "not in consensus group")
} else {
if leader != sr.SelfPubKey() {
sr.AppStatusHandler().Increment(common.MetricCountConsensus)
}
sr.AppStatusHandler().SetStringValue(common.MetricConsensusState, "participant")
}
err = sr.SigningHandler().Reset(pubKeys)
if err != nil {
log.Debug("initCurrentRound.Reset", "error", err.Error())
sr.RoundCanceled = true
return false
}
startTime := sr.RoundTimeStamp
maxTime := sr.RoundHandler().TimeDuration() * time.Duration(sr.processingThresholdPercentage) / 100
if sr.RoundHandler().RemainingTime(startTime, maxTime) < 0 {
log.Debug("canceled round, time is out",
"round", sr.SyncTimer().FormattedCurrentTime(), sr.RoundHandler().Index(),
"subround", sr.Name())
sr.RoundCanceled = true
return false
}
sr.SetStatus(sr.Current(), spos.SsFinished)
// execute stored messages which were received in this new round but before this initialisation
go sr.executeStoredMessages()
return true
}
func (sr *subroundStartRound) computeNumManagedKeysInConsensusGroup(pubKeys []string) int {
numMultiKeysInConsensusGroup := 0
for _, pk := range pubKeys {
pkBytes := []byte(pk)
if sr.IsKeyManagedByCurrentNode(pkBytes) {
sr.IncrementRoundsWithoutReceivedMessages(pkBytes)
numMultiKeysInConsensusGroup++
log.Trace("in consensus group with multi key",
"pk", core.GetTrimmedPk(hex.EncodeToString(pkBytes)))
}
}
if numMultiKeysInConsensusGroup > 0 {
log.Debug("in consensus group with multi keys identities", "num", numMultiKeysInConsensusGroup)
}
return numMultiKeysInConsensusGroup
}
func (sr *subroundStartRound) indexRoundIfNeeded(pubKeys []string) {
sr.outportMutex.RLock()
defer sr.outportMutex.RUnlock()
if !sr.outportHandler.HasDrivers() {
return
}
currentHeader := sr.Blockchain().GetCurrentBlockHeader()
if check.IfNil(currentHeader) {
currentHeader = sr.Blockchain().GetGenesisHeader()
}
epoch := currentHeader.GetEpoch()
shardId := sr.ShardCoordinator().SelfId()
nodesCoordinatorShardID, err := sr.NodesCoordinator().ShardIdForEpoch(epoch)
if err != nil {
log.Debug("initCurrentRound.ShardIdForEpoch",
"epoch", epoch,
"error", err.Error())
return
}
if shardId != nodesCoordinatorShardID {
log.Debug("initCurrentRound.ShardIdForEpoch",
"epoch", epoch,
"shardCoordinator.ShardID", shardId,
"nodesCoordinator.ShardID", nodesCoordinatorShardID)
return
}
signersIndexes, err := sr.NodesCoordinator().GetValidatorsIndexes(pubKeys, epoch)
if err != nil {
log.Error(err.Error())
return
}
round := sr.RoundHandler().Index()
roundInfo := &outportcore.RoundInfo{
Index: uint64(round),
SignersIndexes: signersIndexes,
BlockWasProposed: false,
ShardId: shardId,
Epoch: epoch,
Timestamp: time.Duration(sr.RoundTimeStamp.Unix()),
}
sr.outportHandler.SaveRoundsInfo([]*outportcore.RoundInfo{roundInfo})
}
func (sr *subroundStartRound) generateNextConsensusGroup(roundIndex int64) error {
currentHeader := sr.Blockchain().GetCurrentBlockHeader()
if check.IfNil(currentHeader) {
currentHeader = sr.Blockchain().GetGenesisHeader()
if check.IfNil(currentHeader) {
return spos.ErrNilHeader
}
}
randomSeed := currentHeader.GetRandSeed()
log.Debug("random source for the next consensus group",
"rand", randomSeed)
shardId := sr.ShardCoordinator().SelfId()
nextConsensusGroup, err := sr.GetNextConsensusGroup(
randomSeed,
uint64(sr.RoundIndex),
shardId,
sr.NodesCoordinator(),
currentHeader.GetEpoch(),
)
if err != nil {
return err
}
log.Trace("consensus group is formed by next validators:",
"round", roundIndex)
for i := 0; i < len(nextConsensusGroup); i++ {
log.Trace(core.GetTrimmedPk(hex.EncodeToString([]byte(nextConsensusGroup[i]))))
}
sr.SetConsensusGroup(nextConsensusGroup)
consensusGroupSizeForEpoch := sr.NodesCoordinator().ConsensusGroupSizeForShardAndEpoch(shardId, currentHeader.GetEpoch())
sr.SetConsensusGroupSize(consensusGroupSizeForEpoch)
return nil
}
// EpochStartPrepare wis called when an epoch start event is observed, but not yet confirmed/committed.
// Some components may need to do initialisation on this event
func (sr *subroundStartRound) EpochStartPrepare(metaHdr data.HeaderHandler, _ data.BodyHandler) {
log.Trace(fmt.Sprintf("epoch %d start prepare in consensus", metaHdr.GetEpoch()))
}
// EpochStartAction is called upon a start of epoch event.
func (sr *subroundStartRound) EpochStartAction(hdr data.HeaderHandler) {
log.Trace(fmt.Sprintf("epoch %d start action in consensus", hdr.GetEpoch()))
sr.changeEpoch(hdr.GetEpoch())
}
func (sr *subroundStartRound) changeEpoch(currentEpoch uint32) {
epochNodes, err := sr.NodesCoordinator().GetConsensusWhitelistedNodes(currentEpoch)
if err != nil {
panic(fmt.Sprintf("consensus changing epoch failed with error %s", err.Error()))
}
sr.SetEligibleList(epochNodes)
}
// NotifyOrder returns the notification order for a start of epoch event
func (sr *subroundStartRound) NotifyOrder() uint32 {
return common.ConsensusOrder
}