diff --git a/app/bc.go b/app/bc.go index 81e559f4a..46d1a1ca0 100644 --- a/app/bc.go +++ b/app/bc.go @@ -4,13 +4,13 @@ import ( "fmt" abci "github.com/tendermint/abci/types" - "github.com/tendermint/abci/version" "github.com/tendermint/tmlibs/log" sdk "github.com/cosmos/cosmos-sdk" "github.com/cosmos/cosmos-sdk/errors" "github.com/cosmos/cosmos-sdk/stack" sm "github.com/cosmos/cosmos-sdk/state" + "github.com/cosmos/cosmos-sdk/version" ) // Basecoin - The ABCI application @@ -61,7 +61,13 @@ func (app *Basecoin) InitState(module, key, value string) (string, error) { return "", fmt.Errorf("unknown base option: %s", key) } - return app.handler.InitState(app.Logger(), state, module, key, value) + log, err := app.handler.InitState(app.Logger(), state, module, key, value) + if err != nil { + app.Logger().Error("Genesis App Options", "err", err) + } else { + app.Logger().Info(log) + } + return log, err } // DeliverTx - ABCI @@ -119,25 +125,3 @@ func (app *Basecoin) BeginBlock(req abci.RequestBeginBlock) { app.AddValChange(diff) } } - -// LoadGenesis parses the genesis file and sets the initial -// state based on that -func (app *Basecoin) LoadGenesis(filePath string) error { - init, err := GetInitialState(filePath) - if err != nil { - return err - } - - // execute all the genesis init options - // abort on any error - fmt.Printf("%#v\n", init) - for _, mkv := range init { - log, _ := app.InitState(mkv.Module, mkv.Key, mkv.Value) - // TODO: error out on bad options?? - // if err != nil { - // return err - // } - app.Logger().Info(log) - } - return nil -} diff --git a/app/genesis.go b/app/genesis.go index ceb0e1156..29031d269 100644 --- a/app/genesis.go +++ b/app/genesis.go @@ -14,17 +14,43 @@ const ( ModuleNameBase = "base" ) -// InitState just holds module/key/value triples from +// Option just holds module/key/value triples from // parsing the genesis file -type InitState struct { +type Option struct { Module string Key string Value string } -// GetInitialState parses the genesis file in a format -// that can easily be handed into InitState modules -func GetInitialState(path string) ([]InitState, error) { +// InitStater is anything that can handle app options +// from genesis file. +type InitStater interface { + InitState(module, key, value string) (string, error) +} + +// LoadGenesis parses the genesis file and sets the initial +// state based on that +func LoadGenesis(app InitStater, filePath string) error { + opts, err := GetGenesisOptions(filePath) + if err != nil { + return err + } + + // execute all the genesis init options + // abort on any error + for _, opt := range opts { + _, _ = app.InitState(opt.Module, opt.Key, opt.Value) + // TODO: error out on bad options?? + // if err != nil { + // return err + // } + } + return nil +} + +// GetGenesisOptions parses the genesis file in a format +// that can easily be handed into InitStaters +func GetGenesisOptions(path string) ([]Option, error) { genDoc, err := loadGenesis(path) if err != nil { return nil, err @@ -32,21 +58,21 @@ func GetInitialState(path string) ([]InitState, error) { opts := genDoc.AppOptions cnt := 1 + len(opts.Accounts) + len(opts.pluginOptions) - res := make([]InitState, cnt) + res := make([]Option, cnt) - res[0] = InitState{ModuleNameBase, ChainKey, genDoc.ChainID} + res[0] = Option{ModuleNameBase, ChainKey, genDoc.ChainID} i := 1 // set accounts for _, acct := range opts.Accounts { - res[i] = InitState{"coin", "account", string(acct)} + res[i] = Option{"coin", "account", string(acct)} i++ } // set plugin options for _, kv := range opts.pluginOptions { module, key := splitKey(kv.Key) - res[i] = InitState{module, key, kv.Value} + res[i] = Option{module, key, kv.Value} i++ } diff --git a/app/genesis_test.go b/app/genesis_test.go index ccb1229dc..ee73ceb0f 100644 --- a/app/genesis_test.go +++ b/app/genesis_test.go @@ -25,7 +25,7 @@ func TestLoadGenesisDoNotFailIfAppOptionsAreMissing(t *testing.T) { logger) require.Nil(t, err, "%+v", err) - err = app.LoadGenesis("./testdata/genesis3.json") + err = LoadGenesis(app, "./testdata/genesis3.json") require.Nil(t, err, "%+v", err) } @@ -39,7 +39,7 @@ func TestLoadGenesis(t *testing.T) { logger) require.Nil(err, "%+v", err) - err = app.LoadGenesis(genesisFilepath) + err = LoadGenesis(app, genesisFilepath) require.Nil(err, "%+v", err) // check the chain id @@ -73,7 +73,7 @@ func TestLoadGenesisAccountAddress(t *testing.T) { logger) require.Nil(err, "%+v", err) - err = app.LoadGenesis(genesisAcctFilepath) + err = LoadGenesis(app, genesisAcctFilepath) require.Nil(err, "%+v", err) // check the chain id diff --git a/server/commands/start.go b/server/commands/start.go index 825e9bfd3..117ec6b2b 100644 --- a/server/commands/start.go +++ b/server/commands/start.go @@ -111,7 +111,7 @@ func start(rootDir string, basecoinApp *app.Basecoin) error { // If genesis file exists, set key-value options genesisFile := path.Join(rootDir, "genesis.json") if _, err := os.Stat(genesisFile); err == nil { - err := basecoinApp.LoadGenesis(genesisFile) + err = app.LoadGenesis(basecoinApp, genesisFile) if err != nil { return errors.Errorf("Error in LoadGenesis: %v\n", err) }