-
Notifications
You must be signed in to change notification settings - Fork 189
/
default.go
391 lines (332 loc) · 11.5 KB
/
default.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
// Modifications Copyright 2018 The klaytn Authors
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from quorum/consensus/istanbul/validator/default.go (2018/06/04).
// Modified and improved for the klaytn development.
package validator
import (
"math"
"reflect"
"sort"
"sync"
"sync/atomic"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/consensus/istanbul"
"github.com/klaytn/klaytn/fork"
"github.com/klaytn/klaytn/params"
)
const (
defaultSubSetLength = 21
)
type defaultValidator struct {
address common.Address
}
func (val *defaultValidator) Address() common.Address {
return val.address
}
func (val *defaultValidator) String() string {
return val.Address().String()
}
func (val *defaultValidator) Equal(val2 *defaultValidator) bool {
return val.address == val2.address
}
func (val *defaultValidator) Hash() int64 {
return val.address.Hash().Big().Int64()
}
func (val *defaultValidator) RewardAddress() common.Address { return common.Address{} }
func (val *defaultValidator) VotingPower() uint64 { return 1000 }
func (val *defaultValidator) Weight() uint64 { return 0 }
type defaultSet struct {
subSize uint64
validators istanbul.Validators
policy istanbul.ProposerPolicy
proposer atomic.Value
validatorMu sync.RWMutex
selector istanbul.ProposalSelector
}
func newDefaultSet(addrs []common.Address, policy istanbul.ProposerPolicy) *defaultSet {
valSet := &defaultSet{}
valSet.subSize = defaultSubSetLength
valSet.policy = policy
// init validators
valSet.validators = make([]istanbul.Validator, len(addrs))
for i, addr := range addrs {
valSet.validators[i] = New(addr)
}
// sort validator
sort.Sort(valSet.validators)
// init proposer
if valSet.Size() > 0 {
valSet.proposer.Store(valSet.GetByIndex(0))
}
valSet.selector = roundRobinProposer
if policy == istanbul.Sticky {
valSet.selector = stickyProposer
}
return valSet
}
func newDefaultSubSet(addrs []common.Address, policy istanbul.ProposerPolicy, subSize uint64) *defaultSet {
valSet := &defaultSet{}
valSet.subSize = subSize
valSet.policy = policy
// init validators
valSet.validators = make([]istanbul.Validator, len(addrs))
for i, addr := range addrs {
valSet.validators[i] = New(addr)
}
// sort validator
sort.Sort(valSet.validators)
// init proposer
if valSet.Size() > 0 {
valSet.proposer.Store(valSet.GetByIndex(0))
}
valSet.selector = roundRobinProposer
if policy == istanbul.Sticky {
valSet.selector = stickyProposer
}
return valSet
}
func (valSet *defaultSet) Size() uint64 {
valSet.validatorMu.RLock()
defer valSet.validatorMu.RUnlock()
return uint64(len(valSet.validators))
}
func (valSet *defaultSet) SubGroupSize() uint64 {
return valSet.subSize
}
// SetSubGroupSize sets committee size of the valSet.
func (valSet *defaultSet) SetSubGroupSize(size uint64) {
if size == 0 {
logger.Error("cannot assign committee size to 0")
return
}
valSet.subSize = size
}
func (valSet *defaultSet) List() []istanbul.Validator {
valSet.validatorMu.RLock()
defer valSet.validatorMu.RUnlock()
return valSet.validators
}
func (valSet *defaultSet) DemotedList() []istanbul.Validator {
return nil
}
// SubList composes a committee after setting a proposer with a default value.
// This functions returns whole validators if it failed to compose a committee.
func (valSet *defaultSet) SubList(prevHash common.Hash, view *istanbul.View) []istanbul.Validator {
// TODO-Klaytn-Istanbul: investigate whether `valSet.GetProposer().Address()` is a proper value or the proposer should be calculated based on `view`
proposer := valSet.GetProposer()
if proposer == nil {
return valSet.List()
}
return valSet.SubListWithProposer(prevHash, proposer.Address(), view)
}
// SubListWithProposer composes a committee with given parameters.
// The first member of the committee is set to the given proposer without calculating proposer with the given `view`.
// The second member of the committee is calculated with a round number of the given view and `valSet.blockNum`.
// The reset of the committee is selected with a random seed derived from `prevHash`.
// This functions returns whole validators if it failed to compose a committee.
func (valSet *defaultSet) SubListWithProposer(prevHash common.Hash, proposerAddr common.Address, view *istanbul.View) []istanbul.Validator {
valSet.validatorMu.RLock()
defer valSet.validatorMu.RUnlock()
validators := valSet.validators
validatorSize := uint64(len(validators))
committeeSize := valSet.subSize
// return early if the committee size is equal or larger than the validator size
if committeeSize >= validatorSize {
return validators
}
// find the proposer
proposerIdx, proposer := valSet.GetByAddress(proposerAddr)
if proposerIdx < 0 {
logger.Error("invalid index of the proposer",
"addr", proposerAddr.String(), "index", proposerIdx)
return validators
}
// return early if the committee size is 1
if committeeSize == 1 {
return []istanbul.Validator{proposer}
}
// find the next proposer
nextProposer := valSet.selector(valSet, proposer.Address(), view.Round.Uint64())
nextProposerIdx, _ := valSet.GetByAddress(nextProposer.Address())
if nextProposerIdx < 0 {
logger.Error("invalid index of the next proposer",
"addr", nextProposer.Address().String(), "index", nextProposerIdx)
return validators
}
// seed will be used to select a random committee
seed, err := ConvertHashToSeed(prevHash)
if fork.Rules(view.Sequence).IsIstanbul {
seed += view.Round.Int64()
}
if err != nil {
logger.Error("failed to convert hash to seed", "prevHash", prevHash, "err", err)
return validators
}
// select a random committee
committee := SelectRandomCommittee(validators, committeeSize, seed, proposerIdx, nextProposerIdx)
if committee == nil {
committee = validators
}
logger.Trace("composed committee", "prevHash", prevHash.Hex(), "proposerAddr", proposerAddr,
"committee", committee, "committee size", len(committee), "valSet.subSize", committeeSize)
return committee
}
func (valSet *defaultSet) CheckInSubList(prevHash common.Hash, view *istanbul.View, addr common.Address) bool {
for _, val := range valSet.SubList(prevHash, view) {
if val.Address() == addr {
return true
}
}
return false
}
func (valSet *defaultSet) IsSubSet() bool {
return valSet.Size() > valSet.subSize
}
func (valSet *defaultSet) GetByIndex(i uint64) istanbul.Validator {
valSet.validatorMu.RLock()
defer valSet.validatorMu.RUnlock()
if i < uint64(len(valSet.validators)) {
return valSet.validators[i]
}
return nil
}
func (valSet *defaultSet) GetByAddress(addr common.Address) (int, istanbul.Validator) {
for i, val := range valSet.List() {
if addr == val.Address() {
return i, val
}
}
// TODO-Klaytn-Istanbul: Enable this log when non-committee nodes don't call `core.startNewRound()`
// logger.Warn("failed to find an address in the validator list",
// "address", addr, "validatorAddrs", valSet.validators.AddressStringList())
return -1, nil
}
func (valSet *defaultSet) GetDemotedByAddress(addr common.Address) (int, istanbul.Validator) {
return -1, nil
}
func (valSet *defaultSet) GetProposer() istanbul.Validator {
proposer := valSet.proposer.Load()
if proposer == nil {
logger.Error("Proposer is nil", "validators", valSet.validators)
return nil
}
return proposer.(istanbul.Validator)
}
func (valSet *defaultSet) IsProposer(address common.Address) bool {
_, val := valSet.GetByAddress(address)
return reflect.DeepEqual(valSet.GetProposer(), val)
}
func (valSet *defaultSet) CalcProposer(lastProposer common.Address, round uint64) {
valSet.validatorMu.RLock()
defer valSet.validatorMu.RUnlock()
if len(valSet.validators) == 0 {
logger.Error("len of validators is 0, Proposer is nil", "validators", valSet.validators)
return
}
valSet.proposer.Store(valSet.selector(valSet, lastProposer, round))
}
func calcSeed(valSet istanbul.ValidatorSet, proposer common.Address, round uint64) uint64 {
offset := 0
if idx, val := valSet.GetByAddress(proposer); val != nil {
offset = idx
}
return uint64(offset) + round
}
func emptyAddress(addr common.Address) bool {
return addr == common.Address{}
}
func roundRobinProposer(valSet istanbul.ValidatorSet, proposer common.Address, round uint64) istanbul.Validator {
seed := uint64(0)
if emptyAddress(proposer) {
seed = round
} else {
seed = calcSeed(valSet, proposer, round) + 1
}
pick := seed % uint64(valSet.Size())
return valSet.GetByIndex(pick)
}
func stickyProposer(valSet istanbul.ValidatorSet, proposer common.Address, round uint64) istanbul.Validator {
seed := uint64(0)
if emptyAddress(proposer) {
seed = round
} else {
seed = calcSeed(valSet, proposer, round)
}
pick := seed % uint64(valSet.Size())
return valSet.GetByIndex(pick)
}
func (valSet *defaultSet) AddValidator(address common.Address) bool {
valSet.validatorMu.Lock()
defer valSet.validatorMu.Unlock()
for _, v := range valSet.validators {
if v.Address() == address {
return false
}
}
valSet.validators = append(valSet.validators, New(address))
// TODO: we may not need to re-sort it again
// sort validator
sort.Sort(valSet.validators)
return true
}
func (valSet *defaultSet) RemoveValidator(address common.Address) bool {
valSet.validatorMu.Lock()
defer valSet.validatorMu.Unlock()
for i, v := range valSet.validators {
if v.Address() == address {
valSet.validators = append(valSet.validators[:i], valSet.validators[i+1:]...)
return true
}
}
return false
}
func (valSet *defaultSet) Copy() istanbul.ValidatorSet {
valSet.validatorMu.RLock()
defer valSet.validatorMu.RUnlock()
addresses := make([]common.Address, 0, len(valSet.validators))
for _, v := range valSet.validators {
addresses = append(addresses, v.Address())
}
newValSet := NewSubSet(addresses, valSet.policy, valSet.subSize).(*defaultSet)
_, proposer := newValSet.GetByAddress(valSet.GetProposer().Address())
newValSet.proposer.Store(proposer)
return newValSet
}
func (valSet *defaultSet) F() int {
if valSet.Size() > valSet.subSize {
return int(math.Ceil(float64(valSet.subSize)/3)) - 1
} else {
return int(math.Ceil(float64(valSet.Size())/3)) - 1
}
}
func (valSet *defaultSet) Policy() istanbul.ProposerPolicy { return valSet.policy }
func (valSet *defaultSet) Refresh(hash common.Hash, blockNum uint64, config *params.ChainConfig, isSingle bool, governingNode common.Address, minStaking uint64) error {
return nil
}
func (valSet *defaultSet) SetBlockNum(blockNum uint64) { /* Do nothing */ }
func (valSet *defaultSet) Proposers() []istanbul.Validator { return nil }
func (valSet *defaultSet) TotalVotingPower() uint64 {
sum := uint64(0)
for _, v := range valSet.List() {
sum += v.VotingPower()
}
return sum
}
func (valSet *defaultSet) Selector(valS istanbul.ValidatorSet, lastProposer common.Address, round uint64) istanbul.Validator {
return valSet.selector(valS, lastProposer, round)
}