cool module test fixes

This commit is contained in:
rigelrozanski 2018-03-13 01:23:33 +01:00
parent d4c13b093c
commit b9691f1086
8 changed files with 110 additions and 119 deletions

View File

@ -1,12 +0,0 @@
*.swp
*.swo
vendor
build
app/data
### Vagrant ###
.vagrant/
*.box
*.log
vagrant

View File

@ -81,14 +81,14 @@ func MakeCodec() *wire.Codec {
const msgTypeSend = 0x1 const msgTypeSend = 0x1
const msgTypeIssue = 0x2 const msgTypeIssue = 0x2
const msgTypeWhatCool = 0x3 const msgTypeCool = 0x3
const msgTypeSetWhatCool = 0x4 const msgTypeSetCool = 0x4
var _ = oldwire.RegisterInterface( var _ = oldwire.RegisterInterface(
struct{ sdk.Msg }{}, struct{ sdk.Msg }{},
oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend}, oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend},
oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue}, oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue},
oldwire.ConcreteType{cool.WhatCoolMsg{}, msgTypeWhatCool}, oldwire.ConcreteType{cool.CoolMsg{}, msgTypeCool},
oldwire.ConcreteType{cool.SetWhatCoolMsg{}, msgTypeSetWhatCool}, oldwire.ConcreteType{cool.SetCoolMsg{}, msgTypeSetCool},
) )
const accTypeApp = 0x1 const accTypeApp = 0x1

View File

@ -33,19 +33,19 @@ var (
Outputs: []bank.Output{bank.NewOutput(addr2, coins)}, Outputs: []bank.Output{bank.NewOutput(addr2, coins)},
} }
whatCoolMsg1 = cool.WhatCoolMsg{ coolMsg1 = cool.CoolMsg{
Sender: addr1, Sender: addr1,
CoolerThanCool: "icecold", CoolerThanCool: "icecold",
} }
whatCoolMsg2 = cool.WhatCoolMsg{ coolMsg2 = cool.CoolMsg{
Sender: addr1, Sender: addr1,
CoolerThanCool: "icecold", CoolerThanCool: "icecold",
} }
setWhatCoolMsg = cool.SetWhatCoolMsg{ setCoolMsg = cool.SetCoolMsg{
Sender: addr1, Sender: addr1,
WhatCool: "goodbye", Cool: "goodbye",
} }
) )
@ -64,8 +64,8 @@ func TestMsgs(t *testing.T) {
msg sdk.Msg msg sdk.Msg
}{ }{
{sendMsg}, {sendMsg},
{whatCoolMsg1}, {coolMsg1},
{setWhatCoolMsg}, {setCoolMsg},
} }
chainID := "" chainID := ""
@ -209,56 +209,54 @@ func TestSendMsgWithAccounts(t *testing.T) {
assert.Equal(t, sdk.CodeOK, res.Code, res.Log) assert.Equal(t, sdk.CodeOK, res.Code, res.Log)
} }
//func TestWhatCoolMsg(t *testing.T) { func TestCoolMsg(t *testing.T) {
//bapp := newBasecoinApp() bapp := newBasecoinApp()
//// Construct genesis state // Construct genesis state
//// Construct some genesis bytes to reflect basecoin/types/AppAccount // Construct some genesis bytes to reflect basecoin/types/AppAccount
//// Give 77 foocoin to the first key coins := sdk.Coins{}
//coins, err := sdk.ParseCoins("1icecold") baseAcc := auth.BaseAccount{
//require.Nil(t, err) Address: addr1,
//baseAcc := auth.BaseAccount{ Coins: coins,
//Address: addr1, }
//Coins: coins, acc1 := &types.AppAccount{baseAcc, "foobart"}
//}
//acc1 := &types.AppAccount{baseAcc, "foobart"}
//// Construct genesis state // Construct genesis state
//genesisState := types.GenesisState{ genesisState := types.GenesisState{
//Accounts: []*types.GenesisAccount{ Accounts: []*types.GenesisAccount{
//types.NewGenesisAccount(acc1), types.NewGenesisAccount(acc1),
//}, },
//} }
//stateBytes, err := json.MarshalIndent(genesisState, "", "\t") stateBytes, err := json.MarshalIndent(genesisState, "", "\t")
//require.Nil(t, err) require.Nil(t, err)
//// Initialize the chain (nil) // Initialize the chain (nil)
//vals := []abci.Validator{} vals := []abci.Validator{}
//bapp.InitChain(abci.RequestInitChain{vals, stateBytes}) bapp.InitChain(abci.RequestInitChain{vals, stateBytes})
//bapp.Commit() bapp.Commit()
//// A checkTx context (true) // A checkTx context (true)
//ctxCheck := bapp.BaseApp.NewContext(true, abci.Header{}) ctxCheck := bapp.BaseApp.NewContext(true, abci.Header{})
//res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1) res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1)
//assert.Equal(t, acc1, res1) assert.Equal(t, acc1, res1)
//// Sign the tx // Sign the tx
//tx := sdk.NewStdTx(whatCoolMsg1, []sdk.StdSignature{{ tx := sdk.NewStdTx(coolMsg1, []sdk.StdSignature{{
//PubKey: priv1.PubKey(), PubKey: priv1.PubKey(),
//Signature: priv1.Sign(whatCoolMsg1.GetSignBytes()), Signature: priv1.Sign(coolMsg1.GetSignBytes()),
//}}) }})
//// Run a Check // Run a Check
//res := bapp.Check(tx) res := bapp.Check(tx)
//assert.Equal(t, sdk.CodeOK, res.Code, res.Log) assert.Equal(t, sdk.CodeOK, res.Code, res.Log)
//// Simulate a Block // Simulate a Block
//bapp.BeginBlock(abci.RequestBeginBlock{}) bapp.BeginBlock(abci.RequestBeginBlock{})
//res = bapp.Deliver(tx) res = bapp.Deliver(tx)
//assert.Equal(t, sdk.CodeOK, res.Code, res.Log) assert.Equal(t, sdk.CodeOK, res.Code, res.Log)
//// Check balances // Check balances
//ctxDeliver := bapp.BaseApp.NewContext(false, abci.Header{}) ctxDeliver := bapp.BaseApp.NewContext(false, abci.Header{})
//res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr1) res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr1)
//assert.Equal(t, "70icecold", fmt.Sprintf("%v", res2.GetCoins())) assert.Equal(t, "70icecold", fmt.Sprintf("%v", res2.GetCoins()))
//} }

View File

@ -58,11 +58,11 @@ func main() {
)...) )...)
basecliCmd.AddCommand( basecliCmd.AddCommand(
client.PostCommands( client.PostCommands(
coolcmd.WhatCoolTxCmd(cdc), coolcmd.CoolTxCmd(cdc),
)...) )...)
basecliCmd.AddCommand( basecliCmd.AddCommand(
client.PostCommands( client.PostCommands(
coolcmd.SetWhatCoolTxCmd(cdc), coolcmd.SetCoolTxCmd(cdc),
)...) )...)
// add proxy, version and key info // add proxy, version and key info

View File

@ -13,9 +13,9 @@ import (
) )
// what cool transaction // what cool transaction
func WhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { func CoolTxCmd(cdc *wire.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "whatcool [answer]", Use: "cool [answer]",
Short: "What's cooler than being cool?", Short: "What's cooler than being cool?",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 || len(args[0]) == 0 { if len(args) != 1 || len(args[0]) == 0 {
@ -29,7 +29,7 @@ func WhatCoolTxCmd(cdc *wire.Codec) *cobra.Command {
} }
// create the message // create the message
msg := cool.NewWhatCoolMsg(from, args[0]) msg := cool.NewCoolMsg(from, args[0])
// build and sign the transaction, then broadcast to Tendermint // build and sign the transaction, then broadcast to Tendermint
res, err := builder.SignBuildBroadcast(msg, cdc) res, err := builder.SignBuildBroadcast(msg, cdc)
@ -44,9 +44,9 @@ func WhatCoolTxCmd(cdc *wire.Codec) *cobra.Command {
} }
// set what cool transaction // set what cool transaction
func SetWhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { func SetCoolTxCmd(cdc *wire.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "setwhatcool [answer]", Use: "setcool [answer]",
Short: "You're so cool, tell us what is cool!", Short: "You're so cool, tell us what is cool!",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 || len(args[0]) == 0 { if len(args) != 1 || len(args[0]) == 0 {
@ -60,7 +60,7 @@ func SetWhatCoolTxCmd(cdc *wire.Codec) *cobra.Command {
} }
// create the message // create the message
msg := cool.NewSetWhatCoolMsg(from, args[0]) msg := cool.NewSetCoolMsg(from, args[0])
// build and sign the transaction, then broadcast to Tendermint // build and sign the transaction, then broadcast to Tendermint
res, err := builder.SignBuildBroadcast(msg, cdc) res, err := builder.SignBuildBroadcast(msg, cdc)

View File

@ -22,10 +22,10 @@ import (
func NewHandler(ck bank.CoinKeeper, cm Mapper) sdk.Handler { func NewHandler(ck bank.CoinKeeper, cm Mapper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) { switch msg := msg.(type) {
case SetWhatCoolMsg: case SetCoolMsg:
return handleSetWhatCoolMsg(ctx, cm, msg) return handleSetCoolMsg(ctx, cm, msg)
case WhatCoolMsg: case CoolMsg:
return handleWhatCoolMsg(ctx, ck, cm, msg) return handleCoolMsg(ctx, ck, cm, msg)
default: default:
errMsg := fmt.Sprintf("Unrecognized cool Msg type: %v", reflect.TypeOf(msg).Name()) errMsg := fmt.Sprintf("Unrecognized cool Msg type: %v", reflect.TypeOf(msg).Name())
return sdk.ErrUnknownRequest(errMsg).Result() return sdk.ErrUnknownRequest(errMsg).Result()
@ -33,16 +33,21 @@ func NewHandler(ck bank.CoinKeeper, cm Mapper) sdk.Handler {
} }
} }
// Handle WhatCoolMsg This is the engine of your module // Handle CoolMsg This is the engine of your module
func handleSetWhatCoolMsg(ctx sdk.Context, cm Mapper, msg SetWhatCoolMsg) sdk.Result { func handleSetCoolMsg(ctx sdk.Context, cm Mapper, msg SetCoolMsg) sdk.Result {
cm.SetWhatCool(ctx, msg.WhatCool) cm.SetCool(ctx, msg.Cool)
return sdk.Result{} return sdk.Result{}
} }
// Handle WhatCoolMsg This is the engine of your module // Handle CoolMsg This is the engine of your module
func handleWhatCoolMsg(ctx sdk.Context, ck bank.CoinKeeper, cm Mapper, msg WhatCoolMsg) sdk.Result { func handleCoolMsg(ctx sdk.Context, ck bank.CoinKeeper, cm Mapper, msg CoolMsg) sdk.Result {
whatsCool := cm.GetWhatCool(ctx) whatsCool := cm.GetCool(ctx)
// set default if nothing is set
//if whatsCool == "" {
//cm.SetCool(ctx, "icecold")
//}
if msg.CoolerThanCool == whatsCool { if msg.CoolerThanCool == whatsCool {

View File

@ -14,17 +14,17 @@ func NewMapper(key sdk.StoreKey) Mapper {
} }
// Key to knowing whats cool // Key to knowing whats cool
var whatCoolKey = []byte("WhatsCoolKey") var coolKey = []byte("WhatsCoolKey")
// Implements sdk.AccountMapper. // Implements sdk.AccountMapper.
func (am Mapper) GetWhatCool(ctx sdk.Context) string { func (am Mapper) GetCool(ctx sdk.Context) string {
store := ctx.KVStore(am.key) store := ctx.KVStore(am.key)
bz := store.Get(whatCoolKey) bz := store.Get(coolKey)
return string(bz) return string(bz)
} }
// Implements sdk.AccountMapper. // Implements sdk.AccountMapper.
func (am Mapper) SetWhatCool(ctx sdk.Context, whatscool string) { func (am Mapper) SetCool(ctx sdk.Context, whatscool string) {
store := ctx.KVStore(am.key) store := ctx.KVStore(am.key)
store.Set(whatCoolKey, []byte(whatscool)) store.Set(coolKey, []byte(whatscool))
} }

View File

@ -10,46 +10,46 @@ import (
// A really cool msg type, these fields are can be entirely arbitrary and // A really cool msg type, these fields are can be entirely arbitrary and
// custom to your message // custom to your message
type SetWhatCoolMsg struct { type SetCoolMsg struct {
Sender sdk.Address Sender sdk.Address
WhatCool string Cool string
} }
// New cool message // New cool message
func NewSetWhatCoolMsg(sender sdk.Address, whatcool string) SetWhatCoolMsg { func NewSetCoolMsg(sender sdk.Address, cool string) SetCoolMsg {
return SetWhatCoolMsg{ return SetCoolMsg{
Sender: sender, Sender: sender,
WhatCool: whatcool, Cool: cool,
} }
} }
// enforce the msg type at compile time // enforce the msg type at compile time
var _ sdk.Msg = SetWhatCoolMsg{} var _ sdk.Msg = SetCoolMsg{}
// nolint // nolint
func (msg SetWhatCoolMsg) Type() string { return "cool" } func (msg SetCoolMsg) Type() string { return "cool" }
func (msg SetWhatCoolMsg) Get(key interface{}) (value interface{}) { return nil } func (msg SetCoolMsg) Get(key interface{}) (value interface{}) { return nil }
func (msg SetWhatCoolMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } func (msg SetCoolMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} }
func (msg SetWhatCoolMsg) String() string { func (msg SetCoolMsg) String() string {
return fmt.Sprintf("SetWhatCoolMsg{Sender: %v, WhatCool: %v}", msg.Sender, msg.WhatCool) return fmt.Sprintf("SetCoolMsg{Sender: %v, Cool: %v}", msg.Sender, msg.Cool)
} }
// Validate Basic is used to quickly disqualify obviously invalid messages quickly // Validate Basic is used to quickly disqualify obviously invalid messages quickly
func (msg SetWhatCoolMsg) ValidateBasic() sdk.Error { func (msg SetCoolMsg) ValidateBasic() sdk.Error {
if len(msg.Sender) == 0 { if len(msg.Sender) == 0 {
return sdk.ErrUnrecognizedAddress(msg.Sender).Trace("") return sdk.ErrUnrecognizedAddress(msg.Sender).Trace("")
} }
if strings.Contains(msg.WhatCool, "hot") { if strings.Contains(msg.Cool, "hot") {
return sdk.ErrUnauthorized("").Trace("hot is not cool") return sdk.ErrUnauthorized("").Trace("hot is not cool")
} }
if strings.Contains(msg.WhatCool, "warm") { if strings.Contains(msg.Cool, "warm") {
return sdk.ErrUnauthorized("").Trace("warm is not very cool") return sdk.ErrUnauthorized("").Trace("warm is not very cool")
} }
return nil return nil
} }
// Get the bytes for the message signer to sign on // Get the bytes for the message signer to sign on
func (msg SetWhatCoolMsg) GetSignBytes() []byte { func (msg SetCoolMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg) b, err := json.Marshal(msg)
if err != nil { if err != nil {
panic(err) panic(err)
@ -61,32 +61,32 @@ func (msg SetWhatCoolMsg) GetSignBytes() []byte {
// A really cool msg type, these fields are can be entirely arbitrary and // A really cool msg type, these fields are can be entirely arbitrary and
// custom to your message // custom to your message
type WhatCoolMsg struct { type TestYourCoolnessMsg struct {
Sender sdk.Address Sender sdk.Address
CoolerThanCool string CoolerThanCool string
} }
// New cool message // New cool message
func NewWhatCoolMsg(sender sdk.Address, coolerthancool string) WhatCoolMsg { func NewTestYourCoolnessMsg(sender sdk.Address, coolerthancool string) TestYourCoolnessMsg {
return WhatCoolMsg{ return TestYourCoolnessMsg{
Sender: sender, Sender: sender,
CoolerThanCool: coolerthancool, CoolerThanCool: coolerthancool,
} }
} }
// enforce the msg type at compile time // enforce the msg type at compile time
var _ sdk.Msg = WhatCoolMsg{} var _ sdk.Msg = TestYourCoolnessMsg{}
// nolint // nolint
func (msg WhatCoolMsg) Type() string { return "cool" } func (msg TestYourCoolnessMsg) Type() string { return "cool" }
func (msg WhatCoolMsg) Get(key interface{}) (value interface{}) { return nil } func (msg TestYourCoolnessMsg) Get(key interface{}) (value interface{}) { return nil }
func (msg WhatCoolMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } func (msg TestYourCoolnessMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} }
func (msg WhatCoolMsg) String() string { func (msg TestYourCoolnessMsg) String() string {
return fmt.Sprintf("WhatCoolMsg{Sender: %v, CoolerThanCool: %v}", msg.Sender, msg.CoolerThanCool) return fmt.Sprintf("TestYourCoolnessMsg{Sender: %v, CoolerThanCool: %v}", msg.Sender, msg.CoolerThanCool)
} }
// Validate Basic is used to quickly disqualify obviously invalid messages quickly // Validate Basic is used to quickly disqualify obviously invalid messages quickly
func (msg WhatCoolMsg) ValidateBasic() sdk.Error { func (msg TestYourCoolnessMsg) ValidateBasic() sdk.Error {
if len(msg.Sender) == 0 { if len(msg.Sender) == 0 {
return sdk.ErrUnrecognizedAddress(msg.Sender).Trace("") return sdk.ErrUnrecognizedAddress(msg.Sender).Trace("")
} }
@ -94,7 +94,7 @@ func (msg WhatCoolMsg) ValidateBasic() sdk.Error {
} }
// Get the bytes for the message signer to sign on // Get the bytes for the message signer to sign on
func (msg WhatCoolMsg) GetSignBytes() []byte { func (msg TestYourCoolnessMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg) b, err := json.Marshal(msg)
if err != nil { if err != nil {
panic(err) panic(err)