From a0e4d515a0a10fd9af9c4cf5e98609d80a12e811 Mon Sep 17 00:00:00 2001 From: p0p3ye Date: Mon, 30 Sep 2024 21:48:55 -0400 Subject: [PATCH] Override init cmd --- cmd/fairyringd/cmd/commands.go | 6 +- cmd/fairyringd/cmd/init.go | 188 +++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 cmd/fairyringd/cmd/init.go diff --git a/cmd/fairyringd/cmd/commands.go b/cmd/fairyringd/cmd/commands.go index e1946f17..2b6b5b0c 100644 --- a/cmd/fairyringd/cmd/commands.go +++ b/cmd/fairyringd/cmd/commands.go @@ -2,9 +2,10 @@ package cmd import ( "errors" - "github.com/Fairblock/fairyring/cmd/fairyringd/cmd/secp_encrypter" "io" + "github.com/Fairblock/fairyring/cmd/fairyringd/cmd/secp_encrypter" + "cosmossdk.io/log" confixcmd "cosmossdk.io/tools/confix/cmd" "github.com/CosmWasm/wasmd/x/wasm" @@ -39,7 +40,8 @@ func initRootCmd( basicManager module.BasicManager, ) { rootCmd.AddCommand( - genutilcli.InitCmd(basicManager, app.DefaultNodeHome), + InitCmd(basicManager, app.DefaultNodeHome), + // genutilcli.InitCmd(basicManager, app.DefaultNodeHome), debug.Cmd(), confixcmd.ConfigCommand(), pruning.Cmd(newApp, app.DefaultNodeHome), diff --git a/cmd/fairyringd/cmd/init.go b/cmd/fairyringd/cmd/init.go new file mode 100644 index 00000000..8e4bbcaa --- /dev/null +++ b/cmd/fairyringd/cmd/init.go @@ -0,0 +1,188 @@ +package cmd + +import ( + "bufio" + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "time" + + cfg "github.com/cometbft/cometbft/config" + "github.com/cosmos/go-bip39" + "github.com/spf13/cobra" + + errorsmod "cosmossdk.io/errors" + "cosmossdk.io/math/unsafe" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/input" + "github.com/cosmos/cosmos-sdk/server" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/version" + "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/cosmos/cosmos-sdk/x/genutil/types" +) + +const ( + // FlagOverwrite defines a flag to overwrite an existing genesis JSON file. + FlagOverwrite = "overwrite" + + // FlagSeed defines a flag to initialize the private validator key from a specific seed. + FlagRecover = "recover" + + // FlagDefaultBondDenom defines the default denom to use in the genesis file. + FlagDefaultBondDenom = "default-denom" +) + +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 + } + + _, err = fmt.Fprintf(os.Stderr, "%s\n", out) + + return err +} + +// InitCmd returns a command that initializes all files needed for Tendermint +// and the respective application. +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) + + // Update to a 1 second block time + config.Consensus.TimeoutCommit = time.Second + + chainID, _ := cmd.Flags().GetString(flags.FlagChainID) + switch { + case chainID != "": + case clientCtx.ChainID != "": + chainID = clientCtx.ChainID + default: + chainID = fmt.Sprintf("test-chain-%v", unsafe.Str(6)) + } + + // Get bip39 mnemonic + var mnemonic string + recover, _ := cmd.Flags().GetBool(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") + } + } + + // Get initial height + initHeight, _ := cmd.Flags().GetInt64(flags.FlagInitHeight) + if initHeight < 1 { + initHeight = 1 + } + + nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic) + if err != nil { + return err + } + + config.Moniker = args[0] + + genFile := config.GenesisFile() + overwrite, _ := cmd.Flags().GetBool(FlagOverwrite) + defaultDenom, _ := cmd.Flags().GetString(FlagDefaultBondDenom) + + // use os.Stat to check if the file exists + _, err = os.Stat(genFile) + if !overwrite && !os.IsNotExist(err) { + return fmt.Errorf("genesis.json file already exists: %v", genFile) + } + + // Overwrites the SDK default denom for side-effects + if defaultDenom != "" { + sdk.DefaultBondDenom = defaultDenom + } + appGenState := mbm.DefaultGenesis(cdc) + + appState, err := json.MarshalIndent(appGenState, "", " ") + if err != nil { + return errorsmod.Wrap(err, "Failed to marshal default genesis state") + } + + appGenesis := &types.AppGenesis{} + if _, err := os.Stat(genFile); err != nil { + if !os.IsNotExist(err) { + return err + } + } else { + appGenesis, err = types.AppGenesisFromFile(genFile) + if err != nil { + return errorsmod.Wrap(err, "Failed to read genesis doc from file") + } + } + + appGenesis.AppName = version.AppName + appGenesis.AppVersion = version.Version + appGenesis.ChainID = chainID + appGenesis.AppState = appState + appGenesis.InitialHeight = initHeight + appGenesis.Consensus = &types.ConsensusGenesis{ + Validators: nil, + } + + if err = genutil.ExportGenesisFile(appGenesis, genFile); err != nil { + return errorsmod.Wrap(err, "Failed to export genesis file") + } + + toPrint := newPrintInfo(config.Moniker, chainID, nodeID, "", appState) + + cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) + return displayInfo(toPrint) + }, + } + + cmd.Flags().String(flags.FlagHome, defaultNodeHome, "node's home directory") + cmd.Flags().BoolP(FlagOverwrite, "o", false, "overwrite the genesis.json file") + cmd.Flags().Bool(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") + cmd.Flags().String(FlagDefaultBondDenom, "", "genesis file default denomination, if left blank default value is 'stake'") + cmd.Flags().Int64(flags.FlagInitHeight, 1, "specify the initial block height at genesis") + + return cmd +}