-
Notifications
You must be signed in to change notification settings - Fork 1
/
staker_tx_verification_helpers.go
264 lines (238 loc) · 8.43 KB
/
staker_tx_verification_helpers.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
// Copyright (C) 2019-2023, Lux Partners Limited. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"time"
"github.com/luxdefi/node/database"
"github.com/luxdefi/node/ids"
"github.com/luxdefi/node/utils/constants"
"github.com/luxdefi/node/utils/math"
"github.com/luxdefi/node/vms/platformvm/state"
"github.com/luxdefi/node/vms/platformvm/txs"
)
type addValidatorRules struct {
assetID ids.ID
minValidatorStake uint64
maxValidatorStake uint64
minStakeDuration time.Duration
maxStakeDuration time.Duration
minDelegationFee uint32
}
func getValidatorRules(
backend *Backend,
chainState state.Chain,
subnetID ids.ID,
) (*addValidatorRules, error) {
if subnetID == constants.PrimaryNetworkID {
return &addValidatorRules{
assetID: backend.Ctx.LUXAssetID,
minValidatorStake: backend.Config.MinValidatorStake,
maxValidatorStake: backend.Config.MaxValidatorStake,
minStakeDuration: backend.Config.MinStakeDuration,
maxStakeDuration: backend.Config.MaxStakeDuration,
minDelegationFee: backend.Config.MinDelegationFee,
}, nil
}
transformSubnet, err := GetTransformSubnetTx(chainState, subnetID)
if err != nil {
return nil, err
}
return &addValidatorRules{
assetID: transformSubnet.AssetID,
minValidatorStake: transformSubnet.MinValidatorStake,
maxValidatorStake: transformSubnet.MaxValidatorStake,
minStakeDuration: time.Duration(transformSubnet.MinStakeDuration) * time.Second,
maxStakeDuration: time.Duration(transformSubnet.MaxStakeDuration) * time.Second,
minDelegationFee: transformSubnet.MinDelegationFee,
}, nil
}
type addDelegatorRules struct {
assetID ids.ID
minDelegatorStake uint64
maxValidatorStake uint64
minStakeDuration time.Duration
maxStakeDuration time.Duration
maxValidatorWeightFactor byte
}
func getDelegatorRules(
backend *Backend,
chainState state.Chain,
subnetID ids.ID,
) (*addDelegatorRules, error) {
if subnetID == constants.PrimaryNetworkID {
return &addDelegatorRules{
assetID: backend.Ctx.LUXAssetID,
minDelegatorStake: backend.Config.MinDelegatorStake,
maxValidatorStake: backend.Config.MaxValidatorStake,
minStakeDuration: backend.Config.MinStakeDuration,
maxStakeDuration: backend.Config.MaxStakeDuration,
maxValidatorWeightFactor: MaxValidatorWeightFactor,
}, nil
}
transformSubnet, err := GetTransformSubnetTx(chainState, subnetID)
if err != nil {
return nil, err
}
return &addDelegatorRules{
assetID: transformSubnet.AssetID,
minDelegatorStake: transformSubnet.MinDelegatorStake,
maxValidatorStake: transformSubnet.MaxValidatorStake,
minStakeDuration: time.Duration(transformSubnet.MinStakeDuration) * time.Second,
maxStakeDuration: time.Duration(transformSubnet.MaxStakeDuration) * time.Second,
maxValidatorWeightFactor: transformSubnet.MaxValidatorWeightFactor,
}, 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)
}
// overDelegated returns true if [validator] will be overdelegated when adding [delegator].
//
// A [validator] would become overdelegated if:
// - the maximum total weight on [validator] exceeds [weightLimit]
func overDelegated(
state state.Chain,
validator *state.Staker,
weightLimit uint64,
delegator *state.Staker,
) (bool, error) {
maxWeight, err := GetMaxWeight(state, validator, delegator.StartTime, delegator.EndTime)
if err != nil {
return true, err
}
newMaxWeight, err := math.Add64(maxWeight, delegator.Weight)
if err != nil {
return true, 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.Max(currentMax, currentWeight)
}
var op func(uint64, uint64) (uint64, error)
if isAdded {
op = math.Add64
} else {
op = math.Sub[uint64]
}
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.Max(currentMax, currentWeight), nil
}
func GetTransformSubnetTx(chain state.Chain, subnetID ids.ID) (*txs.TransformSubnetTx, error) {
transformSubnetIntf, err := chain.GetSubnetTransformation(subnetID)
if err != nil {
return nil, err
}
transformSubnet, ok := transformSubnetIntf.Unsigned.(*txs.TransformSubnetTx)
if !ok {
return nil, ErrIsNotTransformSubnetTx
}
return transformSubnet, nil
}