-
Notifications
You must be signed in to change notification settings - Fork 665
/
cache_pending_staker_chain_state.go
324 lines (278 loc) · 9.3 KB
/
cache_pending_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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"bytes"
"fmt"
"sort"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
)
var _ pendingStakerChainState = &pendingStakerChainStateImpl{}
// pendingStakerChainState manages the set of stakers (both validators and
// delegators) that are slated to start staking in the future.
type pendingStakerChainState interface {
GetValidatorTx(nodeID ids.ShortID) (addStakerTx *UnsignedAddValidatorTx, err error)
GetValidator(nodeID ids.ShortID) validator
AddStaker(addStakerTx *Tx) pendingStakerChainState
DeleteStakers(numToRemove int) pendingStakerChainState
// Stakers returns the list of pending validators in order of their removal
// from the pending staker set
Stakers() []*Tx
Apply(InternalState)
}
// pendingStakerChainStateImpl is a copy on write implementation for versioning
// the validator set. None of the slices, maps, or pointers should be modified
// after initialization.
type pendingStakerChainStateImpl struct {
// nodeID -> validator
validatorsByNodeID map[ids.ShortID]*UnsignedAddValidatorTx
validatorExtrasByNodeID map[ids.ShortID]*validatorImpl
// list of pending validators in order of their removal from the pending
// staker set
validators []*Tx
addedStakers []*Tx
deletedStakers []*Tx
}
func (ps *pendingStakerChainStateImpl) GetValidatorTx(nodeID ids.ShortID) (addStakerTx *UnsignedAddValidatorTx, err error) {
vdr, exists := ps.validatorsByNodeID[nodeID]
if !exists {
return nil, database.ErrNotFound
}
return vdr, nil
}
func (ps *pendingStakerChainStateImpl) GetValidator(nodeID ids.ShortID) validator {
if vdr, exists := ps.validatorExtrasByNodeID[nodeID]; exists {
return vdr
}
return &validatorImpl{}
}
func (ps *pendingStakerChainStateImpl) AddStaker(addStakerTx *Tx) pendingStakerChainState {
newPS := &pendingStakerChainStateImpl{
validators: make([]*Tx, len(ps.validators)+1),
addedStakers: []*Tx{addStakerTx},
}
copy(newPS.validators, ps.validators)
newPS.validators[len(ps.validators)] = addStakerTx
sortValidatorsByAddition(newPS.validators)
switch tx := addStakerTx.UnsignedTx.(type) {
case *UnsignedAddValidatorTx:
newPS.validatorExtrasByNodeID = ps.validatorExtrasByNodeID
newPS.validatorsByNodeID = make(map[ids.ShortID]*UnsignedAddValidatorTx, len(ps.validatorsByNodeID)+1)
for nodeID, vdr := range ps.validatorsByNodeID {
newPS.validatorsByNodeID[nodeID] = vdr
}
newPS.validatorsByNodeID[tx.Validator.NodeID] = tx
case *UnsignedAddDelegatorTx:
newPS.validatorsByNodeID = ps.validatorsByNodeID
newPS.validatorExtrasByNodeID = make(map[ids.ShortID]*validatorImpl, len(ps.validatorExtrasByNodeID)+1)
for nodeID, vdr := range ps.validatorExtrasByNodeID {
if nodeID != tx.Validator.NodeID {
newPS.validatorExtrasByNodeID[nodeID] = vdr
}
}
if vdr, exists := ps.validatorExtrasByNodeID[tx.Validator.NodeID]; exists {
newDelegators := make([]*UnsignedAddDelegatorTx, len(vdr.delegators)+1)
copy(newDelegators, vdr.delegators)
newDelegators[len(vdr.delegators)] = tx
sortDelegatorsByAddition(newDelegators)
newPS.validatorExtrasByNodeID[tx.Validator.NodeID] = &validatorImpl{
delegators: newDelegators,
subnets: vdr.subnets,
}
} else {
newPS.validatorExtrasByNodeID[tx.Validator.NodeID] = &validatorImpl{
delegators: []*UnsignedAddDelegatorTx{
tx,
},
}
}
case *UnsignedAddSubnetValidatorTx:
newPS.validatorsByNodeID = ps.validatorsByNodeID
newPS.validatorExtrasByNodeID = make(map[ids.ShortID]*validatorImpl, len(ps.validatorExtrasByNodeID)+1)
for nodeID, vdr := range ps.validatorExtrasByNodeID {
if nodeID != tx.Validator.NodeID {
newPS.validatorExtrasByNodeID[nodeID] = vdr
}
}
if vdr, exists := ps.validatorExtrasByNodeID[tx.Validator.NodeID]; exists {
newSubnets := make(map[ids.ID]*UnsignedAddSubnetValidatorTx, len(vdr.subnets)+1)
for subnet, subnetTx := range vdr.subnets {
newSubnets[subnet] = subnetTx
}
newSubnets[tx.Validator.Subnet] = tx
newPS.validatorExtrasByNodeID[tx.Validator.NodeID] = &validatorImpl{
delegators: vdr.delegators,
subnets: newSubnets,
}
} else {
newPS.validatorExtrasByNodeID[tx.Validator.NodeID] = &validatorImpl{
subnets: map[ids.ID]*UnsignedAddSubnetValidatorTx{
tx.Validator.Subnet: tx,
},
}
}
default:
panic(fmt.Errorf("expected staker tx type but got %T", addStakerTx.UnsignedTx))
}
return newPS
}
func (ps *pendingStakerChainStateImpl) DeleteStakers(numToRemove int) pendingStakerChainState {
newPS := &pendingStakerChainStateImpl{
validatorsByNodeID: make(map[ids.ShortID]*UnsignedAddValidatorTx, len(ps.validatorsByNodeID)),
validatorExtrasByNodeID: make(map[ids.ShortID]*validatorImpl, len(ps.validatorExtrasByNodeID)),
validators: ps.validators[numToRemove:],
deletedStakers: ps.validators[:numToRemove],
}
for nodeID, vdr := range ps.validatorsByNodeID {
newPS.validatorsByNodeID[nodeID] = vdr
}
for nodeID, vdr := range ps.validatorExtrasByNodeID {
newPS.validatorExtrasByNodeID[nodeID] = vdr
}
for _, removedTx := range ps.validators[:numToRemove] {
switch tx := removedTx.UnsignedTx.(type) {
case *UnsignedAddValidatorTx:
delete(newPS.validatorsByNodeID, tx.Validator.NodeID)
case *UnsignedAddDelegatorTx:
vdr := newPS.validatorExtrasByNodeID[tx.Validator.NodeID]
if len(vdr.delegators) == 1 && len(vdr.subnets) == 0 {
delete(newPS.validatorExtrasByNodeID, tx.Validator.NodeID)
break
}
newPS.validatorExtrasByNodeID[tx.Validator.NodeID] = &validatorImpl{
delegators: vdr.delegators[1:], // sorted in order of removal
subnets: vdr.subnets,
}
case *UnsignedAddSubnetValidatorTx:
vdr := newPS.validatorExtrasByNodeID[tx.Validator.NodeID]
if len(vdr.delegators) == 0 && len(vdr.subnets) == 1 {
delete(newPS.validatorExtrasByNodeID, tx.Validator.NodeID)
break
}
newSubnets := make(map[ids.ID]*UnsignedAddSubnetValidatorTx, len(vdr.subnets)-1)
for subnetID, subnetTx := range vdr.subnets {
if subnetID != tx.Validator.Subnet {
newSubnets[subnetID] = subnetTx
}
}
newPS.validatorExtrasByNodeID[tx.Validator.NodeID] = &validatorImpl{
delegators: vdr.delegators,
subnets: newSubnets,
}
default:
panic(fmt.Errorf("expected staker tx type but got %T", removedTx.UnsignedTx))
}
}
return newPS
}
func (ps *pendingStakerChainStateImpl) Stakers() []*Tx {
return ps.validators
}
func (ps *pendingStakerChainStateImpl) Apply(is InternalState) {
for _, added := range ps.addedStakers {
is.AddPendingStaker(added)
}
for _, deleted := range ps.deletedStakers {
is.DeletePendingStaker(deleted)
}
is.SetPendingStakerChainState(ps)
// Validator changes should only be applied once.
ps.addedStakers = nil
ps.deletedStakers = nil
}
type innerSortValidatorsByAddition []*Tx
func (s innerSortValidatorsByAddition) Less(i, j int) bool {
iDel := s[i]
jDel := s[j]
var (
iStartTime time.Time
iPriority byte
)
switch tx := iDel.UnsignedTx.(type) {
case *UnsignedAddValidatorTx:
iStartTime = tx.StartTime()
iPriority = mediumPriority
case *UnsignedAddDelegatorTx:
iStartTime = tx.StartTime()
iPriority = topPriority
case *UnsignedAddSubnetValidatorTx:
iStartTime = tx.StartTime()
iPriority = lowPriority
default:
panic(fmt.Errorf("expected staker tx type but got %T", iDel.UnsignedTx))
}
var (
jStartTime time.Time
jPriority byte
)
switch tx := jDel.UnsignedTx.(type) {
case *UnsignedAddValidatorTx:
jStartTime = tx.StartTime()
jPriority = mediumPriority
case *UnsignedAddDelegatorTx:
jStartTime = tx.StartTime()
jPriority = topPriority
case *UnsignedAddSubnetValidatorTx:
jStartTime = tx.StartTime()
jPriority = lowPriority
default:
panic(fmt.Errorf("expected staker tx type but got %T", jDel.UnsignedTx))
}
if iStartTime.Before(jStartTime) {
return true
}
if jStartTime.Before(iStartTime) {
return false
}
// If the end times are the same, then we sort by the tx type. First we
// add UnsignedAddValidatorTx, then UnsignedAddDelegatorTx, then
// UnsignedAddSubnetValidatorTxs.
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 innerSortValidatorsByAddition) Len() int {
return len(s)
}
func (s innerSortValidatorsByAddition) Swap(i, j int) {
s[j], s[i] = s[i], s[j]
}
func sortValidatorsByAddition(s []*Tx) {
sort.Sort(innerSortValidatorsByAddition(s))
}
type innerSortDelegatorsByAddition []*UnsignedAddDelegatorTx
func (s innerSortDelegatorsByAddition) Less(i, j int) bool {
iDel := s[i]
jDel := s[j]
iStartTime := iDel.StartTime()
jStartTime := jDel.StartTime()
if iStartTime.Before(jStartTime) {
return true
}
if jStartTime.Before(iStartTime) {
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 innerSortDelegatorsByAddition) Len() int {
return len(s)
}
func (s innerSortDelegatorsByAddition) Swap(i, j int) {
s[j], s[i] = s[i], s[j]
}
func sortDelegatorsByAddition(s []*UnsignedAddDelegatorTx) {
sort.Sort(innerSortDelegatorsByAddition(s))
}