This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
add_subnet_validator.go
150 lines (134 loc) · 3.81 KB
/
add_subnet_validator.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package cmd
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/subnet-cli/pkg/color"
"github.com/manifoldco/promptui"
"github.com/onsi/ginkgo/v2/formatter"
"github.com/spf13/cobra"
)
const (
defaultValidateWeight = 1000
)
func newAddSubnetValidatorCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "subnet-validator",
Short: "Adds a subnet to the validator",
Long: `
Adds a subnet to the validator.
$ subnet-cli add subnet-validator \
--private-key-path=.insecure.ewoq.key \
--public-uri=http://localhost:52250 \
--subnet-id="24tZhrm8j8GCJRE9PomW8FaeqbgGS4UAQjJnqqn8pq5NwYSYV1" \
--node-ids="NodeID-4B4rc5vdD1758JSBYL1xyvE5NHGzz6xzH" \
--validate-weight=1000
`,
RunE: createSubnetValidatorFunc,
}
cmd.PersistentFlags().StringVar(&subnetIDs, "subnet-id", "", "subnet ID (must be formatted in ids.ID)")
cmd.PersistentFlags().StringSliceVar(&nodeIDs, "node-ids", nil, "a list of node IDs (must be formatted in ids.ID)")
cmd.PersistentFlags().Uint64Var(&validateWeight, "validate-weight", defaultValidateWeight, "validate weight")
return cmd
}
var errZeroValidateWeight = errors.New("zero validate weight")
func createSubnetValidatorFunc(cmd *cobra.Command, args []string) error {
cli, info, err := InitClient(publicURI, true)
if err != nil {
return err
}
info.subnetID, err = ids.FromString(subnetIDs)
if err != nil {
return err
}
info.txFee = uint64(info.feeData.TxFee)
if err := ParseNodeIDs(cli, info, true); err != nil {
return err
}
if len(info.nodeIDs) == 0 {
color.Outf("{{magenta}}no subnet validators to add{{/}}\n")
return nil
}
info.validateWeight = validateWeight
info.validateRewardFeePercent = 0
if info.validateWeight == 0 {
return errZeroValidateWeight
}
info.rewardAddr = ids.ShortEmpty
info.changeAddr = ids.ShortEmpty
info.txFee *= uint64(len(info.nodeIDs))
info.requiredBalance = info.txFee
if err := info.CheckBalance(); err != nil {
return err
}
msg := CreateAddTable(info)
if enablePrompt {
msg = formatter.F("\n{{blue}}{{bold}}Ready to add subnet validator, should we continue?{{/}}\n") + msg
}
fmt.Fprint(formatter.ColorableStdOut, msg)
if enablePrompt {
prompt := promptui.Select{
Label: "\n",
Stdout: os.Stdout,
Items: []string{
formatter.F("{{green}}Yes, let's create! {{bold}}{{underline}}I agree to pay the fee{{/}}{{green}}!{{/}}"),
formatter.F("{{red}}No, stop it!{{/}}"),
},
}
idx, _, err := prompt.Run()
if err != nil {
return nil //nolint:nilerr
}
if idx == 1 {
return nil
}
}
println()
println()
println()
for _, nodeID := range info.nodeIDs {
// valInfo is not populated because [ParseNodeIDs] called on info.subnetID
//
// TODO: cleanup
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
_, end, err := cli.P().GetValidator(ctx, ids.Empty, nodeID)
cancel()
if err != nil {
return err
}
info.validateStart = time.Now().Add(30 * time.Second)
info.validateEnd = end
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
took, err := cli.P().AddSubnetValidator(
ctx,
info.key,
info.subnetID,
nodeID,
info.validateStart,
info.validateEnd,
validateWeight,
)
cancel()
if err != nil {
return err
}
color.Outf("{{magenta}}added %s to subnet %s validator set{{/}} {{light-gray}}(took %v){{/}}\n\n", nodeID, info.subnetID, took)
}
WaitValidator(cli, info.nodeIDs, info)
info.requiredBalance = 0
info.stakeAmount = 0
info.txFee = 0
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
info.balance, err = cli.P().Balance(ctx, info.key)
cancel()
if err != nil {
return err
}
fmt.Fprint(formatter.ColorableStdOut, CreateAddTable(info))
return nil
}