diff --git a/PENDING.md b/PENDING.md index 3defb10e7d..050646593f 100644 --- a/PENDING.md +++ b/PENDING.md @@ -8,6 +8,7 @@ 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 3d43712fcc..04c1f6283c 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 0000000000..8825a8cd34 --- /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)) + }) + } +}