forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
stakers.go
398 lines (340 loc) · 12.2 KB
/
stakers.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"github.com/google/btree"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/ids"
)
type Stakers interface {
CurrentStakers
PendingStakers
}
type CurrentStakers interface {
// GetCurrentValidator returns the [staker] describing the validator on
// [subnetID] with [nodeID]. If the validator does not exist,
// [database.ErrNotFound] is returned.
GetCurrentValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker, error)
// PutCurrentValidator adds the [staker] describing a validator to the
// staker set.
//
// Invariant: [staker] is not currently a CurrentValidator
PutCurrentValidator(staker *Staker)
// DeleteCurrentValidator removes the [staker] describing a validator from
// the staker set.
//
// Invariant: [staker] is currently a CurrentValidator
DeleteCurrentValidator(staker *Staker)
// SetDelegateeReward sets the accrued delegation rewards for [nodeID] on
// [subnetID] to [amount].
SetDelegateeReward(subnetID ids.ID, nodeID ids.NodeID, amount uint64) error
// GetDelegateeReward returns the accrued delegation rewards for [nodeID] on
// [subnetID].
GetDelegateeReward(subnetID ids.ID, nodeID ids.NodeID) (uint64, error)
// GetCurrentDelegatorIterator returns the delegators associated with the
// validator on [subnetID] with [nodeID]. Delegators are sorted by their
// removal from current staker set.
GetCurrentDelegatorIterator(subnetID ids.ID, nodeID ids.NodeID) (StakerIterator, error)
// PutCurrentDelegator adds the [staker] describing a delegator to the
// staker set.
//
// Invariant: [staker] is not currently a CurrentDelegator
PutCurrentDelegator(staker *Staker)
// DeleteCurrentDelegator removes the [staker] describing a delegator from
// the staker set.
//
// Invariant: [staker] is currently a CurrentDelegator
DeleteCurrentDelegator(staker *Staker)
// GetCurrentStakerIterator returns stakers in order of their removal from
// the current staker set.
GetCurrentStakerIterator() (StakerIterator, error)
}
type PendingStakers interface {
// GetPendingValidator returns the Staker describing the validator on
// [subnetID] with [nodeID]. If the validator does not exist,
// [database.ErrNotFound] is returned.
GetPendingValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker, error)
// PutPendingValidator adds the [staker] describing a validator to the
// staker set.
PutPendingValidator(staker *Staker)
// DeletePendingValidator removes the [staker] describing a validator from
// the staker set.
DeletePendingValidator(staker *Staker)
// GetPendingDelegatorIterator returns the delegators associated with the
// validator on [subnetID] with [nodeID]. Delegators are sorted by their
// removal from pending staker set.
GetPendingDelegatorIterator(subnetID ids.ID, nodeID ids.NodeID) (StakerIterator, error)
// PutPendingDelegator adds the [staker] describing a delegator to the
// staker set.
PutPendingDelegator(staker *Staker)
// DeletePendingDelegator removes the [staker] describing a delegator from
// the staker set.
DeletePendingDelegator(staker *Staker)
// GetPendingStakerIterator returns stakers in order of their removal from
// the pending staker set.
GetPendingStakerIterator() (StakerIterator, error)
}
type baseStakers struct {
// subnetID --> nodeID --> current state for the validator of the subnet
validators map[ids.ID]map[ids.NodeID]*baseStaker
stakers *btree.BTreeG[*Staker]
// subnetID --> nodeID --> diff for that validator since the last db write
validatorDiffs map[ids.ID]map[ids.NodeID]*diffValidator
}
type baseStaker struct {
validator *Staker
delegators *btree.BTreeG[*Staker]
}
func newBaseStakers() *baseStakers {
return &baseStakers{
validators: make(map[ids.ID]map[ids.NodeID]*baseStaker),
stakers: btree.NewG(defaultTreeDegree, (*Staker).Less),
validatorDiffs: make(map[ids.ID]map[ids.NodeID]*diffValidator),
}
}
func (v *baseStakers) GetValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker, error) {
subnetValidators, ok := v.validators[subnetID]
if !ok {
return nil, database.ErrNotFound
}
validator, ok := subnetValidators[nodeID]
if !ok {
return nil, database.ErrNotFound
}
if validator.validator == nil {
return nil, database.ErrNotFound
}
return validator.validator, nil
}
func (v *baseStakers) PutValidator(staker *Staker) {
validator := v.getOrCreateValidator(staker.SubnetID, staker.NodeID)
validator.validator = staker
validatorDiff := v.getOrCreateValidatorDiff(staker.SubnetID, staker.NodeID)
validatorDiff.validatorStatus = added
validatorDiff.validator = staker
v.stakers.ReplaceOrInsert(staker)
}
func (v *baseStakers) DeleteValidator(staker *Staker) {
validator := v.getOrCreateValidator(staker.SubnetID, staker.NodeID)
validator.validator = nil
v.pruneValidator(staker.SubnetID, staker.NodeID)
validatorDiff := v.getOrCreateValidatorDiff(staker.SubnetID, staker.NodeID)
validatorDiff.validatorStatus = deleted
validatorDiff.validator = staker
v.stakers.Delete(staker)
}
func (v *baseStakers) GetDelegatorIterator(subnetID ids.ID, nodeID ids.NodeID) StakerIterator {
subnetValidators, ok := v.validators[subnetID]
if !ok {
return EmptyIterator
}
validator, ok := subnetValidators[nodeID]
if !ok {
return EmptyIterator
}
return NewTreeIterator(validator.delegators)
}
func (v *baseStakers) PutDelegator(staker *Staker) {
validator := v.getOrCreateValidator(staker.SubnetID, staker.NodeID)
if validator.delegators == nil {
validator.delegators = btree.NewG(defaultTreeDegree, (*Staker).Less)
}
validator.delegators.ReplaceOrInsert(staker)
validatorDiff := v.getOrCreateValidatorDiff(staker.SubnetID, staker.NodeID)
if validatorDiff.addedDelegators == nil {
validatorDiff.addedDelegators = btree.NewG(defaultTreeDegree, (*Staker).Less)
}
validatorDiff.addedDelegators.ReplaceOrInsert(staker)
v.stakers.ReplaceOrInsert(staker)
}
func (v *baseStakers) DeleteDelegator(staker *Staker) {
validator := v.getOrCreateValidator(staker.SubnetID, staker.NodeID)
if validator.delegators != nil {
validator.delegators.Delete(staker)
}
v.pruneValidator(staker.SubnetID, staker.NodeID)
validatorDiff := v.getOrCreateValidatorDiff(staker.SubnetID, staker.NodeID)
if validatorDiff.deletedDelegators == nil {
validatorDiff.deletedDelegators = make(map[ids.ID]*Staker)
}
validatorDiff.deletedDelegators[staker.TxID] = staker
v.stakers.Delete(staker)
}
func (v *baseStakers) GetStakerIterator() StakerIterator {
return NewTreeIterator(v.stakers)
}
func (v *baseStakers) getOrCreateValidator(subnetID ids.ID, nodeID ids.NodeID) *baseStaker {
subnetValidators, ok := v.validators[subnetID]
if !ok {
subnetValidators = make(map[ids.NodeID]*baseStaker)
v.validators[subnetID] = subnetValidators
}
validator, ok := subnetValidators[nodeID]
if !ok {
validator = &baseStaker{}
subnetValidators[nodeID] = validator
}
return validator
}
// pruneValidator assumes that the named validator is currently in the
// [validators] map.
func (v *baseStakers) pruneValidator(subnetID ids.ID, nodeID ids.NodeID) {
subnetValidators := v.validators[subnetID]
validator := subnetValidators[nodeID]
if validator.validator != nil {
return
}
if validator.delegators != nil && validator.delegators.Len() > 0 {
return
}
delete(subnetValidators, nodeID)
if len(subnetValidators) == 0 {
delete(v.validators, subnetID)
}
}
func (v *baseStakers) getOrCreateValidatorDiff(subnetID ids.ID, nodeID ids.NodeID) *diffValidator {
subnetValidatorDiffs, ok := v.validatorDiffs[subnetID]
if !ok {
subnetValidatorDiffs = make(map[ids.NodeID]*diffValidator)
v.validatorDiffs[subnetID] = subnetValidatorDiffs
}
validatorDiff, ok := subnetValidatorDiffs[nodeID]
if !ok {
validatorDiff = &diffValidator{
validatorStatus: unmodified,
}
subnetValidatorDiffs[nodeID] = validatorDiff
}
return validatorDiff
}
type diffStakers struct {
// subnetID --> nodeID --> diff for that validator
validatorDiffs map[ids.ID]map[ids.NodeID]*diffValidator
addedStakers *btree.BTreeG[*Staker]
deletedStakers map[ids.ID]*Staker
}
type diffValidator struct {
// validatorStatus describes whether a validator has been added or removed.
//
// validatorStatus is not affected by delegators ops so unmodified does not
// mean that diffValidator hasn't change, since delegators may have changed.
validatorStatus diffValidatorStatus
validator *Staker
addedDelegators *btree.BTreeG[*Staker]
deletedDelegators map[ids.ID]*Staker
}
// GetValidator attempts to fetch the validator with the given subnetID and
// nodeID.
// Invariant: Assumes that the validator will never be removed and then added.
func (s *diffStakers) GetValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker, diffValidatorStatus) {
subnetValidatorDiffs, ok := s.validatorDiffs[subnetID]
if !ok {
return nil, unmodified
}
validatorDiff, ok := subnetValidatorDiffs[nodeID]
if !ok {
return nil, unmodified
}
if validatorDiff.validatorStatus == added {
return validatorDiff.validator, added
}
return nil, validatorDiff.validatorStatus
}
func (s *diffStakers) PutValidator(staker *Staker) {
validatorDiff := s.getOrCreateDiff(staker.SubnetID, staker.NodeID)
validatorDiff.validatorStatus = added
validatorDiff.validator = staker
if s.addedStakers == nil {
s.addedStakers = btree.NewG(defaultTreeDegree, (*Staker).Less)
}
s.addedStakers.ReplaceOrInsert(staker)
}
func (s *diffStakers) DeleteValidator(staker *Staker) {
validatorDiff := s.getOrCreateDiff(staker.SubnetID, staker.NodeID)
if validatorDiff.validatorStatus == added {
// This validator was added and immediately removed in this diff. We
// treat it as if it was never added.
validatorDiff.validatorStatus = unmodified
s.addedStakers.Delete(validatorDiff.validator)
validatorDiff.validator = nil
} else {
validatorDiff.validatorStatus = deleted
validatorDiff.validator = staker
if s.deletedStakers == nil {
s.deletedStakers = make(map[ids.ID]*Staker)
}
s.deletedStakers[staker.TxID] = staker
}
}
func (s *diffStakers) GetDelegatorIterator(
parentIterator StakerIterator,
subnetID ids.ID,
nodeID ids.NodeID,
) StakerIterator {
var (
addedDelegatorIterator = EmptyIterator
deletedDelegators map[ids.ID]*Staker
)
if subnetValidatorDiffs, ok := s.validatorDiffs[subnetID]; ok {
if validatorDiff, ok := subnetValidatorDiffs[nodeID]; ok {
addedDelegatorIterator = NewTreeIterator(validatorDiff.addedDelegators)
deletedDelegators = validatorDiff.deletedDelegators
}
}
return NewMaskedIterator(
NewMergedIterator(
parentIterator,
addedDelegatorIterator,
),
deletedDelegators,
)
}
func (s *diffStakers) PutDelegator(staker *Staker) {
validatorDiff := s.getOrCreateDiff(staker.SubnetID, staker.NodeID)
if validatorDiff.addedDelegators == nil {
validatorDiff.addedDelegators = btree.NewG(defaultTreeDegree, (*Staker).Less)
}
validatorDiff.addedDelegators.ReplaceOrInsert(staker)
if s.addedStakers == nil {
s.addedStakers = btree.NewG(defaultTreeDegree, (*Staker).Less)
}
s.addedStakers.ReplaceOrInsert(staker)
}
func (s *diffStakers) DeleteDelegator(staker *Staker) {
validatorDiff := s.getOrCreateDiff(staker.SubnetID, staker.NodeID)
if validatorDiff.deletedDelegators == nil {
validatorDiff.deletedDelegators = make(map[ids.ID]*Staker)
}
validatorDiff.deletedDelegators[staker.TxID] = staker
if s.deletedStakers == nil {
s.deletedStakers = make(map[ids.ID]*Staker)
}
s.deletedStakers[staker.TxID] = staker
}
func (s *diffStakers) GetStakerIterator(parentIterator StakerIterator) StakerIterator {
return NewMaskedIterator(
NewMergedIterator(
parentIterator,
NewTreeIterator(s.addedStakers),
),
s.deletedStakers,
)
}
func (s *diffStakers) getOrCreateDiff(subnetID ids.ID, nodeID ids.NodeID) *diffValidator {
if s.validatorDiffs == nil {
s.validatorDiffs = make(map[ids.ID]map[ids.NodeID]*diffValidator)
}
subnetValidatorDiffs, ok := s.validatorDiffs[subnetID]
if !ok {
subnetValidatorDiffs = make(map[ids.NodeID]*diffValidator)
s.validatorDiffs[subnetID] = subnetValidatorDiffs
}
validatorDiff, ok := subnetValidatorDiffs[nodeID]
if !ok {
validatorDiff = &diffValidator{
validatorStatus: unmodified,
}
subnetValidatorDiffs[nodeID] = validatorDiff
}
return validatorDiff
}