Add IterateAccounts & account export

This commit is contained in:
Christopher Goes 2018-04-27 16:59:47 +02:00 committed by rigelrozanski
parent 10d2e5ae40
commit ad77affb53
5 changed files with 48 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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

View File

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