diff --git a/PENDING.md b/PENDING.md index ba90d3ac8..050646593 100644 --- a/PENDING.md +++ b/PENDING.md @@ -8,6 +8,8 @@ BREAKING CHANGES * [cli] [\#2728](https://github.com/cosmos/cosmos-sdk/pull/2728) Seperate `tx` and `query` subcommands by module * [cli] [\#2727](https://github.com/cosmos/cosmos-sdk/pull/2727) Fix unbonding command flow * [cli] [\#2786](https://github.com/cosmos/cosmos-sdk/pull/2786) Fix redelegation command flow + * [cli] [\#2829](https://github.com/cosmos/cosmos-sdk/pull/2829) add-genesis-account command now validates state when adding accounts + * [cli] [\#2804](https://github.com/cosmos/cosmos-sdk/issues/2804) Check whether key exists before passing it on to `tx create-validator`. * Gaia diff --git a/cmd/gaia/init/genesis_accts.go b/cmd/gaia/init/genesis_accts.go index 3d43712fc..04c1f6283 100644 --- a/cmd/gaia/init/genesis_accts.go +++ b/cmd/gaia/init/genesis_accts.go @@ -1,6 +1,7 @@ package init import ( + "encoding/json" "fmt" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" @@ -48,11 +49,7 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command return err } - acc := auth.NewBaseAccountWithAddress(addr) - acc.Coins = coins - appState.Accounts = append(appState.Accounts, app.NewGenesisAccount(&acc)) - - appStateJSON, err := cdc.MarshalJSON(appState) + appStateJSON, err := addGenesisAccount(cdc, appState, addr, coins) if err != nil { return err } @@ -64,3 +61,16 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory") return cmd } + +func addGenesisAccount(cdc *codec.Codec, appState app.GenesisState, addr sdk.AccAddress, coins sdk.Coins) (json.RawMessage, error) { + for _, stateAcc := range appState.Accounts { + if stateAcc.Address.Equals(addr) { + return nil, fmt.Errorf("the application state already contains account %v", addr) + } + } + + acc := auth.NewBaseAccountWithAddress(addr) + acc.Coins = coins + appState.Accounts = append(appState.Accounts, app.NewGenesisAccount(&acc)) + return cdc.MarshalJSON(appState) +} diff --git a/cmd/gaia/init/genesis_accts_test.go b/cmd/gaia/init/genesis_accts_test.go new file mode 100644 index 000000000..8825a8cd3 --- /dev/null +++ b/cmd/gaia/init/genesis_accts_test.go @@ -0,0 +1,45 @@ +package init + +import ( + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/secp256k1" + "testing" + + "github.com/cosmos/cosmos-sdk/cmd/gaia/app" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func TestAddGenesisAccount(t *testing.T) { + cdc := codec.New() + addr1 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) + type args struct { + appState app.GenesisState + addr sdk.AccAddress + coins sdk.Coins + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + "valid account", + args{ + app.GenesisState{}, + addr1, + sdk.Coins{}, + }, + false}, + {"dup account", args{ + app.GenesisState{Accounts: []app.GenesisAccount{app.GenesisAccount{Address:addr1}}}, + addr1, + sdk.Coins{}}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := addGenesisAccount(cdc, tt.args.appState, tt.args.addr, tt.args.coins) + require.Equal(t, tt.wantErr, (err != nil)) + }) + } +} diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go index 449eb2b85..6d66f2e30 100644 --- a/cmd/gaia/init/gentx.go +++ b/cmd/gaia/init/gentx.go @@ -3,6 +3,7 @@ package init import ( "fmt" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/server" @@ -61,6 +62,14 @@ following delegation and commission default parameters: return err } + kb, err := keys.GetKeyBaseFromDir(viper.GetString(flagClientHome)) + if err != nil { + return err + } + if _, err = kb.Get(viper.GetString(client.FlagName)); err != nil { + return err + } + // Read --pubkey, if empty take it from priv_validator.json if valPubKeyString := viper.GetString(cli.FlagPubKey); valPubKeyString != "" { valPubKey, err = sdk.GetConsPubKeyBech32(valPubKeyString) diff --git a/docs/README.md b/docs/README.md index 048f9f39f..5d2ae9b2d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -34,7 +34,7 @@ To install the latest version of the `gaia` application and join the public test ## Contribute -See [this file](./DOCS_README.md) for details of the build process and +See [this file](https://github.com/cosmos/cosmos-sdk/blob/master/docs/DOCS_README.md) for details of the build process and considerations when making changes. ## Version