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

R4R: add-genesis-account: validate app state accounts #2829

Merged
merged 3 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions cmd/gaia/init/genesis_accts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package init

import (
"encoding/json"
"fmt"

"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
45 changes: 45 additions & 0 deletions cmd/gaia/init/genesis_accts_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}