/
init.go
233 lines (200 loc) · 6.6 KB
/
init.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package commands
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/zeta-protocol/zeta/core/config"
"github.com/zeta-protocol/zeta/core/config/encoding"
"github.com/zeta-protocol/zeta/core/nodewallets/registry"
vgjson "github.com/zeta-protocol/zeta/libs/json"
"github.com/zeta-protocol/zeta/logging"
"github.com/zeta-protocol/zeta/paths"
"github.com/jessevdk/go-flags"
tmcfg "github.com/tendermint/tendermint/config"
tmos "github.com/tendermint/tendermint/libs/os"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/types"
)
type InitCmd struct {
config.ZetaHomeFlag
config.OutputFlag
config.Passphrase `long:"nodewallet-passphrase-file"`
Force bool `short:"f" long:"force" description:"Erase existing zeta configuration at the specified path"`
NoTendermint bool `long:"no-tendermint" description:"Disable tendermint configuration generation"`
TendermintHome string `long:"tendermint-home" required:"true" description:"Directory for tendermint config and data" default:"$HOME/.cometbft"`
TendermintKey string `long:"tendermint-key" description:"Key type to generate privval file with" choice:"ed25519" choice:"secp256k1" default:"ed25519"`
}
var initCmd InitCmd
func (opts *InitCmd) Usage() string {
return "<full | validator>"
}
func (opts *InitCmd) Execute(args []string) error {
logger := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
defer logger.AtExit()
if len(args) != 1 {
return errors.New("require exactly 1 parameter mode, expected modes [validator, full, seed]")
}
mode, err := encoding.NodeModeFromString(args[0])
if err != nil {
return err
}
output, err := opts.GetOutput()
if err != nil {
return err
}
zetaPaths := paths.New(opts.ZetaHome)
// a nodewallet will be required only for a validator node
var nwRegistry *registry.Loader
if mode == encoding.NodeModeValidator {
pass, err := opts.Get("node wallet", true)
if err != nil {
return err
}
nwRegistry, err = registry.NewLoader(zetaPaths, pass)
if err != nil {
return err
}
}
cfgLoader, err := config.InitialiseLoader(zetaPaths)
if err != nil {
return fmt.Errorf("couldn't initialise configuration loader: %w", err)
}
configExists, err := cfgLoader.ConfigExists()
if err != nil {
return fmt.Errorf("couldn't verify configuration presence: %w", err)
}
if configExists && !opts.Force {
return fmt.Errorf("configuration already exists at `%s` please remove it first or re-run using -f", cfgLoader.ConfigFilePath())
}
if configExists && opts.Force {
if output.IsHuman() {
logger.Info("removing existing configuration", logging.String("path", cfgLoader.ConfigFilePath()))
}
cfgLoader.Remove()
}
cfg := config.NewDefaultConfig()
cfg.NodeMode = mode
cfg.SetDefaultMaxMemoryPercent()
if err := cfgLoader.Save(&cfg); err != nil {
return fmt.Errorf("couldn't save configuration file: %w", err)
}
if output.IsHuman() {
logger.Info("configuration generated successfully",
logging.String("path", cfgLoader.ConfigFilePath()))
}
if !initCmd.NoTendermint {
tmCfg := tmcfg.DefaultConfig()
tmCfg.SetRoot(os.ExpandEnv(initCmd.TendermintHome))
tmcfg.EnsureRoot(tmCfg.RootDir)
if err := initTendermintConfiguration(output, logger, tmCfg); err != nil {
return fmt.Errorf("couldn't initialise tendermint %w", err)
}
}
if output.IsJSON() {
if mode == encoding.NodeModeValidator {
return vgjson.Print(struct {
ConfigFilePath string `json:"configFilePath"`
NodeWalletConfigFilePath string `json:"nodeWalletConfigFilePath"`
}{
ConfigFilePath: cfgLoader.ConfigFilePath(),
NodeWalletConfigFilePath: nwRegistry.RegistryFilePath(),
})
}
return vgjson.Print(struct {
ConfigFilePath string `json:"configFilePath"`
}{
ConfigFilePath: cfgLoader.ConfigFilePath(),
})
}
return nil
}
func initTendermintConfiguration(output config.Output, logger *logging.Logger, config *tmcfg.Config) error {
// private validator
privValKeyFile := config.PrivValidatorKeyFile()
privValStateFile := config.PrivValidatorStateFile()
var pv *privval.FilePV
if tmos.FileExists(privValKeyFile) {
pv = privval.LoadFilePV(privValKeyFile, privValStateFile)
if output.IsHuman() {
logger.Info("Found private validator",
logging.String("keyFile", privValKeyFile),
logging.String("stateFile", privValStateFile),
)
}
} else {
pv = privval.GenFilePV(privValKeyFile, privValStateFile)
pv.Save()
if output.IsHuman() {
logger.Info("Generated private validator",
logging.String("keyFile", privValKeyFile),
logging.String("stateFile", privValStateFile),
)
}
}
nodeKeyFile := config.NodeKeyFile()
if tmos.FileExists(nodeKeyFile) {
if output.IsHuman() {
logger.Info("Found node key", logging.String("path", nodeKeyFile))
}
} else {
if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
return err
}
if output.IsHuman() {
logger.Info("Generated node key", logging.String("path", nodeKeyFile))
}
}
// genesis file
genFile := config.GenesisFile()
if tmos.FileExists(genFile) {
if output.IsHuman() {
logger.Info("Found genesis file", logging.String("path", genFile))
}
} else {
genDoc := types.GenesisDoc{
ChainID: fmt.Sprintf("test-chain-%v", tmrand.Str(6)),
GenesisTime: time.Now().Round(0).UTC(),
ConsensusParams: types.DefaultConsensusParams(),
}
pubKey, err := pv.GetPubKey()
if err != nil {
return fmt.Errorf("can't get pubkey: %w", err)
}
genDoc.Validators = []types.GenesisValidator{{
Address: pubKey.Address(),
PubKey: pubKey,
Power: 10,
}}
if err := genDoc.SaveAs(genFile); err != nil {
return err
}
if output.IsHuman() {
logger.Info("Generated genesis file", logging.String("path", genFile))
}
}
return nil
}
func Init(ctx context.Context, parser *flags.Parser) error {
initCmd = InitCmd{}
var (
short = "Initializes a zeta node"
long = "Generate the minimal configuration required for a zeta node to start. You must specify 'full' or 'validator'"
)
_, err := parser.AddCommand("init", short, long, &initCmd)
return err
}