forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
diff.go
539 lines (464 loc) · 14.3 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"errors"
"fmt"
"time"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
"github.com/MetalBlockchain/metalgo/vms/platformvm/status"
"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
)
var (
_ Diff = (*diff)(nil)
ErrMissingParentState = errors.New("missing parent state")
)
type Diff interface {
Chain
Apply(State) error
}
type diff struct {
parentID ids.ID
stateVersions Versions
timestamp time.Time
// Subnet ID --> supply of native asset of the subnet
currentSupply map[ids.ID]uint64
currentStakerDiffs diffStakers
// map of subnetID -> nodeID -> total accrued delegatee rewards
modifiedDelegateeRewards map[ids.ID]map[ids.NodeID]uint64
pendingStakerDiffs diffStakers
addedSubnets []*txs.Tx
// Subnet ID --> Tx that transforms the subnet
transformedSubnets map[ids.ID]*txs.Tx
cachedSubnets []*txs.Tx
addedChains map[ids.ID][]*txs.Tx
cachedChains map[ids.ID][]*txs.Tx
addedRewardUTXOs map[ids.ID][]*avax.UTXO
addedTxs map[ids.ID]*txAndStatus
// map of modified UTXOID -> *UTXO if the UTXO is nil, it has been removed
modifiedUTXOs map[ids.ID]*avax.UTXO
}
func NewDiff(
parentID ids.ID,
stateVersions Versions,
) (Diff, error) {
parentState, ok := stateVersions.GetState(parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, parentID)
}
return &diff{
parentID: parentID,
stateVersions: stateVersions,
timestamp: parentState.GetTimestamp(),
}, nil
}
func (d *diff) GetTimestamp() time.Time {
return d.timestamp
}
func (d *diff) SetTimestamp(timestamp time.Time) {
d.timestamp = timestamp
}
func (d *diff) GetCurrentSupply(subnetID ids.ID) (uint64, error) {
supply, ok := d.currentSupply[subnetID]
if ok {
return supply, nil
}
// If the subnet supply wasn't modified in this diff, ask the parent state.
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return 0, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetCurrentSupply(subnetID)
}
func (d *diff) SetCurrentSupply(subnetID ids.ID, currentSupply uint64) {
if d.currentSupply == nil {
d.currentSupply = map[ids.ID]uint64{
subnetID: currentSupply,
}
} else {
d.currentSupply[subnetID] = 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, status := d.currentStakerDiffs.GetValidator(subnetID, nodeID)
switch status {
case added:
return newValidator, nil
case deleted:
return nil, database.ErrNotFound
default:
// If the validator wasn't modified in this diff, ask the parent state.
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetCurrentValidator(subnetID, nodeID)
}
}
func (d *diff) SetDelegateeReward(subnetID ids.ID, nodeID ids.NodeID, amount uint64) error {
if d.modifiedDelegateeRewards == nil {
d.modifiedDelegateeRewards = make(map[ids.ID]map[ids.NodeID]uint64)
}
nodes, ok := d.modifiedDelegateeRewards[subnetID]
if !ok {
nodes = make(map[ids.NodeID]uint64)
d.modifiedDelegateeRewards[subnetID] = nodes
}
nodes[nodeID] = amount
return nil
}
func (d *diff) GetDelegateeReward(subnetID ids.ID, nodeID ids.NodeID) (uint64, error) {
amount, modified := d.modifiedDelegateeRewards[subnetID][nodeID]
if modified {
return amount, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return 0, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetDelegateeReward(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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, status := d.pendingStakerDiffs.GetValidator(subnetID, nodeID)
switch status {
case added:
return newValidator, nil
case deleted:
return nil, database.ErrNotFound
default:
// If the validator wasn't modified in this diff, ask the parent state.
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetSubnets()
}
if len(d.cachedSubnets) != 0 {
return d.cachedSubnets, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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) GetSubnetTransformation(subnetID ids.ID) (*txs.Tx, error) {
tx, exists := d.transformedSubnets[subnetID]
if exists {
return tx, nil
}
// If the subnet wasn't transformed in this diff, ask the parent state.
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, ErrMissingParentState
}
return parentState.GetSubnetTransformation(subnetID)
}
func (d *diff) AddSubnetTransformation(transformSubnetTxIntf *txs.Tx) {
transformSubnetTx := transformSubnetTxIntf.Unsigned.(*txs.TransformSubnetTx)
if d.transformedSubnets == nil {
d.transformedSubnets = map[ids.ID]*txs.Tx{
transformSubnetTx.Subnet: transformSubnetTxIntf,
}
} else {
d.transformedSubnets[transformSubnetTx.Subnet] = transformSubnetTxIntf
}
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
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, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetUTXO(utxoID)
}
if utxo == nil {
return nil, database.ErrNotFound
}
return utxo, nil
}
func (d *diff) AddUTXO(utxo *avax.UTXO) {
if d.modifiedUTXOs == nil {
d.modifiedUTXOs = map[ids.ID]*avax.UTXO{
utxo.InputID(): utxo,
}
} else {
d.modifiedUTXOs[utxo.InputID()] = utxo
}
}
func (d *diff) DeleteUTXO(utxoID ids.ID) {
if d.modifiedUTXOs == nil {
d.modifiedUTXOs = map[ids.ID]*avax.UTXO{
utxoID: nil,
}
} else {
d.modifiedUTXOs[utxoID] = nil
}
}
func (d *diff) Apply(baseState State) error {
baseState.SetTimestamp(d.timestamp)
for subnetID, supply := range d.currentSupply {
baseState.SetCurrentSupply(subnetID, supply)
}
for _, subnetValidatorDiffs := range d.currentStakerDiffs.validatorDiffs {
for _, validatorDiff := range subnetValidatorDiffs {
switch validatorDiff.validatorStatus {
case added:
baseState.PutCurrentValidator(validatorDiff.validator)
case deleted:
baseState.DeleteCurrentValidator(validatorDiff.validator)
}
addedDelegatorIterator := NewTreeIterator(validatorDiff.addedDelegators)
for addedDelegatorIterator.Next() {
baseState.PutCurrentDelegator(addedDelegatorIterator.Value())
}
addedDelegatorIterator.Release()
for _, delegator := range validatorDiff.deletedDelegators {
baseState.DeleteCurrentDelegator(delegator)
}
}
}
for subnetID, nodes := range d.modifiedDelegateeRewards {
for nodeID, amount := range nodes {
if err := baseState.SetDelegateeReward(subnetID, nodeID, amount); err != nil {
return err
}
}
}
for _, subnetValidatorDiffs := range d.pendingStakerDiffs.validatorDiffs {
for _, validatorDiff := range subnetValidatorDiffs {
switch validatorDiff.validatorStatus {
case added:
baseState.PutPendingValidator(validatorDiff.validator)
case deleted:
baseState.DeletePendingValidator(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 _, tx := range d.transformedSubnets {
baseState.AddSubnetTransformation(tx)
}
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 utxoID, utxo := range d.modifiedUTXOs {
if utxo != nil {
baseState.AddUTXO(utxo)
} else {
baseState.DeleteUTXO(utxoID)
}
}
return nil
}