test LoadGenesis and change format
This commit is contained in:
+4
-4
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
@@ -68,13 +69,12 @@ func (app *Basecoin) SetOption(key string, value string) string {
|
||||
app.state.SetChainID(value)
|
||||
return "Success"
|
||||
case "account":
|
||||
var err error
|
||||
var acc *types.Account
|
||||
wire.ReadJSONPtr(&acc, []byte(value), &err)
|
||||
var acc types.Account
|
||||
err := json.Unmarshal([]byte(value), &acc)
|
||||
if err != nil {
|
||||
return "Error decoding acc message: " + err.Error()
|
||||
}
|
||||
app.state.SetAccount(acc.PubKey.Address(), acc)
|
||||
app.state.SetAccount(acc.PubKey.Address(), &acc)
|
||||
log.Info("SetAccount", "addr", acc.PubKey.Address(), "acc", acc)
|
||||
return "Success"
|
||||
}
|
||||
|
||||
+14
-21
@@ -2,8 +2,6 @@ package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
cmn "github.com/tendermint/go-common"
|
||||
@@ -15,9 +13,7 @@ func (app *Basecoin) LoadGenesis(path string) error {
|
||||
return err
|
||||
}
|
||||
for _, kv := range kvz {
|
||||
log := app.SetOption(kv.Key, kv.Value)
|
||||
// TODO: remove debug output
|
||||
fmt.Printf("Set %v=%v. Log: %v\n", kv.Key, kv.Value, log)
|
||||
app.SetOption(kv.Key, kv.Value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -28,7 +24,7 @@ type keyValue struct {
|
||||
}
|
||||
|
||||
func loadGenesis(filePath string) (kvz []keyValue, err error) {
|
||||
kvz_ := []interface{}{}
|
||||
kvz_ := []json.RawMessage{}
|
||||
bytes, err := cmn.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "loading genesis file")
|
||||
@@ -40,24 +36,21 @@ func loadGenesis(filePath string) (kvz []keyValue, err error) {
|
||||
if len(kvz_)%2 != 0 {
|
||||
return nil, errors.New("genesis cannot have an odd number of items. Format = [key1, value1, key2, value2, ...]")
|
||||
}
|
||||
|
||||
for i := 0; i < len(kvz_); i += 2 {
|
||||
keyIfc := kvz_[i]
|
||||
valueIfc := kvz_[i+1]
|
||||
var key, value string
|
||||
key, ok := keyIfc.(string)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("genesis had invalid key %v of type %v", keyIfc, reflect.TypeOf(keyIfc))
|
||||
kv := keyValue{}
|
||||
rawK := []byte(kvz_[i])
|
||||
err := json.Unmarshal(rawK, &(kv.Key))
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("Non-string key: %s", string(rawK))
|
||||
}
|
||||
if value_, ok := valueIfc.(string); ok {
|
||||
value = value_
|
||||
} else {
|
||||
valueBytes, err := json.Marshal(valueIfc)
|
||||
if err != nil {
|
||||
return nil, errors.Errorf("genesis had invalid value %v: %v", value_, err.Error())
|
||||
}
|
||||
value = string(valueBytes)
|
||||
// convert value to string if possible (otherwise raw json)
|
||||
rawV := kvz_[i+1]
|
||||
err = json.Unmarshal(rawV, &(kv.Value))
|
||||
if err != nil {
|
||||
kv.Value = string(rawV)
|
||||
}
|
||||
kvz = append(kvz, keyValue{key, value})
|
||||
kvz = append(kvz, kv)
|
||||
}
|
||||
return kvz, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tendermint/go-crypto"
|
||||
eyescli "github.com/tendermint/merkleeyes/client"
|
||||
)
|
||||
|
||||
func TestLoadGenesis(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
eyesCli := eyescli.NewLocalClient("", 0)
|
||||
app := NewBasecoin(eyesCli)
|
||||
err := app.LoadGenesis("./testdata/genesis.json")
|
||||
require.Nil(err, "%+v", err)
|
||||
|
||||
// check the chain id
|
||||
assert.Equal("foo_bar_chain", app.GetState().GetChainID())
|
||||
|
||||
// and check the account info - previously calculated values
|
||||
addr, _ := hex.DecodeString("eb98e0688217cfdeb70eddf4b33cdcc37fc53197")
|
||||
pkbyte, _ := hex.DecodeString("6880db93598e283a67c4d88fc67a8858aa2de70f713fe94a5109e29c137100c2")
|
||||
|
||||
acct := app.GetState().GetAccount(addr)
|
||||
require.NotNil(acct)
|
||||
|
||||
// make sure balance is proper
|
||||
assert.Equal(2, len(acct.Balance))
|
||||
assert.EqualValues(12345, acct.Balance[0].Amount)
|
||||
assert.EqualValues("blank", acct.Balance[0].Denom)
|
||||
|
||||
// and public key is parsed properly
|
||||
apk := acct.PubKey.PubKey
|
||||
require.NotNil(apk)
|
||||
epk, ok := apk.(crypto.PubKeyEd25519)
|
||||
if assert.True(ok) {
|
||||
assert.EqualValues(pkbyte, epk[:])
|
||||
}
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
[
|
||||
"base/chainID", "foo_bar_chain",
|
||||
"base/account", {
|
||||
"pub_key": {
|
||||
"type": "ed25519",
|
||||
"data": "6880db93598e283a67c4d88fc67a8858aa2de70f713fe94a5109e29c137100c2"
|
||||
},
|
||||
"coins": [
|
||||
{
|
||||
"denom": "blank",
|
||||
"amount": 12345
|
||||
},
|
||||
{
|
||||
"denom": "ETH",
|
||||
"amount": 654321
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user