-
Notifications
You must be signed in to change notification settings - Fork 649
/
diff.go
471 lines (399 loc) · 11.6 KB
/
diff.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
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"errors"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)
var (
_ Diff = &diff{}
errMissingParentState = errors.New("missing parent state")
)
type Diff interface {
Chain
Apply(State)
}
type diff struct {
parentID ids.ID
stateVersions Versions
timestamp time.Time
currentSupply uint64
currentStakerDiffs diffStakers
pendingStakerDiffs diffStakers
addedSubnets []*txs.Tx
cachedSubnets []*txs.Tx
addedChains map[ids.ID][]*txs.Tx
cachedChains map[ids.ID][]*txs.Tx
// map of txID -> []*UTXO
addedRewardUTXOs map[ids.ID][]*avax.UTXO
// map of txID -> {*txs.Tx, Status}
addedTxs map[ids.ID]*txAndStatus
// map of modified UTXOID -> *UTXO if the UTXO is nil, it has been removed
modifiedUTXOs map[ids.ID]*utxoModification
}
type utxoModification struct {
utxoID ids.ID
utxo *avax.UTXO
}
func NewDiff(
parentID ids.ID,
stateVersions Versions,
) (Diff, error) {
parentState, ok := stateVersions.GetState(parentID)
if !ok {
return nil, errMissingParentState
}
return &diff{
parentID: parentID,
stateVersions: stateVersions,
timestamp: parentState.GetTimestamp(),
currentSupply: parentState.GetCurrentSupply(),
}, nil
}
func (d *diff) GetTimestamp() time.Time {
return d.timestamp
}
func (d *diff) SetTimestamp(timestamp time.Time) {
d.timestamp = timestamp
}
func (d *diff) GetCurrentSupply() uint64 {
return d.currentSupply
}
func (d *diff) SetCurrentSupply(currentSupply uint64) {
d.currentSupply = currentSupply
}
func (d *diff) GetCurrentValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker, error) {
// If the validator was modified in this diff, return the modified
// validator.
newValidator, ok := d.currentStakerDiffs.GetValidator(subnetID, nodeID)
if ok {
if newValidator == nil {
return nil, database.ErrNotFound
}
return newValidator, nil
}
// If the validator wasn't modified in this diff, ask the parent state.
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
return parentState.GetCurrentValidator(subnetID, nodeID)
}
func (d *diff) PutCurrentValidator(staker *Staker) {
d.currentStakerDiffs.PutValidator(staker)
}
func (d *diff) DeleteCurrentValidator(staker *Staker) {
d.currentStakerDiffs.DeleteValidator(staker)
}
func (d *diff) GetCurrentDelegatorIterator(subnetID ids.ID, nodeID ids.NodeID) (StakerIterator, error) {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
parentIterator, err := parentState.GetCurrentDelegatorIterator(subnetID, nodeID)
if err != nil {
return nil, err
}
return d.currentStakerDiffs.GetDelegatorIterator(parentIterator, subnetID, nodeID), nil
}
func (d *diff) PutCurrentDelegator(staker *Staker) {
d.currentStakerDiffs.PutDelegator(staker)
}
func (d *diff) DeleteCurrentDelegator(staker *Staker) {
d.currentStakerDiffs.DeleteDelegator(staker)
}
func (d *diff) GetCurrentStakerIterator() (StakerIterator, error) {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
parentIterator, err := parentState.GetCurrentStakerIterator()
if err != nil {
return nil, err
}
return d.currentStakerDiffs.GetStakerIterator(parentIterator), nil
}
func (d *diff) GetPendingValidator(subnetID ids.ID, nodeID ids.NodeID) (*Staker, error) {
// If the validator was modified in this diff, return the modified
// validator.
newValidator, ok := d.pendingStakerDiffs.GetValidator(subnetID, nodeID)
if ok {
if newValidator == nil {
return nil, database.ErrNotFound
}
return newValidator, nil
}
// If the validator wasn't modified in this diff, ask the parent state.
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
return parentState.GetPendingValidator(subnetID, nodeID)
}
func (d *diff) PutPendingValidator(staker *Staker) {
d.pendingStakerDiffs.PutValidator(staker)
}
func (d *diff) DeletePendingValidator(staker *Staker) {
d.pendingStakerDiffs.DeleteValidator(staker)
}
func (d *diff) GetPendingDelegatorIterator(subnetID ids.ID, nodeID ids.NodeID) (StakerIterator, error) {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
parentIterator, err := parentState.GetPendingDelegatorIterator(subnetID, nodeID)
if err != nil {
return nil, err
}
return d.pendingStakerDiffs.GetDelegatorIterator(parentIterator, subnetID, nodeID), nil
}
func (d *diff) PutPendingDelegator(staker *Staker) {
d.pendingStakerDiffs.PutDelegator(staker)
}
func (d *diff) DeletePendingDelegator(staker *Staker) {
d.pendingStakerDiffs.DeleteDelegator(staker)
}
func (d *diff) GetPendingStakerIterator() (StakerIterator, error) {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
parentIterator, err := parentState.GetPendingStakerIterator()
if err != nil {
return nil, err
}
return d.pendingStakerDiffs.GetStakerIterator(parentIterator), nil
}
func (d *diff) GetSubnets() ([]*txs.Tx, error) {
if len(d.addedSubnets) == 0 {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
return parentState.GetSubnets()
}
if len(d.cachedSubnets) != 0 {
return d.cachedSubnets, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
subnets, err := parentState.GetSubnets()
if err != nil {
return nil, err
}
newSubnets := make([]*txs.Tx, len(subnets)+len(d.addedSubnets))
copy(newSubnets, subnets)
for i, subnet := range d.addedSubnets {
newSubnets[i+len(subnets)] = subnet
}
d.cachedSubnets = newSubnets
return newSubnets, nil
}
func (d *diff) AddSubnet(createSubnetTx *txs.Tx) {
d.addedSubnets = append(d.addedSubnets, createSubnetTx)
if d.cachedSubnets != nil {
d.cachedSubnets = append(d.cachedSubnets, createSubnetTx)
}
}
func (d *diff) GetChains(subnetID ids.ID) ([]*txs.Tx, error) {
addedChains := d.addedChains[subnetID]
if len(addedChains) == 0 {
// No chains have been added to this subnet
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
return parentState.GetChains(subnetID)
}
// There have been chains added to the requested subnet
if d.cachedChains == nil {
// This is the first time we are going to be caching the subnet chains
d.cachedChains = make(map[ids.ID][]*txs.Tx)
}
cachedChains, cached := d.cachedChains[subnetID]
if cached {
return cachedChains, nil
}
// This chain wasn't cached yet
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
chains, err := parentState.GetChains(subnetID)
if err != nil {
return nil, err
}
newChains := make([]*txs.Tx, len(chains)+len(addedChains))
copy(newChains, chains)
for i, chain := range addedChains {
newChains[i+len(chains)] = chain
}
d.cachedChains[subnetID] = newChains
return newChains, nil
}
func (d *diff) AddChain(createChainTx *txs.Tx) {
tx := createChainTx.Unsigned.(*txs.CreateChainTx)
if d.addedChains == nil {
d.addedChains = map[ids.ID][]*txs.Tx{
tx.SubnetID: {createChainTx},
}
} else {
d.addedChains[tx.SubnetID] = append(d.addedChains[tx.SubnetID], createChainTx)
}
cachedChains, cached := d.cachedChains[tx.SubnetID]
if !cached {
return
}
d.cachedChains[tx.SubnetID] = append(cachedChains, createChainTx)
}
func (d *diff) GetTx(txID ids.ID) (*txs.Tx, status.Status, error) {
if tx, exists := d.addedTxs[txID]; exists {
return tx.tx, tx.status, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, status.Unknown, errMissingParentState
}
return parentState.GetTx(txID)
}
func (d *diff) AddTx(tx *txs.Tx, status status.Status) {
txID := tx.ID()
txStatus := &txAndStatus{
tx: tx,
status: status,
}
if d.addedTxs == nil {
d.addedTxs = map[ids.ID]*txAndStatus{
txID: txStatus,
}
} else {
d.addedTxs[txID] = txStatus
}
}
func (d *diff) GetRewardUTXOs(txID ids.ID) ([]*avax.UTXO, error) {
if utxos, exists := d.addedRewardUTXOs[txID]; exists {
return utxos, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
return parentState.GetRewardUTXOs(txID)
}
func (d *diff) AddRewardUTXO(txID ids.ID, utxo *avax.UTXO) {
if d.addedRewardUTXOs == nil {
d.addedRewardUTXOs = make(map[ids.ID][]*avax.UTXO)
}
d.addedRewardUTXOs[txID] = append(d.addedRewardUTXOs[txID], utxo)
}
func (d *diff) GetUTXO(utxoID ids.ID) (*avax.UTXO, error) {
utxo, modified := d.modifiedUTXOs[utxoID]
if !modified {
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, errMissingParentState
}
return parentState.GetUTXO(utxoID)
}
if utxo.utxo == nil {
return nil, database.ErrNotFound
}
return utxo.utxo, nil
}
func (d *diff) AddUTXO(utxo *avax.UTXO) {
newUTXO := &utxoModification{
utxoID: utxo.InputID(),
utxo: utxo,
}
if d.modifiedUTXOs == nil {
d.modifiedUTXOs = map[ids.ID]*utxoModification{
utxo.InputID(): newUTXO,
}
} else {
d.modifiedUTXOs[utxo.InputID()] = newUTXO
}
}
func (d *diff) DeleteUTXO(utxoID ids.ID) {
newUTXO := &utxoModification{
utxoID: utxoID,
}
if d.modifiedUTXOs == nil {
d.modifiedUTXOs = map[ids.ID]*utxoModification{
utxoID: newUTXO,
}
} else {
d.modifiedUTXOs[utxoID] = newUTXO
}
}
func (d *diff) Apply(baseState State) {
baseState.SetTimestamp(d.timestamp)
baseState.SetCurrentSupply(d.currentSupply)
for _, subnetValidatorDiffs := range d.currentStakerDiffs.validatorDiffs {
for _, validatorDiff := range subnetValidatorDiffs {
if validatorDiff.validatorModified {
if validatorDiff.validatorDeleted {
baseState.DeleteCurrentValidator(validatorDiff.validator)
} else {
baseState.PutCurrentValidator(validatorDiff.validator)
}
}
addedDelegatorIterator := NewTreeIterator(validatorDiff.addedDelegators)
for addedDelegatorIterator.Next() {
baseState.PutCurrentDelegator(addedDelegatorIterator.Value())
}
addedDelegatorIterator.Release()
for _, delegator := range validatorDiff.deletedDelegators {
baseState.DeleteCurrentDelegator(delegator)
}
}
}
for _, subnetValidatorDiffs := range d.pendingStakerDiffs.validatorDiffs {
for _, validatorDiff := range subnetValidatorDiffs {
if validatorDiff.validatorModified {
if validatorDiff.validatorDeleted {
baseState.DeletePendingValidator(validatorDiff.validator)
} else {
baseState.PutPendingValidator(validatorDiff.validator)
}
}
addedDelegatorIterator := NewTreeIterator(validatorDiff.addedDelegators)
for addedDelegatorIterator.Next() {
baseState.PutPendingDelegator(addedDelegatorIterator.Value())
}
addedDelegatorIterator.Release()
for _, delegator := range validatorDiff.deletedDelegators {
baseState.DeletePendingDelegator(delegator)
}
}
}
for _, subnet := range d.addedSubnets {
baseState.AddSubnet(subnet)
}
for _, chains := range d.addedChains {
for _, chain := range chains {
baseState.AddChain(chain)
}
}
for _, tx := range d.addedTxs {
baseState.AddTx(tx.tx, tx.status)
}
for txID, utxos := range d.addedRewardUTXOs {
for _, utxo := range utxos {
baseState.AddRewardUTXO(txID, utxo)
}
}
for _, utxo := range d.modifiedUTXOs {
if utxo.utxo != nil {
baseState.AddUTXO(utxo.utxo)
} else {
baseState.DeleteUTXO(utxo.utxoID)
}
}
}