From 8550d140710aedc019ebae7ff418cfdd43c48fc6 Mon Sep 17 00:00:00 2001 From: Karoly Albert Szabo Date: Wed, 27 Mar 2019 19:38:35 +0100 Subject: [PATCH] Gaiad gentx optional flags (#3897) Add website, details and identity to gentx Closes: #3858 --- ...-details-and-identity-to-gentx-cli-command | 1 + cmd/gaia/init/gentx.go | 16 +++- cmd/gaia/init/gentx_test.go | 85 +++++++++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 .pending/features/sdk/3858-add-website-details-and-identity-to-gentx-cli-command create mode 100644 cmd/gaia/init/gentx_test.go diff --git a/.pending/features/sdk/3858-add-website-details-and-identity-to-gentx-cli-command b/.pending/features/sdk/3858-add-website-details-and-identity-to-gentx-cli-command new file mode 100644 index 000000000000..0d55fa7f3015 --- /dev/null +++ b/.pending/features/sdk/3858-add-website-details-and-identity-to-gentx-cli-command @@ -0,0 +1 @@ +#3858 add website, details and identity to gentx cli command \ No newline at end of file diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go index 166c5a6b2784..970c77e0d861 100644 --- a/cmd/gaia/init/gentx.go +++ b/cmd/gaia/init/gentx.go @@ -47,6 +47,7 @@ func GenTxCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "gentx", Short: "Generate a genesis tx carrying a self delegation", + Args: cobra.NoArgs, Long: fmt.Sprintf(`This command is an alias of the 'gaiad tx create-validator' command'. It creates a genesis piece carrying a self delegation with the @@ -111,8 +112,12 @@ following delegation and commission default parameters: } } + website := viper.GetString(cli.FlagWebsite) + details := viper.GetString(cli.FlagDetails) + identity := viper.GetString(cli.FlagIdentity) + // Set flags for creating gentx - prepareFlagsForTxCreateValidator(config, nodeID, ip, genDoc.ChainID, valPubKey) + prepareFlagsForTxCreateValidator(config, nodeID, ip, genDoc.ChainID, valPubKey, website, details, identity) // Fetch the amount of coins staked amount := viper.GetString(cli.FlagAmount) @@ -200,6 +205,9 @@ following delegation and commission default parameters: "write the genesis transaction JSON document to the given file instead of the default location") cmd.Flags().String(cli.FlagIP, ip, "The node's public IP") cmd.Flags().String(cli.FlagNodeID, "", "The node's NodeID") + cmd.Flags().String(cli.FlagWebsite, "", "The validator's (optional) website") + cmd.Flags().String(cli.FlagDetails, "", "The validator's (optional) details") + cmd.Flags().String(cli.FlagIdentity, "", "The (optional) identity signature (ex. UPort or Keybase)") cmd.Flags().AddFlagSet(cli.FsCommissionCreate) cmd.Flags().AddFlagSet(cli.FsMinSelfDelegation) cmd.Flags().AddFlagSet(cli.FsAmount) @@ -237,9 +245,8 @@ func accountInGenesis(genesisState app.GenesisState, key sdk.AccAddress, coins s } func prepareFlagsForTxCreateValidator( - config *cfg.Config, nodeID, ip, chainID string, valPubKey crypto.PubKey, + config *cfg.Config, nodeID, ip, chainID string, valPubKey crypto.PubKey, website, details, identity string, ) { - viper.Set(tmcli.HomeFlag, viper.GetString(flagClientHome)) viper.Set(client.FlagChainID, chainID) viper.Set(client.FlagFrom, viper.GetString(client.FlagName)) @@ -247,6 +254,9 @@ func prepareFlagsForTxCreateValidator( viper.Set(cli.FlagIP, ip) viper.Set(cli.FlagPubKey, sdk.MustBech32ifyConsPub(valPubKey)) viper.Set(cli.FlagMoniker, config.Moniker) + viper.Set(cli.FlagWebsite, website) + viper.Set(cli.FlagDetails, details) + viper.Set(cli.FlagIdentity, identity) if config.Moniker == "" { viper.Set(cli.FlagMoniker, viper.GetString(client.FlagName)) diff --git a/cmd/gaia/init/gentx_test.go b/cmd/gaia/init/gentx_test.go new file mode 100644 index 000000000000..d99a562fc03c --- /dev/null +++ b/cmd/gaia/init/gentx_test.go @@ -0,0 +1,85 @@ +package init + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/server" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking/client/cli" + "github.com/spf13/viper" + "github.com/stretchr/testify/require" + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" + cfg "github.com/tendermint/tendermint/config" + "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/libs/log" +) + +func Test_prepareFlagsForTxCreateValidator(t *testing.T) { + defer server.SetupViper(t)() + defer setupClientHome(t)() + config, err := tcmd.ParseConfig() + require.Nil(t, err) + logger := log.NewNopLogger() + ctx := server.NewContext(config, logger) + + valPubKey, _ := sdk.GetConsPubKeyBech32("cosmosvalconspub1zcjduepq7jsrkl9fgqk0wj3ahmfr8pgxj6vakj2wzn656s8pehh0zhv2w5as5gd80a") + + type args struct { + config *cfg.Config + nodeID string + ip string + chainID string + valPubKey crypto.PubKey + website string + details string + identity string + } + + type extraParams struct { + amount string + commissionRate string + commissionMaxRate string + commissionMaxChangeRate string + minSelfDelegation string + } + + type testcase struct { + name string + args args + } + + runTest := func(t *testing.T, tt testcase, params extraParams) { + prepareFlagsForTxCreateValidator(tt.args.config, tt.args.nodeID, tt.args.ip, tt.args.chainID, tt.args.valPubKey, tt.args.website, tt.args.details, tt.args.identity) + require.Equal(t, tt.args.website, viper.GetString(cli.FlagWebsite)) + require.Equal(t, tt.args.details, viper.GetString(cli.FlagDetails)) + require.Equal(t, tt.args.identity, viper.GetString(cli.FlagIdentity)) + require.Equal(t, params.amount, viper.GetString(cli.FlagAmount)) + require.Equal(t, params.commissionRate, viper.GetString(cli.FlagCommissionRate)) + require.Equal(t, params.commissionMaxRate, viper.GetString(cli.FlagCommissionMaxRate)) + require.Equal(t, params.commissionMaxChangeRate, viper.GetString(cli.FlagCommissionMaxChangeRate)) + require.Equal(t, params.minSelfDelegation, viper.GetString(cli.FlagMinSelfDelegation)) + } + + tests := []testcase{ + {"No parameters", args{ctx.Config, "X", "0.0.0.0", "chainId", valPubKey, "", "", ""}}, + {"Optional parameters fed", args{ctx.Config, "X", "0.0.0.0", "chainId", valPubKey, "cosmos.network", "details", "identity"}}, + } + + defaultParams := extraParams{defaultAmount, defaultCommissionRate, defaultCommissionMaxRate, defaultCommissionMaxChangeRate, defaultMinSelfDelegation} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(t *testing.T) { runTest(t, tt, defaultParams) }) + }) + } + + // Override default params + params := extraParams{"5stake", "1.0", "1.0", "1.0", "1.0"} + viper.Set(cli.FlagAmount, params.amount) + viper.Set(cli.FlagCommissionRate, params.commissionRate) + viper.Set(cli.FlagCommissionMaxRate, params.commissionMaxRate) + viper.Set(cli.FlagCommissionMaxChangeRate, params.commissionMaxChangeRate) + viper.Set(cli.FlagMinSelfDelegation, params.minSelfDelegation) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { runTest(t, tt, params) }) + } +}