-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathremoval.go
More file actions
400 lines (387 loc) · 9.51 KB
/
removal.go
File metadata and controls
400 lines (387 loc) · 9.51 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validatormanager
import (
"context"
_ "embed"
"errors"
"fmt"
"math/big"
"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/contract"
"github.com/ava-labs/avalanche-cli/pkg/evm"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanche-cli/sdk/interchain"
"github.com/ava-labs/avalanche-cli/sdk/validator"
"github.com/ava-labs/avalanche-cli/sdk/validatormanager"
"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
warp "github.com/ava-labs/avalanchego/vms/platformvm/warp"
warpPayload "github.com/ava-labs/avalanchego/vms/platformvm/warp/payload"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/warp/messages"
"github.com/ethereum/go-ethereum/common"
)
func InitializeValidatorRemoval(
rpcURL string,
managerAddress common.Address,
generateRawTxOnly bool,
managerOwnerAddress common.Address,
privateKey string,
validationID ids.ID,
isPoS bool,
uptimeProofSignedMessage *warp.Message,
force bool,
useACP99 bool,
) (*types.Transaction, *types.Receipt, error) {
if isPoS {
if force {
return contract.TxToMethod(
rpcURL,
false,
common.Address{},
privateKey,
managerAddress,
big.NewInt(0),
"force POS validator removal",
validatormanager.ErrorSignatureToError,
"forceInitializeEndValidation(bytes32,bool,uint32)",
validationID,
false, // no uptime proof if force
uint32(0),
)
}
// remove PoS validator with uptime proof
return contract.TxToMethodWithWarpMessage(
rpcURL,
false,
common.Address{},
privateKey,
managerAddress,
uptimeProofSignedMessage,
big.NewInt(0),
"POS validator removal with uptime proof",
validatormanager.ErrorSignatureToError,
"initializeEndValidation(bytes32,bool,uint32)",
validationID,
true, // submit uptime proof
uint32(0),
)
}
// PoA case
if useACP99 {
return contract.TxToMethod(
rpcURL,
generateRawTxOnly,
managerOwnerAddress,
privateKey,
managerAddress,
big.NewInt(0),
"POA validator removal initialization",
validatormanager.ErrorSignatureToError,
"initiateValidatorRemoval(bytes32)",
validationID,
)
}
return contract.TxToMethod(
rpcURL,
generateRawTxOnly,
managerOwnerAddress,
privateKey,
managerAddress,
big.NewInt(0),
"POA validator removal initialization",
validatormanager.ErrorSignatureToError,
"initializeEndValidation(bytes32)",
validationID,
)
}
func GetUptimeProofMessage(
ctx context.Context,
network models.Network,
aggregatorLogger logging.Logger,
aggregatorQuorumPercentage uint64,
aggregatorExtraPeerEndpoints []info.Peer,
subnetID ids.ID,
blockchainID ids.ID,
validationID ids.ID,
uptime uint64,
) (*warp.Message, error) {
uptimePayload, err := messages.NewValidatorUptime(validationID, uptime)
if err != nil {
return nil, err
}
addressedCall, err := warpPayload.NewAddressedCall(nil, uptimePayload.Bytes())
if err != nil {
return nil, err
}
uptimeProofUnsignedMessage, err := warp.NewUnsignedMessage(
network.ID,
blockchainID,
addressedCall.Bytes(),
)
if err != nil {
return nil, err
}
signatureAggregator, err := interchain.NewSignatureAggregator(
ctx,
network,
aggregatorLogger,
subnetID,
aggregatorQuorumPercentage,
true, // allow private peers
aggregatorExtraPeerEndpoints,
)
if err != nil {
return nil, err
}
return signatureAggregator.Sign(uptimeProofUnsignedMessage, nil)
}
func InitValidatorRemoval(
ctx context.Context,
app *application.Avalanche,
network models.Network,
rpcURL string,
chainSpec contract.ChainSpec,
generateRawTxOnly bool,
ownerAddressStr string,
ownerPrivateKey string,
nodeID ids.NodeID,
aggregatorExtraPeerEndpoints []info.Peer,
aggregatorAllowPrivatePeers bool,
aggregatorLogger logging.Logger,
isPoS bool,
uptimeSec uint64,
force bool,
validatorManagerAddressStr string,
useACP99 bool,
initiateTxHash string,
) (*warp.Message, ids.ID, *types.Transaction, error) {
subnetID, err := contract.GetSubnetID(
app,
network,
chainSpec,
)
if err != nil {
return nil, ids.Empty, nil, err
}
blockchainID, err := contract.GetBlockchainID(
app,
network,
chainSpec,
)
if err != nil {
return nil, ids.Empty, nil, err
}
managerAddress := common.HexToAddress(validatorManagerAddressStr)
ownerAddress := common.HexToAddress(ownerAddressStr)
validationID, err := validator.GetValidationID(
rpcURL,
managerAddress,
nodeID,
)
if err != nil {
return nil, ids.Empty, nil, err
}
if validationID == ids.Empty {
return nil, ids.Empty, nil, fmt.Errorf("node %s is not a L1 validator", nodeID)
}
var unsignedMessage *warp.UnsignedMessage
if initiateTxHash != "" {
unsignedMessage, err = GetL1ValidatorWeightMessageFromTx(
rpcURL,
validationID,
0,
initiateTxHash,
)
if err != nil {
return nil, ids.Empty, nil, err
}
}
var receipt *types.Receipt
if unsignedMessage == nil {
signedUptimeProof := &warp.Message{}
if isPoS {
if uptimeSec == 0 {
uptimeSec, err = utils.GetL1ValidatorUptimeSeconds(rpcURL, nodeID)
if err != nil {
return nil, ids.Empty, nil, evm.TransactionError(nil, err, "failure getting uptime data for nodeID: %s via %s ", nodeID, rpcURL)
}
}
ux.Logger.PrintToUser("Using uptime: %ds", uptimeSec)
signedUptimeProof, err = GetUptimeProofMessage(
ctx,
network,
aggregatorLogger,
0,
aggregatorExtraPeerEndpoints,
subnetID,
blockchainID,
validationID,
uptimeSec,
)
if err != nil {
return nil, ids.Empty, nil, evm.TransactionError(nil, err, "failure getting uptime proof")
}
}
var tx *types.Transaction
tx, receipt, err = InitializeValidatorRemoval(
rpcURL,
managerAddress,
generateRawTxOnly,
ownerAddress,
ownerPrivateKey,
validationID,
isPoS,
signedUptimeProof, // is empty for non-PoS
force,
useACP99,
)
if err != nil {
if !errors.Is(err, validatormanager.ErrInvalidValidatorStatus) {
return nil, ids.Empty, nil, evm.TransactionError(tx, err, "failure initializing validator removal")
}
ux.Logger.PrintToUser(logging.LightBlue.Wrap("The validator removal process was already initialized. Proceeding to the next step"))
} else if generateRawTxOnly {
return nil, ids.Empty, tx, nil
}
} else {
ux.Logger.PrintToUser(logging.LightBlue.Wrap("The validator removal process was already initialized. Proceeding to the next step"))
}
if receipt != nil {
unsignedMessage, err = GetWarpMessageFromLogs(receipt.Logs)
if err != nil {
return nil, ids.Empty, nil, err
}
}
var nonce uint64
if unsignedMessage == nil {
nonce, err = GetValidatorNonce(rpcURL, validationID)
if err != nil {
return nil, ids.Empty, nil, err
}
}
signedMsg, err := GetL1ValidatorWeightMessage(
ctx,
network,
aggregatorLogger,
0,
aggregatorAllowPrivatePeers,
aggregatorExtraPeerEndpoints,
unsignedMessage,
subnetID,
blockchainID,
managerAddress,
validationID,
nonce,
0,
)
return signedMsg, validationID, nil, err
}
func CompleteValidatorRemoval(
rpcURL string,
managerAddress common.Address,
generateRawTxOnly bool,
ownerAddress common.Address,
privateKey string, // not need to be owner atm
subnetValidatorRegistrationSignedMessage *warp.Message,
useACP99 bool,
) (*types.Transaction, *types.Receipt, error) {
if useACP99 {
return contract.TxToMethodWithWarpMessage(
rpcURL,
generateRawTxOnly,
ownerAddress,
privateKey,
managerAddress,
subnetValidatorRegistrationSignedMessage,
big.NewInt(0),
"complete poa validator removal",
validatormanager.ErrorSignatureToError,
"completeValidatorRemoval(uint32)",
uint32(0),
)
}
return contract.TxToMethodWithWarpMessage(
rpcURL,
generateRawTxOnly,
ownerAddress,
privateKey,
managerAddress,
subnetValidatorRegistrationSignedMessage,
big.NewInt(0),
"complete poa validator removal",
validatormanager.ErrorSignatureToError,
"completeEndValidation(uint32)",
uint32(0),
)
}
func FinishValidatorRemoval(
ctx context.Context,
app *application.Avalanche,
network models.Network,
rpcURL string,
chainSpec contract.ChainSpec,
generateRawTxOnly bool,
ownerAddressStr string,
privateKey string,
validationID ids.ID,
aggregatorExtraPeerEndpoints []info.Peer,
aggregatorAllowPrivatePeers bool,
aggregatorLogger logging.Logger,
validatorManagerAddressStr string,
useACP99 bool,
) (*types.Transaction, error) {
managerAddress := common.HexToAddress(validatorManagerAddressStr)
subnetID, err := contract.GetSubnetID(
app,
network,
chainSpec,
)
if err != nil {
return nil, err
}
signedMessage, err := GetPChainL1ValidatorRegistrationMessage(
ctx,
network,
rpcURL,
aggregatorLogger,
0,
aggregatorAllowPrivatePeers,
aggregatorExtraPeerEndpoints,
subnetID,
validationID,
false,
)
if err != nil {
return nil, err
}
if privateKey != "" {
if err := evm.SetupProposerVM(
rpcURL,
privateKey,
); err != nil {
ux.Logger.RedXToUser("failure setting proposer VM on L1: %w", err)
}
}
ownerAddress := common.HexToAddress(ownerAddressStr)
tx, _, err := CompleteValidatorRemoval(
rpcURL,
managerAddress,
generateRawTxOnly,
ownerAddress,
privateKey,
signedMessage,
useACP99,
)
if err != nil {
return nil, evm.TransactionError(tx, err, "failure completing validator removal")
}
if generateRawTxOnly {
return tx, nil
}
return nil, nil
}