-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
306 lines (261 loc) · 7.01 KB
/
validator.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
/*
Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package tbft
import (
"errors"
"fmt"
"sort"
"sync"
"chainmaker.org/chainmaker/protocol/v2"
)
var (
// ErrInvalidIndex implements the error for invalid index of validators
ErrInvalidIndex = errors.New("invalid index")
)
//
// validatorSet
// @Description: validator set
//
type validatorSet struct {
sync.Mutex
logger protocol.Logger
Validators []string
// Validator's current block height
validatorsHeight map[string]uint64
// Validator's beat Time
validatorsBeatTime map[string]int64
// The number of consecutive proposals by the proposer
blocksPerProposer uint64
}
//
// newValidatorSet
// @Description: create a new validator set
// @param logger
// @param validators
// @param blocksPerProposer
// @return *validatorSet
//
func newValidatorSet(logger protocol.Logger, validators []string, blocksPerProposer uint64) *validatorSet {
sort.SliceStable(validators, func(i, j int) bool {
return validators[i] < validators[j]
})
valSet := &validatorSet{
logger: logger,
Validators: validators,
validatorsHeight: make(map[string]uint64),
validatorsBeatTime: make(map[string]int64),
blocksPerProposer: blocksPerProposer,
}
valSet.logger.Infof("new validator set: %v", validators)
return valSet
}
//
// isNilOrEmpty
// @Description: when the validatorSet is nil or empty return true
// @receiver valSet
// @return bool
//
func (valSet *validatorSet) isNilOrEmpty() bool {
if valSet == nil {
return true
}
valSet.Lock()
defer valSet.Unlock()
return len(valSet.Validators) == 0
}
//
// String
// @Description: convert *validatorSet to string
// @receiver valSet
// @return string
//
func (valSet *validatorSet) String() string {
if valSet == nil {
return ""
}
valSet.Lock()
defer valSet.Unlock()
return fmt.Sprintf("%v", valSet.Validators)
}
//
// Size
// @Description: get validatorSet size
// @receiver valSet
// @return int32
//
func (valSet *validatorSet) Size() int32 {
if valSet == nil {
return 0
}
valSet.Lock()
defer valSet.Unlock()
return int32(len(valSet.Validators))
}
// HasValidator holds the lock and return whether validator is in
// the validatorSet
func (valSet *validatorSet) HasValidator(validator string) bool {
if valSet == nil {
return false
}
valSet.Lock()
defer valSet.Unlock()
return valSet.hasValidator(validator)
}
func (valSet *validatorSet) hasValidator(validator string) bool {
for _, val := range valSet.Validators {
if val == validator {
return true
}
}
return false
}
// GetProposerV230 calculate proposer based on height and round in versions earlier than v2.3.1
// we used this function in versions prior to v2.3.1 for version compatibility and node upgrades
func (valSet *validatorSet) GetProposerV230(height uint64, round int32) (validator string, err error) {
if valSet.isNilOrEmpty() {
return "", ErrInvalidIndex
}
heightOffset := int32((height + 1) / valSet.blocksPerProposer)
roundOffset := round % valSet.Size()
proposerIndex := (heightOffset + roundOffset) % valSet.Size()
return valSet.getByIndex(proposerIndex)
}
//
// GetProposer
// @Description:Calculate proposer based on height and round
// @receiver valSet
// @param height
// @param round
// @return validator
// @return err
//
func (valSet *validatorSet) GetProposer(blockVersion uint32, preProposer string,
height uint64, round int32) (validator string, err error) {
if blockVersion < blockVersion231 {
return valSet.GetProposerV230(height, round)
}
if valSet.isNilOrEmpty() {
return "", ErrInvalidIndex
}
proposerOffset := valSet.getIndexByString(preProposer)
if (height % valSet.blocksPerProposer) == 0 {
proposerOffset++
}
roundOffset := round % valSet.Size()
proposerIndex := (roundOffset + proposerOffset) % valSet.Size()
return valSet.getByIndex(proposerIndex)
}
//
// updateValidators
// @Description: Update the collection based on the input and return an array of additions and deletions
// @receiver valSet
// @param validators
// @return addedValidators
// @return removedValidators
// @return err
//
func (valSet *validatorSet) updateValidators(validators []string) (addedValidators []string, removedValidators []string,
err error) {
valSet.Lock()
defer valSet.Unlock()
removedValidatorsMap := make(map[string]bool)
for _, v := range valSet.Validators {
removedValidatorsMap[v] = true
}
for _, v := range validators {
// addedValidators
if !valSet.hasValidator(v) {
addedValidators = append(addedValidators, v)
}
delete(removedValidatorsMap, v)
}
// removedValidators
for k := range removedValidatorsMap {
removedValidators = append(removedValidators, k)
}
sort.SliceStable(validators, func(i, j int) bool {
return validators[i] < validators[j]
})
valSet.Validators = validators
sort.SliceStable(addedValidators, func(i, j int) bool {
return addedValidators[i] < addedValidators[j]
})
sort.SliceStable(removedValidators, func(i, j int) bool {
return removedValidators[i] < removedValidators[j]
})
valSet.logger.Infof("%v update validators, validators: %v, addedValidators: %v, removedValidators: %v",
valSet.Validators, validators, addedValidators, removedValidators)
return
}
//
// updateBlocksPerProposer
// @Description: Update the number of consecutive blocks produced by the proposer
// @receiver valSet
// @param blocks
// @return error
//
func (valSet *validatorSet) updateBlocksPerProposer(blocks uint64) error {
valSet.Lock()
defer valSet.Unlock()
valSet.blocksPerProposer = blocks
return nil
}
//
// getByIndex
// @Description: Get proposer by index
// @receiver valSet
// @param index
// @return validator
// @return err
//
func (valSet *validatorSet) getByIndex(index int32) (validator string, err error) {
if index < 0 || index >= valSet.Size() {
return "", ErrInvalidIndex
}
valSet.Lock()
defer valSet.Unlock()
val := valSet.Validators[index]
return val, nil
}
// getIndexByString
// @Description: Get index of validator
// @receiver valSet
// @param preProposer
// @return index
// @return int32
//
func (valSet *validatorSet) getIndexByString(preProposer string) int32 {
for i, validator := range valSet.Validators {
if validator == preProposer {
return int32(i)
}
}
return 0
}
// checkProposed checks whether the local node can generate the proposal.
// if there are f+1 remote nodes whose height is greater than that of the
// local node, the local node does not need to generate the proposal.
// return true indicates that a proposal can be generated.
func (valSet *validatorSet) checkProposed(height uint64) bool {
if valSet == nil {
return false
}
valSet.Lock()
defer valSet.Unlock()
quorum := 0
for _, v := range valSet.Validators {
validatorHeight := valSet.validatorsHeight[v]
if validatorHeight > height {
quorum++
}
// f+1 remote nodes are higher than the local nodes
if quorum >= len(valSet.Validators)/3+1 {
valSet.logger.Infof("The status of the local node is backward "+
"and no proposal is required, local height = %d", height)
return false
}
}
return true
}