Skip to content

Commit

Permalink
Merge pull request #901 from cosmos/cwgoes/dump-state-to-json
Browse files Browse the repository at this point in the history
Dump all state to JSON
  • Loading branch information
rigelrozanski authored Apr 28, 2018
2 parents ef88ca4 + 81ac153 commit 71dccd4
Show file tree
Hide file tree
Showing 28 changed files with 383 additions and 75 deletions.
1 change: 1 addition & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func (app *BaseApp) setDeliverState(header abci.Header) {
}

//______________________________________________________________________________

// ABCI

// Implements ABCI
Expand Down
2 changes: 1 addition & 1 deletion client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func createHandler(cdc *wire.Codec) http.Handler {
keys.RegisterRoutes(r)
rpc.RegisterRoutes(ctx, r)
tx.RegisterRoutes(ctx, r, cdc)
auth.RegisterRoutes(ctx, r, cdc, "main")
auth.RegisterRoutes(ctx, r, cdc, "acc")
bank.RegisterRoutes(ctx, r, cdc, kb)
ibc.RegisterRoutes(ctx, r, cdc, kb)
return r
Expand Down
31 changes: 29 additions & 2 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"encoding/json"
"os"

abci "github.com/tendermint/abci/types"
Expand Down Expand Up @@ -58,8 +59,14 @@ func NewGaiaApp(logger log.Logger, db dbm.DB) *GaiaApp {
keyStake: sdk.NewKVStoreKey("stake"),
}

// add accountMapper/handlers
app.accountMapper = auth.NewAccountMapper(app.cdc, app.keyMain, &auth.BaseAccount{})
// define the accountMapper
app.accountMapper = auth.NewAccountMapper(
app.cdc,
app.keyAccount, // target store
&auth.BaseAccount{}, // prototype
)

// add handlers
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
Expand Down Expand Up @@ -117,3 +124,23 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci

return abci.ResponseInitChain{}
}

// export the state of gaia for a genesis f
func (app *GaiaApp) ExportAppStateJSON() (appState json.RawMessage, err error) {
ctx := app.NewContext(true, abci.Header{})

// iterate to get the accounts
accounts := []GenesisAccount{}
appendAccount := func(acc sdk.Account) (stop bool) {
account := NewGenesisAccountI(acc)
accounts = append(accounts, account)
return false
}
app.accountMapper.IterateAccounts(ctx, appendAccount)

genState := GenesisState{
Accounts: accounts,
StakeData: stake.WriteGenesis(ctx, app.stakeKeeper),
}
return wire.MarshalJSONIndent(app.cdc, genState)
}
7 changes: 7 additions & 0 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func NewGenesisAccount(acc *auth.BaseAccount) GenesisAccount {
}
}

func NewGenesisAccountI(acc sdk.Account) GenesisAccount {
return GenesisAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
}
}

// convert GenesisAccount to auth.BaseAccount
func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) {
return &auth.BaseAccount{
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
// add query/post commands (custom to binary)
rootCmd.AddCommand(
client.GetCommands(
authcmd.GetAccountCmd("main", cdc, authcmd.GetAccountDecoder(cdc)),
authcmd.GetAccountCmd("acc", cdc, authcmd.GetAccountDecoder(cdc)),
stakecmd.GetCmdQueryCandidate("stake", cdc),
//stakecmd.GetCmdQueryCandidates("stake", cdc),
stakecmd.GetCmdQueryDelegatorBond("stake", cdc),
Expand Down
21 changes: 11 additions & 10 deletions cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"path/filepath"
"encoding/json"

"github.com/spf13/cobra"

Expand All @@ -23,19 +23,20 @@ func main() {
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
}

server.AddCommands(ctx, cdc, rootCmd, app.GaiaAppInit(), generateApp)
server.AddCommands(ctx, cdc, rootCmd, app.GaiaAppInit(),
server.ConstructAppCreator(newApp, "gaia"),
server.ConstructAppExporter(exportAppState, "gaia"))

// prepare and add flags
executor := cli.PrepareBaseCmd(rootCmd, "GA", app.DefaultNodeHome)
executor.Execute()
}

func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
dataDir := filepath.Join(rootDir, "data")
db, err := dbm.NewGoLevelDB("gaia", dataDir)
if err != nil {
return nil, err
}
bapp := app.NewGaiaApp(logger, db)
return bapp, nil
func newApp(logger log.Logger, db dbm.DB) abci.Application {
return app.NewGaiaApp(logger, db)
}

func exportAppState(logger log.Logger, db dbm.DB) (json.RawMessage, error) {
gapp := app.NewGaiaApp(logger, db)
return gapp.ExportAppStateJSON()
}
26 changes: 25 additions & 1 deletion examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"encoding/json"

abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
Expand Down Expand Up @@ -57,7 +59,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
// Define the accountMapper.
app.accountMapper = auth.NewAccountMapper(
cdc,
app.keyMain, // target store
app.keyAccount, // target store
&types.AppAccount{}, // prototype
)

Expand Down Expand Up @@ -119,3 +121,25 @@ func (app *BasecoinApp) initChainer(ctx sdk.Context, req abci.RequestInitChain)
}
return abci.ResponseInitChain{}
}

// Custom logic for state export
func (app *BasecoinApp) ExportAppStateJSON() (appState json.RawMessage, err error) {
ctx := app.NewContext(true, abci.Header{})

// iterate to get the accounts
accounts := []*types.GenesisAccount{}
appendAccount := func(acc sdk.Account) (stop bool) {
account := &types.GenesisAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
}
accounts = append(accounts, account)
return false
}
app.accountMapper.IterateAccounts(ctx, appendAccount)

genState := types.GenesisState{
Accounts: accounts,
}
return wire.MarshalJSONIndent(app.cdc, genState)
}
7 changes: 7 additions & 0 deletions examples/basecoin/cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"

"github.com/cosmos/cosmos-sdk/version"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli"
stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli"

"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
)

// rootCmd is the entry point for this binary
Expand Down Expand Up @@ -47,6 +49,11 @@ func main() {
rootCmd.AddCommand(client.LineBreak)

// add query/post commands (custom to binary)
rootCmd.AddCommand(
client.GetCommands(
authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)),
)...)

rootCmd.AddCommand(
client.PostCommands(
bankcmd.SendTxCmd(cdc),
Expand Down
19 changes: 10 additions & 9 deletions examples/basecoin/cmd/basecoind/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"encoding/json"
"os"
"path/filepath"

"github.com/spf13/cobra"

Expand All @@ -25,20 +25,21 @@ func main() {
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
}

server.AddCommands(ctx, cdc, rootCmd, server.DefaultAppInit, generateApp)
server.AddCommands(ctx, cdc, rootCmd, server.DefaultAppInit,
server.ConstructAppCreator(newApp, "basecoin"),
server.ConstructAppExporter(exportAppState, "basecoin"))

// prepare and add flags
rootDir := os.ExpandEnv("$HOME/.basecoind")
executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir)
executor.Execute()
}

func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
dataDir := filepath.Join(rootDir, "data")
db, err := dbm.NewGoLevelDB("basecoin", dataDir)
if err != nil {
return nil, err
}
func newApp(logger log.Logger, db dbm.DB) abci.Application {
return app.NewBasecoinApp(logger, db)
}

func exportAppState(logger log.Logger, db dbm.DB) (json.RawMessage, error) {
bapp := app.NewBasecoinApp(logger, db)
return bapp, nil
return bapp.ExportAppStateJSON()
}
63 changes: 48 additions & 15 deletions examples/democoin/app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"encoding/json"

abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
Expand Down Expand Up @@ -36,6 +38,13 @@ type DemocoinApp struct {
capKeyIBCStore *sdk.KVStoreKey
capKeyStakingStore *sdk.KVStoreKey

// keepers
coinKeeper bank.Keeper
coolKeeper cool.Keeper
powKeeper pow.Keeper
ibcMapper ibc.Mapper
stakeKeeper simplestake.Keeper

// Manage getting and setting accounts
accountMapper sdk.AccountMapper
}
Expand All @@ -59,26 +68,26 @@ func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp {
// Define the accountMapper.
app.accountMapper = auth.NewAccountMapper(
cdc,
app.capKeyMainStore, // target store
&types.AppAccount{}, // prototype
app.capKeyAccountStore, // target store
&types.AppAccount{}, // prototype
)

// Add handlers.
coinKeeper := bank.NewKeeper(app.accountMapper)
coolKeeper := cool.NewKeeper(app.capKeyMainStore, coinKeeper, app.RegisterCodespace(cool.DefaultCodespace))
powKeeper := pow.NewKeeper(app.capKeyPowStore, pow.NewConfig("pow", int64(1)), coinKeeper, app.RegisterCodespace(pow.DefaultCodespace))
ibcMapper := ibc.NewMapper(app.cdc, app.capKeyIBCStore, app.RegisterCodespace(ibc.DefaultCodespace))
stakeKeeper := simplestake.NewKeeper(app.capKeyStakingStore, coinKeeper, app.RegisterCodespace(simplestake.DefaultCodespace))
app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.coolKeeper = cool.NewKeeper(app.capKeyMainStore, app.coinKeeper, app.RegisterCodespace(cool.DefaultCodespace))
app.powKeeper = pow.NewKeeper(app.capKeyPowStore, pow.NewConfig("pow", int64(1)), app.coinKeeper, app.RegisterCodespace(pow.DefaultCodespace))
app.ibcMapper = ibc.NewMapper(app.cdc, app.capKeyIBCStore, app.RegisterCodespace(ibc.DefaultCodespace))
app.stakeKeeper = simplestake.NewKeeper(app.capKeyStakingStore, app.coinKeeper, app.RegisterCodespace(simplestake.DefaultCodespace))
app.Router().
AddRoute("bank", bank.NewHandler(coinKeeper)).
AddRoute("cool", cool.NewHandler(coolKeeper)).
AddRoute("pow", powKeeper.Handler).
AddRoute("bank", bank.NewHandler(app.coinKeeper)).
AddRoute("cool", cool.NewHandler(app.coolKeeper)).
AddRoute("pow", app.powKeeper.Handler).
AddRoute("sketchy", sketchy.NewHandler()).
AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper)).
AddRoute("simplestake", simplestake.NewHandler(stakeKeeper))
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
AddRoute("simplestake", simplestake.NewHandler(app.stakeKeeper))

// Initialize BaseApp.
app.SetInitChainer(app.initChainerFn(coolKeeper, powKeeper))
app.SetInitChainer(app.initChainerFn(app.coolKeeper, app.powKeeper))
app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore, app.capKeyPowStore, app.capKeyIBCStore, app.capKeyStakingStore)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, auth.BurnFeeHandler))
err := app.LoadLatestVersion(app.capKeyMainStore)
Expand Down Expand Up @@ -127,13 +136,13 @@ func (app *DemocoinApp) initChainerFn(coolKeeper cool.Keeper, powKeeper pow.Keep
}

// Application specific genesis handling
err = coolKeeper.InitGenesis(ctx, genesisState.CoolGenesis)
err = cool.InitGenesis(ctx, app.coolKeeper, genesisState.CoolGenesis)
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}

err = powKeeper.InitGenesis(ctx, genesisState.POWGenesis)
err = pow.InitGenesis(ctx, app.powKeeper, genesisState.POWGenesis)
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
Expand All @@ -142,3 +151,27 @@ func (app *DemocoinApp) initChainerFn(coolKeeper cool.Keeper, powKeeper pow.Keep
return abci.ResponseInitChain{}
}
}

// Custom logic for state export
func (app *DemocoinApp) ExportAppStateJSON() (appState json.RawMessage, err error) {
ctx := app.NewContext(true, abci.Header{})

// iterate to get the accounts
accounts := []*types.GenesisAccount{}
appendAccount := func(acc sdk.Account) (stop bool) {
account := &types.GenesisAccount{
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
}
accounts = append(accounts, account)
return false
}
app.accountMapper.IterateAccounts(ctx, appendAccount)

genState := types.GenesisState{
Accounts: accounts,
POWGenesis: pow.WriteGenesis(ctx, app.powKeeper),
CoolGenesis: cool.WriteGenesis(ctx, app.coolKeeper),
}
return wire.MarshalJSONIndent(app.cdc, genState)
}
2 changes: 1 addition & 1 deletion examples/democoin/cmd/democli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
// start with commands common to basecoin
rootCmd.AddCommand(
client.GetCommands(
authcmd.GetAccountCmd("main", cdc, types.GetAccountDecoder(cdc)),
authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)),
)...)
rootCmd.AddCommand(
client.PostCommands(
Expand Down
Loading

0 comments on commit 71dccd4

Please sign in to comment.