-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
364 lines (325 loc) · 9.8 KB
/
generate.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
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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package upgradecmd
import (
"encoding/json"
"errors"
"fmt"
"math/big"
"time"
"github.com/lasthyphen/dijets-cli/pkg/constants"
"github.com/lasthyphen/dijets-cli/pkg/prompts"
"github.com/lasthyphen/dijets-cli/pkg/ux"
"github.com/lasthyphen/dijets-cli/pkg/vm"
"github.com/lasthyphen/dijetsnode/utils/logging"
"github.com/lasthyphen/subnet-evm/commontype"
"github.com/lasthyphen/subnet-evm/params"
"github.com/lasthyphen/subnet-evm/precompile"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/spf13/cobra"
)
const (
blockTimestampKey = "blockTimestamp"
feeConfigKey = "initialFeeConfig"
initialMintKey = "initialMint"
adminAddressesKey = "adminAddresses"
enabledAddressesKey = "enabledAddresses"
enabledLabel = "enabled"
adminLabel = "admin"
)
// avalanche subnet upgrade generate
func newUpgradeGenerateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate [subnetName]",
Short: "Generate the configuration file to upgrade subnet nodes",
Long: `Upgrades to subnet nodes can be executed by providing a upgrade.json file to the nodes.
This command starts a wizard guiding the user generating the required file.`,
RunE: upgradeGenerateCmd,
Args: cobra.ExactArgs(1),
}
return cmd
}
func upgradeGenerateCmd(_ *cobra.Command, args []string) error {
subnetName := args[0]
if !app.GenesisExists(subnetName) {
ux.Logger.PrintToUser("The provided subnet name %q does not exist", subnetName)
return nil
}
// print some warning/info message
ux.Logger.PrintToUser(logging.Bold.Wrap(logging.Yellow.Wrap(
"Performing a network upgrade requires coordinating the upgrade network-wide.")))
ux.Logger.PrintToUser(logging.White.Wrap(logging.Reset.Wrap(
"A network upgrade changes the rule set used to process and verify blocks, " +
"such that any node that upgrades incorrectly or fails to upgrade by the time " +
"that upgrade goes into effect may become out of sync with the rest of the network.\n")))
ux.Logger.PrintToUser(logging.Bold.Wrap(logging.Red.Wrap(
"Any mistakes in configuring network upgrades or coordinating them on validators " +
"may cause the network to halt and recovering may be difficult.")))
ux.Logger.PrintToUser(logging.Reset.Wrap(
"Please consult " + logging.Cyan.Wrap(
"https://docs.avax.network/subnets/customize-a-subnet#network-upgrades-enabledisable-precompiles ") +
logging.Reset.Wrap("for more information")))
txt := "Press [Enter] to continue, or abort by choosing 'no'"
yes, err := app.Prompt.CaptureYesNo(txt)
if err != nil {
return err
}
if !yes {
ux.Logger.PrintToUser("Aborted by user")
return nil
}
allPreComps := []string{
vm.ContractAllowList,
vm.FeeManager,
vm.NativeMint,
vm.TxAllowList,
}
fmt.Println()
ux.Logger.PrintToUser(logging.Yellow.Wrap(
"DijetsNode and this tool support configuring multiple precompiles. " +
"However, we suggest to only configure one per upgrade."))
fmt.Println()
// use the correct data types from subnet-evm right away
precompiles := params.UpgradeConfig{
PrecompileUpgrades: make([]params.PrecompileUpgrade, 0),
}
for {
precomp, err := app.Prompt.CaptureList("Select the precompile to configure", allPreComps)
if err != nil {
return err
}
ux.Logger.PrintToUser(fmt.Sprintf("Set parameters for the %q precompile", precomp))
if err := promptParams(precomp, &precompiles.PrecompileUpgrades); err != nil {
return err
}
if len(allPreComps) > 1 {
yes, err := app.Prompt.CaptureNoYes("Should we configure another precompile?")
if err != nil {
return err
}
if !yes {
break
}
for i := 0; i < len(allPreComps); i++ {
if allPreComps[i] == precomp {
allPreComps = append(allPreComps[:i], allPreComps[i+1:]...)
break
}
}
}
}
jsonBytes, err := json.Marshal(&precompiles)
if err != nil {
return err
}
return app.WriteUpgradeFile(subnetName, jsonBytes)
}
func queryActivationTimestamp() (time.Time, error) {
const (
in5min = "In 5 minutes"
in1day = "In 1 day"
in1week = "In 1 week"
in2weeks = "In 2 weeks"
custom = "Custom"
)
options := []string{in5min, in1day, in1week, in2weeks, custom}
choice, err := app.Prompt.CaptureList("When should the precompile be activated?", options)
if err != nil {
return time.Time{}, err
}
var date time.Time
now := time.Now()
switch choice {
case in5min:
date = now.Add(5 * time.Minute)
case in1day:
date = now.Add(24 * time.Hour)
case in1week:
date = now.Add(7 * 24 * time.Hour)
case in2weeks:
date = now.Add(14 * 24 * time.Hour)
case custom:
date, err = app.Prompt.CaptureFutureDate(
"Enter the block activation UTC datetime in 'YYYY-MM-DD HH:MM:SS' format", time.Now().Add(time.Minute).UTC())
if err != nil {
return time.Time{}, err
}
}
ux.Logger.PrintToUser("The chosen block activation time is %s", date.Format(constants.TimeParseLayout))
return date, nil
}
func promptParams(precomp string, precompiles *[]params.PrecompileUpgrade) error {
date, err := queryActivationTimestamp()
if err != nil {
return err
}
switch precomp {
case vm.ContractAllowList:
return promptContractAllowListParams(precompiles, date)
case vm.TxAllowList:
return promptTxAllowListParams(precompiles, date)
case vm.NativeMint:
return promptNativeMintParams(precompiles, date)
case vm.FeeManager:
return promptFeeManagerParams(precompiles, date)
default:
return fmt.Errorf("unexpected precompile identifier: %q", precomp)
}
}
func promptNativeMintParams(precompiles *[]params.PrecompileUpgrade, date time.Time) error {
initialMint := map[common.Address]*math.HexOrDecimal256{}
adminAddrs, enabledAddrs, err := promptAdminAndEnabledAddresses()
if err != nil {
return err
}
yes, err := app.Prompt.CaptureYesNo(fmt.Sprintf("Airdrop more tokens? (`%s` section in file)", initialMintKey))
if err != nil {
return err
}
if yes {
_, cancel, err := prompts.CaptureListDecision(
app.Prompt,
"How would you like to distribute your funds",
func(s string) (string, error) {
addr, err := app.Prompt.CaptureAddress("Address to airdrop to")
if err != nil {
return "", err
}
amount, err := app.Prompt.CaptureUint64("Amount to airdrop (in DJTX units)")
if err != nil {
return "", err
}
initialMint[addr] = math.NewHexOrDecimal256(int64(amount))
return fmt.Sprintf("%s-%d", addr.Hex(), amount), nil
},
"Add an address to amount pair",
"Address-Amount",
"Hex-formatted address and it's initial amount value, "+
"for example: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC (address) and 1000000000000000000 (value)",
)
if err != nil {
return err
}
if cancel {
return errors.New("aborted by user")
}
}
config := precompile.NewContractNativeMinterConfig(
big.NewInt(date.Unix()),
adminAddrs,
enabledAddrs,
initialMint,
)
upgrade := params.PrecompileUpgrade{
ContractNativeMinterConfig: config,
}
*precompiles = append(*precompiles, upgrade)
return nil
}
func promptFeeManagerParams(precompiles *[]params.PrecompileUpgrade, date time.Time) error {
adminAddrs, enabledAddrs, err := promptAdminAndEnabledAddresses()
if err != nil {
return err
}
yes, err := app.Prompt.CaptureYesNo(fmt.Sprintf(
"Do you want to update the fee config upon precompile activation? ('%s' section in file)", feeConfigKey))
if err != nil {
return err
}
var feeConfig *commontype.FeeConfig
if yes {
chainConfig, _, err := vm.GetFeeConfig(params.ChainConfig{}, app)
if err != nil {
return err
}
feeConfig = &chainConfig.FeeConfig
}
config := precompile.NewFeeManagerConfig(
big.NewInt(date.Unix()),
adminAddrs,
enabledAddrs,
feeConfig,
)
upgrade := params.PrecompileUpgrade{
FeeManagerConfig: config,
}
*precompiles = append(*precompiles, upgrade)
return nil
}
func promptContractAllowListParams(precompiles *[]params.PrecompileUpgrade, date time.Time) error {
adminAddrs, enabledAddrs, err := promptAdminAndEnabledAddresses()
if err != nil {
return err
}
config := precompile.NewContractDeployerAllowListConfig(
big.NewInt(date.Unix()),
adminAddrs,
enabledAddrs,
)
upgrade := params.PrecompileUpgrade{
ContractDeployerAllowListConfig: config,
}
*precompiles = append(*precompiles, upgrade)
return nil
}
func promptTxAllowListParams(precompiles *[]params.PrecompileUpgrade, date time.Time) error {
adminAddrs, enabledAddrs, err := promptAdminAndEnabledAddresses()
if err != nil {
return err
}
config := precompile.NewTxAllowListConfig(
big.NewInt(date.Unix()),
adminAddrs,
enabledAddrs,
)
upgrade := params.PrecompileUpgrade{
TxAllowListConfig: config,
}
*precompiles = append(*precompiles, upgrade)
return nil
}
func promptAdminAndEnabledAddresses() ([]common.Address, []common.Address, error) {
var admin, enabled []common.Address
for {
if err := captureAddress(adminLabel, &admin); err != nil {
return nil, nil, err
}
if err := captureAddress(enabledLabel, &enabled); err != nil {
return nil, nil, err
}
if len(enabled) == 0 && len(admin) == 0 {
ux.Logger.PrintToUser(fmt.Sprintf(
"We need at least one address for either '%s' or '%s'. Otherwise abort.", enabledAddressesKey, adminAddressesKey))
continue
}
return admin, enabled, nil
}
}
func captureAddress(which string, addrsField *[]common.Address) error {
yes, err := app.Prompt.CaptureYesNo(fmt.Sprintf("Add '%sAddresses'?", which))
if err != nil {
return err
}
if yes {
var (
cancel bool
err error
)
*addrsField, cancel, err = prompts.CaptureListDecision(
app.Prompt,
fmt.Sprintf("Provide '%sAddresses'", which),
app.Prompt.CaptureAddress,
"Add an address",
"Address",
fmt.Sprintf("Hex-formatted %s addresses", which),
)
if err != nil {
return err
}
if cancel {
return errors.New("aborted by user")
}
}
return nil
}