Skip to content

Commit

Permalink
refactor: improve init command
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Oct 12, 2023
1 parent a3c4930 commit 7e00437
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
60 changes: 31 additions & 29 deletions cmd/seda-chaind/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,18 @@ const (
)

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"`
Moniker string `json:"moniker" yaml:"moniker"`
ChainID string `json:"chain_id" yaml:"chain_id"`
NodeID string `json:"node_id" yaml:"node_id"`
Seeds string `json:"seeds" yaml:"seeds"`
}

func newPrintInfo(moniker, chainID, nodeID, genTxsDir string, appMessage json.RawMessage) printInfo {
func newPrintInfo(moniker, chainID, nodeID, seeds string) printInfo {
return printInfo{
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
GenTxsDir: genTxsDir,
AppMessage: appMessage,
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
Seeds: seeds,
}
}

Expand All @@ -62,7 +60,6 @@ func displayInfo(info printInfo) error {
}

_, err = fmt.Fprintf(os.Stderr, "%s\n", sdk.MustSortJSON(out))

return err
}

Expand Down Expand Up @@ -155,7 +152,7 @@ func newNetworkCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Comma
if err = genutil.ExportGenesisFile(genDoc, genFile); err != nil {
return errors.Wrap(err, "Failed to export genesis file")
}
toPrint := newPrintInfo(config.Moniker, ChainID, nodeID, "", appState)
toPrint := newPrintInfo(config.Moniker, ChainID, nodeID, "")
cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)
return displayInfo(toPrint)
},
Expand All @@ -169,7 +166,7 @@ func newNetworkCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Comma
return cmd
}

func joinNetwork(network, configDir, genesisFilePath, mnemonic string, config *cfg.Config) error {
func joinNetwork(network, configDir, genesisFilePath, seedsFilePath, configFilePath, mnemonic string, config *cfg.Config) error {
err := utils.DownloadGitFiles(network, configDir)
if err != nil {
return errors.Wrapf(err, "failed to download network `%s` genesis files", network)
Expand All @@ -179,31 +176,38 @@ func joinNetwork(network, configDir, genesisFilePath, mnemonic string, config *c
if err != nil {
return err
}

var genesisExistingState map[string]json.RawMessage
err = json.Unmarshal(bytes, &genesisExistingState)
if err != nil {
return err
}

genesisState, err := json.MarshalIndent(genesisExistingState, "", " ")
_, err = json.MarshalIndent(genesisExistingState, "", " ")
if err != nil {
return errors.Wrapf(err, "Failed to marshal network `%s` genesis state", network)
}

nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic)
// obtain seeds from seeds file and write to config file
seedsBytes, err := os.ReadFile(seedsFilePath)
if err != nil {
return err
}
seeds := string(seedsBytes)

toPrint := newPrintInfo(config.Moniker, ChainID, nodeID, "", genesisState)
config.P2P.Seeds = seeds
cfg.WriteConfigFile(configFilePath, config)

nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic)
if err != nil {
return err
}

toPrint := newPrintInfo(config.Moniker, ChainID, nodeID, seeds)
return displayInfo(toPrint)
}

func existingNetworkComand(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "network [moniker]",
Use: "join [moniker]",
Short: "Grabs an existing network genesis configuration.",
Long: `Initialize validators's and node's configuration files from an existing configuration.`,
Args: cobra.ExactArgs(1),
Expand All @@ -222,6 +226,9 @@ func existingNetworkComand(mbm module.BasicManager, defaultNodeHome string) *cob
overwrite, _ := cmd.Flags().GetBool(FlagOverwrite)
configDir := filepath.Join(config.RootDir, "config")
genesisFilePath := filepath.Join(configDir, "genesis.json")
seedsFilePath := filepath.Join(configDir, "seeds.txt")
configFilePath := filepath.Join(configDir, "config.toml")

// use os.Stat to check if the file exists
_, err = os.Stat(genesisFilePath)
if !overwrite && !os.IsNotExist(err) {
Expand All @@ -239,14 +246,10 @@ func existingNetworkComand(mbm module.BasicManager, defaultNodeHome string) *cob
}

// TODO should turn the insides here into a function for when we have more than one network
switch network {
case "devnet":
return joinNetwork(network, configDir, genesisFilePath, mnemonic, config)
case "localnet":
return joinNetwork(network, configDir, genesisFilePath, mnemonic, config)
default:
return fmt.Errorf("unsupported network type: %s", network)
if network == "devnet" || network == "testnet" || network == "localnet" {
return joinNetwork(network, configDir, genesisFilePath, seedsFilePath, configFilePath, mnemonic, config)
}
return fmt.Errorf("unsupported network type: %s", network)
},
}

Expand All @@ -261,14 +264,13 @@ func existingNetworkComand(mbm module.BasicManager, defaultNodeHome string) *cob
// and the respective application.
func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "init <new | newtwork> [moniker]",
Use: "init <new | join> [moniker]",
Short: "Initialize private validator, p2p, genesis, and application configuration files",
Long: `Initialize validators's and node's configuration files.`,
Args: cobra.ExactArgs(1),
}

cmd.AddCommand(newNetworkCmd(mbm, defaultNodeHome))
cmd.AddCommand(existingNetworkComand(mbm, defaultNodeHome))

return cmd
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cosmossdk.io/api v0.3.1
cosmossdk.io/errors v1.0.0
cosmossdk.io/math v1.0.1
github.com/BurntSushi/toml v1.2.1
github.com/CosmWasm/wasmd v0.41.0
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ github.com/99designs/keyring v1.2.1 h1:tYLp1ULvO7i3fI5vE21ReQuj99QFSs7lGm0xWyJo8
github.com/99designs/keyring v1.2.1/go.mod h1:fc+wB5KTk9wQ9sDx0kFXB3A0MaeGHM9AwRStKOQ5vOA=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQcITbvhmL4+C4cKA87NW0tfm3Kl9VXRoPywFg=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4=
Expand Down

0 comments on commit 7e00437

Please sign in to comment.