-
Notifications
You must be signed in to change notification settings - Fork 40
/
prep.go
344 lines (292 loc) · 7.81 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
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) 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, limit int) error
GetPRepSize(grade Grade) int
Size() int
TotalBonded() *big.Int
TotalDelegated() *big.Int
GetTotalBondedDelegation(br int64) *big.Int
GetPRepByIndex(i int) *PRep
ToPRepSnapshots(electedPRepCount int, br int64) PRepSnapshots
}
type prepsBase struct {
totalBonded *big.Int
totalDelegated *big.Int // total delegated amount of all active P-Reps
mainPReps int
subPReps int
orderedPReps []*PRep
}
// OnTermEnd initializes all prep status including grade on term end
func (p *prepsBase) OnTermEnd(revision, mainPRepCount, subPRepCount, limit int) error {
mainPReps := 0
subPReps := 0
electedPRepCount := mainPRepCount + subPRepCount
var newGrade Grade
for i, prep := range p.orderedPReps {
if i < mainPRepCount {
newGrade = GradeMain
mainPReps++
} else if i < electedPRepCount {
newGrade = GradeSub
subPReps++
} else {
newGrade = GradeCandidate
}
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 *prepsBase) 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 *prepsBase) Size() int {
return len(p.orderedPReps)
}
func (p *prepsBase) TotalBonded() *big.Int {
return p.totalBonded
}
func (p *prepsBase) TotalDelegated() *big.Int {
return p.totalDelegated
}
func (p *prepsBase) GetTotalBondedDelegation(br int64) *big.Int {
tbd := new(big.Int)
for _, prep := range p.orderedPReps {
tbd.Add(tbd, prep.GetBondedDelegation(br))
}
return tbd
}
func (p *prepsBase) GetPRepByIndex(i int) *PRep {
if i < 0 || i >= len(p.orderedPReps) {
return nil
}
return p.orderedPReps[i]
}
func (p *prepsBase) ToPRepSnapshots(electedPRepCount int, br int64) PRepSnapshots {
size := icutils.Min(len(p.orderedPReps), electedPRepCount)
if size == 0 {
return nil
}
ret := make(PRepSnapshots, size)
for i := 0; i < size; i++ {
prep := p.orderedPReps[i]
ret[i] = NewPRepSnapshot(prep.Owner(), prep.GetBondedDelegation(br))
}
return ret
}
func (p *prepsBase) appendPRep(prep *PRep) {
if prep.PRepStatusState.Status() == Active {
p.orderedPReps = append(p.orderedPReps, prep)
p.totalBonded.Add(p.totalBonded, prep.Bonded())
p.totalDelegated.Add(p.totalDelegated, prep.Delegated())
p.adjustPRepSize(prep.Grade(), true)
}
}
func (p *prepsBase) adjustPRepSize(grade Grade, increment bool) {
delta := 1
if !increment {
delta = -1
}
switch grade {
case GradeMain:
p.mainPReps += delta
case GradeSub:
p.subPReps += delta
case GradeCandidate:
// Nothing to do
default:
panic(errors.Errorf("Invalid grade: %d", grade))
}
}
func (p *prepsBase) sortByBondedDelegation(br int64) {
sort.Slice(p.orderedPReps, func(i, j int) bool {
p0, p1 := p.orderedPReps[i], p.orderedPReps[j]
return lessByBondedDelegation(p0, p1, br)
})
}
func lessByBondedDelegation(p0, p1 *PRep, br int64) bool {
ret := p0.GetBondedDelegation(br).Cmp(p1.GetBondedDelegation(br))
if ret > 0 {
return true
} else if ret < 0 {
return false
}
ret = p0.Delegated().Cmp(p1.Delegated())
if ret > 0 {
return true
} else if ret < 0 {
return false
}
return bytes.Compare(p0.Owner().Bytes(), p1.Owner().Bytes()) > 0
}
// ======================================================================
func NewPRepsOrderedByBondedDelegation(prepList []*PRep, br int64) PRepSet {
preps := &prepsBase{
totalDelegated: new(big.Int),
totalBonded: new(big.Int),
}
for _, prep := range prepList {
preps.appendPRep(prep)
}
preps.sortByBondedDelegation(br)
return preps
}
// ================================================================
type prepsIncludingExtraMainPRep struct {
prepsBase
}
func (p *prepsIncludingExtraMainPRep) sort(
mainPRepCount, extraMainPRepCount, electedPRepCount int, br int64) {
p.sortByBondedDelegation(br)
p.sortForExtraMainPRep(mainPRepCount, extraMainPRepCount, electedPRepCount, br)
}
func (p *prepsIncludingExtraMainPRep) sortForExtraMainPRep(
mainPRepCount, extraMainPRepCount, electedPRepCount int, br int64) {
// No need to consider extra main preps
pureMainPRepCount := mainPRepCount - extraMainPRepCount
size := len(p.orderedPReps)
if size <= pureMainPRepCount || extraMainPRepCount == 0 {
return
}
// Copy the rest of preps excluding pure main preps to dubRestPReps slice
restPReps := p.orderedPReps[pureMainPRepCount:electedPRepCount]
dubRestPReps := make([]*PRep, len(restPReps))
copy(dubRestPReps, restPReps)
// Sort restPReps by LRU logic
sortByLRU(restPReps, br)
// Add extra main preps to map
extraMainPReps := make(map[string]*PRep)
for i := 0; i < extraMainPRepCount; i++ {
prep := restPReps[i]
extraMainPReps[icutils.ToKey(prep.Owner())] = prep
}
// Append sub preps
i := extraMainPRepCount
for _, prep := range dubRestPReps {
// If prep is not a extra main prep
if _, ok := extraMainPReps[icutils.ToKey(prep.Owner())]; !ok {
restPReps[i] = prep
i++
}
}
}
func sortByLRU(preps []*PRep, br int64) {
sort.Slice(preps, func(i, j int) bool {
return lessByLRU(preps[i], preps[j], 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 bondedDelegation
cmp := p0.GetBondedDelegation(br).Cmp(p1.GetBondedDelegation(br))
if cmp > 0 {
return true
} else if cmp < 0 {
return false
}
// Sort by address
return bytes.Compare(p0.Owner().Bytes(), p1.Owner().Bytes()) > 0
}
func NewPRepsIncludingExtraMainPRep(
prepList []*PRep, mainPRepCount, extraMainPRepCount, electedPRepCount int, br int64) PRepSet {
preps := &prepsIncludingExtraMainPRep{
prepsBase: prepsBase{
totalDelegated: new(big.Int),
totalBonded: new(big.Int),
},
}
for _, prep := range prepList {
preps.appendPRep(prep)
}
preps.sort(mainPRepCount, extraMainPRepCount, electedPRepCount, br)
return preps
}