Move generateApp & exportApp to baseapp/helpers

This commit is contained in:
Christopher Goes 2018-04-27 13:38:11 +02:00 committed by rigelrozanski
parent f6cef02904
commit 10d2e5ae40
11 changed files with 83 additions and 73 deletions

View File

@ -241,6 +241,13 @@ 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

View File

@ -1,9 +1,14 @@
package baseapp
import (
"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
@ -22,3 +27,36 @@ 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) (interface{}, *wire.Codec), name string) AppExporter {
return func(rootDir string, logger log.Logger) (interface{}, *wire.Codec, error) {
dataDir := filepath.Join(rootDir, "data")
db, err := dbm.NewGoLevelDB(name, dataDir)
if err != nil {
return nil, nil, err
}
genesis, codec := appFn(logger, db)
return genesis, codec, nil
}
}

View File

@ -119,7 +119,7 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
}
// custom logic for export
func (app *GaiaApp) ExportGenesis() GenesisState {
func (app *GaiaApp) ExportGenesis() interface{} {
return GenesisState{
Accounts: []GenesisAccount{},
StakeData: stake.WriteGenesis(app.NewContext(true, abci.Header{}), app.stakeKeeper),

View File

@ -1,8 +1,6 @@
package main
import (
"path/filepath"
"github.com/spf13/cobra"
abci "github.com/tendermint/abci/types"
@ -10,6 +8,7 @@ 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"
@ -24,29 +23,20 @@ func main() {
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
}
server.AddCommands(ctx, cdc, rootCmd, app.GaiaAppInit(), generateApp, exportApp)
server.AddCommands(ctx, cdc, rootCmd, app.GaiaAppInit(),
baseapp.GenerateFn(newApp, "gaia"),
baseapp.ExportFn(exportApp, "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
}
gapp := app.NewGaiaApp(logger, db)
return gapp, nil
func newApp(logger log.Logger, db dbm.DB) abci.Application {
return app.NewGaiaApp(logger, db)
}
func exportApp(rootDir string, logger log.Logger) (interface{}, *wire.Codec, error) {
dataDir := filepath.Join(rootDir, "data")
db, err := dbm.NewGoLevelDB("gaia", dataDir)
if err != nil {
return nil, nil, err
}
gapp := app.NewGaiaApp(log.NewNopLogger(), db)
return gapp.ExportGenesis(), app.MakeCodec(), nil
func exportApp(logger log.Logger, db dbm.DB) (interface{}, *wire.Codec) {
gapp := app.NewGaiaApp(logger, db)
return gapp.ExportGenesis(), app.MakeCodec()
}

View File

@ -127,7 +127,7 @@ func (app *BasecoinApp) initChainer(ctx sdk.Context, req abci.RequestInitChain)
}
// Custom logic for state export
func (app *BasecoinApp) ExportGenesis() types.GenesisState {
func (app *BasecoinApp) ExportGenesis() interface{} {
return types.GenesisState{
Accounts: []*types.GenesisAccount{},
}

View File

@ -2,7 +2,6 @@ package main
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
@ -11,6 +10,7 @@ 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"
@ -26,7 +26,9 @@ func main() {
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
}
server.AddCommands(ctx, cdc, rootCmd, server.DefaultAppInit, generateApp, exportApp)
server.AddCommands(ctx, cdc, rootCmd, server.DefaultAppInit,
baseapp.GenerateFn(newApp, "basecoin"),
baseapp.ExportFn(exportApp, "basecoin"))
// prepare and add flags
rootDir := os.ExpandEnv("$HOME/.basecoind")
@ -34,22 +36,11 @@ func main() {
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
}
bapp := app.NewBasecoinApp(logger, db)
return bapp, nil
func newApp(logger log.Logger, db dbm.DB) abci.Application {
return app.NewBasecoinApp(logger, db)
}
func exportApp(rootDir string, logger log.Logger) (interface{}, *wire.Codec, error) {
dataDir := filepath.Join(rootDir, "data")
db, err := dbm.NewGoLevelDB("basecoin", dataDir)
if err != nil {
return nil, nil, err
}
func exportApp(logger log.Logger, db dbm.DB) (interface{}, *wire.Codec) {
bapp := app.NewBasecoinApp(logger, db)
return bapp.ExportGenesis(), app.MakeCodec(), nil
return bapp.ExportGenesis(), app.MakeCodec()
}

View File

@ -151,7 +151,7 @@ func (app *DemocoinApp) initChainerFn(coolKeeper cool.Keeper, powKeeper pow.Keep
}
// Custom logic for state export
func (app *DemocoinApp) ExportGenesis() types.GenesisState {
func (app *DemocoinApp) ExportGenesis() interface{} {
ctx := app.NewContext(true, abci.Header{})
return types.GenesisState{
Accounts: []*types.GenesisAccount{},

View File

@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"os"
"path/filepath"
"github.com/spf13/cobra"
@ -12,6 +11,7 @@ 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"
@ -43,23 +43,13 @@ func CoolAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState jso
return
}
func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
db, err := dbm.NewGoLevelDB("democoin", filepath.Join(rootDir, "data"))
if err != nil {
return nil, err
}
bapp := app.NewDemocoinApp(logger, db)
return bapp, nil
func newApp(logger log.Logger, db dbm.DB) abci.Application {
return app.NewDemocoinApp(logger, db)
}
func exportApp(rootDir string, logger log.Logger) (interface{}, *wire.Codec, error) {
dataDir := filepath.Join(rootDir, "data")
db, err := dbm.NewGoLevelDB("democoin", dataDir)
if err != nil {
return nil, nil, err
}
bapp := app.NewDemocoinApp(logger, db)
return bapp.ExportGenesis(), app.MakeCodec(), nil
func exportApp(logger log.Logger, db dbm.DB) (interface{}, *wire.Codec) {
dapp := app.NewDemocoinApp(logger, db)
return dapp.ExportGenesis(), app.MakeCodec()
}
func main() {
@ -72,7 +62,9 @@ func main() {
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
}
server.AddCommands(ctx, cdc, rootCmd, CoolAppInit, generateApp, exportApp)
server.AddCommands(ctx, cdc, rootCmd, CoolAppInit,
baseapp.GenerateFn(newApp, "democoin"),
baseapp.ExportFn(exportApp, "democoin"))
// prepare and add flags
rootDir := os.ExpandEnv("$HOME/.democoind")

View File

@ -6,16 +6,12 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/wire"
)
// AppExporter dumps all app state to JSON-serializable structure
type AppExporter func(home string, log log.Logger) (interface{}, *wire.Codec, error)
// ExportCmd dumps app state to JSON
func ExportCmd(app AppExporter, ctx *Context) *cobra.Command {
func ExportCmd(app baseapp.AppExporter, ctx *Context) *cobra.Command {
export := exportCmd{
appExporter: app,
context: ctx,
@ -29,7 +25,7 @@ func ExportCmd(app AppExporter, ctx *Context) *cobra.Command {
}
type exportCmd struct {
appExporter AppExporter
appExporter baseapp.AppExporter
context *Context
}

View File

@ -6,14 +6,13 @@ import (
"github.com/spf13/viper"
"github.com/tendermint/abci/server"
abci "github.com/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/baseapp"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/proxy"
pvm "github.com/tendermint/tendermint/types/priv_validator"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
)
const (
@ -21,13 +20,9 @@ const (
flagAddress = "address"
)
// AppCreator lets us lazily initialize app, using home dir
// and other flags (?) to start
type AppCreator func(string, log.Logger) (abci.Application, error)
// StartCmd runs the service passed in, either
// stand-alone, or in-process with tendermint
func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command {
func StartCmd(ctx *Context, appCreator baseapp.AppCreator) *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Run the full node",
@ -50,7 +45,7 @@ func StartCmd(ctx *Context, appCreator AppCreator) *cobra.Command {
return cmd
}
func startStandAlone(ctx *Context, appCreator AppCreator) error {
func startStandAlone(ctx *Context, appCreator baseapp.AppCreator) error {
// Generate the app in the proper dir
addr := viper.GetString(flagAddress)
home := viper.GetString("home")
@ -74,7 +69,7 @@ func startStandAlone(ctx *Context, appCreator AppCreator) error {
return nil
}
func startInProcess(ctx *Context, appCreator AppCreator) error {
func startInProcess(ctx *Context, appCreator baseapp.AppCreator) error {
cfg := ctx.Config
home := cfg.RootDir
app, err := appCreator(home, ctx.Logger)

View File

@ -9,6 +9,7 @@ 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"
@ -68,7 +69,7 @@ func PersistentPreRunEFn(context *Context) func(*cobra.Command, []string) error
func AddCommands(
ctx *Context, cdc *wire.Codec,
rootCmd *cobra.Command, appInit AppInit,
appCreator AppCreator, appExporter AppExporter) {
appCreator baseapp.AppCreator, appExporter baseapp.AppExporter) {
rootCmd.PersistentFlags().String("log_level", ctx.Config.LogLevel, "Log level")