refactor
This commit is contained in:
parent
10ddd7a3d3
commit
318e924145
|
@ -242,12 +242,6 @@ func (app *BaseApp) setDeliverState(header abci.Header) {
|
|||
|
||||
//______________________________________________________________________________
|
||||
|
||||
// ExportGenesis returns the genesis state in a wire-serializable format
|
||||
// should be overridden by applications extending baseapp
|
||||
func (app *BaseApp) ExportGenesis() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ABCI
|
||||
|
||||
// Implements ABCI
|
||||
|
|
|
@ -1,15 +1,9 @@
|
|||
package baseapp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
"github.com/tendermint/abci/server"
|
||||
abci "github.com/tendermint/abci/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
)
|
||||
|
||||
// RunForever - BasecoinApp execution and cleanup
|
||||
|
@ -28,36 +22,3 @@ func RunForever(app abci.Application) {
|
|||
srv.Stop()
|
||||
})
|
||||
}
|
||||
|
||||
// AppCreator lets us lazily initialize app, using home dir
|
||||
// and other flags (?) to start
|
||||
type AppCreator func(string, log.Logger) (abci.Application, error)
|
||||
|
||||
// AppExporter dumps all app state to JSON-serializable structure
|
||||
type AppExporter func(home string, log log.Logger) (interface{}, *wire.Codec, error)
|
||||
|
||||
// GenerateFn returns an application generation function
|
||||
func GenerateFn(appFn func(log.Logger, dbm.DB) abci.Application, name string) AppCreator {
|
||||
return func(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||
dataDir := filepath.Join(rootDir, "data")
|
||||
db, err := dbm.NewGoLevelDB(name, dataDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
app := appFn(logger, db)
|
||||
return app, nil
|
||||
}
|
||||
}
|
||||
|
||||
// ExportFn returns an application export function
|
||||
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 := appFn(logger, db)
|
||||
return genesis, codec, nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,7 +126,7 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
|
|||
}
|
||||
|
||||
// export the state of gaia for a genesis f
|
||||
func (app *GaiaApp) ExportAppStateJSON() (appState json.RawGenesis, err error) {
|
||||
func (app *GaiaApp) ExportAppStateJSON() (appState json.RawMessage, err error) {
|
||||
ctx := app.NewContext(true, abci.Header{})
|
||||
|
||||
// iterate to get the accounts
|
||||
|
@ -142,5 +142,5 @@ func (app *GaiaApp) ExportAppStateJSON() (appState json.RawGenesis, err error) {
|
|||
Accounts: accounts,
|
||||
StakeData: stake.WriteGenesis(ctx, app.stakeKeeper),
|
||||
}
|
||||
return wire.MarshalJSONIndent(cdc, genState)
|
||||
return wire.MarshalJSONIndent(app.cdc, genState)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
|
@ -8,10 +10,8 @@ import (
|
|||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -24,8 +24,8 @@ func main() {
|
|||
}
|
||||
|
||||
server.AddCommands(ctx, cdc, rootCmd, app.GaiaAppInit(),
|
||||
baseapp.GenerateFn(newApp, "gaia"),
|
||||
baseapp.ExportFn(exportApp, "gaia"))
|
||||
server.ConstructAppCreator(newApp, "gaia"),
|
||||
server.ConstructAppExporter(exportAppState, "gaia"))
|
||||
|
||||
// prepare and add flags
|
||||
executor := cli.PrepareBaseCmd(rootCmd, "GA", app.DefaultNodeHome)
|
||||
|
@ -36,7 +36,7 @@ func newApp(logger log.Logger, db dbm.DB) abci.Application {
|
|||
return app.NewGaiaApp(logger, db)
|
||||
}
|
||||
|
||||
func exportApp(logger log.Logger, db dbm.DB) (interface{}, *wire.Codec) {
|
||||
func exportAppState(logger log.Logger, db dbm.DB) (json.RawMessage, error) {
|
||||
gapp := app.NewGaiaApp(logger, db)
|
||||
return gapp.ExportGenesis(), app.MakeCodec()
|
||||
return gapp.ExportAppStateJSON()
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
@ -15,7 +17,6 @@ import (
|
|||
"github.com/cosmos/cosmos-sdk/x/stake"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
|
||||
"github.com/cosmos/cosmos-sdk/examples/democoin/x/simplestake"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -33,11 +34,6 @@ type BasecoinApp struct {
|
|||
keyIBC *sdk.KVStoreKey
|
||||
keyStake *sdk.KVStoreKey
|
||||
|
||||
// keepers
|
||||
coinKeeper bank.Keeper
|
||||
ibcMapper ibc.Mapper
|
||||
stakeKeeper simplestake.Keeper
|
||||
|
||||
// Manage getting and setting accounts
|
||||
accountMapper sdk.AccountMapper
|
||||
coinKeeper bank.Keeper
|
||||
|
@ -127,7 +123,7 @@ func (app *BasecoinApp) initChainer(ctx sdk.Context, req abci.RequestInitChain)
|
|||
}
|
||||
|
||||
// Custom logic for state export
|
||||
func (app *BasecoinApp) ExportGenesis() interface{} {
|
||||
func (app *BasecoinApp) ExportAppStateJSON() (appState json.RawMessage, err error) {
|
||||
ctx := app.NewContext(true, abci.Header{})
|
||||
accounts := []*types.GenesisAccount{}
|
||||
app.accountMapper.IterateAccounts(ctx, func(a sdk.Account) bool {
|
||||
|
@ -137,7 +133,8 @@ func (app *BasecoinApp) ExportGenesis() interface{} {
|
|||
})
|
||||
return false
|
||||
})
|
||||
return types.GenesisState{
|
||||
genState := types.GenesisState{
|
||||
Accounts: accounts,
|
||||
}
|
||||
return wire.MarshalJSONIndent(app.cdc, genState)
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ 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"
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -10,10 +11,8 @@ import (
|
|||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -27,8 +26,8 @@ func main() {
|
|||
}
|
||||
|
||||
server.AddCommands(ctx, cdc, rootCmd, server.DefaultAppInit,
|
||||
baseapp.GenerateFn(newApp, "basecoin"),
|
||||
baseapp.ExportFn(exportApp, "basecoin"))
|
||||
server.ConstructAppCreator(newApp, "basecoin"),
|
||||
server.ConstructAppExporter(exportAppState, "basecoin"))
|
||||
|
||||
// prepare and add flags
|
||||
rootDir := os.ExpandEnv("$HOME/.basecoind")
|
||||
|
@ -40,7 +39,7 @@ func newApp(logger log.Logger, db dbm.DB) abci.Application {
|
|||
return app.NewBasecoinApp(logger, db)
|
||||
}
|
||||
|
||||
func exportApp(logger log.Logger, db dbm.DB) (interface{}, *wire.Codec) {
|
||||
func exportAppState(logger log.Logger, db dbm.DB) (json.RawMessage, error) {
|
||||
bapp := app.NewBasecoinApp(logger, db)
|
||||
return bapp.ExportGenesis(), app.MakeCodec()
|
||||
return bapp.ExportAppStateJSON()
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
@ -151,19 +153,25 @@ func (app *DemocoinApp) initChainerFn(coolKeeper cool.Keeper, powKeeper pow.Keep
|
|||
}
|
||||
|
||||
// Custom logic for state export
|
||||
func (app *DemocoinApp) ExportGenesis() interface{} {
|
||||
func (app *DemocoinApp) ExportAppStateJSON() (appState json.RawMessage, err error) {
|
||||
ctx := app.NewContext(true, abci.Header{})
|
||||
|
||||
// iterate to get the accounts
|
||||
accounts := []*types.GenesisAccount{}
|
||||
app.accountMapper.IterateAccounts(ctx, func(a sdk.Account) bool {
|
||||
accounts = append(accounts, &types.GenesisAccount{
|
||||
Address: a.GetAddress(),
|
||||
Coins: a.GetCoins(),
|
||||
})
|
||||
appendAccount := func(acc sdk.Account) (stop bool) {
|
||||
account := &types.GenesisAccount{
|
||||
Address: acc.GetAddress(),
|
||||
Coins: acc.GetCoins(),
|
||||
}
|
||||
accounts = append(accounts, account)
|
||||
return false
|
||||
})
|
||||
return types.GenesisState{
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/examples/democoin/app"
|
||||
"github.com/cosmos/cosmos-sdk/server"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
|
@ -47,9 +46,9 @@ func newApp(logger log.Logger, db dbm.DB) abci.Application {
|
|||
return app.NewDemocoinApp(logger, db)
|
||||
}
|
||||
|
||||
func exportApp(logger log.Logger, db dbm.DB) (interface{}, *wire.Codec) {
|
||||
func exportAppState(logger log.Logger, db dbm.DB) (json.RawMessage, error) {
|
||||
dapp := app.NewDemocoinApp(logger, db)
|
||||
return dapp.ExportGenesis(), app.MakeCodec()
|
||||
return dapp.ExportAppStateJSON()
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -63,8 +62,8 @@ func main() {
|
|||
}
|
||||
|
||||
server.AddCommands(ctx, cdc, rootCmd, CoolAppInit,
|
||||
baseapp.GenerateFn(newApp, "democoin"),
|
||||
baseapp.ExportFn(exportApp, "democoin"))
|
||||
server.ConstructAppCreator(newApp, "democoin"),
|
||||
server.ConstructAppExporter(exportAppState, "democoin"))
|
||||
|
||||
// prepare and add flags
|
||||
rootDir := os.ExpandEnv("$HOME/.democoind")
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/log"
|
||||
)
|
||||
|
||||
// AppCreator lets us lazily initialize app, using home dir
|
||||
// and other flags (?) to start
|
||||
type AppCreator func(string, log.Logger) (abci.Application, error)
|
||||
|
||||
// AppExporter dumps all app state to JSON-serializable structure
|
||||
type AppExporter func(home string, log log.Logger) (json.RawMessage, error)
|
||||
|
||||
// ConstructAppCreator returns an application generation function
|
||||
func ConstructAppCreator(appFn func(log.Logger, dbm.DB) abci.Application, name string) AppCreator {
|
||||
return func(rootDir string, logger log.Logger) (abci.Application, error) {
|
||||
dataDir := filepath.Join(rootDir, "data")
|
||||
db, err := dbm.NewGoLevelDB(name, dataDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
app := appFn(logger, db)
|
||||
return app, nil
|
||||
}
|
||||
}
|
||||
|
||||
// ConstructAppExporter returns an application export function
|
||||
func ConstructAppExporter(appFn func(log.Logger, dbm.DB) (json.RawMessage, error), name string) AppExporter {
|
||||
return func(rootDir string, logger log.Logger) (json.RawMessage, error) {
|
||||
dataDir := filepath.Join(rootDir, "data")
|
||||
db, err := dbm.NewGoLevelDB(name, dataDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return appFn(logger, db)
|
||||
}
|
||||
}
|
|
@ -7,12 +7,11 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
// ExportCmd dumps app state to JSON
|
||||
func ExportCmd(ctx *Context, cdc *wire.Codec, appExporter baseapp.AppExporter) *cobra.Command {
|
||||
func ExportCmd(ctx *Context, cdc *wire.Codec, appExporter AppExporter) *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "export",
|
||||
Short: "Export state to JSON",
|
||||
|
@ -22,7 +21,7 @@ func ExportCmd(ctx *Context, cdc *wire.Codec, appExporter baseapp.AppExporter) *
|
|||
if err != nil {
|
||||
return errors.Errorf("Error exporting state: %v\n", err)
|
||||
}
|
||||
fmt.Println(string(output))
|
||||
fmt.Println(string(appState))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
|
||||
"github.com/tendermint/abci/server"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
||||
"github.com/tendermint/tendermint/node"
|
||||
"github.com/tendermint/tendermint/proxy"
|
||||
|
@ -22,7 +21,7 @@ const (
|
|||
|
||||
// StartCmd runs the service passed in, either
|
||||
// stand-alone, or in-process with tendermint
|
||||
func StartCmd(ctx *Context, appCreator baseapp.AppCreator) *cobra.Command {
|
||||
func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "start",
|
||||
Short: "Run the full node",
|
||||
|
@ -45,7 +44,7 @@ func StartCmd(ctx *Context, appCreator baseapp.AppCreator) *cobra.Command {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func startStandAlone(ctx *Context, appCreator baseapp.AppCreator) error {
|
||||
func startStandAlone(ctx *Context, appCreator AppCreator) error {
|
||||
// Generate the app in the proper dir
|
||||
addr := viper.GetString(flagAddress)
|
||||
home := viper.GetString("home")
|
||||
|
@ -69,7 +68,7 @@ func startStandAlone(ctx *Context, appCreator baseapp.AppCreator) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func startInProcess(ctx *Context, appCreator baseapp.AppCreator) error {
|
||||
func startInProcess(ctx *Context, appCreator AppCreator) error {
|
||||
cfg := ctx.Config
|
||||
home := cfg.RootDir
|
||||
app, err := appCreator(home, ctx.Logger)
|
||||
|
|
|
@ -68,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 AppCreator) {
|
||||
appCreator AppCreator, appExport AppExporter) {
|
||||
|
||||
rootCmd.PersistentFlags().String("log_level", ctx.Config.LogLevel, "Log level")
|
||||
|
||||
|
@ -78,7 +78,7 @@ func AddCommands(
|
|||
UnsafeResetAllCmd(ctx),
|
||||
ShowNodeIDCmd(ctx),
|
||||
ShowValidatorCmd(ctx),
|
||||
ExportCmd(cts, cdc, appCreator),
|
||||
ExportCmd(ctx, cdc, appExport),
|
||||
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, process func(sdk.Account) (stop bool))
|
||||
IterateAccounts(ctx Context, process func(Account) (stop bool))
|
||||
}
|
||||
|
||||
// AccountDecoder unmarshals account bytes
|
||||
|
|
Loading…
Reference in New Issue