From 6305399baf2b82955e5525da79943bb2a23dbe14 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 17 Oct 2017 15:47:33 +0200 Subject: [PATCH] Pulled genesis file parsing into own package, for clarity --- app/app_test.go | 16 ------ app/base.go | 4 +- app/genesis_test.go | 59 ++-------------------- app/store.go | 5 -- app/genesis.go => genesis/parse.go | 12 ++--- genesis/parse_test.go | 78 ++++++++++++++++++++++++++++++ genesis/testdata/genesis.json | 22 +++++++++ handler.go | 7 +++ server/commands/start.go | 3 +- 9 files changed, 120 insertions(+), 86 deletions(-) rename app/genesis.go => genesis/parse.go (95%) create mode 100644 genesis/parse_test.go create mode 100644 genesis/testdata/genesis.json diff --git a/app/app_test.go b/app/app_test.go index 0e90f65413..5d6f84a3c0 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -291,19 +291,3 @@ func TestQuery(t *testing.T) { }) assert.NotEqual(resQueryPreCommit, resQueryPostCommit, "Query should change before/after commit") } - -func TestSplitKey(t *testing.T) { - assert := assert.New(t) - prefix, suffix := splitKey("foo/bar") - assert.EqualValues("foo", prefix) - assert.EqualValues("bar", suffix) - - prefix, suffix = splitKey("foobar") - assert.EqualValues("base", prefix) - assert.EqualValues("foobar", suffix) - - prefix, suffix = splitKey("some/complex/issue") - assert.EqualValues("some", prefix) - assert.EqualValues("complex/issue", suffix) - -} diff --git a/app/base.go b/app/base.go index a0ddbb7e21..8d7e73a1ea 100644 --- a/app/base.go +++ b/app/base.go @@ -95,8 +95,8 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) { func (app *BaseApp) InitState(module, key, value string) (string, error) { state := app.Append() - if module == ModuleNameBase { - if key == ChainKey { + if module == sdk.ModuleNameBase { + if key == sdk.ChainKey { app.info.SetChainID(state, value) return "Success", nil } diff --git a/app/genesis_test.go b/app/genesis_test.go index f62164f696..4707d8c4ee 100644 --- a/app/genesis_test.go +++ b/app/genesis_test.go @@ -2,15 +2,14 @@ package app import ( "encoding/hex" - "encoding/json" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/log" + "github.com/cosmos/cosmos-sdk/genesis" "github.com/cosmos/cosmos-sdk/modules/coin" ) @@ -27,7 +26,7 @@ func TestLoadGenesisDoNotFailIfAppOptionsAreMissing(t *testing.T) { require.Nil(t, err, "%+v", err) app := NewBaseApp(store, DefaultHandler("mycoin"), nil) - err = LoadGenesis(app, "./testdata/genesis3.json") + err = genesis.LoadGenesis(app, "./testdata/genesis3.json") require.Nil(t, err, "%+v", err) } @@ -39,7 +38,7 @@ func TestLoadGenesisFailsWithUnknownOptions(t *testing.T) { require.Nil(err, "%+v", err) app := NewBaseApp(store, DefaultHandler("mycoin"), nil) - err = LoadGenesis(app, genesisFilepath) + err = genesis.LoadGenesis(app, genesisFilepath) require.NotNil(err, "%+v", err) } @@ -52,7 +51,7 @@ func TestLoadGenesisAccountAddress(t *testing.T) { require.Nil(err, "%+v", err) app := NewBaseApp(store, DefaultHandler("mycoin"), nil) - err = LoadGenesis(app, genesisAcctFilepath) + err = genesis.LoadGenesis(app, genesisAcctFilepath) require.Nil(err, "%+v", err) // check the chain id @@ -97,54 +96,6 @@ func TestLoadGenesisAccountInconsistentAddress(t *testing.T) { store, err := MockStoreApp("genesis", logger) require.Nil(err, "%+v", err) app := NewBaseApp(store, DefaultHandler("mycoin"), nil) - err = LoadGenesis(app, genesisBadAcctFilepath) + err = genesis.LoadGenesis(app, genesisBadAcctFilepath) require.NotNil(err) } - -func TestParseGenesisList(t *testing.T) { - assert, require := assert.New(t), require.New(t) - - bytes, err := cmn.ReadFile(genesisFilepath) - require.Nil(err, "loading genesis file %+v", err) - - // the basecoin genesis go-wire/data :) - genDoc := new(FullGenesisDoc) - err = json.Unmarshal(bytes, genDoc) - require.Nil(err, "unmarshaling genesis file %+v", err) - - pluginOpts, err := parseGenesisList(genDoc.AppOptions.PluginOptions) - require.Nil(err, "%+v", err) - genDoc.AppOptions.pluginOptions = pluginOpts - - assert.Equal(genDoc.AppOptions.pluginOptions[0].Key, "plugin1/key1") - assert.Equal(genDoc.AppOptions.pluginOptions[1].Key, "plugin1/key2") - assert.Equal(genDoc.AppOptions.pluginOptions[0].Value, "value1") - assert.Equal(genDoc.AppOptions.pluginOptions[1].Value, "value2") -} - -func TestGetGenesisOptions(t *testing.T) { - assert, require := assert.New(t), require.New(t) - - opts, err := GetGenesisOptions(genesisFilepath) - require.Nil(err, "loading genesis file %+v", err) - - require.Equal(4, len(opts)) - chain := opts[0] - assert.Equal(ModuleNameBase, chain.Module) - assert.Equal(ChainKey, chain.Key) - assert.Equal("foo_bar_chain", chain.Value) - - acct := opts[1] - assert.Equal("coin", acct.Module) - assert.Equal("account", acct.Key) - - p1 := opts[2] - assert.Equal("plugin1", p1.Module) - assert.Equal("key1", p1.Key) - assert.Equal("value1", p1.Value) - - p2 := opts[3] - assert.Equal("plugin1", p2.Module) - assert.Equal("key2", p2.Key) - assert.Equal("value2", p2.Value) -} diff --git a/app/store.go b/app/store.go index 8c31d9cb6f..2ad14915ab 100644 --- a/app/store.go +++ b/app/store.go @@ -17,11 +17,6 @@ import ( sm "github.com/cosmos/cosmos-sdk/state" ) -//nolint -const ( - ChainKey = "chain_id" -) - // StoreApp contains a data store and all info needed // to perform queries and handshakes. // diff --git a/app/genesis.go b/genesis/parse.go similarity index 95% rename from app/genesis.go rename to genesis/parse.go index 17d9bdebea..f0eefd2138 100644 --- a/app/genesis.go +++ b/genesis/parse.go @@ -1,19 +1,15 @@ -package app +package genesis import ( "encoding/json" "strings" + sdk "github.com/cosmos/cosmos-sdk" "github.com/pkg/errors" cmn "github.com/tendermint/tmlibs/common" ) -//nolint -const ( - ModuleNameBase = "base" -) - // Option just holds module/key/value triples from // parsing the genesis file type Option struct { @@ -59,7 +55,7 @@ func GetGenesisOptions(path string) ([]Option, error) { cnt := 1 + len(opts.Accounts) + len(opts.pluginOptions) res := make([]Option, cnt) - res[0] = Option{ModuleNameBase, ChainKey, genDoc.ChainID} + res[0] = Option{sdk.ModuleNameBase, sdk.ChainKey, genDoc.ChainID} i := 1 // set accounts @@ -152,5 +148,5 @@ func splitKey(key string) (string, string) { keyParts := strings.SplitN(key, "/", 2) return keyParts[0], keyParts[1] } - return ModuleNameBase, key + return sdk.ModuleNameBase, key } diff --git a/genesis/parse_test.go b/genesis/parse_test.go new file mode 100644 index 0000000000..5c81d794cc --- /dev/null +++ b/genesis/parse_test.go @@ -0,0 +1,78 @@ +package genesis + +import ( + "encoding/json" + "testing" + + sdk "github.com/cosmos/cosmos-sdk" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + cmn "github.com/tendermint/tmlibs/common" +) + +const genesisFilepath = "./testdata/genesis.json" + +func TestParseGenesisList(t *testing.T) { + assert, require := assert.New(t), require.New(t) + + bytes, err := cmn.ReadFile(genesisFilepath) + require.Nil(err, "loading genesis file %+v", err) + + // the basecoin genesis go-wire/data :) + genDoc := new(FullGenesisDoc) + err = json.Unmarshal(bytes, genDoc) + require.Nil(err, "unmarshaling genesis file %+v", err) + + pluginOpts, err := parseGenesisList(genDoc.AppOptions.PluginOptions) + require.Nil(err, "%+v", err) + genDoc.AppOptions.pluginOptions = pluginOpts + + assert.Equal(genDoc.AppOptions.pluginOptions[0].Key, "plugin1/key1") + assert.Equal(genDoc.AppOptions.pluginOptions[1].Key, "plugin1/key2") + assert.Equal(genDoc.AppOptions.pluginOptions[0].Value, "value1") + assert.Equal(genDoc.AppOptions.pluginOptions[1].Value, "value2") +} + +func TestGetGenesisOptions(t *testing.T) { + assert, require := assert.New(t), require.New(t) + + opts, err := GetGenesisOptions(genesisFilepath) + require.Nil(err, "loading genesis file %+v", err) + + require.Equal(4, len(opts)) + chain := opts[0] + assert.Equal(sdk.ModuleNameBase, chain.Module) + assert.Equal(sdk.ChainKey, chain.Key) + assert.Equal("foo_bar_chain", chain.Value) + + acct := opts[1] + assert.Equal("coin", acct.Module) + assert.Equal("account", acct.Key) + + p1 := opts[2] + assert.Equal("plugin1", p1.Module) + assert.Equal("key1", p1.Key) + assert.Equal("value1", p1.Value) + + p2 := opts[3] + assert.Equal("plugin1", p2.Module) + assert.Equal("key2", p2.Key) + assert.Equal("value2", p2.Value) +} + +func TestSplitKey(t *testing.T) { + assert := assert.New(t) + prefix, suffix := splitKey("foo/bar") + assert.EqualValues("foo", prefix) + assert.EqualValues("bar", suffix) + + prefix, suffix = splitKey("foobar") + assert.EqualValues("base", prefix) + assert.EqualValues("foobar", suffix) + + prefix, suffix = splitKey("some/complex/issue") + assert.EqualValues("some", prefix) + assert.EqualValues("complex/issue", suffix) + +} diff --git a/genesis/testdata/genesis.json b/genesis/testdata/genesis.json new file mode 100644 index 0000000000..ee8879fd28 --- /dev/null +++ b/genesis/testdata/genesis.json @@ -0,0 +1,22 @@ +{ + "chain_id": "foo_bar_chain", + "app_options": { + "accounts": [{ + "pub_key": { + "type": "ed25519", + "data": "6880db93598e283a67c4d88fc67a8858aa2de70f713fe94a5109e29c137100c2" + }, + "coins": [ + { + "denom": "blank", + "amount": 12345 + }, + { + "denom": "ETH", + "amount": 654321 + } + ] + }], + "plugin_options": ["plugin1/key1", "value1", "plugin1/key2", "value2"] + } +} diff --git a/handler.go b/handler.go index 51d8e4a127..f7d6638029 100644 --- a/handler.go +++ b/handler.go @@ -8,6 +8,13 @@ import ( "github.com/cosmos/cosmos-sdk/state" ) +const ( + // ModuleNameBase is the module name for internal functionality + ModuleNameBase = "base" + // ChainKey is the option key for setting the chain id + ChainKey = "chain_id" +) + // Handler is anything that processes a transaction type Handler interface { // Checker verifies there are valid fees and estimates work diff --git a/server/commands/start.go b/server/commands/start.go index 22e79ec1c7..a7f41a329b 100644 --- a/server/commands/start.go +++ b/server/commands/start.go @@ -21,6 +21,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk" "github.com/cosmos/cosmos-sdk/app" + "github.com/cosmos/cosmos-sdk/genesis" "github.com/cosmos/cosmos-sdk/version" ) @@ -119,7 +120,7 @@ func start(rootDir string, basecoinApp *app.BaseApp) error { // If genesis file exists, set key-value options genesisFile := path.Join(rootDir, "genesis.json") if _, err := os.Stat(genesisFile); err == nil { - err = app.LoadGenesis(basecoinApp, genesisFile) + err = genesis.LoadGenesis(basecoinApp, genesisFile) if err != nil { return errors.Errorf("Error in LoadGenesis: %v\n", err) }