/
slasher.go
650 lines (623 loc) · 22.8 KB
/
slasher.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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// Copyright (c) 2020 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package poll
import (
"context"
"math/big"
"strconv"
"github.com/iotexproject/iotex-election/util"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
"github.com/iotexproject/iotex-core/action/protocol/vote"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/crypto"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state"
)
// Slasher is the module to slash candidates
type Slasher struct {
productivity Productivity
getCandidates GetCandidates
getProbationList GetProbationList
getUnprodDelegate GetUnproductiveDelegate
indexer *CandidateIndexer
numCandidateDelegates uint64
numDelegates uint64
numOfBlocksByEpoch uint64
prodThreshold uint64
probationEpochPeriod uint64
maxProbationPeriod uint64
probationIntensity uint32
}
// NewSlasher returns a new Slasher
func NewSlasher(
productivity Productivity,
getCandidates GetCandidates,
getProbationList GetProbationList,
getUnprodDelegate GetUnproductiveDelegate,
indexer *CandidateIndexer,
numCandidateDelegates, numDelegates, dardanellesNumSubEpochs, thres, koPeriod, maxKoPeriod uint64,
koIntensity uint32,
) (*Slasher, error) {
return &Slasher{
productivity: productivity,
getCandidates: getCandidates,
getProbationList: getProbationList,
getUnprodDelegate: getUnprodDelegate,
indexer: indexer,
numCandidateDelegates: numCandidateDelegates,
numDelegates: numDelegates,
numOfBlocksByEpoch: numDelegates * dardanellesNumSubEpochs,
prodThreshold: thres,
probationEpochPeriod: koPeriod,
maxProbationPeriod: maxKoPeriod,
probationIntensity: koIntensity,
}, nil
}
// CreateGenesisStates creates genesis state for slasher
func (sh *Slasher) CreateGenesisStates(ctx context.Context, sm protocol.StateManager, indexer *CandidateIndexer) error {
g := genesis.MustExtractGenesisContext(ctx)
if g.IsEaster(uint64(1)) {
if err := setNextEpochProbationList(sm,
indexer,
uint64(1),
vote.NewProbationList(sh.probationIntensity)); err != nil {
return err
}
}
return nil
}
// CreatePreStates is to setup probation list
func (sh *Slasher) CreatePreStates(ctx context.Context, sm protocol.StateManager, indexer *CandidateIndexer) error {
blkCtx := protocol.MustGetBlockCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
featureWithHeightCtx := protocol.MustGetFeatureWithHeightCtx(ctx)
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
epochNum := rp.GetEpochNum(blkCtx.BlockHeight)
epochStartHeight := rp.GetEpochHeight(epochNum)
epochLastHeight := rp.GetEpochLastBlockHeight(epochNum)
nextEpochStartHeight := rp.GetEpochHeight(epochNum + 1)
if featureCtx.UpdateBlockMeta {
if err := sh.updateCurrentBlockMeta(ctx, sm); err != nil {
return errors.Wrap(err, "faild to update current epoch meta")
}
}
if blkCtx.BlockHeight == epochLastHeight && featureWithHeightCtx.CalculateProbationList(nextEpochStartHeight) {
// if the block height is the end of epoch and next epoch is after the Easter height, calculate probation list for probation and write into state DB
unqualifiedList, err := sh.CalculateProbationList(ctx, sm, epochNum+1)
if err != nil {
return err
}
return setNextEpochProbationList(sm, indexer, nextEpochStartHeight, unqualifiedList)
}
if blkCtx.BlockHeight == epochStartHeight && featureWithHeightCtx.CalculateProbationList(epochStartHeight) {
prevHeight, err := shiftCandidates(sm)
if err != nil {
return err
}
afterHeight, err := shiftProbationList(sm)
if err != nil {
return err
}
if prevHeight != afterHeight {
return errors.Wrap(ErrInconsistentHeight, "shifting candidate height is not same as shifting probation height")
}
}
return nil
}
// ReadState defines slasher's read methods.
func (sh *Slasher) ReadState(
ctx context.Context,
sr protocol.StateReader,
indexer *CandidateIndexer,
method []byte,
args ...[]byte,
) ([]byte, uint64, error) {
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
targetHeight, err := sr.Height()
if err != nil {
return nil, uint64(0), err
}
epochNum := rp.GetEpochNum(targetHeight)
epochStartHeight := rp.GetEpochHeight(epochNum)
if len(args) != 0 {
epochNumArg, err := strconv.ParseUint(string(args[0]), 10, 64)
if err != nil {
return nil, uint64(0), err
}
if indexer == nil {
// consistency check between sr.height and epochNumArg in case of using state reader(not indexer)
if epochNum != epochNumArg {
return nil, uint64(0), errors.New("Slasher ReadState arg epochNumber should be same as state reader height, need to set argument/height consistently")
}
}
epochStartHeight = rp.GetEpochHeight(epochNumArg)
}
switch string(method) {
case "CandidatesByEpoch":
if indexer != nil {
candidates, err := sh.GetCandidatesFromIndexer(ctx, epochStartHeight)
if err == nil {
data, err := candidates.Serialize()
if err != nil {
return nil, uint64(0), err
}
return data, epochStartHeight, nil
}
if err != nil && errors.Cause(err) != ErrIndexerNotExist {
return nil, uint64(0), err
}
}
candidates, height, err := sh.GetCandidates(ctx, sr, false)
if err != nil {
return nil, uint64(0), err
}
data, err := candidates.Serialize()
if err != nil {
return nil, uint64(0), err
}
return data, height, nil
case "BlockProducersByEpoch":
if indexer != nil {
blockProducers, err := sh.GetBPFromIndexer(ctx, epochStartHeight)
if err == nil {
data, err := blockProducers.Serialize()
if err != nil {
return nil, uint64(0), err
}
return data, epochStartHeight, nil
}
if err != nil && errors.Cause(err) != ErrIndexerNotExist {
return nil, uint64(0), err
}
}
bp, height, err := sh.GetBlockProducers(ctx, sr, false)
if err != nil {
return nil, uint64(0), err
}
data, err := bp.Serialize()
if err != nil {
return nil, uint64(0), err
}
return data, height, nil
case "ActiveBlockProducersByEpoch":
if indexer != nil {
activeBlockProducers, err := sh.GetABPFromIndexer(ctx, epochStartHeight)
if err == nil {
data, err := activeBlockProducers.Serialize()
if err != nil {
return nil, uint64(0), err
}
return data, epochStartHeight, nil
}
if err != nil && errors.Cause(err) != ErrIndexerNotExist {
return nil, uint64(0), err
}
}
abp, height, err := sh.GetActiveBlockProducers(ctx, sr, false)
if err != nil {
return nil, uint64(0), err
}
data, err := abp.Serialize()
if err != nil {
return nil, uint64(0), err
}
return data, height, nil
case "ProbationListByEpoch":
if indexer != nil {
probationList, err := indexer.ProbationList(epochStartHeight)
if err == nil {
data, err := probationList.Serialize()
if err != nil {
return nil, uint64(0), err
}
return data, epochStartHeight, nil
}
if err != nil && errors.Cause(err) != ErrIndexerNotExist {
return nil, uint64(0), err
}
}
probationList, height, err := sh.GetProbationList(ctx, sr, false)
if err != nil {
return nil, uint64(0), err
}
data, err := probationList.Serialize()
if err != nil {
return nil, uint64(0), err
}
return data, height, nil
default:
return nil, uint64(0), errors.New("corresponding method isn't found")
}
}
// GetCandidates returns filtered candidate list
func (sh *Slasher) GetCandidates(ctx context.Context, sr protocol.StateReader, readFromNext bool) (state.CandidateList, uint64, error) {
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
featureWithHeightCtx := protocol.MustGetFeatureWithHeightCtx(ctx)
targetHeight, err := sr.Height()
if err != nil {
return nil, uint64(0), err
}
// make sure it's epochStartHeight
targetEpochStartHeight := rp.GetEpochHeight(rp.GetEpochNum(targetHeight))
if readFromNext {
targetEpochNum := rp.GetEpochNum(targetEpochStartHeight) + 1
targetEpochStartHeight = rp.GetEpochHeight(targetEpochNum) // next epoch start height
}
calculate := !featureWithHeightCtx.CalculateProbationList(targetEpochStartHeight)
candidates, stateHeight, err := sh.getCandidates(sr, targetEpochStartHeight, calculate, readFromNext)
if err != nil {
return nil, uint64(0), errors.Wrapf(err, "failed to get candidates at height %d", targetEpochStartHeight)
}
// to catch the corner case that since the new block is committed, shift occurs in the middle of processing the request
if rp.GetEpochNum(targetEpochStartHeight) < rp.GetEpochNum(stateHeight) {
return nil, uint64(0), errors.Wrap(ErrInconsistentHeight, "state factory epoch number became larger than target epoch number")
}
if calculate {
return candidates, stateHeight, nil
}
// After Easter height, probation unqualified delegates based on productivity
unqualifiedList, _, err := sh.GetProbationList(ctx, sr, readFromNext)
if err != nil {
return nil, uint64(0), errors.Wrapf(err, "failed to get probation list at height %d", targetEpochStartHeight)
}
// recalculate the voting power for probationlist delegates
filteredCandidate, err := filterCandidates(candidates, unqualifiedList, targetEpochStartHeight)
if err != nil {
return nil, uint64(0), err
}
return filteredCandidate, stateHeight, nil
}
// GetBlockProducers returns BP list
func (sh *Slasher) GetBlockProducers(ctx context.Context, sr protocol.StateReader, readFromNext bool) (state.CandidateList, uint64, error) {
candidates, height, err := sh.GetCandidates(ctx, sr, readFromNext)
if err != nil {
return nil, uint64(0), err
}
bp, err := sh.calculateBlockProducer(candidates)
if err != nil {
return nil, uint64(0), err
}
return bp, height, nil
}
// GetActiveBlockProducers returns active BP list
func (sh *Slasher) GetActiveBlockProducers(ctx context.Context, sr protocol.StateReader, readFromNext bool) (state.CandidateList, uint64, error) {
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
targetHeight, err := sr.Height()
if err != nil {
return nil, uint64(0), err
}
// make sure it's epochStartHeight
targetEpochStartHeight := rp.GetEpochHeight(rp.GetEpochNum(targetHeight))
if readFromNext {
targetEpochNum := rp.GetEpochNum(targetEpochStartHeight) + 1
targetEpochStartHeight = rp.GetEpochHeight(targetEpochNum) // next epoch start height
}
blockProducers, height, err := sh.GetBlockProducers(ctx, sr, readFromNext)
if err != nil {
return nil, uint64(0), errors.Wrapf(err, "failed to read block producers at height %d", targetEpochStartHeight)
}
abp, err := sh.calculateActiveBlockProducer(ctx, blockProducers, targetEpochStartHeight)
if err != nil {
return nil, uint64(0), err
}
return abp, height, nil
}
// GetCandidatesFromIndexer returns candidate list from indexer
func (sh *Slasher) GetCandidatesFromIndexer(ctx context.Context, epochStartHeight uint64) (state.CandidateList, error) {
featureWithHeightCtx := protocol.MustGetFeatureWithHeightCtx(ctx)
candidates, err := sh.indexer.CandidateList(epochStartHeight)
if err != nil {
return nil, err
}
if !featureWithHeightCtx.CalculateProbationList(epochStartHeight) {
return candidates, nil
}
// After Easter height, probation unqualified delegates based on productivity
probationList, err := sh.indexer.ProbationList(epochStartHeight)
if err != nil {
return nil, err
}
// recalculate the voting power for probationlist delegates
return filterCandidates(candidates, probationList, epochStartHeight)
}
// GetBPFromIndexer returns BP list from indexer
func (sh *Slasher) GetBPFromIndexer(ctx context.Context, epochStartHeight uint64) (state.CandidateList, error) {
candidates, err := sh.GetCandidatesFromIndexer(ctx, epochStartHeight)
if err != nil {
return nil, err
}
return sh.calculateBlockProducer(candidates)
}
// GetABPFromIndexer returns active BP list from indexer
func (sh *Slasher) GetABPFromIndexer(ctx context.Context, epochStartHeight uint64) (state.CandidateList, error) {
blockProducers, err := sh.GetBPFromIndexer(ctx, epochStartHeight)
if err != nil {
return nil, err
}
return sh.calculateActiveBlockProducer(ctx, blockProducers, epochStartHeight)
}
// GetProbationList returns the probation list at given epoch
func (sh *Slasher) GetProbationList(ctx context.Context, sr protocol.StateReader, readFromNext bool) (*vote.ProbationList, uint64, error) {
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
featureWithHeightCtx := protocol.MustGetFeatureWithHeightCtx(ctx)
targetHeight, err := sr.Height()
if err != nil {
return nil, uint64(0), err
}
// make sure it's epochStartHeight
targetEpochStartHeight := rp.GetEpochHeight(rp.GetEpochHeight(targetHeight))
if readFromNext {
targetEpochNum := rp.GetEpochNum(targetEpochStartHeight) + 1
targetEpochStartHeight = rp.GetEpochHeight(targetEpochNum) // next epoch start height
}
if !featureWithHeightCtx.CalculateProbationList(targetEpochStartHeight) {
return nil, uint64(0), errors.New("Before Easter, there is no probation list in stateDB")
}
unqualifiedList, stateHeight, err := sh.getProbationList(sr, readFromNext)
if err != nil {
return nil, uint64(0), err
}
// to catch the corner case that since the new block is committed, shift occurs in the middle of processing the request
if rp.GetEpochNum(targetEpochStartHeight) < rp.GetEpochNum(stateHeight) {
return nil, uint64(0), errors.Wrap(ErrInconsistentHeight, "state factory tip epoch number became larger than target epoch number")
}
return unqualifiedList, stateHeight, nil
}
// CalculateProbationList calculates probation list according to productivity
func (sh *Slasher) CalculateProbationList(
ctx context.Context,
sm protocol.StateManager,
epochNum uint64,
) (*vote.ProbationList, error) {
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
g := genesis.MustExtractGenesisContext(ctx)
easterEpochNum := rp.GetEpochNum(g.EasterBlockHeight)
nextProbationlist := &vote.ProbationList{
IntensityRate: sh.probationIntensity,
}
upd, err := sh.getUnprodDelegate(sm)
if err != nil {
if errors.Cause(err) == state.ErrStateNotExist {
if upd, err = vote.NewUnproductiveDelegate(sh.probationEpochPeriod, sh.maxProbationPeriod); err != nil {
return nil, errors.Wrap(err, "failed to make new upd")
}
} else {
return nil, errors.Wrapf(err, "failed to read upd struct from state DB at epoch number %d", epochNum)
}
}
unqualifiedDelegates := make(map[string]uint32)
if epochNum <= easterEpochNum+sh.probationEpochPeriod {
// if epoch number is smaller than easterEpochNum+K(probation period), calculate it one-by-one (initialize).
log.L().Debug("Before using probation list",
zap.Uint64("epochNum", epochNum),
zap.Uint64("easterEpochNum", easterEpochNum),
zap.Uint64("probationEpochPeriod", sh.probationEpochPeriod),
)
existinglist := upd.DelegateList()
for _, listByEpoch := range existinglist {
for _, addr := range listByEpoch {
if _, ok := unqualifiedDelegates[addr]; !ok {
unqualifiedDelegates[addr] = 1
} else {
unqualifiedDelegates[addr]++
}
}
}
// calculate upd of epochNum-1 (latest)
uq, err := sh.calculateUnproductiveDelegates(ctx, sm)
if err != nil {
return nil, errors.Wrapf(err, "failed to calculate current epoch upd %d", epochNum-1)
}
for _, addr := range uq {
if _, ok := unqualifiedDelegates[addr]; !ok {
unqualifiedDelegates[addr] = 1
} else {
unqualifiedDelegates[addr]++
}
}
if err := upd.AddRecentUPD(uq); err != nil {
return nil, errors.Wrap(err, "failed to add recent upd")
}
nextProbationlist.ProbationInfo = unqualifiedDelegates
return nextProbationlist, setUnproductiveDelegates(sm, upd)
}
// ProbationList[N] = ProbationList[N-1] - Low-productivity-list[N-K-1] + Low-productivity-list[N-1]
log.L().Debug("Using probationList",
zap.Uint64("epochNum", epochNum),
zap.Uint64("easterEpochNum", easterEpochNum),
zap.Uint64("probationEpochPeriod", sh.probationEpochPeriod),
)
prevProbationlist, _, err := sh.getProbationList(sm, false)
if err != nil {
return nil, errors.Wrap(err, "failed to read latest probation list")
}
probationMap := prevProbationlist.ProbationInfo
if probationMap == nil {
probationMap = make(map[string]uint32)
}
skipList := upd.ReadOldestUPD()
for _, addr := range skipList {
if _, ok := probationMap[addr]; !ok {
log.L().Fatal("skipping list element doesn't exist among one of existing map")
continue
}
probationMap[addr]--
}
addList, err := sh.calculateUnproductiveDelegates(ctx, sm)
if err != nil {
return nil, errors.Wrapf(err, "failed to calculate current epoch upd %d", epochNum-1)
}
if err := upd.AddRecentUPD(addList); err != nil {
return nil, errors.Wrap(err, "failed to add recent upd")
}
for _, addr := range addList {
if _, ok := probationMap[addr]; ok {
probationMap[addr]++
continue
}
probationMap[addr] = 1
}
for addr, count := range probationMap {
if count == 0 {
delete(probationMap, addr)
}
}
nextProbationlist.ProbationInfo = probationMap
return nextProbationlist, setUnproductiveDelegates(sm, upd)
}
func (sh *Slasher) calculateUnproductiveDelegates(ctx context.Context, sr protocol.StateReader) ([]string, error) {
blkCtx := protocol.MustGetBlockCtx(ctx)
bcCtx := protocol.MustGetBlockchainCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
epochNum := rp.GetEpochNum(blkCtx.BlockHeight)
delegates, _, err := sh.GetActiveBlockProducers(ctx, sr, false)
if err != nil {
return nil, err
}
productivityFunc := sh.productivity
if featureCtx.CurrentEpochProductivity {
productivityFunc = func(start, end uint64) (map[string]uint64, error) {
return currentEpochProductivity(sr, start, end, sh.numOfBlocksByEpoch)
}
}
numBlks, produce, err := rp.ProductivityByEpoch(
epochNum,
bcCtx.Tip.Height,
productivityFunc,
)
if err != nil {
return nil, err
}
// The current block is not included, so add it
numBlks++
if _, ok := produce[blkCtx.Producer.String()]; ok {
produce[blkCtx.Producer.String()]++
} else {
produce[blkCtx.Producer.String()] = 1
}
for _, abp := range delegates {
if _, ok := produce[abp.Address]; !ok {
produce[abp.Address] = 0
}
}
unqualified := make([]string, 0)
expectedNumBlks := numBlks / uint64(len(produce))
for addr, actualNumBlks := range produce {
if actualNumBlks*100/expectedNumBlks < sh.prodThreshold {
unqualified = append(unqualified, addr)
}
}
return unqualified, nil
}
func (sh *Slasher) updateCurrentBlockMeta(ctx context.Context, sm protocol.StateManager) error {
blkCtx := protocol.MustGetBlockCtx(ctx)
currentBlockMeta := NewBlockMeta(blkCtx.BlockHeight, blkCtx.Producer.String(), blkCtx.BlockTimeStamp)
return setCurrentBlockMeta(sm, currentBlockMeta, blkCtx.BlockHeight, sh.numOfBlocksByEpoch)
}
// calculateBlockProducer calculates block producer by given candidate list
func (sh *Slasher) calculateBlockProducer(candidates state.CandidateList) (state.CandidateList, error) {
var blockProducers state.CandidateList
for i, candidate := range candidates {
if uint64(i) >= sh.numCandidateDelegates {
break
}
if candidate.Votes.Cmp(big.NewInt(0)) == 0 {
// if the voting power is 0, exclude from being a block producer(hard probation)
continue
}
blockProducers = append(blockProducers, candidate)
}
return blockProducers, nil
}
// calculateActiveBlockProducer calculates active block producer by given block producer list
func (sh *Slasher) calculateActiveBlockProducer(
ctx context.Context,
blockProducers state.CandidateList,
epochStartHeight uint64,
) (state.CandidateList, error) {
var blockProducerList []string
blockProducerMap := make(map[string]*state.Candidate)
for _, bp := range blockProducers {
blockProducerList = append(blockProducerList, bp.Address)
blockProducerMap[bp.Address] = bp
}
crypto.SortCandidates(blockProducerList, epochStartHeight, crypto.CryptoSeed)
length := int(sh.numDelegates)
if len(blockProducerList) < length {
// TODO: if the number of delegates is smaller than expected, should it return error or not?
length = len(blockProducerList)
log.L().Warn(
"the number of block producer is less than expected",
zap.Int("actual block producer", len(blockProducerList)),
zap.Uint64("expected", sh.numDelegates),
)
}
var activeBlockProducers state.CandidateList
for i := 0; i < length; i++ {
activeBlockProducers = append(activeBlockProducers, blockProducerMap[blockProducerList[i]])
}
return activeBlockProducers, nil
}
// filterCandidates returns filtered candidate list by given raw candidate/ probation list
func filterCandidates(
candidates state.CandidateList,
unqualifiedList *vote.ProbationList,
epochStartHeight uint64,
) (state.CandidateList, error) {
candidatesMap := make(map[string]*state.Candidate)
updatedVotingPower := make(map[string]*big.Int)
intensityRate := float64(uint32(100)-unqualifiedList.IntensityRate) / float64(100)
for _, cand := range candidates {
filterCand := cand.Clone()
if _, ok := unqualifiedList.ProbationInfo[cand.Address]; ok {
// if it is an unqualified delegate, multiply the voting power with probation intensity rate
votingPower := new(big.Float).SetInt(filterCand.Votes)
filterCand.Votes, _ = votingPower.Mul(votingPower, big.NewFloat(intensityRate)).Int(nil)
}
updatedVotingPower[filterCand.Address] = filterCand.Votes
candidatesMap[filterCand.Address] = filterCand
}
// sort again with updated voting power
sorted := util.Sort(updatedVotingPower, epochStartHeight)
var verifiedCandidates state.CandidateList
for _, name := range sorted {
verifiedCandidates = append(verifiedCandidates, candidatesMap[name])
}
return verifiedCandidates, nil
}
// currentEpochProductivity returns the map of the number of blocks produced per delegate of current epoch
func currentEpochProductivity(sr protocol.StateReader, start uint64, end uint64, numOfBlocksByEpoch uint64) (map[string]uint64, error) {
log.L().Debug("Read current epoch productivity",
zap.Uint64("start height", start),
zap.Uint64("end height", end),
)
stats := make(map[string]uint64)
blockmetas, err := allBlockMetasFromDB(sr, numOfBlocksByEpoch)
if err != nil {
return nil, err
}
expectedCount := end - start + 1
count := uint64(0)
for _, blockmeta := range blockmetas {
if blockmeta.Height < start || blockmeta.Height > end {
continue
}
stats[blockmeta.Producer]++
count++
}
if expectedCount != count {
log.L().Debug(
"block metas from stateDB count is not same as expected",
zap.Uint64("expected", expectedCount),
zap.Uint64("actual", count),
)
return nil, errors.New("block metas from stateDB doesn't have enough data for given start, end height")
}
return stats, nil
}