Merge PR #6651: More CLI cleanup

This commit is contained in:
Alexander Bezobchuk
2020-07-08 13:57:45 -04:00
committed by GitHub
parent 589c1a531e
commit b25e3fc76d
7 changed files with 71 additions and 166 deletions
+3 -4
View File
@@ -6,10 +6,10 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/cli"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
@@ -18,7 +18,7 @@ import (
const flagGenTxDir = "gentx-dir"
// CollectGenTxsCmd - return the cobra command to collect genesis transactions
func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, defaultNodeHome string) *cobra.Command {
func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator) *cobra.Command {
cmd := &cobra.Command{
Use: "collect-gentxs",
Short: "Collect genesis txs and output a genesis.json file",
@@ -57,12 +57,11 @@ func CollectGenTxsCmd(genBalIterator types.GenesisBalancesIterator, defaultNodeH
toPrint.AppMessage = appMessage
// print out some key information
return displayInfo(cdc, toPrint)
},
}
cmd.Flags().String(cli.HomeFlag, defaultNodeHome, "node's home directory")
cmd.Flags().String(flags.FlagHome, "", "The application home directory")
cmd.Flags().String(flagGenTxDir, "", "override default \"gentx\" directory from which collect and execute genesis transactions; default [--home]/config/gentx/")
return cmd
+44 -48
View File
@@ -22,6 +22,7 @@ import (
"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"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
@@ -30,46 +31,52 @@ import (
)
// GenTxCmd builds the application's gentx command.
// nolint: errcheck
func GenTxCmd(mbm module.BasicManager, genBalIterator types.GenesisBalancesIterator, defaultNodeHome string) *cobra.Command {
func GenTxCmd(mbm module.BasicManager, genBalIterator types.GenesisBalancesIterator) *cobra.Command {
ipDefault, _ := server.ExternalIP()
fsCreateValidator, defaultsDesc := cli.CreateValidatorMsgFlagSet(ipDefault)
cmd := &cobra.Command{
Use: "gentx",
Use: "gentx [key_name]",
Short: "Generate a genesis tx carrying a self delegation",
Args: cobra.NoArgs,
Long: fmt.Sprintf(`This command is an alias of the 'tx create-validator' command'.
It creates a genesis transaction to create a validator.
The following default parameters are included:
%s`, defaultsDesc),
Args: cobra.ExactArgs(1),
Long: fmt.Sprintf(`Generate a genesis transaction that creates a validator with a self-delegation,
that is signed by the key in the Keyring referenced by a given name. A node ID and Bech32 consensus
pubkey may optionally be provided. If they are omitted, they will be retrieved from the priv_validator.json
file. The following default parameters are included:
%s
Example:
$ %s gentx my-key-name --home=/path/to/home/dir --keyring-backend=os --chain-id=test-chain-1 \
--amount=1000000stake \
--moniker="myvalidator" \
--commission-max-change-rate=0.01 \
--commission-max-rate=1.0 \
--commission-rate=0.07 \
--details="..." \
--security-contact="..." \
--website="..."
`, defaultsDesc, version.AppName,
),
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := server.GetServerContextFromCmd(cmd)
clientCtx := client.GetClientContextFromCmd(cmd)
cdc := clientCtx.JSONMarshaler
home, _ := cmd.Flags().GetString(flags.FlagHome)
config := serverCtx.Config
config.SetRoot(home)
config.SetRoot(clientCtx.HomeDir)
nodeID, valPubKey, err := genutil.InitializeNodeValidatorFiles(serverCtx.Config)
if err != nil {
return errors.Wrap(err, "failed to initialize node validator files")
}
// Read --nodeID, if empty take it from priv_validator.json
nodeIDString, _ := cmd.Flags().GetString(cli.FlagNodeID)
if nodeIDString != "" {
// read --nodeID, if empty take it from priv_validator.json
if nodeIDString, _ := cmd.Flags().GetString(cli.FlagNodeID); nodeIDString != "" {
nodeID = nodeIDString
}
// Read --pubkey, if empty take it from priv_validator.json
valPubKeyString, _ := cmd.Flags().GetString(cli.FlagPubKey)
if valPubKeyString != "" {
// read --pubkey, if empty take it from priv_validator.json
if valPubKeyString, _ := cmd.Flags().GetString(cli.FlagPubKey); valPubKeyString != "" {
valPubKey, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, valPubKeyString)
if err != nil {
return errors.Wrap(err, "failed to get consensus node public key")
@@ -92,27 +99,30 @@ func GenTxCmd(mbm module.BasicManager, genBalIterator types.GenesisBalancesItera
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
inBuf := bufio.NewReader(cmd.InOrStdin())
kb, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
kr, err := keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf)
if err != nil {
return errors.Wrap(err, "failed to initialize keybase")
return errors.Wrap(err, "failed to initialize keyring")
}
name, _ := cmd.Flags().GetString(flags.FlagName)
key, err := kb.Key(name)
name := args[0]
key, err := kr.Key(name)
if err != nil {
return errors.Wrap(err, "failed to read from keybase")
return errors.Wrapf(err, "failed to fetch '%s' from the keyring", name)
}
// Set flags for creating gentx
createValCfg, err := cli.PrepareConfigForTxCreateValidator(config, cmd.Flags(), nodeID, genDoc.ChainID, valPubKey)
moniker := config.Moniker
if m, _ := cmd.Flags().GetString(cli.FlagMoniker); m != "" {
moniker = m
}
// set flags for creating a gentx
createValCfg, err := cli.PrepareConfigForTxCreateValidator(cmd.Flags(), moniker, nodeID, genDoc.ChainID, valPubKey)
if err != nil {
return errors.Wrap(err, "error creating configuration to create validator msg")
}
// Fetch the amount of coins staked
amount, _ := cmd.Flags().GetString(cli.FlagAmount)
coins, err := sdk.ParseCoins(amount)
if err != nil {
return errors.Wrap(err, "failed to parse coins")
@@ -129,14 +139,7 @@ func GenTxCmd(mbm module.BasicManager, genBalIterator types.GenesisBalancesItera
}
txBldr = txBldr.WithTxEncoder(authclient.GetTxEncoder(clientCtx.Codec))
from, _ := cmd.Flags().GetString(flags.FlagFrom)
fromAddress, _, err := client.GetFromFields(txBldr.Keybase(), from, false)
if err != nil {
return errors.Wrap(err, "error getting from address")
}
clientCtx = clientCtx.WithInput(inBuf).WithFromAddress(fromAddress)
clientCtx = clientCtx.WithInput(inBuf).WithFromAddress(key.GetAddress())
// create a 'create-validator' message
txBldr, msg, err := cli.BuildCreateValidatorMsg(clientCtx, createValCfg, txBldr, true)
@@ -169,9 +172,7 @@ func GenTxCmd(mbm module.BasicManager, genBalIterator types.GenesisBalancesItera
return errors.Wrap(err, "failed to sign std tx")
}
// Fetch output file name
outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
if outputDocument == "" {
outputDocument, err = makeOutputFilepath(config.RootDir, nodeID)
if err != nil {
@@ -185,16 +186,13 @@ func GenTxCmd(mbm module.BasicManager, genBalIterator types.GenesisBalancesItera
cmd.PrintErrf("Genesis transaction written to %q\n", outputDocument)
return nil
},
}
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().String(flags.FlagName, "", "name of private key with which to sign the gentx")
cmd.Flags().String(flags.FlagOutputDocument, "", "write the genesis transaction JSON document to the given file instead of the default location")
cmd.Flags().String(flags.FlagHome, "", "The application home directory")
cmd.Flags().String(flags.FlagOutputDocument, "", "Write the genesis transaction JSON document to the given file instead of the default location")
cmd.Flags().String(flags.FlagChainID, "", "The network chain ID")
cmd.Flags().AddFlagSet(fsCreateValidator)
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.MarkFlagRequired(flags.FlagName)
flags.PostCommands(cmd)
@@ -238,5 +236,3 @@ func writeSignedGenTx(cdc codec.JSONMarshaler, outputDocument string, tx authtyp
return err
}
// DONTCOVER
+4 -16
View File
@@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/client"
@@ -329,6 +328,7 @@ func CreateValidatorMsgFlagSet(ipDefault string) (fs *flag.FlagSet, defaultsDesc
fsCreateValidator := flag.NewFlagSet("", flag.ContinueOnError)
fsCreateValidator.String(FlagIP, ipDefault, "The node's public IP")
fsCreateValidator.String(FlagNodeID, "", "The node's NodeID")
fsCreateValidator.String(FlagMoniker, "", "The validator's (optional) moniker")
fsCreateValidator.String(FlagWebsite, "", "The validator's (optional) website")
fsCreateValidator.String(FlagSecurityContact, "", "The validator's (optional) security contact email")
fsCreateValidator.String(FlagDetails, "", "The validator's (optional) details")
@@ -353,7 +353,6 @@ func CreateValidatorMsgFlagSet(ipDefault string) (fs *flag.FlagSet, defaultsDesc
type TxCreateValidatorConfig struct {
ChainID string
From string
NodeID string
Moniker string
@@ -374,9 +373,7 @@ type TxCreateValidatorConfig struct {
Identity string
}
func PrepareConfigForTxCreateValidator(
config *cfg.Config, flagSet *flag.FlagSet, nodeID, chainID string, valPubKey crypto.PubKey,
) (TxCreateValidatorConfig, error) {
func PrepareConfigForTxCreateValidator(flagSet *flag.FlagSet, moniker, nodeID, chainID string, valPubKey crypto.PubKey) (TxCreateValidatorConfig, error) {
c := TxCreateValidatorConfig{}
ip, err := flagSet.GetString(FlagIP)
@@ -413,12 +410,6 @@ func PrepareConfigForTxCreateValidator(
}
c.Identity = identity
c.ChainID = chainID
c.From, err = flagSet.GetString(flags.FlagName)
if err != nil {
return c, err
}
c.Amount, err = flagSet.GetString(FlagAmount)
if err != nil {
return c, err
@@ -447,15 +438,12 @@ func PrepareConfigForTxCreateValidator(
c.NodeID = nodeID
c.TrustNode = true
c.PubKey = sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey)
c.Moniker = config.Moniker
c.Website = website
c.SecurityContact = securityContact
c.Details = details
c.Identity = identity
if config.Moniker == "" {
c.Moniker = c.From
}
c.ChainID = chainID
c.Moniker = moniker
if c.Amount == "" {
c.Amount = defaultAmount
+8 -85
View File
@@ -4,12 +4,9 @@ import (
"testing"
"github.com/spf13/pflag"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/stretchr/testify/require"
cfg "github.com/tendermint/tendermint/config"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -18,21 +15,15 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
ip := "1.1.1.1"
nodeID := "nodeID"
valPubKey, _ := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, "cosmosvalconspub1zcjduepq7jsrkl9fgqk0wj3ahmfr8pgxj6vakj2wzn656s8pehh0zhv2w5as5gd80a")
moniker := "myMoniker"
moniker := "DefaultMoniker"
tests := []struct {
name string
config func() *cfg.Config
fsModify func(fs *pflag.FlagSet)
expectedCfg TxCreateValidatorConfig
}{
{
name: "all defaults",
config: func() *cfg.Config {
config := &cfg.Config{BaseConfig: cfg.TestBaseConfig()}
config.Moniker = moniker
return config
},
fsModify: func(fs *pflag.FlagSet) {
return
},
@@ -50,48 +41,14 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
MinSelfDelegation: "1",
},
},
{
name: "If moniker is empty it sets from Flag.",
config: func() *cfg.Config {
config := &cfg.Config{BaseConfig: cfg.TestBaseConfig()}
config.Moniker = ""
return config
},
fsModify: func(fs *pflag.FlagSet) {
fs.Set(flags.FlagName, "theNameFlag")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
From: "theNameFlag",
Moniker: "theNameFlag",
ChainID: chainID,
NodeID: nodeID,
TrustNode: true,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: "100000000stake",
CommissionRate: "0.1",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.01",
MinSelfDelegation: "1",
},
},
{
name: "Custom amount",
config: func() *cfg.Config {
config := &cfg.Config{BaseConfig: cfg.TestBaseConfig()}
config.Moniker = ""
return config
},
fsModify: func(fs *pflag.FlagSet) {
fs.Set(flags.FlagName, "theNameFlag")
fs.Set(FlagAmount, "2000stake")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
From: "theNameFlag",
Moniker: "theNameFlag",
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
TrustNode: true,
@@ -105,20 +62,12 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
},
{
name: "Custom commission rate",
config: func() *cfg.Config {
config := &cfg.Config{BaseConfig: cfg.TestBaseConfig()}
config.Moniker = ""
return config
},
fsModify: func(fs *pflag.FlagSet) {
fs.Set(flags.FlagName, "theNameFlag")
fs.Set(FlagCommissionRate, "0.54")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
From: "theNameFlag",
Moniker: "theNameFlag",
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
TrustNode: true,
@@ -132,20 +81,12 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
},
{
name: "Custom commission max rate",
config: func() *cfg.Config {
config := &cfg.Config{BaseConfig: cfg.TestBaseConfig()}
config.Moniker = ""
return config
},
fsModify: func(fs *pflag.FlagSet) {
fs.Set(flags.FlagName, "theNameFlag")
fs.Set(FlagCommissionMaxRate, "0.89")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
From: "theNameFlag",
Moniker: "theNameFlag",
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
TrustNode: true,
@@ -159,20 +100,12 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
},
{
name: "Custom commission max change rate",
config: func() *cfg.Config {
config := &cfg.Config{BaseConfig: cfg.TestBaseConfig()}
config.Moniker = ""
return config
},
fsModify: func(fs *pflag.FlagSet) {
fs.Set(flags.FlagName, "theNameFlag")
fs.Set(FlagCommissionMaxChangeRate, "0.55")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
From: "theNameFlag",
Moniker: "theNameFlag",
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
TrustNode: true,
@@ -186,20 +119,12 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
},
{
name: "Custom min self delegations",
config: func() *cfg.Config {
config := &cfg.Config{BaseConfig: cfg.TestBaseConfig()}
config.Moniker = ""
return config
},
fsModify: func(fs *pflag.FlagSet) {
fs.Set(flags.FlagName, "theNameFlag")
fs.Set(FlagMinSelfDelegation, "0.33")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
From: "theNameFlag",
Moniker: "theNameFlag",
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
TrustNode: true,
@@ -221,9 +146,7 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
tc.fsModify(fs)
config := tc.config()
cvCfg, err := PrepareConfigForTxCreateValidator(config, fs, nodeID, chainID, valPubKey)
cvCfg, err := PrepareConfigForTxCreateValidator(fs, moniker, nodeID, chainID, valPubKey)
require.NoError(t, err)
require.Equal(t, tc.expectedCfg, cvCfg)