Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create-validator now takes a json file as arg #14864

Merged
merged 43 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4cc3059
wip: take json file as arg for create-validator cmd
likhita-809 Feb 1, 2023
53ab328
wip: cleanup
likhita-809 Feb 1, 2023
98caf65
wip: more cleanup
likhita-809 Feb 1, 2023
545471b
remove unncessary fields from validator struct
likhita-809 Feb 1, 2023
2b2d84c
wip: more cleanup
likhita-809 Feb 1, 2023
0aabe92
wip: refactor code
likhita-809 Feb 1, 2023
c358a48
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 1, 2023
540c20e
add tests
likhita-809 Feb 1, 2023
434e8e9
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 1, 2023
6eeabda
remove unused flags from create-validator cmd
likhita-809 Feb 2, 2023
50e335b
wip: address review comments and add changelog
likhita-809 Feb 2, 2023
f766120
fix relevant tests
likhita-809 Feb 2, 2023
c2b85aa
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 2, 2023
1cf4d15
add optional json fields and remove flags
likhita-809 Feb 2, 2023
1a4ca2e
fix warnings and tests
likhita-809 Feb 2, 2023
139f3d5
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 2, 2023
56861aa
fix tests
likhita-809 Feb 2, 2023
edb60c1
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 2, 2023
09c41c5
use explicit Example cobra field in create-validator cmd
likhita-809 Feb 3, 2023
2f2a81d
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 3, 2023
a769166
wip: use interface for pubkey
likhita-809 Feb 3, 2023
348b947
wip: use json.RawMessage for pubkey type
likhita-809 Feb 6, 2023
47bcc81
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 6, 2023
2bbba8f
fix tests
likhita-809 Feb 6, 2023
e346655
address review comments
likhita-809 Feb 6, 2023
631cf02
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 6, 2023
97ef612
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 6, 2023
bd3a134
Merge branch 'main' into likhita/val-json-wip
atheeshp Feb 6, 2023
0481b64
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 6, 2023
1ba91f6
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 7, 2023
b50b47b
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 7, 2023
41b2a84
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 8, 2023
e4b67f4
Merge branch 'main' into likhita/val-json-wip
atheeshp Feb 9, 2023
42f921b
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 10, 2023
36eceeb
Merge branch 'main' into likhita/val-json-wip
atheeshp Feb 13, 2023
7299331
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 14, 2023
28c516b
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 15, 2023
4a5fc22
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 16, 2023
c825b2f
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into likh…
likhita-809 Feb 20, 2023
3f67566
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 21, 2023
34f340d
fix tests
likhita-809 Feb 21, 2023
10204a4
Merge branch 'main' into likhita/val-json-wip
atheeshp Feb 21, 2023
cb31f4b
Merge branch 'main' into likhita/val-json-wip
likhita-809 Feb 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (x/staking) [14864](https://github.com/cosmos/cosmos-sdk/pull/14864) `create-validator` CLI command now takes a json file as an arg instead of having a bunch of required flags to it.
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
* (store) [#14410](https://github.com/cosmos/cosmos-sdk/pull/14410) `rootmulti.Store.loadVersion` has validation to check if all the module stores' height is correct, it will error if any module store has incorrect height.
* (x/evidence) [#14757](https://github.com/cosmos/cosmos-sdk/pull/14757) Evidence messages do not need to implement a `.Type()` anymore.
* (x/auth/tx) [#14751](https://github.com/cosmos/cosmos-sdk/pull/14751) Remove `.Type()` and `Route()` methods from all msgs and `legacytx.LegacyMsg` interface.
Expand Down
81 changes: 53 additions & 28 deletions tests/e2e/staking/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
Expand Down Expand Up @@ -97,11 +96,6 @@ func (s *E2ETestSuite) TestNewCreateValidatorCmd() {
require := s.Require()
val := s.network.Validators[0]

consPrivKey := ed25519.GenPrivKey()
consPubKeyBz, err := s.cfg.Codec.MarshalInterfaceJSON(consPrivKey.PubKey())
require.NoError(err)
require.NotNil(consPubKeyBz)

k, _, err := val.ClientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
require.NoError(err)

Expand All @@ -120,6 +114,55 @@ func (s *E2ETestSuite) TestNewCreateValidatorCmd() {
require.NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())

validJSON := fmt.Sprintf(`
{
"pubkey": "{\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"oWg2ISpLF405Jcm2vXV+2v4fnjodh6aafuIdeoW+rUw=\"}",
"amount": "%dstake",
"moniker": "NewValidator",
"commission-rate": "0.5",
"commission-max-rate": "1.0",
"commission-max-change-rate": "0.1",
"min-self-delegation": "1"
}`, 100)
validJSONFile := testutil.WriteToNewTempFile(s.T(), validJSON)
defer validJSONFile.Close()
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved

noAmountJSON := `
{
"pubkey": "{\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"oWg2ISpLF405Jcm2vXV+2v4fnjodh6aafuIdeoW+rUw=\"}",
"moniker": "NewValidator",
"commission-rate": "0.5",
"commission-max-rate": "1.0",
"commission-max-change-rate": "0.1",
"min-self-delegation": "1"
}`
noAmountJSONFile := testutil.WriteToNewTempFile(s.T(), noAmountJSON)
defer noAmountJSONFile.Close()
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

noPubKeyJSON := fmt.Sprintf(`
{
"amount": "%dstake",
"moniker": "NewValidator",
"commission-rate": "0.5",
"commission-max-rate": "1.0",
"commission-max-change-rate": "0.1",
"min-self-delegation": "1"
}`, 100)
noPubKeyJSONFile := testutil.WriteToNewTempFile(s.T(), noPubKeyJSON)
defer noPubKeyJSONFile.Close()
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

noMonikerJSON := fmt.Sprintf(`
{
"pubkey": "{\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"oWg2ISpLF405Jcm2vXV+2v4fnjodh6aafuIdeoW+rUw=\"}",
"amount": "%dstake",
"commission-rate": "0.5",
"commission-max-rate": "1.0",
"commission-max-change-rate": "0.1",
"min-self-delegation": "1"
}`, 100)
noMonikerJSONFile := testutil.WriteToNewTempFile(s.T(), noMonikerJSON)
defer noMonikerJSONFile.Close()
Fixed Show fixed Hide fixed

testCases := []struct {
name string
args []string
Expand All @@ -130,14 +173,11 @@ func (s *E2ETestSuite) TestNewCreateValidatorCmd() {
{
"invalid transaction (missing amount)",
[]string{
noAmountJSONFile.Name(),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
fmt.Sprintf("--%s='Hey, I am a new validator. Please delegate!'", cli.FlagDetails),
fmt.Sprintf("--%s=0.5", cli.FlagCommissionRate),
fmt.Sprintf("--%s=1.0", cli.FlagCommissionMaxRate),
fmt.Sprintf("--%s=0.1", cli.FlagCommissionMaxChangeRate),
fmt.Sprintf("--%s=1", cli.FlagMinSelfDelegation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
Expand All @@ -148,15 +188,11 @@ func (s *E2ETestSuite) TestNewCreateValidatorCmd() {
{
"invalid transaction (missing pubkey)",
[]string{
fmt.Sprintf("--%s=%dstake", cli.FlagAmount, 100),
noPubKeyJSONFile.Name(),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
fmt.Sprintf("--%s='Hey, I am a new validator. Please delegate!'", cli.FlagDetails),
fmt.Sprintf("--%s=0.5", cli.FlagCommissionRate),
fmt.Sprintf("--%s=1.0", cli.FlagCommissionMaxRate),
fmt.Sprintf("--%s=0.1", cli.FlagCommissionMaxChangeRate),
fmt.Sprintf("--%s=1", cli.FlagMinSelfDelegation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
Expand All @@ -167,16 +203,11 @@ func (s *E2ETestSuite) TestNewCreateValidatorCmd() {
{
"invalid transaction (missing moniker)",
[]string{
fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKeyBz),
fmt.Sprintf("--%s=%dstake", cli.FlagAmount, 100),
noMonikerJSONFile.Name(),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
fmt.Sprintf("--%s='Hey, I am a new validator. Please delegate!'", cli.FlagDetails),
fmt.Sprintf("--%s=0.5", cli.FlagCommissionRate),
fmt.Sprintf("--%s=1.0", cli.FlagCommissionMaxRate),
fmt.Sprintf("--%s=0.1", cli.FlagCommissionMaxChangeRate),
fmt.Sprintf("--%s=1", cli.FlagMinSelfDelegation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
Expand All @@ -187,17 +218,11 @@ func (s *E2ETestSuite) TestNewCreateValidatorCmd() {
{
"valid transaction",
[]string{
fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKeyBz),
fmt.Sprintf("--%s=%dstake", cli.FlagAmount, 100),
fmt.Sprintf("--%s=NewValidator", cli.FlagMoniker),
validJSONFile.Name(),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
fmt.Sprintf("--%s='Hey, I am a new validator. Please delegate!'", cli.FlagDetails),
fmt.Sprintf("--%s=0.5", cli.FlagCommissionRate),
fmt.Sprintf("--%s=1.0", cli.FlagCommissionMaxRate),
fmt.Sprintf("--%s=0.1", cli.FlagCommissionMaxChangeRate),
fmt.Sprintf("--%s=1", cli.FlagMinSelfDelegation),
fmt.Sprintf("--%s=%s", flags.FlagFrom, newAddr),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
Expand Down
1 change: 0 additions & 1 deletion x/staking/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func flagSetCommissionUpdate() *flag.FlagSet {
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")
Expand Down
77 changes: 31 additions & 46 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,43 @@ func NewTxCmd() *cobra.Command {
// NewCreateValidatorCmd returns a CLI command handler for creating a MsgCreateValidator transaction.
func NewCreateValidatorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-validator",
Use: "create-validator [path/to/validator.json]",
Short: "create new validator initialized with a self-delegation to it",
Args: cobra.ExactArgs(1),
Long: strings.TrimSpace(
fmt.Sprintf(`Submit a JSON file with the new validator details.
Example:
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
$ %s tx staking create-validator path/to/validator.json --from keyname

Where validator.json contains:

{
"pubkey": "{\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"oWg2ISpLF405Jcm2vXV+2v4fnjodh6aafuIdeoW+rUw=\"}",
"amount": "1000000stake",
"moniker": "myvalidator",
"commission-rate": "0.1",
"commission-max-rate": "0.2",
"commission-max-change-rate": "0.01",
"min-self-delegation": "1"
}
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

validator, err := parseAndValidateValidatorJSON(clientCtx.Codec, args[0])
if err != nil {
return err
}

txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).
WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)
txf, msg, err := newBuildCreateValidatorMsg(clientCtx, txf, cmd.Flags())
txf, msg, err := newBuildCreateValidatorMsg(clientCtx, txf, cmd.Flags(), validator)
if err != nil {
return err
}
Expand All @@ -75,20 +101,13 @@ func NewCreateValidatorCmd() *cobra.Command {
},
}

cmd.Flags().AddFlagSet(FlagSetPublicKey())
cmd.Flags().AddFlagSet(FlagSetAmount())
cmd.Flags().AddFlagSet(flagSetDescriptionCreate())
cmd.Flags().AddFlagSet(FlagSetCommissionCreate())
cmd.Flags().AddFlagSet(FlagSetMinSelfDelegation())

cmd.Flags().String(FlagIP, "", fmt.Sprintf("The node's public IP. It takes effect only when used in combination with --%s", flags.FlagGenerateOnly))
cmd.Flags().String(FlagNodeID, "", "The node's ID")
flags.AddTxFlagsToCmd(cmd)

_ = cmd.MarkFlagRequired(flags.FlagFrom)
_ = cmd.MarkFlagRequired(FlagAmount)
_ = cmd.MarkFlagRequired(FlagPubKey)
_ = cmd.MarkFlagRequired(FlagMoniker)

return cmd
}
Expand Down Expand Up @@ -337,57 +356,23 @@ $ %s tx staking cancel-unbond %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100stake
return cmd
}

func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, *types.MsgCreateValidator, error) {
fAmount, _ := fs.GetString(FlagAmount)
amount, err := sdk.ParseCoinNormalized(fAmount)
if err != nil {
return txf, nil, err
}

func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet, val validator) (tx.Factory, *types.MsgCreateValidator, error) {
valAddr := clientCtx.GetFromAddress()
pkStr, err := fs.GetString(FlagPubKey)
if err != nil {
return txf, nil, err
}

var pk cryptotypes.PubKey
if err := clientCtx.Codec.UnmarshalInterfaceJSON([]byte(pkStr), &pk); err != nil {
return txf, nil, err
}

moniker, _ := fs.GetString(FlagMoniker)
identity, _ := fs.GetString(FlagIdentity)
website, _ := fs.GetString(FlagWebsite)
likhita-809 marked this conversation as resolved.
Show resolved Hide resolved
security, _ := fs.GetString(FlagSecurityContact)
details, _ := fs.GetString(FlagDetails)
description := types.NewDescription(
moniker,
val.Moniker,
identity,
website,
security,
details,
)

// get the initial validator commission parameters
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, _ := fs.GetString(FlagMinSelfDelegation)

minSelfDelegation, ok := sdk.NewIntFromString(msbStr)
if !ok {
return txf, nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "minimum self delegation must be a positive integer")
}

msg, err := types.NewMsgCreateValidator(
sdk.ValAddress(valAddr), pk, amount, description, commissionRates, minSelfDelegation,
sdk.ValAddress(valAddr), val.PubKey, val.Amount, description, val.CommissionRates, val.MinSelfDelegation,
)
if err != nil {
return txf, nil, err
Expand Down
Loading