-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathtrigger.go
More file actions
535 lines (441 loc) · 16.8 KB
/
trigger.go
File metadata and controls
535 lines (441 loc) · 16.8 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package metachain
import (
"bytes"
"fmt"
"math"
"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/core/closing"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/display"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/storage"
)
var log = logger.GetOrCreate("epochStart/metachain")
var _ dataRetriever.EpochHandler = (*trigger)(nil)
var _ epochStart.TriggerHandler = (*trigger)(nil)
var _ process.EpochStartTriggerHandler = (*trigger)(nil)
var _ process.EpochBootstrapper = (*trigger)(nil)
var _ closing.Closer = (*trigger)(nil)
const minimumNonceToStartEpoch = 4
const disabledRoundForForceEpochStart = math.MaxUint64
// ArgsNewMetaEpochStartTrigger defines struct needed to create a new start of epoch trigger
type ArgsNewMetaEpochStartTrigger struct {
GenesisTime time.Time
Settings *config.EpochStartConfig
Epoch uint32
EpochStartRound uint64
EpochStartNotifier epochStart.Notifier
Marshalizer marshal.Marshalizer
Hasher hashing.Hasher
Storage dataRetriever.StorageService
AppStatusHandler core.AppStatusHandler
DataPool dataRetriever.PoolsHolder
ChainParametersHandler process.ChainParametersHandler
}
type trigger struct {
isEpochStart bool
epoch uint32
epochStartMeta data.HeaderHandler
epochFinalityAttestingRound uint64
currEpochStartRound uint64
prevEpochStartRound uint64
nextEpochStartRound uint64
epochStartMetaHash []byte
triggerStateKey []byte
epochStartTime time.Time
mutTrigger sync.RWMutex
epochStartNotifier epochStart.Notifier
metaHeaderStorage storage.Storer
triggerStorage storage.Storer
marshaller marshal.Marshalizer
hasher hashing.Hasher
appStatusHandler core.AppStatusHandler
validatorInfoPool epochStart.ValidatorInfoCacher
chainParametersHandler process.ChainParametersHandler
epochChangeProposed bool
}
// NewEpochStartTrigger creates a trigger for start of epoch
func NewEpochStartTrigger(args *ArgsNewMetaEpochStartTrigger) (*trigger, error) {
if args == nil {
return nil, epochStart.ErrNilArgsNewMetaEpochStartTrigger
}
if args.Settings == nil {
return nil, epochStart.ErrNilEpochStartSettings
}
if check.IfNil(args.EpochStartNotifier) {
return nil, epochStart.ErrNilEpochStartNotifier
}
if check.IfNil(args.Marshalizer) {
return nil, epochStart.ErrNilMarshalizer
}
if check.IfNil(args.Storage) {
return nil, epochStart.ErrNilStorageService
}
if check.IfNil(args.Hasher) {
return nil, epochStart.ErrNilHasher
}
if check.IfNil(args.AppStatusHandler) {
return nil, epochStart.ErrNilStatusHandler
}
if check.IfNil(args.DataPool) {
return nil, epochStart.ErrNilDataPoolsHolder
}
if check.IfNil(args.DataPool.CurrentEpochValidatorInfo()) {
return nil, epochStart.ErrNilCurrentEpochValidatorsInfoPool
}
if check.IfNil(args.ChainParametersHandler) {
return nil, process.ErrNilChainParametersHandler
}
triggerStorage, err := args.Storage.GetStorer(dataRetriever.BootstrapUnit)
if err != nil {
return nil, err
}
metaBlockStorage, err := args.Storage.GetStorer(dataRetriever.MetaBlockUnit)
if err != nil {
return nil, err
}
trigggerStateKey := common.TriggerRegistryInitialKeyPrefix + fmt.Sprintf("%d", args.Epoch)
trig := &trigger{
triggerStateKey: []byte(trigggerStateKey),
epochStartTime: args.GenesisTime,
currEpochStartRound: args.EpochStartRound,
prevEpochStartRound: args.EpochStartRound,
epoch: args.Epoch,
mutTrigger: sync.RWMutex{},
epochFinalityAttestingRound: args.EpochStartRound,
epochStartNotifier: args.EpochStartNotifier,
metaHeaderStorage: metaBlockStorage,
triggerStorage: triggerStorage,
marshaller: args.Marshalizer,
hasher: args.Hasher,
epochStartMeta: &block.MetaBlock{},
appStatusHandler: args.AppStatusHandler,
nextEpochStartRound: disabledRoundForForceEpochStart,
validatorInfoPool: args.DataPool.CurrentEpochValidatorInfo(),
chainParametersHandler: args.ChainParametersHandler,
}
err = trig.saveState(trig.triggerStateKey)
if err != nil {
return nil, err
}
return trig, nil
}
// IsEpochStart return true if conditions are fulfilled for start of epoch
func (t *trigger) IsEpochStart() bool {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()
return t.isEpochStart
}
// EpochStartRound returns the start round of the current epoch
func (t *trigger) EpochStartRound() uint64 {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()
return t.currEpochStartRound
}
// EpochFinalityAttestingRound returns the round when epoch start block was finalized
func (t *trigger) EpochFinalityAttestingRound() uint64 {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()
return t.epochFinalityAttestingRound
}
// ForceEpochStart sets the round at which the new epoch will start
func (t *trigger) ForceEpochStart(round uint64) {
t.mutTrigger.Lock()
defer t.mutTrigger.Unlock()
t.nextEpochStartRound = round
if t.nextEpochStartRound > t.currEpochStartRound+t.getRoundsPerEpoch(t.epoch)-t.getOffsetPerEpoch(t.epoch) {
t.nextEpochStartRound = disabledRoundForForceEpochStart
log.Debug("can not force epoch start because the resulting round is in the next epoch")
return
}
minRoundsBetweenEpochs := t.getMinRoundsBetweenEpochs(t.epoch)
if t.nextEpochStartRound-t.currEpochStartRound < minRoundsBetweenEpochs {
t.nextEpochStartRound = t.currEpochStartRound + minRoundsBetweenEpochs
log.Debug("can not force epoch start on provided round",
"provided round", round, "computed round", t.nextEpochStartRound)
}
log.Debug("set new epoch start round", "round", t.nextEpochStartRound)
}
func (t *trigger) getRoundsPerEpoch(epoch uint32) uint64 {
chainParametersForEpoch, err := t.chainParametersHandler.ChainParametersForEpoch(epoch)
if err != nil {
log.Warn("could not get rounds per epoch for epoch, returned current chain parameters", "epoch", epoch, "error", err)
chainParametersForEpoch = t.chainParametersHandler.CurrentChainParameters()
}
return uint64(chainParametersForEpoch.RoundsPerEpoch)
}
func (t *trigger) getOffsetPerEpoch(epoch uint32) uint64 {
chainParametersForEpoch, err := t.chainParametersHandler.ChainParametersForEpoch(epoch)
if err != nil {
log.Warn("could not get rounds per epoch for epoch, returned current chain parameters", "epoch", epoch, "error", err)
chainParametersForEpoch = t.chainParametersHandler.CurrentChainParameters()
}
return uint64(chainParametersForEpoch.Offset)
}
func (t *trigger) getMinRoundsBetweenEpochs(epoch uint32) uint64 {
chainParametersForEpoch, err := t.chainParametersHandler.ChainParametersForEpoch(epoch)
if err != nil {
log.Warn("could not get min rounds between epoch, returned current chain parameters", "epoch", epoch, "error", err)
chainParametersForEpoch = t.chainParametersHandler.CurrentChainParameters()
}
return uint64(chainParametersForEpoch.MinRoundsBetweenEpochs)
}
// ShouldProposeEpochChange will return true if an epoch change event should be trigger
func (t *trigger) ShouldProposeEpochChange(currentRound uint64, currentNonce uint64) bool {
t.mutTrigger.Lock()
defer t.mutTrigger.Unlock()
shouldTriggerEpochStart := t.shouldTriggerEpochStart(currentRound, currentNonce)
if shouldTriggerEpochStart && !t.epochChangeProposed {
return true
}
return false
}
// SetEpochChangeProposed sets the epoch change proposed flag to true
func (t *trigger) SetEpochChangeProposed(value bool) {
t.mutTrigger.Lock()
defer t.mutTrigger.Unlock()
t.epochChangeProposed = value
}
func (t *trigger) shouldTriggerEpochStart(currentRound uint64, currentNonce uint64) bool {
isZeroEpochEdgeCase := currentNonce < minimumNonceToStartEpoch
isNormalEpochStart := currentRound > t.currEpochStartRound+t.getRoundsPerEpoch(t.epoch)-t.getOffsetPerEpoch(t.epoch)
isWithEarlyEndOfEpoch := currentRound >= t.nextEpochStartRound
shouldTriggerEpochStart := (isNormalEpochStart || isWithEarlyEndOfEpoch) && !isZeroEpochEdgeCase
return shouldTriggerEpochStart
}
// Update processes changes in the trigger
func (t *trigger) Update(round uint64, nonce uint64) {
t.mutTrigger.Lock()
defer t.mutTrigger.Unlock()
if t.isEpochStart {
return
}
if t.shouldTriggerEpochStart(round, nonce) {
t.setEpochChange(round)
}
}
// SetEpochChange will increment the epoch field and all fields related with epoch change
func (t *trigger) SetEpochChange(round uint64) {
t.mutTrigger.Lock()
defer t.mutTrigger.Unlock()
t.setEpochChange(round)
}
func (t *trigger) setEpochChange(round uint64) {
t.epoch += 1
t.isEpochStart = true
t.prevEpochStartRound = t.currEpochStartRound
t.currEpochStartRound = round
msg := fmt.Sprintf("EPOCH %d BEGINS IN ROUND (%d)", t.epoch, t.currEpochStartRound)
common.SetSuperNovaActivationRound(t.epoch, round)
log.Debug(display.Headline(msg, "", "#"))
log.Debug("trigger.Update", "isEpochStart", t.isEpochStart)
logger.SetCorrelationEpoch(t.epoch)
t.nextEpochStartRound = disabledRoundForForceEpochStart
}
// SetProcessed sets start of epoch to false and cleans underlying structure
func (t *trigger) SetProcessed(header data.HeaderHandler, body data.BodyHandler) {
t.mutTrigger.Lock()
defer t.mutTrigger.Unlock()
metaBlock, ok := header.(data.MetaHeaderHandler)
if !ok {
return
}
if !metaBlock.IsStartOfEpochBlock() {
return
}
if header.IsHeaderV3() {
t.setEpochChange(header.GetRound())
}
metaBuff, errNotCritical := t.marshaller.Marshal(metaBlock)
if errNotCritical != nil {
log.Debug("SetProcessed marshal", "error", errNotCritical.Error())
}
t.appStatusHandler.SetUInt64Value(common.MetricRoundAtEpochStart, metaBlock.GetRound())
t.appStatusHandler.SetUInt64Value(common.MetricNonceAtEpochStart, metaBlock.GetNonce())
metaHash := t.hasher.Compute(string(metaBuff))
t.currEpochStartRound = metaBlock.GetRound()
t.epoch = metaBlock.GetEpoch()
t.isEpochStart = false
t.epochStartMeta = metaBlock
t.epochStartMetaHash = metaHash
t.epochStartNotifier.NotifyAllPrepare(metaBlock, body)
t.epochStartNotifier.NotifyAll(metaBlock)
t.saveCurrentState(metaBlock.GetRound())
log.Debug("trigger.SetProcessed", "isEpochStart", t.isEpochStart)
epochStartIdentifier := core.EpochStartIdentifier(metaBlock.GetEpoch())
errNotCritical = t.triggerStorage.Put([]byte(epochStartIdentifier), metaBuff)
if errNotCritical != nil {
log.Warn("SetProcessed put into triggerStorage", "error", errNotCritical.Error())
}
errNotCritical = t.metaHeaderStorage.Put([]byte(epochStartIdentifier), metaBuff)
if errNotCritical != nil {
log.Warn("SetProcessed put into metaHdrStorage", "error", errNotCritical.Error())
}
}
// SetFinalityAttestingRound sets the round which finalized the start of epoch block
func (t *trigger) SetFinalityAttestingRound(round uint64) {
t.mutTrigger.Lock()
defer t.mutTrigger.Unlock()
if round > t.currEpochStartRound {
t.epochFinalityAttestingRound = round
t.saveCurrentState(round)
t.epochStartNotifier.NotifyEpochChangeConfirmed(t.epoch)
}
}
// RevertStateToBlock will revert the state of the trigger to the current block
func (t *trigger) RevertStateToBlock(header data.HeaderHandler) error {
if check.IfNil(header) {
return epochStart.ErrNilHeaderHandler
}
if header.IsStartOfEpochBlock() {
log.Debug("RevertStateToBlock with epoch start block called")
t.SetProcessed(header, nil)
return nil
}
t.mutTrigger.RLock()
prevMeta := t.epochStartMeta
t.mutTrigger.RUnlock()
currentHeaderHash, err := core.CalculateHash(t.marshaller, t.hasher, header)
if err != nil {
log.Warn("RevertStateToBlock error on hashing", "error", err)
return err
}
if !bytes.Equal(prevMeta.GetPrevHash(), currentHeaderHash) {
return nil
}
log.Debug("RevertStateToBlock to revert behind epoch start block is called")
err = t.revert(prevMeta)
if err != nil {
return err
}
return nil
}
func (t *trigger) revert(header data.HeaderHandler) error {
if check.IfNil(header) || !header.IsStartOfEpochBlock() || header.GetEpoch() == 0 {
return nil
}
metaHdr, ok := header.(data.MetaHeaderHandler)
if !ok {
log.Warn("wrong type assertion in Revert metachain trigger")
return epochStart.ErrWrongTypeAssertion
}
t.mutTrigger.Lock()
defer t.mutTrigger.Unlock()
prevEpochStartIdentifier := core.EpochStartIdentifier(metaHdr.GetEpoch() - 1)
epochStartMetaBuff, err := t.metaHeaderStorage.SearchFirst([]byte(prevEpochStartIdentifier))
if err != nil {
log.Warn("Revert get previous meta from storage", "error", err)
return err
}
epochStartMeta, err := process.UnmarshalMetaHeader(t.marshaller, epochStartMetaBuff)
if err != nil {
log.Warn("Revert unmarshal previous meta", "error", err)
return err
}
epochStartIdentifier := core.EpochStartIdentifier(metaHdr.GetEpoch())
errNotCritical := t.triggerStorage.Remove([]byte(epochStartIdentifier))
if errNotCritical != nil {
log.Debug("Revert remove from triggerStorage", "error", errNotCritical.Error())
}
errNotCritical = t.metaHeaderStorage.Remove([]byte(epochStartIdentifier))
if errNotCritical != nil {
log.Debug("Revert remove from triggerStorage", "error", errNotCritical.Error())
}
t.currEpochStartRound = metaHdr.GetEpochStartHandler().GetEconomicsHandler().GetPrevEpochStartRound()
t.epoch = metaHdr.GetEpoch() - 1
t.isEpochStart = false
t.epochStartMeta = epochStartMeta
log.Debug("trigger.revert",
"isEpochStart", t.isEpochStart,
"epoch", t.epoch,
"epochStartRound", t.currEpochStartRound)
return nil
}
// Epoch return the current epoch
func (t *trigger) Epoch() uint32 {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()
return t.epoch
}
// MetaEpoch return the current epoch
func (t *trigger) MetaEpoch() uint32 {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()
return t.epoch
}
// RequestEpochStartIfNeeded request the needed epoch start block if metablock with new epoch was received
func (t *trigger) RequestEpochStartIfNeeded(_ data.HeaderHandler) {
}
// EpochStartMetaHdrHash returns the announcing meta header hash which created the new epoch
func (t *trigger) EpochStartMetaHdrHash() []byte {
return t.epochStartMetaHash
}
// LastCommitedEpochStartHdr returns the header of the epoch start block
func (t *trigger) LastCommitedEpochStartHdr() (data.HeaderHandler, error) {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()
// marshal + unmarshal deep copy
headerBytes, err := t.marshaller.Marshal(t.epochStartMeta)
if err != nil {
return nil, err
}
return process.UnmarshalMetaHeader(t.marshaller, headerBytes)
}
// GetEpochStartHdrFromStorage returns the header of the epoch start block from storage
func (t *trigger) GetEpochStartHdrFromStorage(epoch uint32) (data.HeaderHandler, error) {
t.mutTrigger.RLock()
defer t.mutTrigger.RUnlock()
epochStartIdentifier := core.EpochStartIdentifier(epoch)
epochStartMetaBuff, err := t.metaHeaderStorage.SearchFirst([]byte(epochStartIdentifier))
if err != nil {
log.Warn("GetEpochStartHdrFromStorage search first", "epoch", epoch, "identifier", epochStartIdentifier, "error", err)
return nil, err
}
metaHdr, err := process.UnmarshalMetaHeader(t.marshaller, epochStartMetaBuff)
if err != nil {
return nil, err
}
return metaHdr, nil
}
// GetSavedStateKey returns the last saved trigger state key
func (t *trigger) GetSavedStateKey() []byte {
return t.triggerStateKey
}
// SetEpochStartMetaHdrHash sets the epoch start meta header has
func (t *trigger) SetEpochStartMetaHdrHash(metaHdrHash []byte) {
t.epochStartMetaHash = metaHdrHash
}
// SetCurrentEpochStartRound sets the round when the current epoch started
func (t *trigger) SetCurrentEpochStartRound(round uint64) {
t.mutTrigger.Lock()
t.currEpochStartRound = round
t.saveCurrentState(round)
t.mutTrigger.Unlock()
}
// Close will close the endless running go routine
func (t *trigger) Close() error {
return nil
}
// IsInterfaceNil return true if underlying object is nil
func (t *trigger) IsInterfaceNil() bool {
return t == nil
}
// needs to be called under locked mutex
func (t *trigger) saveCurrentState(round uint64) {
t.triggerStateKey = []byte(fmt.Sprint(round))
err := t.saveState(t.triggerStateKey)
if err != nil {
log.Warn("error saving trigger state", "error", err, "key", t.triggerStateKey)
}
}