-
Notifications
You must be signed in to change notification settings - Fork 199
/
subroundStartRound.go
310 lines (248 loc) · 8.33 KB
/
subroundStartRound.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
package bls
import (
"encoding/hex"
"fmt"
"time"
elasticIndexer "github.com/ElrondNetwork/elastic-indexer-go"
"github.com/ElrondNetwork/elrond-go/consensus/spos"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/indexer"
)
// subroundStartRound defines the data needed by the subround StartRound
type subroundStartRound struct {
*spos.Subround
processingThresholdPercentage int
executeStoredMessages func()
resetConsensusMessages func()
indexer spos.ConsensusDataIndexer
}
// 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,
indexer: elasticIndexer.NewNilIndexer(),
}
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
}
// SetIndexer method set indexer
func (sr *subroundStartRound) SetIndexer(indexer spos.ConsensusDataIndexer) {
sr.indexer = indexer
}
// doStartRoundJob method does the job of the subround StartRound
func (sr *subroundStartRound) doStartRoundJob() bool {
sr.ResetConsensusState()
sr.RoundIndex = sr.Rounder().Index()
sr.RoundTimeStamp = sr.Rounder().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 != core.NsSynchronized { // if node is not synchronized yet, it has to continue the bootstrapping mechanism
return false
}
sr.AppStatusHandler().SetStringValue(core.MetricConsensusRoundState, "")
err := sr.generateNextConsensusGroup(sr.Rounder().Index())
if err != nil {
log.Debug("initCurrentRound.generateNextConsensusGroup",
"round index", sr.Rounder().Index(),
"error", err.Error())
sr.RoundCanceled = true
return false
}
if sr.NodeRedundancyHandler().IsRedundancyNode() {
sr.NodeRedundancyHandler().AdjustInactivityIfNeeded(
sr.SelfPubKey(),
sr.ConsensusGroup(),
sr.Rounder().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 leader == sr.SelfPubKey() {
sr.AppStatusHandler().Increment(core.MetricCountLeader)
sr.AppStatusHandler().SetStringValue(core.MetricConsensusRoundState, "proposed")
sr.AppStatusHandler().SetStringValue(core.MetricConsensusState, "proposer")
msg = " (my turn)"
}
log.Debug("step 0: preparing the round",
"leader", core.GetTrimmedPk(hex.EncodeToString([]byte(leader))),
"messsage", msg)
pubKeys := sr.ConsensusGroup()
sr.indexRoundIfNeeded(pubKeys)
selfIndex, err := sr.SelfConsensusGroupIndex()
if err != nil {
log.Debug("not in consensus group")
sr.AppStatusHandler().SetStringValue(core.MetricConsensusState, "not in consensus group")
} else {
if leader != sr.SelfPubKey() {
sr.AppStatusHandler().Increment(core.MetricCountConsensus)
}
sr.AppStatusHandler().SetStringValue(core.MetricConsensusState, "participant")
}
err = sr.MultiSigner().Reset(pubKeys, uint16(selfIndex))
if err != nil {
log.Debug("initCurrentRound.Reset", "error", err.Error())
sr.RoundCanceled = true
return false
}
startTime := sr.RoundTimeStamp
maxTime := sr.Rounder().TimeDuration() * time.Duration(sr.processingThresholdPercentage) / 100
if sr.Rounder().RemainingTime(startTime, maxTime) < 0 {
log.Debug("canceled round, time is out",
"round", sr.SyncTimer().FormattedCurrentTime(), sr.Rounder().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) indexRoundIfNeeded(pubKeys []string) {
if check.IfNil(sr.indexer) {
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.Rounder().Index()
roundInfo := &indexer.RoundInfo{
Index: uint64(round),
SignersIndexes: signersIndexes,
BlockWasProposed: false,
ShardId: shardId,
Timestamp: time.Duration(sr.RoundTimeStamp.Unix()),
}
sr.indexer.SaveRoundsInfo([]*indexer.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)
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 core.ConsensusOrder
}