-
Notifications
You must be signed in to change notification settings - Fork 39
/
prep.go
453 lines (392 loc) · 10.1 KB
/
prep.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
package icstate
import (
"bytes"
"math/big"
"sort"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/icon/icmodule"
"github.com/icon-project/goloop/icon/iiss/icutils"
"github.com/icon-project/goloop/module"
)
type PRep struct {
owner module.Address
state *State
pb *PRepBaseState
*PRepStatusState
}
func (p *PRep) Owner() module.Address {
return p.owner
}
func (p *PRep) IRep() *big.Int {
pb := p.getPRepBaseState()
if pb == nil {
return new(big.Int)
}
return pb.IRep()
}
func (p *PRep) NodeAddress() module.Address {
pb := p.getPRepBaseState()
if pb == nil {
return nil
}
return pb.GetNode(p.owner)
}
func (p *PRep) ToJSON(blockHeight int64, bondRequirement int64) map[string]interface{} {
pb := p.getPRepBaseState()
jso := icutils.MergeMaps(pb.ToJSON(p.owner), p.PRepStatusState.ToJSON(blockHeight, bondRequirement))
jso["address"] = p.owner
return jso
}
func (p *PRep) init() error {
ps := p.state.GetPRepStatusByOwner(p.owner, false)
if ps == nil {
return errors.Errorf("PRepStatus not found: %s", p.owner)
}
p.PRepStatusState = ps
return nil
}
func (p *PRep) getPRepBaseState() *PRepBaseState {
if p.pb == nil {
p.pb = p.state.GetPRepBaseByOwner(p.owner, false)
}
return p.pb
}
func (p *PRep) Info() *PRepInfo {
pb := p.getPRepBaseState()
if pb == nil {
return nil
}
return pb.info()
}
func NewPRep(owner module.Address, state *State) *PRep {
prep := &PRep{owner: owner, state: state}
if err := prep.init(); err != nil {
return nil
}
return prep
}
// ===============================================================
type PRepSet interface {
OnTermEnd(revision, mainPRepCount, subPRepCount, extraMainPRepCount, limit int, br int64) error
GetPRepSize(grade Grade) int
GetElectedPRepSize() int
Size() int
TotalBonded() *big.Int
TotalDelegated() *big.Int
GetTotalPower(br int64) *big.Int
GetByIndex(i int) PRepSetEntry
ToPRepSnapshots(electedPRepCount int, br int64) PRepSnapshots
Sort(mainPRepCount, subPRepCount, extraMainPRepCount int, br int64, revision int)
SortByGrade(br int64)
}
type PRepSetEntry interface {
PRep() *PRep
Status() Status
Grade() Grade
Power(br int64) *big.Int
Delegated() *big.Int
Bonded() *big.Int
Owner() module.Address
HasPubKey() bool
}
type prepSetEntry struct {
prep *PRep
pubKey bool
}
func (p *prepSetEntry) PRep() *PRep {
return p.prep
}
func (p *prepSetEntry) Status() Status {
return p.prep.Status()
}
func (p *prepSetEntry) Grade() Grade {
return p.prep.Grade()
}
func (p *prepSetEntry) Power(br int64) *big.Int {
return p.prep.GetPower(br)
}
func (p *prepSetEntry) Delegated() *big.Int {
return p.prep.Delegated()
}
func (p *prepSetEntry) Bonded() *big.Int {
return p.prep.Bonded()
}
func (p *prepSetEntry) Owner() module.Address {
return p.prep.Owner()
}
func (p *prepSetEntry) HasPubKey() bool {
return p.pubKey
}
func NewPRepSetEntry(prep *PRep, pubKey bool) *prepSetEntry {
return &prepSetEntry{
prep: prep,
pubKey: pubKey,
}
}
type prepSetImpl struct {
totalBonded *big.Int
totalDelegated *big.Int // total delegated amount of all active P-Reps
mainPReps int
subPReps int
entries []PRepSetEntry
}
// OnTermEnd initializes all prep status including grade on term end
func (p *prepSetImpl) OnTermEnd(revision, mainPRepCount, subPRepCount, extraMainPRepCount, limit int, br int64) error {
mainPReps := 0
subPReps := 0
electedPRepCount := mainPRepCount + subPRepCount
var newGrade Grade
for i, entry := range p.entries {
if revision >= icmodule.RevisionBTP2 &&
(entry.Power(br).Sign() == 0 || entry.HasPubKey() == false) {
newGrade = GradeCandidate
} else if i < mainPRepCount {
newGrade = GradeMain
mainPReps++
} else if i < mainPRepCount+extraMainPRepCount && entry.Power(br).Sign() > 0 {
// Prevent a prep with 0 power from being an extra main prep
newGrade = GradeMain
mainPReps++
} else if i < electedPRepCount {
newGrade = GradeSub
subPReps++
} else {
newGrade = GradeCandidate
}
prep := entry.PRep()
if err := prep.OnTermEnd(newGrade, limit); err != nil {
return err
}
if revision == icmodule.RevisionResetPenaltyMask {
prep.ResetVPenaltyMask()
}
}
p.mainPReps = mainPReps
p.subPReps = subPReps
return nil
}
func (p *prepSetImpl) GetPRepSize(grade Grade) int {
switch grade {
case GradeMain:
return p.mainPReps
case GradeSub:
return p.subPReps
case GradeCandidate:
return p.Size() - p.mainPReps - p.subPReps
default:
panic(errors.Errorf("Invalid grade: %d", grade))
}
}
func (p *prepSetImpl) GetElectedPRepSize() int {
return p.mainPReps + p.subPReps
}
func (p *prepSetImpl) Size() int {
return len(p.entries)
}
func (p *prepSetImpl) TotalBonded() *big.Int {
return p.totalBonded
}
func (p *prepSetImpl) TotalDelegated() *big.Int {
return p.totalDelegated
}
func (p *prepSetImpl) GetTotalPower(br int64) *big.Int {
totalPower := new(big.Int)
for _, entry := range p.entries {
totalPower.Add(totalPower, entry.Power(br))
}
return totalPower
}
func (p *prepSetImpl) GetByIndex(i int) PRepSetEntry {
if i < 0 || i >= len(p.entries) {
return nil
}
return p.entries[i]
}
func (p *prepSetImpl) ToPRepSnapshots(electedPRepCount int, br int64) PRepSnapshots {
size := icutils.Min(len(p.entries), electedPRepCount)
if size == 0 {
return nil
}
ret := make(PRepSnapshots, size)
for i := 0; i < size; i++ {
entry := p.entries[i]
ret[i] = NewPRepSnapshot(entry.Owner(), entry.Power(br))
}
return ret
}
func (p *prepSetImpl) Sort(mainPRepCount, subPRepCount, extraMainPRepCount int, br int64, rev int) {
if rev < icmodule.RevisionExtraMainPReps {
p.sort(br, nil)
} else {
if rev < icmodule.RevisionBTP2 {
p.sort(br, nil)
p.sortForExtraMainPRep(mainPRepCount, subPRepCount, extraMainPRepCount, br)
} else {
p.sort(br, cmpPubKey)
var electable int
p.visitAll(func(idx int, e1 PRepSetEntry) bool {
if e1.Power(br).Sign() > 0 && e1.HasPubKey() {
electable += 1
return true
} else {
return false
}
})
if electable > mainPRepCount {
if electable < mainPRepCount+subPRepCount {
subPRepCount = electable - mainPRepCount
}
p.sortForExtraMainPRep(mainPRepCount, subPRepCount, extraMainPRepCount, br)
}
}
}
}
func (p *prepSetImpl) SortByGrade(br int64) {
p.sort(br, cmpGrade)
}
func (p *prepSetImpl) sort(br int64, cmp func(i, j PRepSetEntry) int) {
sort.Slice(p.entries, func(i, j int) bool {
p0, p1 := p.entries[i], p.entries[j]
return lessByPower(p0, p1, br, cmp)
})
}
func cmpPubKey(e0, e1 PRepSetEntry) int {
if e0.HasPubKey() != e1.HasPubKey() {
if e0.HasPubKey() {
return 1
}
return -1
}
return 0
}
func cmpGrade(e0, e1 PRepSetEntry) int {
return e0.Grade().Cmp(e1.Grade())
}
func lessByPower(e0, e1 PRepSetEntry, br int64, cmp func(i, j PRepSetEntry) int) bool {
if cmp != nil {
ret := cmp(e0, e1)
if ret > 0 {
return true
} else if ret < 0 {
return false
}
}
ret := e0.Power(br).Cmp(e1.Power(br))
if ret > 0 {
return true
} else if ret < 0 {
return false
}
ret = e0.Delegated().Cmp(e1.Delegated())
if ret > 0 {
return true
} else if ret < 0 {
return false
}
return bytes.Compare(e0.Owner().Bytes(), e1.Owner().Bytes()) > 0
}
func (p *prepSetImpl) sortForExtraMainPRep(
mainPRepCount, subPRepCount, extraMainPRepCount int, br int64) {
// All counts are configuration values; Default: 22, 78, 3
size := len(p.entries)
if size <= mainPRepCount || extraMainPRepCount == 0 {
// Not enough number of active preps to be extra main preps
return
}
electedPRepCount := mainPRepCount + subPRepCount
if electedPRepCount > size {
electedPRepCount = size
}
// extraMainPRepCount MUST be larger than zero
subPRepCount = size - mainPRepCount
if subPRepCount < extraMainPRepCount {
extraMainPRepCount = subPRepCount
}
// Copy sub preps from entries to subPReps
subPRepEntries := p.entries[mainPRepCount:electedPRepCount]
dupSubPRepEntries := make([]PRepSetEntry, len(subPRepEntries))
copy(dupSubPRepEntries, subPRepEntries)
// sort subPReps by LRU logic
sortByLRU(subPRepEntries, br)
// Add extra main preps to map
i := 0
extraMainPReps := make(map[string]bool)
for _, entry := range subPRepEntries {
if entry.Power(br).Sign() > 0 {
// Prevent the prep whose power is 0 from being an extra main prep
extraMainPReps[icutils.ToKey(entry.Owner())] = true
subPRepEntries[i] = entry
i++
if i == extraMainPRepCount {
// All extra main preps are selected
break
}
}
}
// Append remaining sub preps excluding extra main preps
for _, entry := range dupSubPRepEntries {
// If prep is not an extra main prep
if _, ok := extraMainPReps[icutils.ToKey(entry.Owner())]; !ok {
subPRepEntries[i] = entry
i++
}
}
}
func (p *prepSetImpl) visitAll(visit func(idx int, e1 PRepSetEntry) bool) {
for i, e := range p.entries {
if ok := visit(i, e); !ok {
return
}
}
}
func sortByLRU(prepSet []PRepSetEntry, br int64) {
sort.Slice(prepSet, func(i, j int) bool {
return lessByLRU(prepSet[i].PRep(), prepSet[j].PRep(), br)
})
}
func lessByLRU(p0, p1 *PRep, br int64) bool {
// Sort by lastState
if p0.LastState() == None {
if p1.LastState() != None {
return true
}
} else {
if p1.LastState() == None {
return false
}
}
// p0 and p1 have the same last states at this moment
// Sort by lastHeight
if p0.LastState() == None && p0.LastHeight() != p1.LastHeight() {
return p0.LastHeight() < p1.LastHeight()
}
// Sort by power
cmp := p0.GetPower(br).Cmp(p1.GetPower(br))
if cmp == 0 {
// Sort by address
return bytes.Compare(p0.Owner().Bytes(), p1.Owner().Bytes()) > 0
}
return cmp > 0
}
func NewPRepSet(prepList []PRepSetEntry) PRepSet {
prepSet := &prepSetImpl{
totalDelegated: new(big.Int),
totalBonded: new(big.Int),
entries: prepList,
}
for _, entry := range prepList {
prepSet.totalBonded.Add(prepSet.totalBonded, entry.Bonded())
prepSet.totalDelegated.Add(prepSet.totalDelegated, entry.Delegated())
switch entry.Grade() {
case GradeMain:
prepSet.mainPReps += 1
case GradeSub:
prepSet.subPReps += 1
case GradeCandidate:
// Nothing to do
default:
panic(errors.Errorf("Invalid grade: %d", entry.Grade()))
}
}
return prepSet
}