/
root.go
446 lines (379 loc) · 15.8 KB
/
root.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// The root command contains everything under `$ althea`, notably the `tx` and
// `q` commands, the `start` command for validators, and all defaults are set here
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
cfg "github.com/tendermint/tendermint/config"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/debug"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/snapshots"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/auth/types"
vestingcli "github.com/cosmos/cosmos-sdk/x/auth/vesting/client/cli"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
"github.com/cosmos/go-bip39"
"github.com/pkg/errors"
"github.com/spf13/cast"
"github.com/spf13/cobra"
// EVM
ethermintclient "github.com/evmos/ethermint/client"
ethermintserver "github.com/evmos/ethermint/server"
ethermintserverconfig "github.com/evmos/ethermint/server/config"
ethermintserverflags "github.com/evmos/ethermint/server/flags"
// Althea
althea "github.com/althea-net/althea-L1/app"
"github.com/althea-net/althea-L1/app/params"
altheacfg "github.com/althea-net/althea-L1/config"
"github.com/althea-net/althea-L1/crypto/keyring"
)
const EnvPrefix = "althea"
type printInfo struct {
Moniker string `json:"moniker" yaml:"moniker"`
ChainID string `json:"chain_id" yaml:"chain_id"`
NodeID string `json:"node_id" yaml:"node_id"`
GenTxsDir string `json:"gentxs_dir" yaml:"gentxs_dir"`
AppMessage json.RawMessage `json:"app_message" yaml:"app_message"`
}
func newPrintInfo(moniker, chainID, nodeID, genTxsDir string, appMessage json.RawMessage) printInfo {
return printInfo{
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
GenTxsDir: genTxsDir,
AppMessage: appMessage,
}
}
func displayInfo(info printInfo) error {
out, err := json.MarshalIndent(info, "", " ")
if err != nil {
return err
}
if _, err := fmt.Fprintf(os.Stderr, "%s\n", string(sdk.MustSortJSON(out))); err != nil {
return err
}
return nil
}
// NewRootCmd creates a new root command for althea. It is called once in the
// main function. The name of the binary is controlled by the Makefile in the
// project root. Most everything else is controlled here via cobra.
// The module subcommands are automatically created by passing the ModuleBasics
// (from app.go) in initRootCmd().
// The module subcommands should be in x/{module}/client/cli
func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
encodingConfig := althea.MakeEncodingConfig()
initClientCtx := client.Context{}.
// Encoding and interfaces
WithCodec(encodingConfig.Codec).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastBlock).
WithHomeDir(althea.DefaultNodeHome).
WithKeyringOptions(keyring.Option()).
WithViper(EnvPrefix)
rootCmd := &cobra.Command{
Use: althea.Name,
Short: "Althea L1: Submit transactions or run a validator",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())
initClientCtx, err := config.ReadFromClientConfig(initClientCtx)
if err != nil {
return fmt.Errorf("unable to update context with client.toml config: %v", err)
}
// Calls ReadPersistentCmdFlags and SetCmdClientContext
if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
return err
}
altheaAppTemplate, altheaAppConfig := initAppConfig()
// Takes all these configurations and applies them, additionally configuring Tendermint
// to adhere to these desires
return server.InterceptConfigsPreRunHandler(cmd, altheaAppTemplate, altheaAppConfig)
},
}
initRootCmd(rootCmd, &encodingConfig)
return rootCmd, encodingConfig
}
// initAppConfig defines the default configuration. These defaults can be overridden via an
// app.toml file or with flags provided on the command line
func initAppConfig() (string, interface{}) {
// DEFAULT SERVER CONFIGURATIONS
appTempl, appCfg := ethermintserverconfig.AppConfig(altheacfg.BaseDenom)
return appTempl, appCfg
}
// Execute executes the root command.
func Execute(rootCmd *cobra.Command, defaultHome string) error {
// Create and set a client.Context on the command's Context. During the pre-run
// of the root command, a default initialized client.Context is provided to
// seed child command execution with values such as AccountRetriver, Keyring,
// and a Tendermint RPC. This requires the use of a pointer reference when
// getting and setting the client.Context. Ideally, we utilize
// https://github.com/spf13/cobra/pull/1118.
srvCtx := server.NewDefaultContext()
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{})
ctx = context.WithValue(ctx, server.ServerContextKey, srvCtx)
rootCmd.PersistentFlags().String("log_level", "info", "The logging level in the format of <module>:<level>,...")
rootCmd.PersistentFlags().String(flags.FlagLogFormat, cfg.LogFormatPlain, "The logging format (json|plain)")
executor := tmcli.PrepareBaseCmd(rootCmd, "", defaultHome)
return executor.ExecuteContext(ctx)
}
// Setup all of the subcommands for the root command
func initRootCmd(rootCmd *cobra.Command, encodingConfig *params.EncodingConfig) {
rootCmd.AddCommand(
// ValidateChainID will make sure the configured chain id adheres to strings like althea_1234-1
ethermintclient.ValidateChainID(InitCmd(althea.ModuleBasics, althea.DefaultNodeHome)),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, althea.DefaultNodeHome),
genutilcli.GenTxCmd(althea.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, althea.DefaultNodeHome),
ValidateGenesisCmd(althea.ModuleBasics),
AddGenesisAccountCmd(althea.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
testnetCmd(althea.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(), // Output useful info about keys
config.Cmd(), // Set config options one by one
)
ac := appCreator{encodingConfig}
// The ethermint server commands perform a lot of modifications on top of the base ones, notably setting up the
// EVM JSONRPC server, tx indexer, and some various improvements like closing the DB automatically
ethermintserver.AddCommands(rootCmd, althea.DefaultNodeHome, ac.newApp, ac.createSimappAndExport, addModuleInitFlags)
rootCmd.AddCommand(ethermintserver.NewIndexTxCmd())
// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
rpc.StatusCommand(),
// queryCommand registers the query subcommands by looking at ModuleBasics
queryCommand(),
// txCommand registers the tx subcommands by looking at ModuleBasics
txCommand(),
// Adds the same commands as sdk keys.Commands(), but enables dry run and sets the default keytype to eth_secp256k1
// keys.Commands(althea.DefaultNodeHome),
ethermintclient.KeyCommands(althea.DefaultNodeHome),
)
rootCmd, err := ethermintserverflags.AddTxFlags(rootCmd)
if err != nil {
panic(err)
}
rootCmd.AddCommand(server.RosettaCommand(encodingConfig.InterfaceRegistry, encodingConfig.Codec))
}
// InitCmd returns a command that initializes all files needed for Tendermint
// and the respective application.
// Note that this is mostly a copy of the default InitCmd found in genutil, however we need to overwrite the default
// chain id to one which will not cause a panic in ethermint/x/evm/genesis.go
func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "init [moniker]",
Short: "Initialize private validator, p2p, genesis, and application configuration files",
Long: `Initialize validators's and node's configuration files.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
cdc := clientCtx.Codec
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
config.SetRoot(clientCtx.HomeDir)
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
if chainID == "" {
chainID = altheacfg.DefaultChainID()
}
// Get bip39 mnemonic
var mnemonic string
recover, _ := cmd.Flags().GetBool(genutilcli.FlagRecover)
if recover {
inBuf := bufio.NewReader(cmd.InOrStdin())
value, err := input.GetString("Enter your bip39 mnemonic", inBuf)
if err != nil {
return err
}
mnemonic = value
if !bip39.IsMnemonicValid(mnemonic) {
return errors.New("invalid mnemonic")
}
}
nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic)
if err != nil {
return err
}
config.Moniker = args[0]
genFile := config.GenesisFile()
overwrite, _ := cmd.Flags().GetBool(genutilcli.FlagOverwrite)
if !overwrite && tmos.FileExists(genFile) {
return fmt.Errorf("genesis.json file already exists: %v", genFile)
}
appState, err := json.MarshalIndent(mbm.DefaultGenesis(cdc), "", " ")
if err != nil {
return errors.Wrap(err, "Failed to marshall default genesis state")
}
genDoc := &tmtypes.GenesisDoc{}
if _, err := os.Stat(genFile); err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
genDoc, err = tmtypes.GenesisDocFromFile(genFile)
if err != nil {
return errors.Wrap(err, "Failed to read genesis doc from file")
}
}
genDoc.ChainID = chainID
genDoc.Validators = nil
genDoc.AppState = appState
if err = genutil.ExportGenesisFile(genDoc, genFile); err != nil {
return errors.Wrap(err, "Failed to export gensis file")
}
toPrint := newPrintInfo(config.Moniker, chainID, nodeID, "", appState)
cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)
return displayInfo(toPrint)
},
}
cmd.Flags().String(tmcli.HomeFlag, defaultNodeHome, "node's home directory")
cmd.Flags().BoolP(genutilcli.FlagOverwrite, "o", false, "overwrite the genesis.json file")
cmd.Flags().Bool(genutilcli.FlagRecover, false, "provide seed phrase to recover existing key instead of creating")
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
return cmd
}
// Add the --x-crisis-skip-assert-invariants flag, perhaps more in the future
func addModuleInitFlags(startCmd *cobra.Command) {
crisis.AddModuleInitFlags(startCmd)
}
// Generate the query subcommands for each module in ModuleBasics and other manually
// registered commands
func queryCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Querying subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(
authcmd.GetAccountCmd(),
rpc.ValidatorCommand(),
rpc.BlockCommand(),
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
)
althea.ModuleBasics.AddQueryCommands(cmd)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")
return cmd
}
// Generate the tx subcommands for each module in ModuleBasics and other manually
// registered commands
func txCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(
authcmd.GetSignCommand(),
authcmd.GetSignBatchCommand(),
authcmd.GetMultiSignCommand(),
authcmd.GetMultiSignBatchCmd(),
authcmd.GetValidateSignaturesCommand(),
flags.LineBreak,
authcmd.GetBroadcastCommand(),
authcmd.GetEncodeCommand(),
authcmd.GetDecodeCommand(),
flags.LineBreak,
vestingcli.GetTxCmd(),
)
althea.ModuleBasics.AddTxCommands(cmd)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")
return cmd
}
// Convenience type to make parameter passing simpler + remove duplication of encoding config
type appCreator struct {
encCfg *params.EncodingConfig
}
// newApp is an AppCreator used for the start command, anything which must be passed to NewAltheaApp (in app.go)
// can be fetched and added here
func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
var cache sdk.MultiStorePersistentCache
if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) {
cache = store.NewCommitKVStoreCacheManager()
}
skipUpgradeHeights := make(map[int64]bool)
for _, h := range cast.ToIntSlice(appOpts.Get(server.FlagUnsafeSkipUpgrades)) {
skipUpgradeHeights[int64(h)] = true
}
pruningOpts, err := server.GetPruningOptionsFromFlags(appOpts)
if err != nil {
panic(err)
}
snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots")
snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir)
if err != nil {
panic(err)
}
snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir)
if err != nil {
panic(err)
}
return althea.NewAltheaApp(
logger, db, traceStore, true, skipUpgradeHeights,
cast.ToString(appOpts.Get(flags.FlagHome)),
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
*a.encCfg,
appOpts,
baseapp.SetPruning(pruningOpts),
baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(server.FlagMinGasPrices))),
baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))),
baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))),
baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))),
baseapp.SetInterBlockCache(cache),
baseapp.SetTrace(cast.ToBool(appOpts.Get(server.FlagTrace))),
baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents))),
baseapp.SetSnapshotStore(snapshotStore),
baseapp.SetSnapshotInterval(cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval))),
baseapp.SetSnapshotKeepRecent(cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent))),
)
}
// Creates an app which will not run, instead used for state exports
// Pass -1 to export the current state, any other positive value to export that state (if it is available)
func (a appCreator) createSimappAndExport(
logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailAllowedAddrs []string,
appOpts servertypes.AppOptions,
) (servertypes.ExportedApp, error) {
var altheaApp *althea.AltheaApp
homePath, ok := appOpts.Get(flags.FlagHome).(string)
if !ok || homePath == "" {
return servertypes.ExportedApp{}, errors.New("application home not set")
}
if height != -1 {
altheaApp = althea.NewAltheaApp(logger, db, traceStore, false, map[int64]bool{}, "", uint(1), *a.encCfg, appOpts)
if err := altheaApp.LoadHeight(height); err != nil {
return servertypes.ExportedApp{}, err
}
} else {
altheaApp = althea.NewAltheaApp(logger, db, traceStore, true, map[int64]bool{}, "", uint(1), *a.encCfg, appOpts)
}
return altheaApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs)
}