forked from iotexproject/iotex-election
/
committee.go
523 lines (480 loc) · 14.7 KB
/
committee.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
// Copyright (c) 2019 IoTeX
// This program is free software: you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If
// not, see <http://www.gnu.org/licenses/>.
package committee
import (
"context"
"math"
"math/big"
"sort"
"sync"
"sync/atomic"
"time"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/iotexproject/iotex-election/carrier"
"github.com/iotexproject/iotex-election/db"
"github.com/iotexproject/iotex-election/types"
"github.com/iotexproject/iotex-election/util"
)
// Namespace to store the result in db
const Namespace = "electionNS"
// CalcGravityChainHeight calculates the corresponding gravity chain height for an epoch
type CalcGravityChainHeight func(uint64) (uint64, error)
// Config defines the config of the committee
type Config struct {
NumOfRetries uint8 `yaml:"numOfRetries"`
GravityChainAPIs []string `yaml:"gravityChainAPIs"`
GravityChainHeightInterval uint64 `yaml:"gravityChainHeightInterval"`
GravityChainStartHeight uint64 `yaml:"gravityChainStartHeight"`
RegisterContractAddress string `yaml:"registerContractAddress"`
StakingContractAddress string `yaml:"stakingContractAddress"`
PaginationSize uint8 `yaml:"paginationSize"`
VoteThreshold string `yaml:"voteThreshold"`
ScoreThreshold string `yaml:"scoreThreshold"`
SelfStakingThreshold string `yaml:"selfStakingThreshold"`
CacheSize uint32 `yaml:"cacheSize"`
NumOfFetchInParallel uint8 `yaml:"numOfFetchInParallel"`
SkipManifiedCandidate bool `yaml:"skipManifiedCandidate"`
}
// STATUS represents the status of committee
type STATUS uint8
const (
// STARTING stands for a starting status
STARTING STATUS = iota
// ACTIVE stands for an active status
ACTIVE
// INACTIVE stands for an inactive status
INACTIVE
)
// Committee defines an interface of an election committee
// It could be considered as a light state db of gravity chain, that
type Committee interface {
// Start starts the committee service
Start(context.Context) error
// Stop stops the committee service
Stop(context.Context) error
// ResultByHeight returns the result on a specific ethereum height
ResultByHeight(height uint64) (*types.ElectionResult, error)
// FetchResultByHeight returns the votes
FetchResultByHeight(height uint64) (*types.ElectionResult, error)
// HeightByTime returns the nearest result before time
HeightByTime(timestamp time.Time) (uint64, error)
// LatestHeight returns the height with latest result
LatestHeight() uint64
// Status returns the committee status
Status() STATUS
}
type committee struct {
db db.KVStore
carrier carrier.Carrier
retryLimit uint8
paginationSize uint8
fetchInParallel uint8
skipManifiedCandidate bool
voteThreshold *big.Int
scoreThreshold *big.Int
selfStakingThreshold *big.Int
interval uint64
cache *resultCache
heightManager *heightManager
startHeight uint64
nextHeight uint64
currentHeight uint64
lastUpdateTimestamp int64
terminate chan bool
mutex sync.RWMutex
}
// NewCommitteeWithKVStoreWithNamespace creates a committee with kvstore with namespace
func NewCommitteeWithKVStoreWithNamespace(kvstore db.KVStoreWithNamespace, cfg Config) (Committee, error) {
return NewCommittee(db.NewKVStoreWithNamespaceWrapper(Namespace, kvstore), cfg)
}
// NewCommittee creates a committee
func NewCommittee(kvstore db.KVStore, cfg Config) (Committee, error) {
if !common.IsHexAddress(cfg.StakingContractAddress) {
return nil, errors.New("Invalid staking contract address")
}
carrier, err := carrier.NewEthereumVoteCarrier(
cfg.GravityChainAPIs,
common.HexToAddress(cfg.RegisterContractAddress),
common.HexToAddress(cfg.StakingContractAddress),
)
zap.L().Info(
"Carrier created",
zap.String("registerContractAddress", cfg.RegisterContractAddress),
zap.String("stakingContractAddress", cfg.StakingContractAddress),
)
if err != nil {
return nil, err
}
voteThreshold, ok := new(big.Int).SetString(cfg.VoteThreshold, 10)
if !ok {
return nil, errors.New("Invalid vote threshold")
}
scoreThreshold, ok := new(big.Int).SetString(cfg.ScoreThreshold, 10)
if !ok {
return nil, errors.New("Invalid score threshold")
}
selfStakingThreshold, ok := new(big.Int).SetString(cfg.SelfStakingThreshold, 10)
if !ok {
return nil, errors.New("Invalid self staking threshold")
}
fetchInParallel := uint8(10)
if cfg.NumOfFetchInParallel > 0 {
fetchInParallel = cfg.NumOfFetchInParallel
}
return &committee{
db: kvstore,
cache: newResultCache(cfg.CacheSize),
heightManager: newHeightManager(),
carrier: carrier,
retryLimit: cfg.NumOfRetries,
paginationSize: cfg.PaginationSize,
fetchInParallel: fetchInParallel,
skipManifiedCandidate: cfg.SkipManifiedCandidate,
voteThreshold: voteThreshold,
scoreThreshold: scoreThreshold,
selfStakingThreshold: selfStakingThreshold,
terminate: make(chan bool),
startHeight: cfg.GravityChainStartHeight,
interval: cfg.GravityChainHeightInterval,
currentHeight: 0,
nextHeight: cfg.GravityChainStartHeight,
}, nil
}
func (ec *committee) Start(ctx context.Context) (err error) {
ec.mutex.Lock()
defer ec.mutex.Unlock()
if err := ec.db.Start(ctx); err != nil {
return errors.Wrap(err, "error when starting db")
}
if startHeight, err := ec.db.Get(db.NextHeightKey); err == nil {
zap.L().Info("restoring from db")
ec.nextHeight = util.BytesToUint64(startHeight)
for height := ec.startHeight; height < ec.nextHeight; height += ec.interval {
zap.L().Info("loading", zap.Uint64("height", height))
data, err := ec.db.Get(ec.dbKey(height))
if err != nil {
return err
}
r := &types.ElectionResult{}
if err := r.Deserialize(data); err != nil {
return err
}
ec.cache.insert(height, r)
if err := ec.heightManager.add(height, r.MintTime()); err != nil {
return err
}
}
}
zap.L().Info("catching up via network")
tipHeight, err := ec.carrier.TipHeight()
if err != nil {
return errors.Wrap(err, "failed to get tip height")
}
results, errs := ec.fetchInBatch(tipHeight)
if err := ec.storeInBatch(results, errs); err != nil {
return errors.Wrap(err, "failed to catch up via network")
}
zap.L().Info("subscribing to new block")
heightChan := make(chan uint64)
reportChan := make(chan error)
go func() {
for {
select {
case <-ec.terminate:
ec.terminate <- true
return
case height := <-heightChan:
if err := ec.Sync(height); err != nil {
zap.L().Error("failed to sync", zap.Error(err))
}
case err := <-reportChan:
zap.L().Error("something goes wrong", zap.Error(err))
}
}
}()
ec.carrier.SubscribeNewBlock(heightChan, reportChan, ec.terminate)
return nil
}
func (ec *committee) Stop(ctx context.Context) error {
ec.mutex.Lock()
defer ec.mutex.Unlock()
ec.terminate <- true
ec.carrier.Close()
return ec.db.Stop(ctx)
}
func (ec *committee) Status() STATUS {
lastUpdateTimestamp := atomic.LoadInt64(&ec.lastUpdateTimestamp)
switch {
case lastUpdateTimestamp == 0:
return STARTING
case lastUpdateTimestamp > time.Now().Add(-60*time.Second).Unix():
return ACTIVE
default:
return INACTIVE
}
}
func (ec *committee) Sync(tipHeight uint64) error {
results, errs := ec.fetchInBatch(tipHeight)
ec.mutex.Lock()
defer ec.mutex.Unlock()
return ec.storeInBatch(results, errs)
}
func (ec *committee) fetchInBatch(tipHeight uint64) (
map[uint64]*types.ElectionResult,
map[uint64]error,
) {
zap.L().Info("new ethereum block", zap.Uint64("height", tipHeight))
if ec.currentHeight < tipHeight {
ec.currentHeight = tipHeight
}
var wg sync.WaitGroup
limiter := make(chan bool, ec.fetchInParallel)
results := map[uint64]*types.ElectionResult{}
errs := map[uint64]error{}
for nextHeight := ec.nextHeight; nextHeight < ec.currentHeight; nextHeight += ec.interval {
if nextHeight > ec.currentHeight-12 {
break
}
wg.Add(1)
go func(height uint64) {
defer func() {
<-limiter
wg.Done()
}()
limiter <- true
results[height], errs[height] = ec.retryFetchResultByHeight(height)
}(nextHeight)
}
wg.Wait()
return results, errs
}
func (ec *committee) storeInBatch(
results map[uint64]*types.ElectionResult,
errs map[uint64]error,
) error {
var heights []uint64
for height := range results {
heights = append(heights, height)
}
sort.Slice(heights, func(i, j int) bool {
return heights[i] < heights[j]
})
for _, height := range heights {
result := results[height]
if err := errs[height]; err != nil {
return err
}
if err := ec.heightManager.validate(height, result.MintTime()); err != nil {
zap.L().Fatal(
"Unexpected status that the upcoming block height or time is invalid",
zap.Error(err),
)
}
if err := ec.storeResult(height, result); err != nil {
return errors.Wrapf(err, "failed to store result of height %d", height)
}
ec.nextHeight = height + ec.interval
atomic.StoreInt64(&ec.lastUpdateTimestamp, time.Now().Unix())
}
return nil
}
func (ec *committee) LatestHeight() uint64 {
ec.mutex.RLock()
defer ec.mutex.RUnlock()
return ec.heightManager.lastestHeight()
}
func (ec *committee) HeightByTime(ts time.Time) (uint64, error) {
ec.mutex.RLock()
defer ec.mutex.RUnlock()
height := ec.heightManager.nearestHeightBefore(ts)
if height == 0 {
return 0, db.ErrNotExist
}
return height, nil
}
func (ec *committee) ResultByHeight(height uint64) (*types.ElectionResult, error) {
ec.mutex.RLock()
defer ec.mutex.RUnlock()
return ec.resultByHeight(height)
}
func (ec *committee) resultByHeight(height uint64) (*types.ElectionResult, error) {
if height < ec.startHeight {
return nil, errors.Errorf(
"height %d is lower than start height %d",
height,
ec.startHeight,
)
}
if (height-ec.startHeight)%ec.interval != 0 {
return nil, errors.Errorf(
"height %d is an invalid height",
height,
)
}
result := ec.cache.get(height)
if result != nil {
return result, nil
}
data, err := ec.db.Get(ec.dbKey(height))
if err != nil {
return nil, err
}
if data == nil {
return nil, db.ErrNotExist
}
result = &types.ElectionResult{}
return result, result.Deserialize(data)
}
func (ec *committee) calcWeightedVotes(v *types.Vote, now time.Time) *big.Int {
if now.Before(v.StartTime()) {
return big.NewInt(0)
}
remainingTime := v.RemainingTime(now).Seconds()
weight := float64(1)
if remainingTime > 0 {
weight += math.Log(math.Ceil(remainingTime/86400)) / math.Log(1.2) / 100
}
amount := new(big.Float).SetInt(v.Amount())
weightedAmount, _ := amount.Mul(amount, big.NewFloat(weight)).Int(nil)
return weightedAmount
}
func (ec *committee) fetchVotesByHeight(height uint64) ([]*types.Vote, error) {
var allVotes []*types.Vote
previousIndex := big.NewInt(0)
for {
var votes []*types.Vote
var err error
if previousIndex, votes, err = ec.carrier.Votes(
height,
previousIndex,
ec.paginationSize,
); err != nil {
return nil, err
}
allVotes = append(allVotes, votes...)
if len(votes) < int(ec.paginationSize) {
break
}
}
return allVotes, nil
}
func (ec *committee) voteFilter(v *types.Vote) bool {
return ec.voteThreshold.Cmp(v.Amount()) > 0
}
func (ec *committee) candidateFilter(c *types.Candidate) bool {
return ec.selfStakingThreshold.Cmp(c.SelfStakingTokens()) > 0 ||
ec.scoreThreshold.Cmp(c.Score()) > 0
}
func (ec *committee) calculator(height uint64) (*types.ResultCalculator, error) {
mintTime, err := ec.carrier.BlockTimestamp(height)
switch errors.Cause(err) {
case nil:
break
case ethereum.NotFound:
return nil, db.ErrNotExist
default:
return nil, err
}
return types.NewResultCalculator(
mintTime,
ec.skipManifiedCandidate,
ec.voteFilter,
ec.calcWeightedVotes,
ec.candidateFilter,
), nil
}
func (ec *committee) fetchCandidatesByHeight(height uint64) ([]*types.Candidate, error) {
var allCandidates []*types.Candidate
previousIndex := big.NewInt(1)
for {
var candidates []*types.Candidate
var err error
if previousIndex, candidates, err = ec.carrier.Candidates(
height,
previousIndex,
ec.paginationSize,
); err != nil {
return nil, err
}
allCandidates = append(allCandidates, candidates...)
if len(candidates) < int(ec.paginationSize) {
break
}
}
return allCandidates, nil
}
func (ec *committee) FetchResultByHeight(height uint64) (*types.ElectionResult, error) {
if height == 0 {
var err error
height, err = ec.carrier.TipHeight()
if err != nil {
return nil, err
}
}
return ec.fetchResultByHeight(height)
}
func (ec *committee) fetchResultByHeight(height uint64) (*types.ElectionResult, error) {
zap.L().Info("fetch result from ethereum", zap.Uint64("height", height))
calculator, err := ec.calculator(height)
if err != nil {
return nil, err
}
candidates, err := ec.fetchCandidatesByHeight(height)
if err != nil {
return nil, err
}
if err := calculator.AddCandidates(candidates); err != nil {
return nil, err
}
votes, err := ec.fetchVotesByHeight(height)
if err != nil {
return nil, err
}
if err := calculator.AddVotes(votes); err != nil {
return nil, err
}
return calculator.Calculate()
}
func (ec *committee) dbKey(height uint64) []byte {
return util.Uint64ToBytes(height)
}
func (ec *committee) storeResult(height uint64, result *types.ElectionResult) error {
data, err := result.Serialize()
if err != nil {
return err
}
if err := ec.db.Put(ec.dbKey(height), data); err != nil {
return errors.Wrapf(err, "failed to put election result into db")
}
if err := ec.db.Put(db.NextHeightKey, ec.dbKey(height+ec.interval)); err != nil {
return err
}
ec.cache.insert(height, result)
return ec.heightManager.add(height, result.MintTime())
}
func (ec *committee) retryFetchResultByHeight(height uint64) (*types.ElectionResult, error) {
var result *types.ElectionResult
var err error
for i := uint8(0); i < ec.retryLimit; i++ {
if result, err = ec.fetchResultByHeight(height); err == nil {
return result, nil
}
zap.L().Error(
"failed to fetch result by height",
zap.Error(err),
zap.Uint64("height", height),
zap.Uint8("tried", i+1),
)
}
return result, err
}