This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
/
validators.go
281 lines (246 loc) · 6.03 KB
/
validators.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
package main
import (
"crypto/sha256"
"encoding/binary"
"math"
)
type Validator struct {
ID []byte
VotingPower int64
Number int
}
type Validators struct {
ValidatorSet []Validator
}
func (vals Validators) GetVotingPower() int64 {
sum := int64(0)
for _, val := range vals.ValidatorSet {
sum += val.VotingPower
}
return sum
}
func nextSeed(seed []byte) []byte {
h := sha256.New()
h.Write(seed)
seed = h.Sum(nil)
h = sha256.New()
h.Write(seed[len(seed)-3:])
seed = h.Sum(nil)
h = sha256.New()
h.Write(seed[len(seed)-3:])
seed = h.Sum(nil)
return seed
}
func luckyDraw(seed []byte, vals Validators, randomRange int64) (Validator, Validators) {
luckyNumber := int64(binary.BigEndian.Uint64(seed)) % randomRange
if luckyNumber <= 0 {
luckyNumber *= int64(-1)
}
sum := int64(0)
luckyIdx := 0
for idx := 0; sum < luckyNumber && idx < len(vals.ValidatorSet); idx++ {
sum += vals.ValidatorSet[idx].VotingPower
luckyIdx = idx
}
luckyVal := vals.ValidatorSet[luckyIdx]
if luckyIdx != 0 {
vals.ValidatorSet = append(vals.ValidatorSet[:luckyIdx], vals.ValidatorSet[luckyIdx+1:]...)
} else {
vals.ValidatorSet = vals.ValidatorSet[luckyIdx+1:]
}
return luckyVal, vals
}
func getRandomRange(vals Validators, round, amount, num, algo int) int64 {
randomRange := int64(0)
switch algo {
case 0:
blockSize := num / amount
t := (amount - round) * blockSize
if round == 0 {
t = len(vals.ValidatorSet)
}
for idx := 0; idx < t; idx++ {
randomRange += vals.ValidatorSet[idx].VotingPower
}
if randomRange == 0 {
randomRange = 1
}
case 1:
t := num / (1 << round)
for idx := 0; idx < t; idx++ {
randomRange += vals.ValidatorSet[idx].VotingPower
}
if randomRange == 0 {
randomRange = 1
}
case 2:
c := 5
n := len(vals.ValidatorSet)
m := float64(n-c) / float64(amount-1)
t := int(m*float64(amount-round-1) + float64(c))
// fmt.Println("->", t)
for idx := 0; idx < t; idx++ {
randomRange += vals.ValidatorSet[idx].VotingPower
}
if randomRange == 0 {
randomRange = 1
}
case 3:
c := amount - round + 1
// c := 2
n := len(vals.ValidatorSet)
x := float64(amount-round-1) / float64(amount-1) * math.Log(float64(n-c))
t := math.Exp(x) + float64(c-1)
if round == 0 {
t = float64(n)
}
if t > float64(n) {
t = float64(n)
}
// fmt.Println("->", t)
for idx := 0; idx < int(t); idx++ {
randomRange += vals.ValidatorSet[idx].VotingPower
}
if randomRange == 0 {
randomRange = 1
}
case 4:
c := amount - round + 1
// c := 2
n := len(vals.ValidatorSet)
x := float64(amount-round-1) / float64(amount-1) * math.Log(float64(n-c))
t := math.Exp(x) + float64(c-1)
if round == 0 {
t = float64(n)
}
if t > float64(n) {
t = float64(n)
}
// fmt.Println("->", t)
for idx := 0; idx < int(t); idx++ {
randomRange += vals.ValidatorSet[idx].VotingPower
}
if randomRange == 0 {
randomRange = 1
}
case 5:
// c := amount - round + 1
c := 2
n := len(vals.ValidatorSet)
x := float64(amount-round-1) / float64(amount-1) * math.Log(float64(n-c))
t := math.Exp(x) + float64(c-1)
if round == 0 {
t = float64(n)
}
if t > float64(n) {
t = float64(n)
}
// fmt.Println("->", t)
for idx := 0; idx < int(t); idx++ {
randomRange += vals.ValidatorSet[idx].VotingPower
}
if randomRange == 0 {
randomRange = 1
}
default:
panic("ERROR")
}
return randomRange
}
func getWorseCaseVp(vals Validators, amount, num, algo int) int64 {
sumVotingPower := int64(0)
switch algo {
case 0:
blockSize := len(vals.ValidatorSet) / amount
for round := 0; round < amount; round++ {
t := (round + 1) * blockSize
sumVotingPower += vals.ValidatorSet[t-1].VotingPower
}
case 1:
ct := 0
use := make(map[int]bool)
for round := 0; round < amount; round++ {
t := num / (1 << round)
if t-1 > 0 {
sumVotingPower += vals.ValidatorSet[t-1].VotingPower
ct++
use[t-1] = true
}
}
for idx := 0; ct < amount; idx++ {
if _, ok := use[idx]; !ok {
sumVotingPower += vals.ValidatorSet[idx].VotingPower
ct++
use[idx] = true
}
}
case 2:
for round := 0; round < amount; round++ {
c := 5
n := len(vals.ValidatorSet)
m := float64(n-c) / float64(amount-1)
t := int(m*float64(amount-round-1) + float64(c))
// fmt.Println("!!->", t, m)
if t > n {
t = n
}
sumVotingPower += vals.ValidatorSet[t-1].VotingPower
}
case 3:
for round := 0; round < amount; round++ {
c := amount - round + 1
// c := 2
n := len(vals.ValidatorSet)
x := float64(amount-round-1) / float64(amount-1) * math.Log(float64(n-c))
t := math.Exp(x) + float64(c-1)
if t > float64(n) {
t = float64(n)
}
// fmt.Println("->", t)
sumVotingPower += vals.ValidatorSet[int(t)-1].VotingPower
}
case 4:
for round := 0; round < amount; round++ {
c := amount - round + 1
// c := 2
n := len(vals.ValidatorSet)
x := float64(amount-round-1) / float64(amount-1) * math.Log(float64(n-c))
t := math.Exp(x) + float64(c-1)
if t > float64(n) {
t = float64(n)
}
// fmt.Println("->", t)
sumVotingPower += vals.ValidatorSet[int(t)-1].VotingPower
}
case 5:
for round := 0; round < amount; round++ {
// c := amount - round + 1
c := 2
n := len(vals.ValidatorSet)
x := float64(amount-round-1) / float64(amount-1) * math.Log(float64(n-c))
t := math.Exp(x) + float64(c-1)
if t > float64(n) {
t = float64(n)
}
// fmt.Println("->", t)
sumVotingPower += vals.ValidatorSet[int(t)-1].VotingPower
}
default:
panic("ERROR")
}
return sumVotingPower
}
func randomValidators(seed []byte, vals Validators, amount int, algo int) (Validators, int64) {
vals.GetVotingPower()
var luckyVal Validators
var val Validator
num := len(vals.ValidatorSet)
worseCase := getWorseCaseVp(vals, amount, num, algo)
for round := 0; round < amount; round++ {
seed = nextSeed(seed)
randomRange := getRandomRange(vals, round, amount, num, algo)
val, vals = luckyDraw(seed, vals, randomRange)
luckyVal.ValidatorSet = append(luckyVal.ValidatorSet, val)
}
return luckyVal, worseCase
}