Skip to content

Commit

Permalink
Remove viper in Staking module. (cosmos#6631)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgimeno authored Jul 8, 2020
1 parent b25e3fc commit a417b38
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 54 deletions.
2 changes: 1 addition & 1 deletion tests/cli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
3 changes: 2 additions & 1 deletion x/bank/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion x/bank/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion x/staking/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
52 changes: 35 additions & 17 deletions x/staking/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
9 changes: 6 additions & 3 deletions x/staking/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
73 changes: 43 additions & 30 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxGenerator(clientCtx.TxGenerator).WithAccountRetriever(clientCtx.AccountRetriever)

txf, msg, err := NewBuildCreateValidatorMsg(clientCtx, txf)
txf, msg, err := NewBuildCreateValidatorMsg(clientCtx, txf, cmd.Flags())
if err != nil {
return err
}
Expand All @@ -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())

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -263,40 +269,46 @@ $ %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 {
return txf, nil, err
}

// get the initial validator min self delegation
msbStr := viper.GetString(FlagMinSelfDelegation)
msbStr, _ := fs.GetString(FlagMinSelfDelegation)

minSelfDelegation, ok := sdk.NewIntFromString(msbStr)
if !ok {
Expand All @@ -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))
Expand Down

0 comments on commit a417b38

Please sign in to comment.