fixes from review

This commit is contained in:
Ethan Buchman
2018-02-13 07:30:51 -05:00
parent 658d7633a3
commit d9ebe34c32
6 changed files with 61 additions and 104 deletions
+3 -3
View File
@@ -51,15 +51,15 @@ func NewBasecoinApp(genesisPath string) *BasecoinApp {
panic(fmt.Errorf("error loading genesis state: %v", err))
}
// TODO: InitChain with validators from genesis transaction bytes
// set up the cache store for ctx, get ctx
// TODO: can InitChain handle this too ?
app.BaseApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{}})
ctx := app.BaseApp.NewContext(false, nil) // context for DeliverTx
// TODO: combine with InitChain and let tendermint invoke it.
err = app.BaseApp.InitStater(ctx, genesisiDoc.AppState)
if err != nil {
panic(fmt.Errorf("error loading application genesis state: %v", err))
panic(fmt.Errorf("error initializing application genesis state: %v", err))
}
app.loadStores()
+7 -7
View File
@@ -52,17 +52,17 @@ func TestSendMsg(t *testing.T) {
coins, err := sdk.ParseCoins("77foocoin,99barcoin")
require.Nil(t, err)
baseAcc := auth.BaseAccount{
Address: addr,
Coins: coins,
PubKey: pk,
Sequence: 0,
Address: addr,
Coins: coins,
}
acc := &types.AppAccount{baseAcc, "foobart"}
gaccs := []*GenesisAccount{
NewGenesisAccount(acc),
genesisState := GenesisState{
Accounts: []*GenesisAccount{
NewGenesisAccount(acc),
},
}
bytes, err := json.MarshalIndent(&gaccs, "", "\t")
bytes, err := json.MarshalIndent(genesisState, "", "\t")
app := tba.BasecoinApp
ctx := app.BaseApp.NewContext(false, nil) // context for DeliverTx
+36 -43
View File
@@ -8,7 +8,6 @@ import (
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.
@@ -34,44 +33,6 @@ 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: ga.Name,
}, nil
}
// define the custom logic for basecoin initialization
func (app *BasecoinApp) initBaseAppInitStater() {
accountMapper := app.accountMapper
@@ -81,14 +42,13 @@ func (app *BasecoinApp) initBaseAppInitStater() {
return nil
}
var gaccs []*GenesisAccount
err := json.Unmarshal(state, &gaccs)
genesisState := new(GenesisState)
err := json.Unmarshal(state, genesisState)
if err != nil {
return sdk.ErrGenesisParse("").TraceCause(err, "")
}
for _, gacc := range gaccs {
for _, gacc := range genesisState.Accounts {
acc, err := gacc.toAppAccount()
if err != nil {
return sdk.ErrGenesisParse("").TraceCause(err, "")
@@ -98,3 +58,36 @@ func (app *BasecoinApp) initBaseAppInitStater() {
return nil
})
}
//-----------------------------------------------------
type GenesisState struct {
Accounts []*GenesisAccount `accounts`
}
// GenesisAccount doesn't need pubkey or sequence
type GenesisAccount struct {
Name string `json:"name"`
Address crypto.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
}
func NewGenesisAccount(aa *types.AppAccount) *GenesisAccount {
return &GenesisAccount{
Name: aa.Name,
Address: aa.Address,
Coins: aa.Coins,
}
}
// convert GenesisAccount to AppAccount
func (ga *GenesisAccount) toAppAccount() (acc *types.AppAccount, err error) {
baseAcc := auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins,
}
return &types.AppAccount{
BaseAccount: baseAcc,
Name: ga.Name,
}, nil
}