Merge PR #2829: Add-genesis-account: validate app state accounts

* add-genesis-account: validate app state accounts
* Update PENDING.md
This commit is contained in:
Christopher Goes 2018-11-16 11:55:54 +01:00 committed by GitHub
commit bd723fa909
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 5 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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))
})
}
}