From 6d3b5cb40248cd271a8193378eadd68a19ba7461 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sat, 20 Jan 2018 14:19:44 -0500 Subject: [PATCH] basecoin: fix codecs, add some tests --- .../basecoin/app/{basecoin_app.go => app.go} | 1 + examples/basecoin/app/msgs.go | 4 +++ examples/basecoin/app/stores.go | 2 +- examples/basecoin/types/account.go | 25 ++++++++++++++----- types/account.go | 2 +- types/tx_msg.go | 2 +- x/auth/account.go | 2 ++ 7 files changed, 29 insertions(+), 9 deletions(-) rename examples/basecoin/app/{basecoin_app.go => app.go} (98%) diff --git a/examples/basecoin/app/basecoin_app.go b/examples/basecoin/app/app.go similarity index 98% rename from examples/basecoin/app/basecoin_app.go rename to examples/basecoin/app/app.go index a56e18466..dec0652fb 100644 --- a/examples/basecoin/app/basecoin_app.go +++ b/examples/basecoin/app/app.go @@ -78,6 +78,7 @@ func (app *BasecoinApp) initSDKApp() { func (app *BasecoinApp) initCodec() { app.cdc = wire.NewCodec() + app.registerMsgs() } // depends on initSDKApp() diff --git a/examples/basecoin/app/msgs.go b/examples/basecoin/app/msgs.go index 43fd04539..48526b7bb 100644 --- a/examples/basecoin/app/msgs.go +++ b/examples/basecoin/app/msgs.go @@ -3,6 +3,7 @@ package app import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank" + crypto "github.com/tendermint/go-crypto" ) // Set via `app.App.SetTxDecoder(app.decodeTx)` @@ -16,6 +17,9 @@ func (app *BasecoinApp) decodeTx(txBytes []byte) (sdk.Tx, error) { func (app *BasecoinApp) registerMsgs() { cdc := app.cdc + // Register the crypto + crypto.RegisterWire(cdc) + // Register the Msg interface. cdc.RegisterInterface((*sdk.Msg)(nil), nil) cdc.RegisterConcrete(bank.SendMsg{}, "cosmos-sdk/SendMsg", nil) // XXX refactor out diff --git a/examples/basecoin/app/stores.go b/examples/basecoin/app/stores.go index acd35d69f..d2c76c894 100644 --- a/examples/basecoin/app/stores.go +++ b/examples/basecoin/app/stores.go @@ -40,6 +40,6 @@ func (app *BasecoinApp) initMultiStore() { func (app *BasecoinApp) initAppStore() { app.accStore = auth.NewAccountStore( app.mainStoreKey, - types.AppAccountCodec{}, + types.NewAppAccountCodecFromWireCodec(app.cdc), ) } diff --git a/examples/basecoin/types/account.go b/examples/basecoin/types/account.go index f56c4000a..f21d93ed5 100644 --- a/examples/basecoin/types/account.go +++ b/examples/basecoin/types/account.go @@ -1,9 +1,14 @@ package types import ( + wire "github.com/tendermint/go-wire" + + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" ) +var _ sdk.Account = (*AppAccount)(nil) + type AppAccount struct { auth.BaseAccount @@ -21,16 +26,24 @@ func (acc *AppAccount) SetName(name string) { //---------------------------------------- -type AppAccountCodec struct{} +type AppAccountCodec struct { + cdc *wire.Codec +} + +func NewAppAccountCodecFromWireCodec(cdc *wire.Codec) AppAccountCodec { + return AppAccountCodec{cdc} +} func (_ AppAccountCodec) Prototype() interface{} { - return AppAccount{} + return &AppAccount{} } -func (_ AppAccountCodec) Encode(o interface{}) (bz []byte, err error) { - panic("not yet implemented") +func (aac AppAccountCodec) Encode(o interface{}) (bz []byte, err error) { + return aac.cdc.MarshalBinary(o) } -func (_ AppAccountCodec) Decode(bz []byte) (o interface{}, err error) { - panic("not yet implemented") +func (aac AppAccountCodec) Decode(bz []byte) (o interface{}, err error) { + o = aac.Prototype() + err = aac.cdc.UnmarshalBinary(bz, o) + return o, err } diff --git a/types/account.go b/types/account.go index 3fc326f6a..c79454924 100644 --- a/types/account.go +++ b/types/account.go @@ -17,7 +17,7 @@ type Account interface { SetSequence(int64) error GetCoins() Coins - SetCoins(Coins) + SetCoins(Coins) error Get(key interface{}) (value interface{}, err error) Set(key interface{}, value interface{}) error diff --git a/types/tx_msg.go b/types/tx_msg.go index b01bff56e..57e0a97f4 100644 --- a/types/tx_msg.go +++ b/types/tx_msg.go @@ -57,7 +57,7 @@ type StdTx struct { func (tx StdTx) GetFeePayer() crypto.Address { return tx.Signatures[0].PubKey.Address() } func (tx StdTx) GetTxBytes() []byte { - bz, err := wire.MarshalBinary(tx) + bz, err := wire.MarshalBinary(tx) // XXX: this is bad if err != nil { panic(err) } diff --git a/x/auth/account.go b/x/auth/account.go index 3a3f45509..5ab91b3c1 100644 --- a/x/auth/account.go +++ b/x/auth/account.go @@ -11,6 +11,8 @@ import ( //----------------------------------------------------------- // BaseAccount +var _ sdk.Account = (*BaseAccount)(nil) + // BaseAccount - coin account structure type BaseAccount struct { Address crypto.Address `json:"address"`