From 80767474d784f2cc8e1e8470f904137e52c935a5 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Wed, 22 May 2019 17:06:49 -0400 Subject: [PATCH] Merge PR #4398: Refactor genaccounts genesis state --- simapp/sim_test.go | 5 ++-- .../genaccounts/client/cli/genesis_accts.go | 13 +++++---- x/auth/genaccounts/export.go | 2 +- x/auth/genaccounts/genesis_account.go | 8 ++---- x/auth/genaccounts/genesis_state.go | 22 +++++---------- x/auth/genaccounts/genesis_state_test.go | 28 +++++++++---------- x/auth/genaccounts/init.go | 6 ++-- x/auth/genaccounts/module.go | 2 +- 8 files changed, 38 insertions(+), 48 deletions(-) diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 56fd23621..13fbb57a4 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -84,7 +84,7 @@ func appStateFromGenesisFileFn(r *rand.Rand, accs []simulation.Account, genesisT cdc.MustUnmarshalJSON(bytes, &genesis) var appState GenesisState cdc.MustUnmarshalJSON(genesis.AppState, &appState) - accounts := genaccounts.GetGenesisStateFromAppState(cdc, appState).Accounts + accounts := genaccounts.GetGenesisStateFromAppState(cdc, appState) var newAccs []simulation.Account for _, acc := range accounts { @@ -165,8 +165,7 @@ func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimest genesisAccounts = append(genesisAccounts, gacc) } - genaccsGenesis := genaccounts.NewGenesisState(genesisAccounts) - genesisState[genaccounts.ModuleName] = cdc.MustMarshalJSON(genaccsGenesis) + genesisState[genaccounts.ModuleName] = cdc.MustMarshalJSON(genesisAccounts) authGenesis := auth.NewGenesisState( nil, diff --git a/x/auth/genaccounts/client/cli/genesis_accts.go b/x/auth/genaccounts/client/cli/genesis_accts.go index fbd1e9e5c..98518e172 100644 --- a/x/auth/genaccounts/client/cli/genesis_accts.go +++ b/x/auth/genaccounts/client/cli/genesis_accts.go @@ -73,14 +73,17 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec, } // add genesis account to the app state - var genesisState genaccounts.GenesisState - cdc.MustUnmarshalJSON(appState[genaccounts.ModuleName], &genesisState) - if genesisState.Accounts.Contains(addr) { + var genesisAccounts genaccounts.GenesisAccounts + + cdc.MustUnmarshalJSON(appState[genaccounts.ModuleName], &genesisAccounts) + + if genesisAccounts.Contains(addr) { return fmt.Errorf("cannot add account at existing address %v", addr) } - genesisState.Accounts = append(genesisState.Accounts, genAcc) - genesisStateBz := cdc.MustMarshalJSON(genesisState) + genesisAccounts = append(genesisAccounts, genAcc) + + genesisStateBz := cdc.MustMarshalJSON(genaccounts.GenesisState(genesisAccounts)) appState[genaccounts.ModuleName] = genesisStateBz appStateJSON, err := cdc.MarshalJSON(appState) diff --git a/x/auth/genaccounts/export.go b/x/auth/genaccounts/export.go index f6445c9c6..bb1d54638 100644 --- a/x/auth/genaccounts/export.go +++ b/x/auth/genaccounts/export.go @@ -21,5 +21,5 @@ func ExportGenesis(ctx sdk.Context, accountKeeper AccountKeeper) GenesisState { }, ) - return NewGenesisState(accounts) + return accounts } diff --git a/x/auth/genaccounts/genesis_account.go b/x/auth/genaccounts/genesis_account.go index d0801b2f6..35d15171f 100644 --- a/x/auth/genaccounts/genesis_account.go +++ b/x/auth/genaccounts/genesis_account.go @@ -88,15 +88,13 @@ func NewGenesisAccountI(acc auth.Account) (GenesisAccount, error) { // convert GenesisAccount to auth.Account func (ga *GenesisAccount) ToAccount() auth.Account { - - bacc := auth.NewBaseAccount(ga.Address, ga.Coins.Sort(), - nil, ga.AccountNumber, ga.Sequence) + bacc := auth.NewBaseAccount(ga.Address, ga.Coins.Sort(), nil, ga.AccountNumber, ga.Sequence) if !ga.OriginalVesting.IsZero() { - baseVestingAcc := auth.NewBaseVestingAccount( bacc, ga.OriginalVesting, ga.DelegatedFree, - ga.DelegatedVesting, ga.EndTime) + ga.DelegatedVesting, ga.EndTime, + ) switch { case ga.StartTime != 0 && ga.EndTime != 0: diff --git a/x/auth/genaccounts/genesis_state.go b/x/auth/genaccounts/genesis_state.go index 5596cd00c..834ee5301 100644 --- a/x/auth/genaccounts/genesis_state.go +++ b/x/auth/genaccounts/genesis_state.go @@ -10,16 +10,7 @@ import ( ) // State to Unmarshal -type GenesisState struct { - Accounts GenesisAccounts `json:"accounts"` -} - -// NewGenesisState creates a new GenesisState object -func NewGenesisState(accounts GenesisAccounts) GenesisState { - return GenesisState{ - Accounts: accounts, - } -} +type GenesisState GenesisAccounts // get the genesis state from the expected app state func GetGenesisStateFromAppState(cdc *codec.Codec, appState map[string]json.RawMessage) GenesisState { @@ -27,6 +18,7 @@ func GetGenesisStateFromAppState(cdc *codec.Codec, appState map[string]json.RawM if appState[ModuleName] != nil { cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState) } + return genesisState } @@ -41,11 +33,11 @@ func SetGenesisStateInAppState(cdc *codec.Codec, // Sanitize sorts accounts and coin sets. func (gs GenesisState) Sanitize() { - sort.Slice(gs.Accounts, func(i, j int) bool { - return gs.Accounts[i].AccountNumber < gs.Accounts[j].AccountNumber + sort.Slice(gs, func(i, j int) bool { + return gs[i].AccountNumber < gs[j].AccountNumber }) - for _, acc := range gs.Accounts { + for _, acc := range gs { acc.Coins = acc.Coins.Sort() } } @@ -54,8 +46,8 @@ func (gs GenesisState) Sanitize() { // ensures that there are no duplicate accounts in the genesis state and any // provided vesting accounts are valid. func ValidateGenesis(genesisState GenesisState) error { - addrMap := make(map[string]bool, len(genesisState.Accounts)) - for _, acc := range genesisState.Accounts { + addrMap := make(map[string]bool, len(genesisState)) + for _, acc := range genesisState { addrStr := acc.Address.String() // disallow any duplicate accounts diff --git a/x/auth/genaccounts/genesis_state_test.go b/x/auth/genaccounts/genesis_state_test.go index a9ebf05ee..4977fab37 100644 --- a/x/auth/genaccounts/genesis_state_test.go +++ b/x/auth/genaccounts/genesis_state_test.go @@ -28,17 +28,17 @@ func TestSanitize(t *testing.T) { }) genAcc2 := NewGenesisAccount(&authAcc2) - genesisState := NewGenesisState([]GenesisAccount{genAcc1, genAcc2}) + genesisState := GenesisState([]GenesisAccount{genAcc1, genAcc2}) require.NoError(t, ValidateGenesis(genesisState)) - require.True(t, genesisState.Accounts[0].AccountNumber > genesisState.Accounts[1].AccountNumber) - require.Equal(t, genesisState.Accounts[0].Coins[0].Denom, "bcoin") - require.Equal(t, genesisState.Accounts[0].Coins[1].Denom, "acoin") - require.Equal(t, genesisState.Accounts[1].Address, addr2) + require.True(t, genesisState[0].AccountNumber > genesisState[1].AccountNumber) + require.Equal(t, genesisState[0].Coins[0].Denom, "bcoin") + require.Equal(t, genesisState[0].Coins[1].Denom, "acoin") + require.Equal(t, genesisState[1].Address, addr2) genesisState.Sanitize() - require.False(t, genesisState.Accounts[0].AccountNumber > genesisState.Accounts[1].AccountNumber) - require.Equal(t, genesisState.Accounts[1].Address, addr1) - require.Equal(t, genesisState.Accounts[1].Coins[0].Denom, "acoin") - require.Equal(t, genesisState.Accounts[1].Coins[1].Denom, "bcoin") + require.False(t, genesisState[0].AccountNumber > genesisState[1].AccountNumber) + require.Equal(t, genesisState[1].Address, addr1) + require.Equal(t, genesisState[1].Coins[0].Denom, "acoin") + require.Equal(t, genesisState[1].Coins[1].Denom, "bcoin") } var ( @@ -57,7 +57,7 @@ func TestValidateGenesisDuplicateAccounts(t *testing.T) { genAccs[0] = NewGenesisAccount(&acc1) genAccs[1] = NewGenesisAccount(&acc1) - genesisState := NewGenesisState(genAccs) + genesisState := GenesisState(genAccs) err := ValidateGenesis(genesisState) require.Error(t, err) } @@ -73,13 +73,13 @@ func TestValidateGenesisInvalidAccounts(t *testing.T) { genAccs[0] = NewGenesisAccount(&acc1) genAccs[1] = NewGenesisAccount(&acc2) - genesisState := NewGenesisState(genAccs) - genesisState.Accounts[0].OriginalVesting = genesisState.Accounts[0].Coins + genesisState := GenesisState(genAccs) + genesisState[0].OriginalVesting = genesisState[0].Coins err := ValidateGenesis(genesisState) require.Error(t, err) - genesisState.Accounts[0].StartTime = 1548888000 - genesisState.Accounts[0].EndTime = 1548775410 + genesisState[0].StartTime = 1548888000 + genesisState[0].EndTime = 1548775410 err = ValidateGenesis(genesisState) require.Error(t, err) } diff --git a/x/auth/genaccounts/init.go b/x/auth/genaccounts/init.go index b40e6f978..e58beef1d 100644 --- a/x/auth/genaccounts/init.go +++ b/x/auth/genaccounts/init.go @@ -6,13 +6,11 @@ import ( ) // initialize accounts and deliver genesis transactions -func InitGenesis(ctx sdk.Context, cdc *codec.Codec, - accountKeeper AccountKeeper, genesisState GenesisState) { - +func InitGenesis(ctx sdk.Context, _ *codec.Codec, accountKeeper AccountKeeper, genesisState GenesisState) { genesisState.Sanitize() // load the accounts - for _, gacc := range genesisState.Accounts { + for _, gacc := range genesisState { acc := gacc.ToAccount() acc = accountKeeper.NewAccount(ctx, acc) // set account number accountKeeper.SetAccount(ctx, acc) diff --git a/x/auth/genaccounts/module.go b/x/auth/genaccounts/module.go index 2ffc0c4a5..89925e185 100644 --- a/x/auth/genaccounts/module.go +++ b/x/auth/genaccounts/module.go @@ -50,7 +50,7 @@ func (AppModuleBasic) IterateGenesisAccounts(cdc *codec.Codec, appGenesis map[st iterateFn func(auth.Account) (stop bool)) { genesisState := GetGenesisStateFromAppState(cdc, appGenesis) - for _, genAcc := range genesisState.Accounts { + for _, genAcc := range genesisState { acc := genAcc.ToAccount() if iterateFn(acc) { break