-
Notifications
You must be signed in to change notification settings - Fork 660
/
proposal_tx_executor.go
708 lines (622 loc) · 21.1 KB
/
proposal_tx_executor.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"errors"
"fmt"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
)
const (
// Maximum future start time for staking/delegating
MaxFutureStartTime = 24 * 7 * 2 * time.Hour
// SyncBound is the synchrony bound used for safe decision making
SyncBound = 10 * time.Second
MaxValidatorWeightFactor = 5
)
var (
_ txs.Visitor = &ProposalTxExecutor{}
errChildBlockNotAfterParent = errors.New("proposed timestamp not after current chain time")
errInvalidState = errors.New("generated output isn't valid state")
errShouldBePermissionlessStaker = errors.New("expected permissionless staker")
errWrongTxType = errors.New("wrong transaction type")
errInvalidID = errors.New("invalid ID")
errProposedAddStakerTxAfterBlueberry = errors.New("staker transaction proposed after Blueberry")
errAdvanceTimeTxIssuedAfterBlueberry = errors.New("AdvanceTimeTx issued after Blueberry")
)
type ProposalTxExecutor struct {
// inputs, to be filled before visitor methods are called
*Backend
Tx *txs.Tx
// [OnCommitState] is the state used for validation.
// In practice, both [OnCommitState] and [onAbortState] are
// identical when passed into this struct, so we could use either.
// [OnCommitState] is modified by this struct's methods to
// reflect changes made to the state if the proposal is committed.
OnCommitState state.Diff
// [OnAbortState] is modified by this struct's methods to
// reflect changes made to the state if the proposal is aborted.
OnAbortState state.Diff
// outputs populated by this struct's methods:
//
// [PrefersCommit] is true iff this node initially prefers to
// commit this block transaction.
PrefersCommit bool
}
func (*ProposalTxExecutor) CreateChainTx(*txs.CreateChainTx) error { return errWrongTxType }
func (*ProposalTxExecutor) CreateSubnetTx(*txs.CreateSubnetTx) error { return errWrongTxType }
func (*ProposalTxExecutor) ImportTx(*txs.ImportTx) error { return errWrongTxType }
func (*ProposalTxExecutor) ExportTx(*txs.ExportTx) error { return errWrongTxType }
func (*ProposalTxExecutor) RemoveSubnetValidatorTx(*txs.RemoveSubnetValidatorTx) error {
return errWrongTxType
}
func (*ProposalTxExecutor) TransformSubnetTx(*txs.TransformSubnetTx) error { return errWrongTxType }
func (*ProposalTxExecutor) AddPermissionlessValidatorTx(*txs.AddPermissionlessValidatorTx) error {
return errWrongTxType
}
func (*ProposalTxExecutor) AddPermissionlessDelegatorTx(*txs.AddPermissionlessDelegatorTx) error {
return errWrongTxType
}
func (e *ProposalTxExecutor) AddValidatorTx(tx *txs.AddValidatorTx) error {
// AddValidatorTx is a proposal transaction until the Blueberry fork
// activation. Following the activation, AddValidatorTxs must be issued into
// StandardBlocks.
currentTimestamp := e.OnCommitState.GetTimestamp()
if e.Config.IsBlueberryActivated(currentTimestamp) {
return fmt.Errorf(
"%w: timestamp (%s) >= Blueberry fork time (%s)",
errProposedAddStakerTxAfterBlueberry,
currentTimestamp,
e.Config.BlueberryTime,
)
}
onAbortOuts, err := verifyAddValidatorTx(
e.Backend,
e.OnCommitState,
e.Tx,
tx,
)
if err != nil {
return err
}
txID := e.Tx.ID()
// Set up the state if this tx is committed
// Consume the UTXOs
utxo.Consume(e.OnCommitState, tx.Ins)
// Produce the UTXOs
utxo.Produce(e.OnCommitState, txID, tx.Outs)
newStaker := state.NewPendingStaker(txID, tx)
e.OnCommitState.PutPendingValidator(newStaker)
// Set up the state if this tx is aborted
// Consume the UTXOs
utxo.Consume(e.OnAbortState, tx.Ins)
// Produce the UTXOs
utxo.Produce(e.OnAbortState, txID, onAbortOuts)
e.PrefersCommit = tx.StartTime().After(e.Clk.Time())
return nil
}
func (e *ProposalTxExecutor) AddSubnetValidatorTx(tx *txs.AddSubnetValidatorTx) error {
// AddSubnetValidatorTx is a proposal transaction until the Blueberry fork
// activation. Following the activation, AddSubnetValidatorTxs must be
// issued into StandardBlocks.
currentTimestamp := e.OnCommitState.GetTimestamp()
if e.Config.IsBlueberryActivated(currentTimestamp) {
return fmt.Errorf(
"%w: timestamp (%s) >= Blueberry fork time (%s)",
errProposedAddStakerTxAfterBlueberry,
currentTimestamp,
e.Config.BlueberryTime,
)
}
if err := verifyAddSubnetValidatorTx(
e.Backend,
e.OnCommitState,
e.Tx,
tx,
); err != nil {
return err
}
txID := e.Tx.ID()
// Set up the state if this tx is committed
// Consume the UTXOs
utxo.Consume(e.OnCommitState, tx.Ins)
// Produce the UTXOs
utxo.Produce(e.OnCommitState, txID, tx.Outs)
newStaker := state.NewPendingStaker(txID, tx)
e.OnCommitState.PutPendingValidator(newStaker)
// Set up the state if this tx is aborted
// Consume the UTXOs
utxo.Consume(e.OnAbortState, tx.Ins)
// Produce the UTXOs
utxo.Produce(e.OnAbortState, txID, tx.Outs)
e.PrefersCommit = tx.StartTime().After(e.Clk.Time())
return nil
}
func (e *ProposalTxExecutor) AddDelegatorTx(tx *txs.AddDelegatorTx) error {
// AddDelegatorTx is a proposal transaction until the Blueberry fork
// activation. Following the activation, AddDelegatorTxs must be issued into
// StandardBlocks.
currentTimestamp := e.OnCommitState.GetTimestamp()
if e.Config.IsBlueberryActivated(currentTimestamp) {
return fmt.Errorf(
"%w: timestamp (%s) >= Blueberry fork time (%s)",
errProposedAddStakerTxAfterBlueberry,
currentTimestamp,
e.Config.BlueberryTime,
)
}
onAbortOuts, err := verifyAddDelegatorTx(
e.Backend,
e.OnCommitState,
e.Tx,
tx,
)
if err != nil {
return err
}
txID := e.Tx.ID()
// Set up the state if this tx is committed
// Consume the UTXOs
utxo.Consume(e.OnCommitState, tx.Ins)
// Produce the UTXOs
utxo.Produce(e.OnCommitState, txID, tx.Outs)
newStaker := state.NewPendingStaker(txID, tx)
e.OnCommitState.PutPendingDelegator(newStaker)
// Set up the state if this tx is aborted
// Consume the UTXOs
utxo.Consume(e.OnAbortState, tx.Ins)
// Produce the UTXOs
utxo.Produce(e.OnAbortState, txID, onAbortOuts)
e.PrefersCommit = tx.StartTime().After(e.Clk.Time())
return nil
}
func (e *ProposalTxExecutor) AdvanceTimeTx(tx *txs.AdvanceTimeTx) error {
switch {
case tx == nil:
return txs.ErrNilTx
case len(e.Tx.Creds) != 0:
return errWrongNumberOfCredentials
}
// Validate [newChainTime]
newChainTime := tx.Timestamp()
if e.Config.IsBlueberryActivated(newChainTime) {
return fmt.Errorf(
"%w: proposed timestamp (%s) >= Blueberry fork time (%s)",
errAdvanceTimeTxIssuedAfterBlueberry,
newChainTime,
e.Config.BlueberryTime,
)
}
parentChainTime := e.OnCommitState.GetTimestamp()
if !newChainTime.After(parentChainTime) {
return fmt.Errorf(
"%w, proposed timestamp (%s), chain time (%s)",
errChildBlockNotAfterParent,
parentChainTime,
parentChainTime,
)
}
// Only allow timestamp to move forward as far as the time of next staker
// set change time
nextStakerChangeTime, err := GetNextStakerChangeTime(e.OnCommitState)
if err != nil {
return err
}
now := e.Clk.Time()
if err := VerifyNewChainTime(
newChainTime,
nextStakerChangeTime,
now,
); err != nil {
return err
}
changes, err := AdvanceTimeTo(e.Backend, e.OnCommitState, newChainTime)
if err != nil {
return err
}
// Update the state if this tx is committed
e.OnCommitState.SetTimestamp(newChainTime)
changes.Apply(e.OnCommitState)
e.PrefersCommit = !newChainTime.After(now.Add(SyncBound))
// Note that state doesn't change if this proposal is aborted
return nil
}
func (e *ProposalTxExecutor) RewardValidatorTx(tx *txs.RewardValidatorTx) error {
switch {
case tx == nil:
return txs.ErrNilTx
case tx.TxID == ids.Empty:
return errInvalidID
case len(e.Tx.Creds) != 0:
return errWrongNumberOfCredentials
}
currentStakerIterator, err := e.OnCommitState.GetCurrentStakerIterator()
if err != nil {
return err
}
if !currentStakerIterator.Next() {
return fmt.Errorf("failed to get next staker to remove: %w", database.ErrNotFound)
}
stakerToRemove := currentStakerIterator.Value()
currentStakerIterator.Release()
if stakerToRemove.TxID != tx.TxID {
return fmt.Errorf(
"attempting to remove TxID: %s. Should be removing %s",
tx.TxID,
stakerToRemove.TxID,
)
}
// Verify that the chain's timestamp is the validator's end time
currentChainTime := e.OnCommitState.GetTimestamp()
if !stakerToRemove.EndTime.Equal(currentChainTime) {
return fmt.Errorf(
"attempting to remove TxID: %s before their end time %s",
tx.TxID,
stakerToRemove.EndTime,
)
}
primaryNetworkValidator, err := e.OnCommitState.GetCurrentValidator(
constants.PrimaryNetworkID,
stakerToRemove.NodeID,
)
if err != nil {
// This should never error because the staker set is in memory and
// primary network validators are removed last.
return err
}
stakerTx, _, err := e.OnCommitState.GetTx(stakerToRemove.TxID)
if err != nil {
return fmt.Errorf("failed to get next removed staker tx: %w", err)
}
switch uStakerTx := stakerTx.Unsigned.(type) {
case txs.ValidatorTx:
e.OnCommitState.DeleteCurrentValidator(stakerToRemove)
e.OnAbortState.DeleteCurrentValidator(stakerToRemove)
stake := uStakerTx.Stake()
outputs := uStakerTx.Outputs()
// Invariant: The staked asset must be equal to the reward asset.
stakeAsset := stake[0].Asset
// Refund the stake here
for i, out := range stake {
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: tx.TxID,
OutputIndex: uint32(len(outputs) + i),
},
Asset: out.Asset,
Out: out.Output(),
}
e.OnCommitState.AddUTXO(utxo)
e.OnAbortState.AddUTXO(utxo)
}
// Provide the reward here
if stakerToRemove.PotentialReward > 0 {
validationRewardsOwner := uStakerTx.ValidationRewardsOwner()
outIntf, err := e.Fx.CreateOutput(stakerToRemove.PotentialReward, validationRewardsOwner)
if err != nil {
return fmt.Errorf("failed to create output: %w", err)
}
out, ok := outIntf.(verify.State)
if !ok {
return errInvalidState
}
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: tx.TxID,
OutputIndex: uint32(len(outputs) + len(stake)),
},
Asset: stakeAsset,
Out: out,
}
e.OnCommitState.AddUTXO(utxo)
e.OnCommitState.AddRewardUTXO(tx.TxID, utxo)
}
// Invariant: A [txs.DelegatorTx] does not also implement the
// [txs.ValidatorTx] interface.
case txs.DelegatorTx:
e.OnCommitState.DeleteCurrentDelegator(stakerToRemove)
e.OnAbortState.DeleteCurrentDelegator(stakerToRemove)
stake := uStakerTx.Stake()
outputs := uStakerTx.Outputs()
stakeAsset := stake[0].Asset
// Refund the stake here
for i, out := range stake {
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: tx.TxID,
OutputIndex: uint32(len(outputs) + i),
},
Asset: out.Asset,
Out: out.Output(),
}
e.OnCommitState.AddUTXO(utxo)
e.OnAbortState.AddUTXO(utxo)
}
// We're removing a delegator, so we need to fetch the validator they
// are delegated to.
vdrStaker, err := e.OnCommitState.GetCurrentValidator(
stakerToRemove.SubnetID,
stakerToRemove.NodeID,
)
if err != nil {
return fmt.Errorf(
"failed to get whether %s is a validator: %w",
stakerToRemove.NodeID,
err,
)
}
vdrTxIntf, _, err := e.OnCommitState.GetTx(vdrStaker.TxID)
if err != nil {
return fmt.Errorf(
"failed to get whether %s is a validator: %w",
stakerToRemove.NodeID,
err,
)
}
// Invariant: Delegators must only be able to reference validator
// transactions that implement [txs.ValidatorTx]. All
// validator transactions implement this interface except the
// AddSubnetValidatorTx.
vdrTx, ok := vdrTxIntf.Unsigned.(txs.ValidatorTx)
if !ok {
return errWrongTxType
}
// Calculate split of reward between delegator/delegatee
// The delegator gives stake to the validatee
validatorShares := vdrTx.Shares()
delegatorShares := reward.PercentDenominator - uint64(validatorShares) // parentTx.Shares <= reward.PercentDenominator so no underflow
delegatorReward := delegatorShares * (stakerToRemove.PotentialReward / reward.PercentDenominator) // delegatorShares <= reward.PercentDenominator so no overflow
// Delay rounding as long as possible for small numbers
if optimisticReward, err := math.Mul64(delegatorShares, stakerToRemove.PotentialReward); err == nil {
delegatorReward = optimisticReward / reward.PercentDenominator
}
delegateeReward := stakerToRemove.PotentialReward - delegatorReward // delegatorReward <= reward so no underflow
offset := 0
// Reward the delegator here
if delegatorReward > 0 {
rewardsOwner := uStakerTx.RewardsOwner()
outIntf, err := e.Fx.CreateOutput(delegatorReward, rewardsOwner)
if err != nil {
return fmt.Errorf("failed to create output: %w", err)
}
out, ok := outIntf.(verify.State)
if !ok {
return errInvalidState
}
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: tx.TxID,
OutputIndex: uint32(len(outputs) + len(stake)),
},
Asset: stakeAsset,
Out: out,
}
e.OnCommitState.AddUTXO(utxo)
e.OnCommitState.AddRewardUTXO(tx.TxID, utxo)
offset++
}
// Reward the delegatee here
if delegateeReward > 0 {
delegationRewardsOwner := vdrTx.DelegationRewardsOwner()
outIntf, err := e.Fx.CreateOutput(delegateeReward, delegationRewardsOwner)
if err != nil {
return fmt.Errorf("failed to create output: %w", err)
}
out, ok := outIntf.(verify.State)
if !ok {
return errInvalidState
}
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: tx.TxID,
OutputIndex: uint32(len(outputs) + len(stake) + offset),
},
Asset: stakeAsset,
Out: out,
}
e.OnCommitState.AddUTXO(utxo)
e.OnCommitState.AddRewardUTXO(tx.TxID, utxo)
}
default:
// Invariant: Permissioned stakers are removed by the advancement of
// time and the current chain timestamp is == this staker's
// EndTime. This means only permissionless stakers should be
// left in the staker set.
return errShouldBePermissionlessStaker
}
// If the reward is aborted, then the current supply should be decreased.
currentSupply, err := e.OnAbortState.GetCurrentSupply(stakerToRemove.SubnetID)
if err != nil {
return err
}
newSupply, err := math.Sub64(currentSupply, stakerToRemove.PotentialReward)
if err != nil {
return err
}
e.OnAbortState.SetCurrentSupply(stakerToRemove.SubnetID, newSupply)
var expectedUptimePercentage float64
if stakerToRemove.SubnetID != constants.PrimaryNetworkID {
transformSubnetIntf, err := e.OnCommitState.GetSubnetTransformation(stakerToRemove.SubnetID)
if err != nil {
return err
}
transformSubnet, ok := transformSubnetIntf.Unsigned.(*txs.TransformSubnetTx)
if !ok {
return errIsNotTransformSubnetTx
}
expectedUptimePercentage = float64(transformSubnet.UptimeRequirement) / reward.PercentDenominator
} else {
expectedUptimePercentage = e.Config.UptimePercentage
}
// TODO: calculate subnet uptimes
uptime, err := e.Uptimes.CalculateUptimePercentFrom(
primaryNetworkValidator.NodeID,
primaryNetworkValidator.StartTime,
)
if err != nil {
return fmt.Errorf("failed to calculate uptime: %w", err)
}
e.PrefersCommit = uptime >= expectedUptimePercentage
return nil
}
// GetNextStakerChangeTime returns the next time a staker will be either added
// or removed to/from the current validator set.
func GetNextStakerChangeTime(state state.Chain) (time.Time, error) {
currentStakerIterator, err := state.GetCurrentStakerIterator()
if err != nil {
return time.Time{}, err
}
defer currentStakerIterator.Release()
pendingStakerIterator, err := state.GetPendingStakerIterator()
if err != nil {
return time.Time{}, err
}
defer pendingStakerIterator.Release()
hasCurrentStaker := currentStakerIterator.Next()
hasPendingStaker := pendingStakerIterator.Next()
switch {
case hasCurrentStaker && hasPendingStaker:
nextCurrentTime := currentStakerIterator.Value().NextTime
nextPendingTime := pendingStakerIterator.Value().NextTime
if nextCurrentTime.Before(nextPendingTime) {
return nextCurrentTime, nil
}
return nextPendingTime, nil
case hasCurrentStaker:
return currentStakerIterator.Value().NextTime, nil
case hasPendingStaker:
return pendingStakerIterator.Value().NextTime, nil
default:
return time.Time{}, database.ErrNotFound
}
}
// GetValidator returns information about the given validator, which may be a
// current validator or pending validator.
func GetValidator(state state.Chain, subnetID ids.ID, nodeID ids.NodeID) (*state.Staker, error) {
validator, err := state.GetCurrentValidator(subnetID, nodeID)
if err == nil {
// This node is currently validating the subnet.
return validator, nil
}
if err != database.ErrNotFound {
// Unexpected error occurred.
return nil, err
}
return state.GetPendingValidator(subnetID, nodeID)
}
// canDelegate returns true if [delegator] can be added as a delegator of
// [validator].
//
// A [delegator] can be added if:
// - [delegator]'s start time is not before [validator]'s start time
// - [delegator]'s end time is not after [validator]'s end time
// - the maximum total weight on [validator] will not exceed [weightLimit]
func canDelegate(
state state.Chain,
validator *state.Staker,
weightLimit uint64,
delegator *state.Staker,
) (bool, error) {
if delegator.StartTime.Before(validator.StartTime) {
return false, nil
}
if delegator.EndTime.After(validator.EndTime) {
return false, nil
}
maxWeight, err := GetMaxWeight(state, validator, delegator.StartTime, delegator.EndTime)
if err != nil {
return false, err
}
newMaxWeight, err := math.Add64(maxWeight, delegator.Weight)
if err != nil {
return false, err
}
return newMaxWeight <= weightLimit, nil
}
// GetMaxWeight returns the maximum total weight of the [validator], including
// its own weight, between [startTime] and [endTime].
// The weight changes are applied in the order they will be applied as chain
// time advances.
// Invariant:
// - [validator.StartTime] <= [startTime] < [endTime] <= [validator.EndTime]
func GetMaxWeight(
chainState state.Chain,
validator *state.Staker,
startTime time.Time,
endTime time.Time,
) (uint64, error) {
currentDelegatorIterator, err := chainState.GetCurrentDelegatorIterator(validator.SubnetID, validator.NodeID)
if err != nil {
return 0, err
}
// TODO: We can optimize this by moving the current total weight to be
// stored in the validator state.
//
// Calculate the current total weight on this validator, including the
// weight of the actual validator and the sum of the weights of all of the
// currently active delegators.
currentWeight := validator.Weight
for currentDelegatorIterator.Next() {
currentDelegator := currentDelegatorIterator.Value()
currentWeight, err = math.Add64(currentWeight, currentDelegator.Weight)
if err != nil {
currentDelegatorIterator.Release()
return 0, err
}
}
currentDelegatorIterator.Release()
currentDelegatorIterator, err = chainState.GetCurrentDelegatorIterator(validator.SubnetID, validator.NodeID)
if err != nil {
return 0, err
}
pendingDelegatorIterator, err := chainState.GetPendingDelegatorIterator(validator.SubnetID, validator.NodeID)
if err != nil {
currentDelegatorIterator.Release()
return 0, err
}
delegatorChangesIterator := state.NewStakerDiffIterator(currentDelegatorIterator, pendingDelegatorIterator)
defer delegatorChangesIterator.Release()
// Iterate over the future stake weight changes and calculate the maximum
// total weight on the validator, only including the points in the time
// range [startTime, endTime].
var currentMax uint64
for delegatorChangesIterator.Next() {
delegator, isAdded := delegatorChangesIterator.Value()
// [delegator.NextTime] > [endTime]
if delegator.NextTime.After(endTime) {
// This delegation change (and all following changes) occurs after
// [endTime]. Since we're calculating the max amount staked in
// [startTime, endTime], we can stop.
break
}
// [delegator.NextTime] >= [startTime]
if !delegator.NextTime.Before(startTime) {
// We have advanced time to be at the inside of the delegation
// window. Make sure that the max weight is updated accordingly.
currentMax = math.Max64(currentMax, currentWeight)
}
var op func(uint64, uint64) (uint64, error)
if isAdded {
op = math.Add64
} else {
op = math.Sub64
}
currentWeight, err = op(currentWeight, delegator.Weight)
if err != nil {
return 0, err
}
}
// Because we assume [startTime] < [endTime], we have advanced time to
// be at the end of the delegation window. Make sure that the max weight is
// updated accordingly.
return math.Max64(currentMax, currentWeight), nil
}