-
Notifications
You must be signed in to change notification settings - Fork 660
/
state_changes.go
221 lines (189 loc) · 6.67 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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"errors"
"fmt"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/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
}
func NextBlockTime(state state.Chain, clk *mockable.Clock) (time.Time, bool, error) {
var (
timestamp = clk.Time()
parentTime = state.GetTimestamp()
)
if parentTime.After(timestamp) {
timestamp = parentTime
}
// [timestamp] = max(now, parentTime)
nextStakerChangeTime, err := GetNextStakerChangeTime(state)
if err != nil {
return time.Time{}, false, fmt.Errorf("failed getting next staker change time: %w", err)
}
// timeWasCapped means that [timestamp] was reduced to [nextStakerChangeTime]
timeWasCapped := !timestamp.Before(nextStakerChangeTime)
if timeWasCapped {
timestamp = nextStakerChangeTime
}
// [timestamp] = min(max(now, parentTime), nextStakerChangeTime)
return timestamp, timeWasCapped, nil
}
// AdvanceTimeTo applies all state changes to [parentState] resulting from
// advancing the chain time to [newChainTime].
// Returns true iff the validator set changed.
func AdvanceTimeTo(
backend *Backend,
parentState state.Chain,
newChainTime time.Time,
) (bool, error) {
// We promote pending stakers to current stakers first and remove
// completed stakers from the current staker set. We assume that any
// promoted staker will not immediately be removed from the current staker
// set. This is guaranteed by the following invariants.
//
// Invariant: MinStakeDuration > 0 => guarantees [StartTime] != [EndTime]
// Invariant: [newChainTime] <= nextStakerChangeTime.
changes, err := state.NewDiffOn(parentState)
if err != nil {
return false, err
}
pendingStakerIterator, err := parentState.GetPendingStakerIterator()
if err != nil {
return false, err
}
defer pendingStakerIterator.Release()
var changed bool
// Promote any pending stakers to current if [StartTime] <= [newChainTime].
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.PutCurrentValidator(&stakerToAdd)
changes.DeletePendingValidator(stakerToRemove)
changed = true
continue
}
supply, err := changes.GetCurrentSupply(stakerToRemove.SubnetID)
if err != nil {
return false, err
}
rewards, err := GetRewardsCalculator(backend, parentState, stakerToRemove.SubnetID)
if err != nil {
return false, 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.SetCurrentSupply(stakerToRemove.SubnetID, supply+potentialReward)
switch stakerToRemove.Priority {
case txs.PrimaryNetworkValidatorPendingPriority, txs.SubnetPermissionlessValidatorPendingPriority:
changes.PutCurrentValidator(&stakerToAdd)
changes.DeletePendingValidator(stakerToRemove)
case txs.PrimaryNetworkDelegatorApricotPendingPriority, txs.PrimaryNetworkDelegatorBanffPendingPriority, txs.SubnetPermissionlessDelegatorPendingPriority:
changes.PutCurrentDelegator(&stakerToAdd)
changes.DeletePendingDelegator(stakerToRemove)
default:
return false, fmt.Errorf("expected staker priority got %d", stakerToRemove.Priority)
}
changed = true
}
// Remove any current stakers whose [EndTime] <= [newChainTime].
currentStakerIterator, err := parentState.GetCurrentStakerIterator()
if err != nil {
return false, 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.DeleteCurrentValidator(stakerToRemove)
changed = true
}
if err := changes.Apply(parentState); err != nil {
return false, err
}
parentState.SetTimestamp(newChainTime)
return changed, nil
}
func GetRewardsCalculator(
backend *Backend,
parentState state.Chain,
subnetID ids.ID,
) (reward.Calculator, error) {
if subnetID == constants.PrimaryNetworkID {
return backend.Rewards, nil
}
transformSubnet, err := GetTransformSubnetTx(parentState, subnetID)
if err != nil {
return nil, err
}
return reward.NewCalculator(reward.Config{
MaxConsumptionRate: transformSubnet.MaxConsumptionRate,
MinConsumptionRate: transformSubnet.MinConsumptionRate,
MintingPeriod: backend.Config.RewardConfig.MintingPeriod,
SupplyCap: transformSubnet.MaximumSupply,
}), nil
}