refactor export function, working
This commit is contained in:
parent
67961476b4
commit
10ddd7a3d3
@ -1,6 +1,7 @@
|
||||
package baseapp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
@ -49,14 +50,14 @@ func GenerateFn(appFn func(log.Logger, dbm.DB) abci.Application, name string) Ap
|
||||
}
|
||||
|
||||
// ExportFn returns an application export function
|
||||
func ExportFn(appFn func(log.Logger, dbm.DB) (interface{}, *wire.Codec), name string) AppExporter {
|
||||
return func(rootDir string, logger log.Logger) (interface{}, *wire.Codec, error) {
|
||||
func ExportFn(appFn func(log.Logger, dbm.DB) json.RawMessage, name string) AppExporter {
|
||||
return func(rootDir string, logger log.Logger) (interface{}, error) {
|
||||
dataDir := filepath.Join(rootDir, "data")
|
||||
db, err := dbm.NewGoLevelDB(name, dataDir)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
genesis, codec := appFn(logger, db)
|
||||
genesis := appFn(logger, db)
|
||||
return genesis, codec, nil
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
@ -124,19 +125,22 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
|
||||
return abci.ResponseInitChain{}
|
||||
}
|
||||
|
||||
// custom logic for export
|
||||
func (app *GaiaApp) ExportGenesis() interface{} {
|
||||
// export the state of gaia for a genesis f
|
||||
func (app *GaiaApp) ExportAppStateJSON() (appState json.RawGenesis, err error) {
|
||||
ctx := app.NewContext(true, abci.Header{})
|
||||
|
||||
// iterate to get the accounts
|
||||
accounts := []GenesisAccount{}
|
||||
app.accountMapper.IterateAccounts(ctx, func(a sdk.Account) bool {
|
||||
accounts = append(accounts, GenesisAccount{
|
||||
Address: a.GetAddress(),
|
||||
Coins: a.GetCoins(),
|
||||
})
|
||||
appendAccount := func(acc sdk.Account) (stop bool) {
|
||||
account := NewGenesisAccountI(acc)
|
||||
accounts = append(accounts, account)
|
||||
return false
|
||||
})
|
||||
return GenesisState{
|
||||
}
|
||||
app.accountMapper.IterateAccounts(ctx, appendAccount)
|
||||
|
||||
genState := GenesisState{
|
||||
Accounts: accounts,
|
||||
StakeData: stake.WriteGenesis(ctx, app.stakeKeeper),
|
||||
}
|
||||
return wire.MarshalJSONIndent(cdc, genState)
|
||||
}
|
||||
|
||||
@ -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{
|
||||
|
||||
@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
@ -11,34 +12,18 @@ import (
|
||||
)
|
||||
|
||||
// ExportCmd dumps app state to JSON
|
||||
func ExportCmd(app baseapp.AppExporter, ctx *Context) *cobra.Command {
|
||||
export := exportCmd{
|
||||
appExporter: app,
|
||||
context: ctx,
|
||||
}
|
||||
cmd := &cobra.Command{
|
||||
func ExportCmd(ctx *Context, cdc *wire.Codec, appExporter baseapp.AppExporter) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "export",
|
||||
Short: "Export state to JSON",
|
||||
RunE: export.run,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
home := viper.GetString("home")
|
||||
appState, err := appExporter(home, ctx.Logger)
|
||||
if err != nil {
|
||||
return errors.Errorf("Error exporting state: %v\n", err)
|
||||
}
|
||||
fmt.Println(string(output))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
type exportCmd struct {
|
||||
appExporter baseapp.AppExporter
|
||||
context *Context
|
||||
}
|
||||
|
||||
func (e exportCmd) run(cmd *cobra.Command, args []string) error {
|
||||
home := viper.GetString("home")
|
||||
genesis, cdc, err := e.appExporter(home, e.context.Logger)
|
||||
if err != nil {
|
||||
return errors.Errorf("Error exporting state: %v\n", err)
|
||||
}
|
||||
output, err := wire.MarshalJSONIndent(cdc, genesis)
|
||||
if err != nil {
|
||||
return errors.Errorf("Error marshalling state: %v\n", err)
|
||||
}
|
||||
fmt.Println(string(output))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -9,7 +9,6 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/version"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
||||
@ -69,7 +68,7 @@ func PersistentPreRunEFn(context *Context) func(*cobra.Command, []string) error
|
||||
func AddCommands(
|
||||
ctx *Context, cdc *wire.Codec,
|
||||
rootCmd *cobra.Command, appInit AppInit,
|
||||
appCreator baseapp.AppCreator, appExporter baseapp.AppExporter) {
|
||||
appCreator AppCreator) {
|
||||
|
||||
rootCmd.PersistentFlags().String("log_level", ctx.Config.LogLevel, "Log level")
|
||||
|
||||
@ -79,7 +78,7 @@ func AddCommands(
|
||||
UnsafeResetAllCmd(ctx),
|
||||
ShowNodeIDCmd(ctx),
|
||||
ShowValidatorCmd(ctx),
|
||||
ExportCmd(appExporter, ctx),
|
||||
ExportCmd(cts, cdc, appCreator),
|
||||
UnsafeResetAllCmd(ctx),
|
||||
version.VersionCmd,
|
||||
)
|
||||
|
||||
@ -48,7 +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)
|
||||
IterateAccounts(ctx Context, process func(sdk.Account) (stop bool))
|
||||
}
|
||||
|
||||
// AccountDecoder unmarshals account bytes
|
||||
|
||||
@ -63,7 +63,7 @@ func (am accountMapper) SetAccount(ctx sdk.Context, acc sdk.Account) {
|
||||
}
|
||||
|
||||
// Implements sdk.AccountMapper.
|
||||
func (am accountMapper) IterateAccounts(ctx sdk.Context, cont func(sdk.Account) bool) {
|
||||
func (am accountMapper) IterateAccounts(ctx sdk.Context, process func(sdk.Account) (stop bool)) {
|
||||
store := ctx.KVStore(am.key)
|
||||
iter := store.Iterator(nil, nil)
|
||||
for {
|
||||
@ -72,7 +72,7 @@ func (am accountMapper) IterateAccounts(ctx sdk.Context, cont func(sdk.Account)
|
||||
}
|
||||
val := iter.Value()
|
||||
acc := am.decodeAccount(val)
|
||||
if cont(acc) {
|
||||
if process(acc) {
|
||||
return
|
||||
}
|
||||
iter.Next()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user