Skip to content

Commit

Permalink
chore: bring join command to root
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 committed Jan 5, 2024
1 parent 399da75 commit b92181b
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 134 deletions.
2 changes: 1 addition & 1 deletion DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ BIN=./build/seda-chaind

$BIN tendermint unsafe-reset-all
rm -rf ~/.seda-chain
$BIN init new node0
$BIN init node0

$BIN keys add satoshi --keyring-backend test
$BIN add-genesis-account $($BIN keys show satoshi --keyring-backend test -a) 10000000000000000seda
Expand Down
130 changes: 2 additions & 128 deletions cmd/seda-chaind/cmd/init_cmds.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"

cfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/libs/cli"
cmtos "github.com/cometbft/cometbft/libs/os"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"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"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"

"github.com/sedaprotocol/seda-chain/app/params"
)

const (
Expand All @@ -33,132 +22,17 @@ const (
FlagRecover = "recover"
// FlagNetwork defines a flag to indicate which network to connect to.
FlagNetwork = "network"

// Default things
defaultChainID = "sedachain"
)

// add initialization commands
func InitCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize private validator, p2p, genesis, and application configuration files",
}
cmd.AddCommand(newNetworkCmd(mbm, defaultNodeHome))
cmd.AddCommand(joinNetworkCommand(mbm, defaultNodeHome))
return cmd
}

// preserve old logic for if we want to create a new network
// though its slightly modified to set default settings.
func newNetworkCmd(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "new [moniker]",
Short: "Initialize private validator, p2p, genesis, and application configuration files",
Long: `Initialize validator and node configuration files for a new network.`,
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)

var mnemonic string
var err error
if recover, _ := cmd.Flags().GetBool(FlagRecover); recover {
mnemonic, err = readInMnemonic(cmd)
if err != nil {
return err
}
}

// get chain ID
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)

// get initial height
initHeight, _ := cmd.Flags().GetInt64(flags.FlagInitHeight)
if initHeight < 1 {
initHeight = 1
}

// Before initializing the node, if a mnemonic is not given and
// the private validator key file does not exist, create a validator
// using a key generated on secp256k1.
if len(mnemonic) == 0 && !cmtos.FileExists(config.PrivValidatorKeyFile()) {
err = generateValidatorWithSecp256k1Key(config)
if err != nil {
return err
}
}

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

// write to config file and genesis file and display info
config.Moniker = args[0]

overwrite, _ := cmd.Flags().GetBool(FlagOverwrite)
genFile := config.GenesisFile()
// 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)
}

sdk.DefaultBondDenom = params.DefaultBondDenom
appGenState := mbm.DefaultGenesis(cdc)

appState, err := json.MarshalIndent(appGenState, "", " ")
if err != nil {
return errors.Wrap(err, "Failed to marshal default genesis state")
}

if _, err := os.Stat(genFile); err != nil {
if !os.IsNotExist(err) {
return err
}
}

appGenesis := &genutiltypes.AppGenesis{
ChainID: chainID,
AppState: appState,
Consensus: &genutiltypes.ConsensusGenesis{
Validators: nil,
},
InitialHeight: initHeight,
}

if err = genutil.ExportGenesisFile(appGenesis, genFile); err != nil {
return errors.Wrap(err, "Failed to export genesis file")
}
toPrint := newPrintInfo(config.Moniker, chainID, nodeID, "")
cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)
return displayInfo(toPrint)
},
}

cmd.Flags().String(cli.HomeFlag, 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, defaultChainID, "genesis file chain-id")
cmd.Flags().Int64(flags.FlagInitHeight, 1, "specify the initial block height at genesis")

return cmd
}

func joinNetworkCommand(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
func JoinNetworkCommand(mbm module.BasicManager, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "join [moniker]",
Short: "Grabs an existing network configuration and initializes node based on it",
Long: strings.TrimSpace(
fmt.Sprintf(`Initialize validator and node configuration files for an existing network.
Example:
$ %s init join moniker --network devnet
$ %s join moniker --network devnet
`,
version.AppName,
),
Expand Down
3 changes: 2 additions & 1 deletion cmd/seda-chaind/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig app.EncodingConfig, basi

gentxModule := app.ModuleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic)
rootCmd.AddCommand(
InitCmd(basicManager, app.DefaultNodeHome),
genutilcli.InitCmd(basicManager, app.DefaultNodeHome),
JoinNetworkCommand(basicManager, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(
banktypes.GenesisBalancesIterator{},
app.DefaultNodeHome,
Expand Down
2 changes: 1 addition & 1 deletion scripts/local_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rm -rf ~/.seda-chain || true
$BIN config set client chain-id sedachain

# initialize the chain
$BIN init new node0
$BIN init node0

cat $HOME/.seda-chain/config/genesis.json | jq '.app_state["gov"]["voting_params"]["voting_period"]="30s"' > $HOME/.seda-chain/config/tmp_genesis.json && mv $HOME/.seda-chain/config/tmp_genesis.json $HOME/.seda-chain/config/genesis.json
# cat $HOME/.seda-chain/config/genesis.json | jq '.app_state["gov"]["params"]["voting_period"]="30s"' > $HOME/.seda-chain/config/tmp_genesis.json && mv $HOME/.seda-chain/config/tmp_genesis.json $HOME/.seda-chain/config/genesis.json
Expand Down
2 changes: 1 addition & 1 deletion scripts/testnet/add_wasm_state_to_genesis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ TEMP_CHAIN_ID=temp-seda-chain
#
# SCRIPT BEGINS - START CHAIN
#
$BIN init new node0 --home $TMP_HOME --chain-id $TEMP_CHAIN_ID
$BIN init node0 --home $TMP_HOME --chain-id $TEMP_CHAIN_ID

cat $TMP_HOME/config/genesis.json | jq '.consensus["params"]["validator"]["pub_key_types"]=["secp256k1"]' > $TMP_HOME/config/tmp_genesis.json && mv $TMP_HOME/config/tmp_genesis.json $TMP_HOME/config/genesis.json

Expand Down
4 changes: 2 additions & 2 deletions scripts/testnet/create_genesis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ rm -rf $NODE_DIR
#
# CREATE GENESIS AND ADJUST GOV PARAMETERS
#
$BIN init new node0 --chain-id $CHAIN_ID
$BIN init node0 --chain-id $CHAIN_ID

cat $HOME/.seda-chain/config/genesis.json | jq '.app_state["gov"]["voting_params"]["voting_period"]="180s"' > $HOME/.seda-chain/config/tmp_genesis.json && mv $HOME/.seda-chain/config/tmp_genesis.json $HOME/.seda-chain/config/genesis.json
cat $HOME/.seda-chain/config/genesis.json | jq '.app_state["gov"]["params"]["voting_period"]="180s"' > $HOME/.seda-chain/config/tmp_genesis.json && mv $HOME/.seda-chain/config/tmp_genesis.json $HOME/.seda-chain/config/genesis.json
Expand Down Expand Up @@ -58,7 +58,7 @@ for i in ${!MONIKERS[@]}; do
INDIVIDUAL_VAL_HOME_DIR=$NODE_DIR/${MONIKERS[$i]}
INDIVIDUAL_VAL_CONFIG_DIR="$INDIVIDUAL_VAL_HOME_DIR/config"

$BIN init new ${MONIKERS[$i]} --home $INDIVIDUAL_VAL_HOME_DIR --chain-id $CHAIN_ID
$BIN init ${MONIKERS[$i]} --home $INDIVIDUAL_VAL_HOME_DIR --chain-id $CHAIN_ID
$BIN keys add ${MONIKERS[$i]} --keyring-backend=test --home $INDIVIDUAL_VAL_HOME_DIR

VALIDATOR_ADDRESS=$($BIN keys show ${MONIKERS[$i]} --keyring-backend test --home $INDIVIDUAL_VAL_HOME_DIR -a)
Expand Down

0 comments on commit b92181b

Please sign in to comment.