working genesis tests

This commit is contained in:
rigelrozanski 2018-02-09 16:24:21 +00:00
parent 7206c434d4
commit 2b9633db4a
2 changed files with 62 additions and 10 deletions

View File

@ -57,11 +57,16 @@ func TestSendMsg(t *testing.T) {
PubKey: pk,
Sequence: 0,
}
accs := []types.AppAccount{
{baseAcc, "foobart"},
{baseAcc, "endofzeworld"},
accs := []*GenesisAccount{
NewGenesisAccount(types.AppAccount{baseAcc, "foobart"}),
NewGenesisAccount(types.AppAccount{baseAcc, "endofzeworld"}),
}
bytes, err := json.MarshalIndent(&accs, "", "\t")
_ = bytes
// XXX test the json bytes in the InitStater
app := tba.BasecoinApp
ctxCheckTx := app.BaseApp.NewContext(true, nil)
ctxDeliverTx := app.BaseApp.NewContext(false, nil)
err = app.BaseApp.InitStater(ctxCheckTx, ctxDeliverTx, bytes)
require.Nil(t, err)
}

View File

@ -6,6 +6,9 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
)
// initCapKeys, initBaseApp, initStores, initHandlers.
@ -31,6 +34,44 @@ func (app *BasecoinApp) initBaseAppTxDecoder() {
})
}
// We use GenesisAccount instead of types.AppAccount for cleaner json input of PubKey
type GenesisAccount struct {
Name string `json:"name"`
Address crypto.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
PubKey cmn.HexBytes `json:"public_key"`
Sequence int64 `json:"sequence"`
}
func NewGenesisAccount(aa types.AppAccount) *GenesisAccount {
return &GenesisAccount{
Name: aa.Name,
Address: aa.Address,
Coins: aa.Coins,
PubKey: aa.PubKey.Bytes(),
Sequence: aa.Sequence,
}
}
// convert GenesisAccount to AppAccount
func (ga *GenesisAccount) toAppAccount() (acc types.AppAccount, err error) {
pk, err := crypto.PubKeyFromBytes(ga.PubKey)
if err != nil {
return
}
baseAcc := auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins,
PubKey: pk,
Sequence: ga.Sequence,
}
return types.AppAccount{
BaseAccount: baseAcc,
Name: "foobart",
}, nil
}
// define the custom logic for basecoin initialization
func (app *BasecoinApp) initBaseAppInitStater() {
accountMapper := app.accountMapper
@ -40,16 +81,22 @@ func (app *BasecoinApp) initBaseAppInitStater() {
return nil
}
var accs []*types.AppAccount
var gaccs []*GenesisAccount
err := json.Unmarshal(state, &accs)
err := json.Unmarshal(state, &gaccs)
if err != nil {
return sdk.ErrGenesisParse("").TraceCause(err, "")
}
for _, acc := range accs {
accountMapper.SetAccount(ctxCheckTx, acc)
accountMapper.SetAccount(ctxDeliverTx, acc)
for _, gacc := range gaccs {
acc, err := gacc.toAppAccount()
if err != nil {
return sdk.ErrGenesisParse("").TraceCause(err, "")
}
//panic(fmt.Sprintf("debug acc: %s\n", acc))
accountMapper.SetAccount(ctxCheckTx, &acc.BaseAccount)
accountMapper.SetAccount(ctxDeliverTx, &acc.BaseAccount)
}
return nil
})