From ad77affb53096e1017bfdba28da8ac7f9547705d Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 27 Apr 2018 16:59:47 +0200 Subject: [PATCH] Add IterateAccounts & account export --- cmd/gaia/app/app.go | 13 +++++++++++-- examples/basecoin/app/app.go | 11 ++++++++++- examples/democoin/app/app.go | 10 +++++++++- types/account.go | 1 + x/auth/mapper.go | 17 +++++++++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/cmd/gaia/app/app.go b/cmd/gaia/app/app.go index 126422e2ce..532a1e095c 100644 --- a/cmd/gaia/app/app.go +++ b/cmd/gaia/app/app.go @@ -120,8 +120,17 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci // custom logic for export func (app *GaiaApp) ExportGenesis() interface{} { + ctx := app.NewContext(true, abci.Header{}) + accounts := []GenesisAccount{} + app.accountMapper.IterateAccounts(ctx, func(a sdk.Account) bool { + accounts = append(accounts, GenesisAccount{ + Address: a.GetAddress(), + Coins: a.GetCoins(), + }) + return false + }) return GenesisState{ - Accounts: []GenesisAccount{}, - StakeData: stake.WriteGenesis(app.NewContext(true, abci.Header{}), app.stakeKeeper), + Accounts: accounts, + StakeData: stake.WriteGenesis(ctx, app.stakeKeeper), } } diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index eb5076942f..532c25f764 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -128,7 +128,16 @@ func (app *BasecoinApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) // Custom logic for state export func (app *BasecoinApp) ExportGenesis() interface{} { + ctx := app.NewContext(true, abci.Header{}) + accounts := []*types.GenesisAccount{} + app.accountMapper.IterateAccounts(ctx, func(a sdk.Account) bool { + accounts = append(accounts, &types.GenesisAccount{ + Address: a.GetAddress(), + Coins: a.GetCoins(), + }) + return false + }) return types.GenesisState{ - Accounts: []*types.GenesisAccount{}, + Accounts: accounts, } } diff --git a/examples/democoin/app/app.go b/examples/democoin/app/app.go index 3b383bef8f..5f2cd32d80 100644 --- a/examples/democoin/app/app.go +++ b/examples/democoin/app/app.go @@ -153,8 +153,16 @@ func (app *DemocoinApp) initChainerFn(coolKeeper cool.Keeper, powKeeper pow.Keep // Custom logic for state export func (app *DemocoinApp) ExportGenesis() interface{} { ctx := app.NewContext(true, abci.Header{}) + accounts := []*types.GenesisAccount{} + app.accountMapper.IterateAccounts(ctx, func(a sdk.Account) bool { + accounts = append(accounts, &types.GenesisAccount{ + Address: a.GetAddress(), + Coins: a.GetCoins(), + }) + return false + }) return types.GenesisState{ - Accounts: []*types.GenesisAccount{}, + Accounts: accounts, POWGenesis: pow.WriteGenesis(ctx, app.powKeeper), CoolGenesis: cool.WriteGenesis(ctx, app.coolKeeper), } diff --git a/types/account.go b/types/account.go index 91ad499795..33332867c6 100644 --- a/types/account.go +++ b/types/account.go @@ -48,6 +48,7 @@ type AccountMapper interface { NewAccountWithAddress(ctx Context, addr Address) Account GetAccount(ctx Context, addr Address) Account SetAccount(ctx Context, acc Account) + IterateAccounts(ctx Context, cont func(Account) bool) } // AccountDecoder unmarshals account bytes diff --git a/x/auth/mapper.go b/x/auth/mapper.go index b815fada7a..fea98876d1 100644 --- a/x/auth/mapper.go +++ b/x/auth/mapper.go @@ -62,6 +62,23 @@ func (am accountMapper) SetAccount(ctx sdk.Context, acc sdk.Account) { store.Set(addr, bz) } +// Implements sdk.AccountMapper. +func (am accountMapper) IterateAccounts(ctx sdk.Context, cont func(sdk.Account) bool) { + store := ctx.KVStore(am.key) + iter := store.Iterator(nil, nil) + for { + if !iter.Valid() { + return + } + val := iter.Value() + acc := am.decodeAccount(val) + if cont(acc) { + return + } + iter.Next() + } +} + //---------------------------------------- // misc.