Abort on error with genesis app_options

This commit is contained in:
Ethan Frey 2017-10-16 19:36:03 +02:00
parent f65215ad92
commit 3316bfcfb8
4 changed files with 104 additions and 44 deletions

View File

@ -39,11 +39,10 @@ func LoadGenesis(app InitStater, filePath string) error {
// 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
// }
_, err = app.InitState(opt.Module, opt.Key, opt.Value)
if err != nil {
return err
}
}
return nil
}

View File

@ -17,6 +17,10 @@ import (
const genesisFilepath = "./testdata/genesis.json"
const genesisAcctFilepath = "./testdata/genesis2.json"
// 2b is just like 2, but add carl who has inconsistent
// pubkey and address
const genesisBadAcctFilepath = "./testdata/genesis2b.json"
func TestLoadGenesisDoNotFailIfAppOptionsAreMissing(t *testing.T) {
logger := log.TestingLogger()
store, err := MockStoreApp("genesis", logger)
@ -27,35 +31,16 @@ func TestLoadGenesisDoNotFailIfAppOptionsAreMissing(t *testing.T) {
require.Nil(t, err, "%+v", err)
}
func TestLoadGenesis(t *testing.T) {
assert, require := assert.New(t), require.New(t)
func TestLoadGenesisFailsWithUnknownOptions(t *testing.T) {
require := require.New(t)
logger := log.TestingLogger()
store, err := MockStoreApp("genesis", logger)
require.Nil(err, "%+v", err)
app := NewBaseApp(store, DefaultHandler("mycoin"), nil)
err = LoadGenesis(app, genesisFilepath)
require.Nil(err, "%+v", err)
// check the chain id
assert.Equal("foo_bar_chain", app.GetChainID())
// and check the account info - previously calculated values
addr, _ := hex.DecodeString("eb98e0688217cfdeb70eddf4b33cdcc37fc53197")
coins, err := getAddr(addr, app.Append())
require.Nil(err)
assert.True(coins.IsPositive())
// make sure balance is proper
assert.Equal(2, len(coins))
assert.True(coins.IsValid())
// note, that we now sort them to be valid
assert.EqualValues(654321, coins[0].Amount)
assert.EqualValues("ETH", coins[0].Denom)
assert.EqualValues(12345, coins[1].Amount)
assert.EqualValues("blank", coins[1].Denom)
require.NotNil(err, "%+v", err)
}
// Fix for issue #89, change the parse format for accounts in genesis.json
@ -84,9 +69,6 @@ func TestLoadGenesisAccountAddress(t *testing.T) {
{"62035D628DE7543332544AA60D90D3693B6AD51B", true, true, coin.Coins{{"one", 111}}},
// this comes from an address, should be stored proper (bob)
{"C471FB670E44D219EE6DF2FC284BE38793ACBCE1", true, false, coin.Coins{{"two", 222}}},
// this one had a mismatched address and pubkey, should not store under either (carl)
{"1234ABCDD18E8EFE3FFC4B0506BF9BF8E5B0D9E9", false, false, nil}, // this is given addr
{"700BEC5ED18E8EFE3FFC4B0506BF9BF8E5B0D9E9", false, false, nil}, // this is addr of the given pubkey
// this comes from a secp256k1 public key, should be stored proper (sam)
{"979F080B1DD046C452C2A8A250D18646C6B669D4", true, true, coin.Coins{{"four", 444}}},
}
@ -106,6 +88,19 @@ func TestLoadGenesisAccountAddress(t *testing.T) {
}
}
// When you define an account in genesis with address
// and pubkey that don't match
func TestLoadGenesisAccountInconsistentAddress(t *testing.T) {
require := require.New(t)
logger := log.TestingLogger()
store, err := MockStoreApp("genesis", logger)
require.Nil(err, "%+v", err)
app := NewBaseApp(store, DefaultHandler("mycoin"), nil)
err = LoadGenesis(app, genesisBadAcctFilepath)
require.NotNil(err)
}
func TestParseGenesisList(t *testing.T) {
assert, require := assert.New(t), require.New(t)
@ -126,3 +121,30 @@ func TestParseGenesisList(t *testing.T) {
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)
}

View File

@ -22,19 +22,6 @@
"amount": 222
}
]
}, {
"name": "carl",
"address": "1234ABCDD18E8EFE3FFC4B0506BF9BF8E5B0D9E9",
"pub_key": {
"type": "ed25519",
"data": "177C0AC45E86257F0708DC085D592AB22AAEECD1D26381B757F7C96135921858"
},
"coins": [
{
"denom": "three",
"amount": 333
}
]
}, {
"name": "sam",
"pub_key": {

52
app/testdata/genesis2b.json vendored Normal file
View File

@ -0,0 +1,52 @@
{
"chain_id": "addr_accounts_chain",
"app_options": {
"accounts": [{
"name": "alice",
"pub_key": {
"type": "ed25519",
"data": "DBD9A46C45868F0A37C92B53113C09B048FBD87B5FBC2F8B199052973B8FAA36"
},
"coins": [
{
"denom": "one",
"amount": 111
}
]
}, {
"name": "bob",
"address": "C471FB670E44D219EE6DF2FC284BE38793ACBCE1",
"coins": [
{
"denom": "two",
"amount": 222
}
]
}, {
"name": "carl",
"address": "1234ABCDD18E8EFE3FFC4B0506BF9BF8E5B0D9E9",
"pub_key": {
"type": "ed25519",
"data": "177C0AC45E86257F0708DC085D592AB22AAEECD1D26381B757F7C96135921858"
},
"coins": [
{
"denom": "three",
"amount": 333
}
]
}, {
"name": "sam",
"pub_key": {
"type": "secp256k1",
"data": "02AA8342F63CCCCE6DDB128525BA048CE0B2993DA3B4308746E1F216361A87651E"
},
"coins": [
{
"denom": "four",
"amount": 444
}
]
}]
}
}