Pulled genesis file parsing into own package, for clarity
This commit is contained in:
parent
fbe88ee275
commit
6305399baf
@ -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)
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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.
|
||||
//
|
||||
|
||||
@ -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
|
||||
}
|
||||
78
genesis/parse_test.go
Normal file
78
genesis/parse_test.go
Normal file
@ -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)
|
||||
|
||||
}
|
||||
22
genesis/testdata/genesis.json
vendored
Normal file
22
genesis/testdata/genesis.json
vendored
Normal file
@ -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"]
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user