-
Notifications
You must be signed in to change notification settings - Fork 649
/
add_subnet_validator_tx.go
299 lines (261 loc) · 8.89 KB
/
add_subnet_validator_tx.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"errors"
"fmt"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
pChainValidator "github.com/ava-labs/avalanchego/vms/platformvm/validator"
)
var (
errDSValidatorSubset = errors.New("all subnets' staking period must be a subset of the primary network")
_ UnsignedProposalTx = &UnsignedAddSubnetValidatorTx{}
_ TimedTx = &UnsignedAddSubnetValidatorTx{}
)
// UnsignedAddSubnetValidatorTx is an unsigned addSubnetValidatorTx
type UnsignedAddSubnetValidatorTx struct {
// Metadata, inputs and outputs
BaseTx `serialize:"true"`
// The validator
Validator pChainValidator.SubnetValidator `serialize:"true" json:"validator"`
// Auth that will be allowing this validator into the network
SubnetAuth verify.Verifiable `serialize:"true" json:"subnetAuthorization"`
}
// StartTime of this validator
func (tx *UnsignedAddSubnetValidatorTx) StartTime() time.Time {
return tx.Validator.StartTime()
}
// EndTime of this validator
func (tx *UnsignedAddSubnetValidatorTx) EndTime() time.Time {
return tx.Validator.EndTime()
}
// Weight of this validator
func (tx *UnsignedAddSubnetValidatorTx) Weight() uint64 {
return tx.Validator.Weight()
}
// SyntacticVerify returns nil iff [tx] is valid
func (tx *UnsignedAddSubnetValidatorTx) SyntacticVerify(ctx *snow.Context) error {
switch {
case tx == nil:
return errNilTx
case tx.syntacticallyVerified: // already passed syntactic verification
return nil
}
if err := tx.BaseTx.SyntacticVerify(ctx); err != nil {
return err
}
if err := verify.All(&tx.Validator, tx.SubnetAuth); err != nil {
return err
}
// cache that this is valid
tx.syntacticallyVerified = true
return nil
}
// Attempts to verify this transaction with the provided state.
func (tx *UnsignedAddSubnetValidatorTx) SemanticVerify(vm *VM, parentState MutableState, stx *Tx) error {
startTime := tx.StartTime()
maxLocalStartTime := vm.clock.Time().Add(maxFutureStartTime)
if startTime.After(maxLocalStartTime) {
return errFutureStakeTime
}
_, _, err := tx.Execute(vm, parentState, stx)
// We ignore [errFutureStakeTime] here because an advanceTimeTx will be
// issued before this transaction is issued.
if errors.Is(err, errFutureStakeTime) {
return nil
}
return err
}
// Execute this transaction.
func (tx *UnsignedAddSubnetValidatorTx) Execute(
vm *VM,
parentState MutableState,
stx *Tx,
) (
VersionedState,
VersionedState,
error,
) {
// Verify the tx is well-formed
if err := tx.SyntacticVerify(vm.ctx); err != nil {
return nil, nil, err
}
duration := tx.Validator.Duration()
switch {
case duration < vm.MinStakeDuration: // Ensure staking length is not too short
return nil, nil, errStakeTooShort
case duration > vm.MaxStakeDuration: // Ensure staking length is not too long
return nil, nil, errStakeTooLong
case len(stx.Creds) == 0:
return nil, nil, errWrongNumberOfCredentials
}
currentStakers := parentState.CurrentStakerChainState()
pendingStakers := parentState.PendingStakerChainState()
if vm.bootstrapped.GetValue() {
currentTimestamp := parentState.GetTimestamp()
// Ensure the proposed validator starts after the current timestamp
validatorStartTime := tx.StartTime()
if !currentTimestamp.Before(validatorStartTime) {
return nil, nil, fmt.Errorf(
"validator's start time (%s) is at or after current chain timestamp (%s)",
currentTimestamp,
validatorStartTime,
)
}
currentValidator, err := currentStakers.GetValidator(tx.Validator.NodeID)
if err != nil && err != database.ErrNotFound {
return nil, nil, fmt.Errorf(
"failed to find whether %s is a validator: %w",
tx.Validator.NodeID,
err,
)
}
var vdrTx *UnsignedAddValidatorTx
if err == nil {
// This validator is attempting to validate with a currently
// validing node.
vdrTx = currentValidator.AddValidatorTx()
// Ensure that this transaction isn't a duplicate add validator tx.
subnets := currentValidator.SubnetValidators()
if _, validates := subnets[tx.Validator.Subnet]; validates {
return nil, nil, fmt.Errorf(
"already validating subnet %s",
tx.Validator.Subnet,
)
}
} else {
// This validator is attempting to validate with a node that hasn't
// started validating yet.
vdrTx, err = pendingStakers.GetValidatorTx(tx.Validator.NodeID)
if err != nil {
if err == database.ErrNotFound {
return nil, nil, errDSValidatorSubset
}
return nil, nil, fmt.Errorf(
"failed to find whether %s is a validator: %w",
tx.Validator.NodeID,
err,
)
}
}
// Ensure that the period this validator validates the specified subnet
// is a subset of the time they validate the primary network.
if !tx.Validator.BoundedBy(vdrTx.StartTime(), vdrTx.EndTime()) {
return nil, nil, errDSValidatorSubset
}
// Ensure that this transaction isn't a duplicate add validator tx.
pendingValidator := pendingStakers.GetValidator(tx.Validator.NodeID)
subnets := pendingValidator.SubnetValidators()
if _, validates := subnets[tx.Validator.Subnet]; validates {
return nil, nil, fmt.Errorf(
"already validating subnet %s",
tx.Validator.Subnet,
)
}
baseTxCredsLen := len(stx.Creds) - 1
baseTxCreds := stx.Creds[:baseTxCredsLen]
subnetCred := stx.Creds[baseTxCredsLen]
subnetIntf, _, err := parentState.GetTx(tx.Validator.Subnet)
if err != nil {
if err == database.ErrNotFound {
return nil, nil, errDSValidatorSubset
}
return nil, nil, fmt.Errorf(
"couldn't find subnet %s with %w",
tx.Validator.Subnet,
err,
)
}
subnet, ok := subnetIntf.UnsignedTx.(*UnsignedCreateSubnetTx)
if !ok {
return nil, nil, fmt.Errorf(
"%s is not a subnet",
tx.Validator.Subnet,
)
}
if err := vm.fx.VerifyPermission(tx, tx.SubnetAuth, subnetCred, subnet.Owner); err != nil {
return nil, nil, err
}
// Verify the flowcheck
if err := vm.semanticVerifySpend(parentState, tx, tx.Ins, tx.Outs, baseTxCreds, vm.TxFee, vm.ctx.AVAXAssetID); err != nil {
return nil, nil, err
}
// Make sure the tx doesn't start too far in the future. This is done
// last to allow SemanticVerification to explicitly check for this
// error.
maxStartTime := currentTimestamp.Add(maxFutureStartTime)
if validatorStartTime.After(maxStartTime) {
return nil, nil, errFutureStakeTime
}
}
// Set up the state if this tx is committed
newlyPendingStakers := pendingStakers.AddStaker(stx)
onCommitState := newVersionedState(parentState, currentStakers, newlyPendingStakers)
// Consume the UTXOS
consumeInputs(onCommitState, tx.Ins)
// Produce the UTXOS
txID := tx.ID()
produceOutputs(onCommitState, txID, vm.ctx.AVAXAssetID, tx.Outs)
// Set up the state if this tx is aborted
onAbortState := newVersionedState(parentState, currentStakers, pendingStakers)
// Consume the UTXOS
consumeInputs(onAbortState, tx.Ins)
// Produce the UTXOS
produceOutputs(onAbortState, txID, vm.ctx.AVAXAssetID, tx.Outs)
return onCommitState, onAbortState, nil
}
// InitiallyPrefersCommit returns true if the proposed validators start time is
// after the current wall clock time,
func (tx *UnsignedAddSubnetValidatorTx) InitiallyPrefersCommit(vm *VM) bool {
return tx.StartTime().After(vm.clock.Time())
}
// Create a new transaction
func (vm *VM) newAddSubnetValidatorTx(
weight, // Sampling weight of the new validator
startTime, // Unix time they start delegating
endTime uint64, // Unix time they top delegating
nodeID ids.NodeID, // ID of the node validating
subnetID ids.ID, // ID of the subnet the validator will validate
keys []*crypto.PrivateKeySECP256K1R, // Keys to use for adding the validator
changeAddr ids.ShortID, // Address to send change to, if there is any
) (*Tx, error) {
ins, outs, _, signers, err := vm.stake(keys, 0, vm.TxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
subnetAuth, subnetSigners, err := vm.authorize(vm.internalState, subnetID, keys)
if err != nil {
return nil, fmt.Errorf("couldn't authorize tx's subnet restrictions: %w", err)
}
signers = append(signers, subnetSigners)
// Create the tx
utx := &UnsignedAddSubnetValidatorTx{
BaseTx: BaseTx{BaseTx: avax.BaseTx{
NetworkID: vm.ctx.NetworkID,
BlockchainID: vm.ctx.ChainID,
Ins: ins,
Outs: outs,
}},
Validator: pChainValidator.SubnetValidator{
Validator: pChainValidator.Validator{
NodeID: nodeID,
Start: startTime,
End: endTime,
Wght: weight,
},
Subnet: subnetID,
},
SubnetAuth: subnetAuth,
}
tx := &Tx{UnsignedTx: utx}
if err := tx.Sign(Codec, signers); err != nil {
return nil, err
}
return tx, utx.SyntacticVerify(vm.ctx)
}