forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
state_changes.go
235 lines (203 loc) · 7.73 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
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
// 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/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils/constants"
"github.com/MetalBlockchain/metalgo/vms/platformvm/reward"
"github.com/MetalBlockchain/metalgo/vms/platformvm/state"
"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
)
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 {
updatedSupplies map[ids.ID]uint64
currentValidatorsToAdd []*state.Staker
currentDelegatorsToAdd []*state.Staker
pendingValidatorsToRemove []*state.Staker
pendingDelegatorsToRemove []*state.Staker
currentValidatorsToRemove []*state.Staker
}
func (s *stateChanges) Apply(stateDiff state.Diff) {
for subnetID, supply := range s.updatedSupplies {
stateDiff.SetCurrentSupply(subnetID, 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(
backend *Backend,
parentState state.Chain,
newChainTime time.Time,
) (StateChanges, error) {
pendingStakerIterator, err := parentState.GetPendingStakerIterator()
if err != nil {
return nil, err
}
defer pendingStakerIterator.Release()
changes := &stateChanges{
updatedSupplies: make(map[ids.ID]uint64),
}
// Add to the staker set any pending stakers whose start time is at or
// before the new timestamp
// Note: we process pending stakers ready to be promoted to current ones and
// then we process current stakers to be demoted out of stakers set. It is
// guaranteed that no promoted stakers would be demoted immediately. A
// failure of this invariant would cause a staker to be added to
// StateChanges and be persisted among current stakers even if it already
// expired. The following invariants ensure this does not happens:
// Invariant: minimum stake duration is > 0, so staker.StartTime != staker.EndTime.
// Invariant: [newChainTime] does not skip stakers set change times.
for pendingStakerIterator.Next() {
stakerToRemove := pendingStakerIterator.Value()
if stakerToRemove.StartTime.After(newChainTime) {
break
}
stakerToAdd := *stakerToRemove
stakerToAdd.NextTime = stakerToRemove.EndTime
stakerToAdd.Priority = txs.PendingToCurrentPriorities[stakerToRemove.Priority]
if stakerToRemove.Priority == txs.SubnetPermissionedValidatorPendingPriority {
changes.currentValidatorsToAdd = append(changes.currentValidatorsToAdd, &stakerToAdd)
changes.pendingValidatorsToRemove = append(changes.pendingValidatorsToRemove, stakerToRemove)
continue
}
supply, ok := changes.updatedSupplies[stakerToRemove.SubnetID]
if !ok {
supply, err = parentState.GetCurrentSupply(stakerToRemove.SubnetID)
if err != nil {
return nil, err
}
}
rewards, err := GetRewardsCalculator(backend, parentState, stakerToRemove.SubnetID)
if err != nil {
return nil, err
}
potentialReward := rewards.Calculate(
stakerToRemove.EndTime.Sub(stakerToRemove.StartTime),
stakerToRemove.Weight,
supply,
)
stakerToAdd.PotentialReward = potentialReward
// Invariant: [rewards.Calculate] can never return a [potentialReward]
// such that [supply + potentialReward > maximumSupply].
changes.updatedSupplies[stakerToRemove.SubnetID] = supply + potentialReward
switch stakerToRemove.Priority {
case txs.PrimaryNetworkValidatorPendingPriority, txs.SubnetPermissionlessValidatorPendingPriority:
changes.currentValidatorsToAdd = append(changes.currentValidatorsToAdd, &stakerToAdd)
changes.pendingValidatorsToRemove = append(changes.pendingValidatorsToRemove, stakerToRemove)
case txs.PrimaryNetworkDelegatorApricotPendingPriority, txs.PrimaryNetworkDelegatorBanffPendingPriority, txs.SubnetPermissionlessDelegatorPendingPriority:
changes.currentDelegatorsToAdd = append(changes.currentDelegatorsToAdd, &stakerToAdd)
changes.pendingDelegatorsToRemove = append(changes.pendingDelegatorsToRemove, stakerToRemove)
default:
return nil, fmt.Errorf("expected staker priority got %d", stakerToRemove.Priority)
}
}
currentStakerIterator, err := parentState.GetCurrentStakerIterator()
if err != nil {
return nil, err
}
defer currentStakerIterator.Release()
for currentStakerIterator.Next() {
stakerToRemove := currentStakerIterator.Value()
if stakerToRemove.EndTime.After(newChainTime) {
break
}
// Invariant: Permissioned stakers are encountered first for a given
// timestamp because their priority is the smallest.
if stakerToRemove.Priority != txs.SubnetPermissionedValidatorCurrentPriority {
// Permissionless stakers are removed by the RewardValidatorTx, not
// an AdvanceTimeTx.
break
}
changes.currentValidatorsToRemove = append(changes.currentValidatorsToRemove, stakerToRemove)
}
return changes, nil
}
func GetRewardsCalculator(
backend *Backend,
parentState state.Chain,
subnetID ids.ID,
) (reward.Calculator, error) {
if subnetID == constants.PrimaryNetworkID {
return backend.Rewards, nil
}
transformSubnetIntf, err := parentState.GetSubnetTransformation(subnetID)
if err != nil {
return nil, err
}
transformSubnet, ok := transformSubnetIntf.Unsigned.(*txs.TransformSubnetTx)
if !ok {
return nil, errIsNotTransformSubnetTx
}
return reward.NewCalculator(reward.Config{
MaxConsumptionRate: transformSubnet.MaxConsumptionRate,
MinConsumptionRate: transformSubnet.MinConsumptionRate,
MintingPeriod: backend.Config.RewardConfig.MintingPeriod,
SupplyCap: transformSubnet.MaximumSupply,
}), nil
}