-
Notifications
You must be signed in to change notification settings - Fork 669
/
state_changes.go
192 lines (167 loc) · 6.17 KB
/
state_changes.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
// 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/utils/math"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
)
var (
errChildBlockAfterStakerChangeTime = errors.New("proposed timestamp later than next staker change time")
errChildBlockBeyondSyncBound = errors.New("proposed timestamp is too far in the future relative to local time")
)
// VerifyNewChainTime returns nil if the [newChainTime] is a valid chain time
// given the wall clock time ([now]) and when the next staking set change occurs
// ([nextStakerChangeTime]).
// Requires:
// - [newChainTime] <= [nextStakerChangeTime]: so that no staking set changes
// are skipped.
// - [newChainTime] <= [now] + [SyncBound]: to ensure chain time approximates
// "real" time.
func VerifyNewChainTime(
newChainTime,
nextStakerChangeTime,
now time.Time,
) error {
// Only allow timestamp to move as far forward as the time of the next
// staker set change
if newChainTime.After(nextStakerChangeTime) {
return fmt.Errorf(
"%w, proposed timestamp (%s), next staker change time (%s)",
errChildBlockAfterStakerChangeTime,
newChainTime,
nextStakerChangeTime,
)
}
// Only allow timestamp to reasonably far forward
maxNewChainTime := now.Add(SyncBound)
if newChainTime.After(maxNewChainTime) {
return fmt.Errorf(
"%w, proposed time (%s), local time (%s)",
errChildBlockBeyondSyncBound,
newChainTime,
now,
)
}
return nil
}
type StateChanges interface {
Apply(onAccept state.Diff)
Len() int
}
type stateChanges struct {
supply uint64
currentValidatorsToAdd []*state.Staker
currentDelegatorsToAdd []*state.Staker
pendingValidatorsToRemove []*state.Staker
pendingDelegatorsToRemove []*state.Staker
currentValidatorsToRemove []*state.Staker
}
func (s *stateChanges) Apply(stateDiff state.Diff) {
stateDiff.SetCurrentSupply(s.supply)
for _, currentValidatorToAdd := range s.currentValidatorsToAdd {
stateDiff.PutCurrentValidator(currentValidatorToAdd)
}
for _, pendingValidatorToRemove := range s.pendingValidatorsToRemove {
stateDiff.DeletePendingValidator(pendingValidatorToRemove)
}
for _, currentDelegatorToAdd := range s.currentDelegatorsToAdd {
stateDiff.PutCurrentDelegator(currentDelegatorToAdd)
}
for _, pendingDelegatorToRemove := range s.pendingDelegatorsToRemove {
stateDiff.DeletePendingDelegator(pendingDelegatorToRemove)
}
for _, currentValidatorToRemove := range s.currentValidatorsToRemove {
stateDiff.DeleteCurrentValidator(currentValidatorToRemove)
}
}
func (s *stateChanges) Len() int {
return len(s.currentValidatorsToAdd) + len(s.currentDelegatorsToAdd) +
len(s.pendingValidatorsToRemove) + len(s.pendingDelegatorsToRemove) +
len(s.currentValidatorsToRemove)
}
// AdvanceTimeTo does not modify [parentState].
// Instead it returns all the StateChanges caused by advancing the chain time to
// the [newChainTime].
func AdvanceTimeTo(parentState state.Chain, newChainTime time.Time, rewards reward.Calculator) (StateChanges, error) {
pendingStakerIterator, err := parentState.GetPendingStakerIterator()
if err != nil {
return nil, err
}
changes := &stateChanges{
supply: parentState.GetCurrentSupply(),
}
// Add to the staker set any pending stakers whose start time is at or
// before the new timestamp
for pendingStakerIterator.Next() {
stakerToRemove := pendingStakerIterator.Value()
if stakerToRemove.StartTime.After(newChainTime) {
break
}
stakerToAdd := *stakerToRemove
stakerToAdd.NextTime = stakerToRemove.EndTime
stakerToAdd.Priority = state.PendingToCurrentPriorities[stakerToRemove.Priority]
switch stakerToRemove.Priority {
case state.PrimaryNetworkDelegatorPendingPriority:
potentialReward := rewards.Calculate(
stakerToRemove.EndTime.Sub(stakerToRemove.StartTime),
stakerToRemove.Weight,
changes.supply,
)
changes.supply, err = math.Add64(changes.supply, potentialReward)
if err != nil {
pendingStakerIterator.Release()
return nil, err
}
stakerToAdd.PotentialReward = potentialReward
changes.currentDelegatorsToAdd = append(changes.currentDelegatorsToAdd, &stakerToAdd)
changes.pendingDelegatorsToRemove = append(changes.pendingDelegatorsToRemove, stakerToRemove)
case state.PrimaryNetworkValidatorPendingPriority:
potentialReward := rewards.Calculate(
stakerToRemove.EndTime.Sub(stakerToRemove.StartTime),
stakerToRemove.Weight,
changes.supply,
)
changes.supply, err = math.Add64(changes.supply, potentialReward)
if err != nil {
pendingStakerIterator.Release()
return nil, err
}
stakerToAdd.PotentialReward = potentialReward
changes.currentValidatorsToAdd = append(changes.currentValidatorsToAdd, &stakerToAdd)
changes.pendingValidatorsToRemove = append(changes.pendingValidatorsToRemove, stakerToRemove)
case state.SubnetPermissionedValidatorPendingPriority:
// We require that the [txTimestamp] <= [nextStakerChangeTime].
// Additionally, the minimum stake duration is > 0. This means we
// know that the staker we are adding here should never be attempted
// to be removed in the following loop.
changes.currentValidatorsToAdd = append(changes.currentValidatorsToAdd, &stakerToAdd)
changes.pendingValidatorsToRemove = append(changes.pendingValidatorsToRemove, stakerToRemove)
default:
pendingStakerIterator.Release()
return nil, fmt.Errorf("expected staker priority got %d", stakerToRemove.Priority)
}
}
pendingStakerIterator.Release()
currentStakerIterator, err := parentState.GetCurrentStakerIterator()
if err != nil {
return nil, err
}
for currentStakerIterator.Next() {
stakerToRemove := currentStakerIterator.Value()
if stakerToRemove.EndTime.After(newChainTime) {
break
}
if stakerToRemove.Priority != state.SubnetPermissionedValidatorCurrentPriority {
// Permissionless stakers are removed by the RewardValidatorTx, not
// an AdvanceTimeTx.
break
}
changes.currentValidatorsToRemove = append(changes.currentValidatorsToRemove, stakerToRemove)
}
currentStakerIterator.Release()
return changes, nil
}