diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 15b98bd4c..8687d47dc 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -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) + } diff --git a/examples/basecoin/app/init_baseapp.go b/examples/basecoin/app/init_baseapp.go index f0528f98c..14402ef4a 100644 --- a/examples/basecoin/app/init_baseapp.go +++ b/examples/basecoin/app/init_baseapp.go @@ -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 })