From a417b3892664411ce76598f6eba55808ff913e2b Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Wed, 8 Jul 2020 22:00:34 +0200 Subject: [PATCH] Remove viper in Staking module. (#6631) --- tests/cli/helpers.go | 2 +- x/bank/types/params.go | 3 +- x/bank/types/params_test.go | 3 +- x/staking/client/cli/cli_test.go | 3 +- x/staking/client/cli/flags.go | 52 +++++++++++++++-------- x/staking/client/cli/query.go | 9 ++-- x/staking/client/cli/tx.go | 73 +++++++++++++++++++------------- 7 files changed, 91 insertions(+), 54 deletions(-) diff --git a/tests/cli/helpers.go b/tests/cli/helpers.go index b3030263b..daaa7dacd 100644 --- a/tests/cli/helpers.go +++ b/tests/cli/helpers.go @@ -74,7 +74,7 @@ func (f *Fixtures) AddGenesisAccount(address sdk.AccAddress, coins sdk.Coins, fl // GenTx is simd gentx func (f *Fixtures) GenTx(name string, flags ...string) { - cmd := fmt.Sprintf("%s gentx --name=%s --home=%s --home-client=%s --keyring-backend=test --chain-id=%s", f.SimdBinary, name, f.SimdHome, f.SimcliHome, f.ChainID) + cmd := fmt.Sprintf("%s gentx --name=%s --home=%s --keyring-backend=test --chain-id=%s", f.SimdBinary, name, f.SimdHome, f.ChainID) ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags)) } diff --git a/x/bank/types/params.go b/x/bank/types/params.go index 2d0398ef7..ed94ead34 100644 --- a/x/bank/types/params.go +++ b/x/bank/types/params.go @@ -3,9 +3,10 @@ package types import ( "fmt" + yaml "gopkg.in/yaml.v2" + sdk "github.com/cosmos/cosmos-sdk/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" - yaml "gopkg.in/yaml.v2" ) const ( diff --git a/x/bank/types/params_test.go b/x/bank/types/params_test.go index 502c83984..3bcb76e4d 100644 --- a/x/bank/types/params_test.go +++ b/x/bank/types/params_test.go @@ -3,8 +3,9 @@ package types import ( "testing" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + + sdk "github.com/cosmos/cosmos-sdk/types" ) func Test_validateSendEnabledParam(t *testing.T) { diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 9885f42e9..4bb58b05a 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -16,7 +16,8 @@ import ( ) func TestCLICreateValidator(t *testing.T) { - t.SkipNow() // TODO: Bring back once viper is refactored. + t.SkipNow() // Recreate when using CLI tests. + t.Parallel() f := cli.InitFixtures(t) diff --git a/x/staking/client/cli/flags.go b/x/staking/client/cli/flags.go index f272cf52a..094de343d 100644 --- a/x/staking/client/cli/flags.go +++ b/x/staking/client/cli/flags.go @@ -34,28 +34,14 @@ const ( // common flagsets to add to various functions var ( - fsShares = flag.NewFlagSet("", flag.ContinueOnError) - fsDescriptionCreate = flag.NewFlagSet("", flag.ContinueOnError) - fsCommissionUpdate = flag.NewFlagSet("", flag.ContinueOnError) - fsDescriptionEdit = flag.NewFlagSet("", flag.ContinueOnError) - fsValidator = flag.NewFlagSet("", flag.ContinueOnError) - fsRedelegation = flag.NewFlagSet("", flag.ContinueOnError) + fsShares = flag.NewFlagSet("", flag.ContinueOnError) + fsValidator = flag.NewFlagSet("", flag.ContinueOnError) + fsRedelegation = flag.NewFlagSet("", flag.ContinueOnError) ) func init() { fsShares.String(FlagSharesAmount, "", "Amount of source-shares to either unbond or redelegate as a positive integer or decimal") fsShares.String(FlagSharesFraction, "", "Fraction of source-shares to either unbond or redelegate as a positive integer or decimal >0 and <=1") - fsDescriptionCreate.String(FlagMoniker, "", "The validator's name") - fsDescriptionCreate.String(FlagIdentity, "", "The optional identity signature (ex. UPort or Keybase)") - fsDescriptionCreate.String(FlagWebsite, "", "The validator's (optional) website") - fsDescriptionCreate.String(FlagSecurityContact, "", "The validator's (optional) security contact email") - fsDescriptionCreate.String(FlagDetails, "", "The validator's (optional) details") - fsCommissionUpdate.String(FlagCommissionRate, "", "The new commission rate percentage") - fsDescriptionEdit.String(FlagMoniker, types.DoNotModifyDesc, "The validator's name") - fsDescriptionEdit.String(FlagIdentity, types.DoNotModifyDesc, "The (optional) identity signature (ex. UPort or Keybase)") - fsDescriptionEdit.String(FlagWebsite, types.DoNotModifyDesc, "The validator's (optional) website") - fsDescriptionEdit.String(FlagSecurityContact, types.DoNotModifyDesc, "The validator's (optional) security contact email") - fsDescriptionEdit.String(FlagDetails, types.DoNotModifyDesc, "The validator's (optional) details") fsValidator.String(FlagAddressValidator, "", "The Bech32 address of the validator") fsRedelegation.String(FlagAddressValidatorSrc, "", "The Bech32 address of the source validator") fsRedelegation.String(FlagAddressValidatorDst, "", "The Bech32 address of the destination validator") @@ -92,3 +78,35 @@ func FlagSetPublicKey() *flag.FlagSet { fs.String(FlagPubKey, "", "The Bech32 encoded PubKey of the validator") return fs } + +func flagSetDescriptionEdit() *flag.FlagSet { + fs := flag.NewFlagSet("", flag.ContinueOnError) + + fs.String(FlagMoniker, types.DoNotModifyDesc, "The validator's name") + fs.String(FlagIdentity, types.DoNotModifyDesc, "The (optional) identity signature (ex. UPort or Keybase)") + fs.String(FlagWebsite, types.DoNotModifyDesc, "The validator's (optional) website") + fs.String(FlagSecurityContact, types.DoNotModifyDesc, "The validator's (optional) security contact email") + fs.String(FlagDetails, types.DoNotModifyDesc, "The validator's (optional) details") + + return fs +} + +func flagSetCommissionUpdate() *flag.FlagSet { + fs := flag.NewFlagSet("", flag.ContinueOnError) + + fs.String(FlagCommissionRate, "", "The new commission rate percentage") + + return fs +} + +func flagSetDescriptionCreate() *flag.FlagSet { + fs := flag.NewFlagSet("", flag.ContinueOnError) + + fs.String(FlagMoniker, "", "The validator's name") + fs.String(FlagIdentity, "", "The optional identity signature (ex. UPort or Keybase)") + fs.String(FlagWebsite, "", "The validator's (optional) website") + fs.String(FlagSecurityContact, "", "The validator's (optional) security contact email") + fs.String(FlagDetails, "", "The validator's (optional) details") + + return fs +} diff --git a/x/staking/client/cli/query.go b/x/staking/client/cli/query.go index 29af34391..451dbc0c0 100644 --- a/x/staking/client/cli/query.go +++ b/x/staking/client/cli/query.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -147,7 +146,9 @@ $ %s query staking unbonding-delegations-from cosmosvaloper1gghjut3ccd8ay0zduzj6 return err } - bz, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit))) + page, _ := cmd.Flags().GetInt(flags.FlagPage) + limit, _ := cmd.Flags().GetInt(flags.FlagLimit) + bz, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr, page, limit)) if err != nil { return err } @@ -333,7 +334,9 @@ $ %s query staking delegations-to cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ld return err } - bz, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr, viper.GetInt(flags.FlagPage), viper.GetInt(flags.FlagLimit))) + page, _ := cmd.Flags().GetInt(flags.FlagPage) + limit, _ := cmd.Flags().GetInt(flags.FlagLimit) + bz, err := cdc.MarshalJSON(types.NewQueryValidatorParams(valAddr, page, limit)) if err != nil { return err } diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 0e87a77d0..65ca79f23 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -7,7 +7,6 @@ import ( "github.com/spf13/cobra" flag "github.com/spf13/pflag" - "github.com/spf13/viper" "github.com/tendermint/tendermint/crypto" "github.com/cosmos/cosmos-sdk/client" @@ -54,10 +53,11 @@ func NewCreateValidatorCmd(clientCtx client.Context) *cobra.Command { Use: "create-validator", Short: "create new validator initialized with a self-delegation to it", RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := clientCtx.InitWithInput(cmd.InOrStdin()) - txf := tx.NewFactoryFromDeprecated(clientCtx.Input).WithTxGenerator(clientCtx.TxGenerator).WithAccountRetriever(clientCtx.AccountRetriever) + clientCtx.WithInput(cmd.InOrStdin()) - txf, msg, err := NewBuildCreateValidatorMsg(clientCtx, txf) + txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxGenerator(clientCtx.TxGenerator).WithAccountRetriever(clientCtx.AccountRetriever) + + txf, msg, err := NewBuildCreateValidatorMsg(clientCtx, txf, cmd.Flags()) if err != nil { return err } @@ -66,7 +66,7 @@ func NewCreateValidatorCmd(clientCtx client.Context) *cobra.Command { } cmd.Flags().AddFlagSet(FlagSetPublicKey()) cmd.Flags().AddFlagSet(FlagSetAmount()) - cmd.Flags().AddFlagSet(fsDescriptionCreate) + cmd.Flags().AddFlagSet(flagSetDescriptionCreate()) cmd.Flags().AddFlagSet(FlagSetCommissionCreate()) cmd.Flags().AddFlagSet(FlagSetMinSelfDelegation()) @@ -86,20 +86,26 @@ func NewEditValidatorCmd(clientCtx client.Context) *cobra.Command { Use: "edit-validator", Short: "edit an existing validator account", RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := clientCtx.InitWithInput(cmd.InOrStdin()) + clientCtx := clientCtx.WithInput(cmd.InOrStdin()) valAddr := clientCtx.GetFromAddress() + + moniker, _ := cmd.Flags().GetString(FlagMoniker) + identity, _ := cmd.Flags().GetString(FlagIdentity) + website, _ := cmd.Flags().GetString(FlagWebsite) + security, _ := cmd.Flags().GetString(FlagSecurityContact) + details, _ := cmd.Flags().GetString(FlagDetails) description := types.NewDescription( - viper.GetString(FlagMoniker), - viper.GetString(FlagIdentity), - viper.GetString(FlagWebsite), - viper.GetString(FlagSecurityContact), - viper.GetString(FlagDetails), + moniker, + identity, + website, + security, + details, ) var newRate *sdk.Dec - commissionRate := viper.GetString(FlagCommissionRate) + commissionRate, _ := cmd.Flags().GetString(FlagCommissionRate) if commissionRate != "" { rate, err := sdk.NewDecFromStr(commissionRate) if err != nil { @@ -111,7 +117,7 @@ func NewEditValidatorCmd(clientCtx client.Context) *cobra.Command { var newMinSelfDelegation *sdk.Int - minSelfDelegationString := viper.GetString(FlagMinSelfDelegation) + minSelfDelegationString, _ := cmd.Flags().GetString(FlagMinSelfDelegation) if minSelfDelegationString != "" { msb, ok := sdk.NewIntFromString(minSelfDelegationString) if !ok { @@ -131,8 +137,8 @@ func NewEditValidatorCmd(clientCtx client.Context) *cobra.Command { }, } - cmd.Flags().AddFlagSet(fsDescriptionEdit) - cmd.Flags().AddFlagSet(fsCommissionUpdate) + cmd.Flags().AddFlagSet(flagSetDescriptionEdit()) + cmd.Flags().AddFlagSet(flagSetCommissionUpdate()) cmd.Flags().AddFlagSet(FlagSetMinSelfDelegation()) return cmd @@ -263,32 +269,38 @@ $ %s tx staking unbond cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100s return cmd } -func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory) (tx.Factory, sdk.Msg, error) { - amount, err := sdk.ParseCoin(viper.GetString(FlagAmount)) +func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) { + fAmount, _ := fs.GetString(FlagAmount) + amount, err := sdk.ParseCoin(fAmount) if err != nil { return txf, nil, err } valAddr := clientCtx.GetFromAddress() - pkStr := viper.GetString(FlagPubKey) + pkStr, _ := fs.GetString(FlagPubKey) pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkStr) if err != nil { return txf, nil, err } + moniker, _ := fs.GetString(FlagMoniker) + identity, _ := fs.GetString(FlagIdentity) + website, _ := fs.GetString(FlagWebsite) + security, _ := fs.GetString(FlagSecurityContact) + details, _ := fs.GetString(FlagDetails) description := types.NewDescription( - viper.GetString(FlagMoniker), - viper.GetString(FlagIdentity), - viper.GetString(FlagWebsite), - viper.GetString(FlagSecurityContact), - viper.GetString(FlagDetails), + moniker, + identity, + website, + security, + details, ) // get the initial validator commission parameters - rateStr := viper.GetString(FlagCommissionRate) - maxRateStr := viper.GetString(FlagCommissionMaxRate) - maxChangeRateStr := viper.GetString(FlagCommissionMaxChangeRate) + rateStr, _ := fs.GetString(FlagCommissionRate) + maxRateStr, _ := fs.GetString(FlagCommissionMaxRate) + maxChangeRateStr, _ := fs.GetString(FlagCommissionMaxChangeRate) commissionRates, err := buildCommissionRates(rateStr, maxRateStr, maxChangeRateStr) if err != nil { @@ -296,7 +308,7 @@ func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory) (tx.Fa } // get the initial validator min self delegation - msbStr := viper.GetString(FlagMinSelfDelegation) + msbStr, _ := fs.GetString(FlagMinSelfDelegation) minSelfDelegation, ok := sdk.NewIntFromString(msbStr) if !ok { @@ -310,9 +322,10 @@ func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory) (tx.Fa return txf, nil, err } - if viper.GetBool(flags.FlagGenerateOnly) { - ip := viper.GetString(FlagIP) - nodeID := viper.GetString(FlagNodeID) + genOnly, _ := fs.GetBool(flags.FlagGenerateOnly) + if genOnly { + ip, _ := fs.GetString(FlagIP) + nodeID, _ := fs.GetString(FlagNodeID) if nodeID != "" && ip != "" { txf = txf.WithMemo(fmt.Sprintf("%s@%s:26656", nodeID, ip))