test LoadGenesis and change format
This commit is contained in:
parent
1fd2d17cd9
commit
a6f62023b2
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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[:])
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
|
@ -1,12 +1,15 @@
|
|||
[
|
||||
"base/chainID", "test_chain_id",
|
||||
"base/account", {
|
||||
"pub_key": [1, "619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"],
|
||||
"pub_key": {
|
||||
"type": "ed25519",
|
||||
"data": "619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"
|
||||
},
|
||||
"coins": [
|
||||
{
|
||||
"denom": "mycoin",
|
||||
"amount": 9007199254740992
|
||||
}
|
||||
{
|
||||
"denom": "mycoin",
|
||||
"amount": 9007199254740992
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
[
|
||||
"base/chainID", "test_chain_1",
|
||||
"base/account", {
|
||||
"pub_key": [1, "B3588BDC92015ED3CDB6F57A86379E8C79A7111063610B7E625487C76496F4DF"],
|
||||
"pub_key": {
|
||||
"type": "ed25519",
|
||||
"data": "B3588BDC92015ED3CDB6F57A86379E8C79A7111063610B7E625487C76496F4DF"
|
||||
},
|
||||
"coins": [
|
||||
{
|
||||
"denom": "mycoin",
|
||||
"amount": 9007199254740992
|
||||
}
|
||||
{
|
||||
"denom": "mycoin",
|
||||
"amount": 9007199254740992
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
[
|
||||
"base/chainID", "test_chain_2",
|
||||
"base/account", {
|
||||
"pub_key": [1, "0628C8E6C2D50B15764B443394E06C6A64F3082CE966A2A8C1A55A4D63D0FC5D"],
|
||||
"pub_key": {
|
||||
"type": "ed25519",
|
||||
"data": "0628C8E6C2D50B15764B443394E06C6A64F3082CE966A2A8C1A55A4D63D0FC5D"
|
||||
},
|
||||
"coins": [
|
||||
{
|
||||
"denom": "mycoin",
|
||||
"amount": 9007199254740992
|
||||
}
|
||||
{
|
||||
"denom": "mycoin",
|
||||
"amount": 9007199254740992
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package counter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/basecoin/app"
|
||||
"github.com/tendermint/basecoin/types"
|
||||
|
@ -19,7 +21,7 @@ func TestCounterPlugin(t *testing.T) {
|
|||
chainID := "test_chain_id"
|
||||
bcApp := app.NewBasecoin(eyesCli)
|
||||
bcApp.SetOption("base/chainID", chainID)
|
||||
t.Log(bcApp.Info())
|
||||
// t.Log(bcApp.Info())
|
||||
|
||||
// Add Counter plugin
|
||||
counterPlugin := New()
|
||||
|
@ -31,7 +33,9 @@ func TestCounterPlugin(t *testing.T) {
|
|||
// Seed Basecoin with account
|
||||
test1Acc := test1PrivAcc.Account
|
||||
test1Acc.Balance = types.Coins{{"", 1000}, {"gold", 1000}}
|
||||
bcApp.SetOption("base/account", string(wire.JSONBytes(test1Acc)))
|
||||
accOpt, err := json.Marshal(test1Acc)
|
||||
require.Nil(t, err)
|
||||
bcApp.SetOption("base/account", string(accOpt))
|
||||
|
||||
// Deliver a CounterTx
|
||||
DeliverCounterTx := func(gas int64, fee types.Coin, inputCoins types.Coins, inputSequence int, appFee types.Coins) abci.Result {
|
||||
|
@ -46,10 +50,10 @@ func TestCounterPlugin(t *testing.T) {
|
|||
|
||||
// Sign request
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
t.Logf("Sign bytes: %X\n", signBytes)
|
||||
// t.Logf("Sign bytes: %X\n", signBytes)
|
||||
sig := test1PrivAcc.Sign(signBytes)
|
||||
tx.Input.Signature = crypto.SignatureS{sig}
|
||||
t.Logf("Signed TX bytes: %X\n", wire.BinaryBytes(struct{ types.Tx }{tx}))
|
||||
// t.Logf("Signed TX bytes: %X\n", wire.BinaryBytes(struct{ types.Tx }{tx}))
|
||||
|
||||
// Write request
|
||||
txBytes := wire.BinaryBytes(struct{ types.Tx }{tx})
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package tmsp_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tendermint/basecoin/app"
|
||||
"github.com/tendermint/basecoin/types"
|
||||
cmn "github.com/tendermint/go-common"
|
||||
|
@ -16,7 +19,7 @@ func TestSendTx(t *testing.T) {
|
|||
chainID := "test_chain_id"
|
||||
bcApp := app.NewBasecoin(eyesCli)
|
||||
bcApp.SetOption("base/chainID", chainID)
|
||||
t.Log(bcApp.Info())
|
||||
// t.Log(bcApp.Info())
|
||||
|
||||
test1PrivAcc := types.PrivAccountFromSecret("test1")
|
||||
test2PrivAcc := types.PrivAccountFromSecret("test2")
|
||||
|
@ -24,7 +27,9 @@ func TestSendTx(t *testing.T) {
|
|||
// Seed Basecoin with account
|
||||
test1Acc := test1PrivAcc.Account
|
||||
test1Acc.Balance = types.Coins{{"", 1000}}
|
||||
t.Log(bcApp.SetOption("base/account", string(wire.JSONBytes(test1Acc))))
|
||||
accOpt, err := json.Marshal(test1Acc)
|
||||
require.Nil(t, err)
|
||||
bcApp.SetOption("base/account", string(accOpt))
|
||||
|
||||
// Construct a SendTx signature
|
||||
tx := &types.SendTx{
|
||||
|
@ -43,18 +48,16 @@ func TestSendTx(t *testing.T) {
|
|||
|
||||
// Sign request
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
t.Log("Sign bytes: %X\n", signBytes)
|
||||
// t.Log("Sign bytes: %X\n", signBytes)
|
||||
sig := test1PrivAcc.Sign(signBytes)
|
||||
tx.Inputs[0].Signature = crypto.SignatureS{sig}
|
||||
t.Log("Signed TX bytes: %X\n", wire.BinaryBytes(types.TxS{tx}))
|
||||
// t.Log("Signed TX bytes: %X\n", wire.BinaryBytes(types.TxS{tx}))
|
||||
|
||||
// Write request
|
||||
txBytes := wire.BinaryBytes(types.TxS{tx})
|
||||
res := bcApp.DeliverTx(txBytes)
|
||||
t.Log(res)
|
||||
if res.IsErr() {
|
||||
t.Errorf("Failed: %v", res.Error())
|
||||
}
|
||||
// t.Log(res)
|
||||
assert.False(t, res.IsErr(), "Failed: %v", res.Error())
|
||||
}
|
||||
|
||||
func TestSequence(t *testing.T) {
|
||||
|
@ -62,13 +65,15 @@ func TestSequence(t *testing.T) {
|
|||
chainID := "test_chain_id"
|
||||
bcApp := app.NewBasecoin(eyesCli)
|
||||
bcApp.SetOption("base/chainID", chainID)
|
||||
t.Log(bcApp.Info())
|
||||
// t.Log(bcApp.Info())
|
||||
|
||||
// Get the test account
|
||||
test1PrivAcc := types.PrivAccountFromSecret("test1")
|
||||
test1Acc := test1PrivAcc.Account
|
||||
test1Acc.Balance = types.Coins{{"", 1 << 53}}
|
||||
t.Log(bcApp.SetOption("base/account", string(wire.JSONBytes(test1Acc))))
|
||||
accOpt, err := json.Marshal(test1Acc)
|
||||
require.Nil(t, err)
|
||||
bcApp.SetOption("base/account", string(accOpt))
|
||||
|
||||
sequence := int(1)
|
||||
// Make a bunch of PrivAccounts
|
||||
|
@ -103,16 +108,11 @@ func TestSequence(t *testing.T) {
|
|||
// Write request
|
||||
txBytes := wire.BinaryBytes(struct{ types.Tx }{tx})
|
||||
res := bcApp.DeliverTx(txBytes)
|
||||
if res.IsErr() {
|
||||
t.Errorf("DeliverTx error: " + res.Error())
|
||||
}
|
||||
|
||||
assert.False(t, res.IsErr(), "DeliverTx error: %v", res.Error())
|
||||
}
|
||||
|
||||
res := bcApp.Commit()
|
||||
if res.IsErr() {
|
||||
t.Errorf("Failed Commit: %v", res.Error())
|
||||
}
|
||||
assert.False(t, res.IsErr(), "Failed Commit: %v", res.Error())
|
||||
|
||||
t.Log("-------------------- RANDOM SENDS --------------------")
|
||||
|
||||
|
@ -152,8 +152,6 @@ func TestSequence(t *testing.T) {
|
|||
// Write request
|
||||
txBytes := wire.BinaryBytes(struct{ types.Tx }{tx})
|
||||
res := bcApp.DeliverTx(txBytes)
|
||||
if res.IsErr() {
|
||||
t.Errorf("DeliverTx error: " + res.Error())
|
||||
}
|
||||
assert.False(t, res.IsErr(), "DeliverTx error: %v", res.Error())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue