-
Notifications
You must be signed in to change notification settings - Fork 672
/
cache_current_staker_chain_state.go
458 lines (389 loc) · 12.6 KB
/
cache_current_staker_chain_state.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
454
455
456
457
458
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"bytes"
"errors"
"fmt"
"sort"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
safemath "github.com/ava-labs/avalanchego/utils/math"
)
var (
errNotEnoughValidators = errors.New("not enough validators")
_ currentStakerChainState = ¤tStakerChainStateImpl{}
)
type currentStakerChainState interface {
// The NextStaker value returns the next staker that is going to be removed
// using a RewardValidatorTx. Therefore, only AddValidatorTxs and
// AddDelegatorTxs will be returned. AddSubnetValidatorTxs are removed using
// AdvanceTimestampTxs.
GetNextStaker() (addStakerTx *Tx, potentialReward uint64, err error)
GetStaker(txID ids.ID) (tx *Tx, potentialReward uint64, err error)
GetValidator(nodeID ids.ShortID) (currentValidator, error)
UpdateStakers(
addValidators []*validatorReward,
addDelegators []*validatorReward,
addSubnetValidators []*Tx,
numTxsToRemove int,
) (currentStakerChainState, error)
DeleteNextStaker() (currentStakerChainState, error)
// Stakers returns the current stakers on the network sorted in order of the
// order of their future removal from the validator set.
Stakers() []*Tx
Apply(InternalState)
// Return the current validator set of [subnetID].
ValidatorSet(subnetID ids.ID) (validators.Set, error)
}
// currentStakerChainStateImpl is a copy on write implementation for versioning
// the validator set. None of the slices, maps, or pointers should be modified
// after initialization.
type currentStakerChainStateImpl struct {
nextStaker *validatorReward
// nodeID -> validator
validatorsByNodeID map[ids.ShortID]*currentValidatorImpl
// txID -> tx
validatorsByTxID map[ids.ID]*validatorReward
// list of current validators in order of their removal from the validator
// set
validators []*Tx
addedStakers []*validatorReward
deletedStakers []*Tx
}
type validatorReward struct {
addStakerTx *Tx
potentialReward uint64
}
func (cs *currentStakerChainStateImpl) GetNextStaker() (addStakerTx *Tx, potentialReward uint64, err error) {
if cs.nextStaker == nil {
return nil, 0, database.ErrNotFound
}
return cs.nextStaker.addStakerTx, cs.nextStaker.potentialReward, nil
}
func (cs *currentStakerChainStateImpl) GetValidator(nodeID ids.ShortID) (currentValidator, error) {
vdr, exists := cs.validatorsByNodeID[nodeID]
if !exists {
return nil, database.ErrNotFound
}
return vdr, nil
}
func (cs *currentStakerChainStateImpl) UpdateStakers(
addValidatorTxs []*validatorReward,
addDelegatorTxs []*validatorReward,
addSubnetValidatorTxs []*Tx,
numTxsToRemove int,
) (currentStakerChainState, error) {
if numTxsToRemove > len(cs.validators) {
return nil, errNotEnoughValidators
}
newCS := ¤tStakerChainStateImpl{
validatorsByNodeID: make(map[ids.ShortID]*currentValidatorImpl, len(cs.validatorsByNodeID)+len(addValidatorTxs)),
validatorsByTxID: make(map[ids.ID]*validatorReward, len(cs.validatorsByTxID)+len(addValidatorTxs)+len(addDelegatorTxs)+len(addSubnetValidatorTxs)),
validators: cs.validators[numTxsToRemove:], // sorted in order of removal
addedStakers: append(addValidatorTxs, addDelegatorTxs...),
deletedStakers: cs.validators[:numTxsToRemove],
}
for nodeID, vdr := range cs.validatorsByNodeID {
newCS.validatorsByNodeID[nodeID] = vdr
}
for txID, vdr := range cs.validatorsByTxID {
newCS.validatorsByTxID[txID] = vdr
}
if numAdded := len(addValidatorTxs) + len(addDelegatorTxs) + len(addSubnetValidatorTxs); numAdded != 0 {
numCurrent := len(newCS.validators)
newSize := numCurrent + numAdded
newValidators := make([]*Tx, newSize)
copy(newValidators, newCS.validators)
copy(newValidators[numCurrent:], addSubnetValidatorTxs)
numStart := numCurrent + len(addSubnetValidatorTxs)
for i, tx := range addValidatorTxs {
newValidators[numStart+i] = tx.addStakerTx
}
numStart = numCurrent + len(addSubnetValidatorTxs) + len(addValidatorTxs)
for i, tx := range addDelegatorTxs {
newValidators[numStart+i] = tx.addStakerTx
}
sortValidatorsByRemoval(newValidators)
newCS.validators = newValidators
for _, vdr := range addValidatorTxs {
switch tx := vdr.addStakerTx.UnsignedTx.(type) {
case *UnsignedAddValidatorTx:
newCS.validatorsByNodeID[tx.Validator.NodeID] = ¤tValidatorImpl{
addValidatorTx: tx,
potentialReward: vdr.potentialReward,
}
newCS.validatorsByTxID[vdr.addStakerTx.ID()] = vdr
default:
return nil, errWrongTxType
}
}
for _, vdr := range addDelegatorTxs {
switch tx := vdr.addStakerTx.UnsignedTx.(type) {
case *UnsignedAddDelegatorTx:
oldVdr := newCS.validatorsByNodeID[tx.Validator.NodeID]
newVdr := *oldVdr
newVdr.delegators = make([]*UnsignedAddDelegatorTx, len(oldVdr.delegators)+1)
copy(newVdr.delegators, oldVdr.delegators)
newVdr.delegators[len(oldVdr.delegators)] = tx
sortDelegatorsByRemoval(newVdr.delegators)
newVdr.delegatorWeight += tx.Validator.Wght
newCS.validatorsByNodeID[tx.Validator.NodeID] = &newVdr
newCS.validatorsByTxID[vdr.addStakerTx.ID()] = vdr
default:
return nil, errWrongTxType
}
}
for _, vdr := range addSubnetValidatorTxs {
switch tx := vdr.UnsignedTx.(type) {
case *UnsignedAddSubnetValidatorTx:
oldVdr := newCS.validatorsByNodeID[tx.Validator.NodeID]
newVdr := *oldVdr
newVdr.subnets = make(map[ids.ID]*UnsignedAddSubnetValidatorTx, len(oldVdr.subnets)+1)
for subnetID, addTx := range oldVdr.subnets {
newVdr.subnets[subnetID] = addTx
}
newVdr.subnets[tx.Validator.Subnet] = tx
newCS.validatorsByNodeID[tx.Validator.NodeID] = &newVdr
default:
return nil, errWrongTxType
}
wrappedTx := &validatorReward{
addStakerTx: vdr,
}
newCS.validatorsByTxID[vdr.ID()] = wrappedTx
newCS.addedStakers = append(newCS.addedStakers, wrappedTx)
}
}
for i := 0; i < numTxsToRemove; i++ {
removed := cs.validators[i]
removedID := removed.ID()
delete(newCS.validatorsByTxID, removedID)
switch tx := removed.UnsignedTx.(type) {
case *UnsignedAddSubnetValidatorTx:
oldVdr := newCS.validatorsByNodeID[tx.Validator.NodeID]
newVdr := *oldVdr
newVdr.subnets = make(map[ids.ID]*UnsignedAddSubnetValidatorTx, len(oldVdr.subnets)-1)
for subnetID, addTx := range oldVdr.subnets {
if removedID != addTx.ID() {
newVdr.subnets[subnetID] = addTx
}
}
newCS.validatorsByNodeID[tx.Validator.NodeID] = &newVdr
default:
return nil, errWrongTxType
}
}
newCS.setNextStaker()
return newCS, nil
}
func (cs *currentStakerChainStateImpl) DeleteNextStaker() (currentStakerChainState, error) {
removedTx, _, err := cs.GetNextStaker()
if err != nil {
return nil, err
}
removedTxID := removedTx.ID()
newCS := ¤tStakerChainStateImpl{
validatorsByNodeID: make(map[ids.ShortID]*currentValidatorImpl, len(cs.validatorsByNodeID)),
validatorsByTxID: make(map[ids.ID]*validatorReward, len(cs.validatorsByTxID)-1),
validators: cs.validators[1:], // sorted in order of removal
deletedStakers: []*Tx{removedTx},
}
switch tx := removedTx.UnsignedTx.(type) {
case *UnsignedAddValidatorTx:
for nodeID, vdr := range cs.validatorsByNodeID {
if nodeID != tx.Validator.NodeID {
newCS.validatorsByNodeID[nodeID] = vdr
}
}
case *UnsignedAddDelegatorTx:
for nodeID, vdr := range cs.validatorsByNodeID {
if nodeID != tx.Validator.NodeID {
newCS.validatorsByNodeID[nodeID] = vdr
} else {
newCS.validatorsByNodeID[nodeID] = ¤tValidatorImpl{
validatorImpl: validatorImpl{
delegators: vdr.delegators[1:], // sorted in order of removal
subnets: vdr.subnets,
},
addValidatorTx: vdr.addValidatorTx,
delegatorWeight: vdr.delegatorWeight - tx.Validator.Wght,
potentialReward: vdr.potentialReward,
}
}
}
default:
return nil, errWrongTxType
}
for txID, vdr := range cs.validatorsByTxID {
if txID != removedTxID {
newCS.validatorsByTxID[txID] = vdr
}
}
newCS.setNextStaker()
return newCS, nil
}
func (cs *currentStakerChainStateImpl) Stakers() []*Tx {
return cs.validators
}
func (cs *currentStakerChainStateImpl) Apply(is InternalState) {
for _, added := range cs.addedStakers {
is.AddCurrentStaker(added.addStakerTx, added.potentialReward)
}
for _, deleted := range cs.deletedStakers {
is.DeleteCurrentStaker(deleted)
}
is.SetCurrentStakerChainState(cs)
// Validator changes should only be applied once.
cs.addedStakers = nil
cs.deletedStakers = nil
}
func (cs *currentStakerChainStateImpl) ValidatorSet(subnetID ids.ID) (validators.Set, error) {
if subnetID == constants.PrimaryNetworkID {
return cs.primaryValidatorSet()
}
return cs.subnetValidatorSet(subnetID)
}
func (cs *currentStakerChainStateImpl) primaryValidatorSet() (validators.Set, error) {
vdrs := validators.NewSet()
var err error
for nodeID, vdr := range cs.validatorsByNodeID {
vdrWeight := vdr.addValidatorTx.Validator.Wght
vdrWeight, err = safemath.Add64(vdrWeight, vdr.delegatorWeight)
if err != nil {
return nil, err
}
if err := vdrs.AddWeight(nodeID, vdrWeight); err != nil {
return nil, err
}
}
return vdrs, nil
}
func (cs *currentStakerChainStateImpl) subnetValidatorSet(subnetID ids.ID) (validators.Set, error) {
vdrs := validators.NewSet()
for nodeID, vdr := range cs.validatorsByNodeID {
subnetVDR, exists := vdr.subnets[subnetID]
if !exists {
continue
}
if err := vdrs.AddWeight(nodeID, subnetVDR.Validator.Wght); err != nil {
return nil, err
}
}
return vdrs, nil
}
func (cs *currentStakerChainStateImpl) GetStaker(txID ids.ID) (tx *Tx, reward uint64, err error) {
staker, exists := cs.validatorsByTxID[txID]
if !exists {
return nil, 0, database.ErrNotFound
}
return staker.addStakerTx, staker.potentialReward, nil
}
// setNextStaker to the next staker that will be removed using a
// RewardValidatorTx.
func (cs *currentStakerChainStateImpl) setNextStaker() {
for _, tx := range cs.validators {
switch tx.UnsignedTx.(type) {
case *UnsignedAddValidatorTx, *UnsignedAddDelegatorTx:
cs.nextStaker = cs.validatorsByTxID[tx.ID()]
return
}
}
}
type innerSortValidatorsByRemoval []*Tx
func (s innerSortValidatorsByRemoval) Less(i, j int) bool {
iDel := s[i]
jDel := s[j]
var (
iEndTime time.Time
iPriority byte
)
switch tx := iDel.UnsignedTx.(type) {
case *UnsignedAddValidatorTx:
iEndTime = tx.EndTime()
iPriority = lowPriority
case *UnsignedAddDelegatorTx:
iEndTime = tx.EndTime()
iPriority = mediumPriority
case *UnsignedAddSubnetValidatorTx:
iEndTime = tx.EndTime()
iPriority = topPriority
default:
panic(fmt.Errorf("expected staker tx type but got %T", iDel.UnsignedTx))
}
var (
jEndTime time.Time
jPriority byte
)
switch tx := jDel.UnsignedTx.(type) {
case *UnsignedAddValidatorTx:
jEndTime = tx.EndTime()
jPriority = lowPriority
case *UnsignedAddDelegatorTx:
jEndTime = tx.EndTime()
jPriority = mediumPriority
case *UnsignedAddSubnetValidatorTx:
jEndTime = tx.EndTime()
jPriority = topPriority
default:
panic(fmt.Errorf("expected staker tx type but got %T", jDel.UnsignedTx))
}
if iEndTime.Before(jEndTime) {
return true
}
if jEndTime.Before(iEndTime) {
return false
}
// If the end times are the same, then we sort by the tx type. First we
// remove UnsignedAddSubnetValidatorTxs, then UnsignedAddDelegatorTx, then
// UnsignedAddValidatorTx.
if iPriority > jPriority {
return true
}
if iPriority < jPriority {
return false
}
// If the end times are the same, and the tx types are the same, then we
// sort by the txID.
iTxID := iDel.ID()
jTxID := jDel.ID()
return bytes.Compare(iTxID[:], jTxID[:]) == -1
}
func (s innerSortValidatorsByRemoval) Len() int {
return len(s)
}
func (s innerSortValidatorsByRemoval) Swap(i, j int) {
s[j], s[i] = s[i], s[j]
}
func sortValidatorsByRemoval(s []*Tx) {
sort.Sort(innerSortValidatorsByRemoval(s))
}
type innerSortDelegatorsByRemoval []*UnsignedAddDelegatorTx
func (s innerSortDelegatorsByRemoval) Less(i, j int) bool {
iDel := s[i]
jDel := s[j]
iEndTime := iDel.EndTime()
jEndTime := jDel.EndTime()
if iEndTime.Before(jEndTime) {
return true
}
if jEndTime.Before(iEndTime) {
return false
}
// If the end times are the same, then we sort by the txID
iTxID := iDel.ID()
jTxID := jDel.ID()
return bytes.Compare(iTxID[:], jTxID[:]) == -1
}
func (s innerSortDelegatorsByRemoval) Len() int {
return len(s)
}
func (s innerSortDelegatorsByRemoval) Swap(i, j int) {
s[j], s[i] = s[i], s[j]
}
func sortDelegatorsByRemoval(s []*UnsignedAddDelegatorTx) {
sort.Sort(innerSortDelegatorsByRemoval(s))
}