From b9691f1086d801aad1cbbd212b76026ac567322f Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 13 Mar 2018 01:23:33 +0100 Subject: [PATCH 1/5] cool module test fixes --- examples/basecoin/.gitignore | 12 --- examples/basecoin/app/app.go | 8 +- examples/basecoin/app/app_test.go | 102 ++++++++++---------- examples/basecoin/cmd/basecli/main.go | 4 +- examples/basecoin/x/cool/commands/cooltx.go | 12 +-- examples/basecoin/x/cool/handler.go | 25 +++-- examples/basecoin/x/cool/mapper.go | 10 +- examples/basecoin/x/cool/types.go | 56 +++++------ 8 files changed, 110 insertions(+), 119 deletions(-) delete mode 100644 examples/basecoin/.gitignore diff --git a/examples/basecoin/.gitignore b/examples/basecoin/.gitignore deleted file mode 100644 index 25e54fd6bb..0000000000 --- a/examples/basecoin/.gitignore +++ /dev/null @@ -1,12 +0,0 @@ -*.swp -*.swo -vendor -build -app/data - - -### Vagrant ### -.vagrant/ -*.box -*.log -vagrant diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index d315f977ef..bb6308d8c0 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -81,14 +81,14 @@ func MakeCodec() *wire.Codec { const msgTypeSend = 0x1 const msgTypeIssue = 0x2 - const msgTypeWhatCool = 0x3 - const msgTypeSetWhatCool = 0x4 + const msgTypeCool = 0x3 + const msgTypeSetCool = 0x4 var _ = oldwire.RegisterInterface( struct{ sdk.Msg }{}, oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend}, oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue}, - oldwire.ConcreteType{cool.WhatCoolMsg{}, msgTypeWhatCool}, - oldwire.ConcreteType{cool.SetWhatCoolMsg{}, msgTypeSetWhatCool}, + oldwire.ConcreteType{cool.CoolMsg{}, msgTypeCool}, + oldwire.ConcreteType{cool.SetCoolMsg{}, msgTypeSetCool}, ) const accTypeApp = 0x1 diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 148f0411fc..dbc4e00543 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -33,19 +33,19 @@ var ( Outputs: []bank.Output{bank.NewOutput(addr2, coins)}, } - whatCoolMsg1 = cool.WhatCoolMsg{ + coolMsg1 = cool.CoolMsg{ Sender: addr1, CoolerThanCool: "icecold", } - whatCoolMsg2 = cool.WhatCoolMsg{ + coolMsg2 = cool.CoolMsg{ Sender: addr1, CoolerThanCool: "icecold", } - setWhatCoolMsg = cool.SetWhatCoolMsg{ - Sender: addr1, - WhatCool: "goodbye", + setCoolMsg = cool.SetCoolMsg{ + Sender: addr1, + Cool: "goodbye", } ) @@ -64,8 +64,8 @@ func TestMsgs(t *testing.T) { msg sdk.Msg }{ {sendMsg}, - {whatCoolMsg1}, - {setWhatCoolMsg}, + {coolMsg1}, + {setCoolMsg}, } chainID := "" @@ -209,56 +209,54 @@ func TestSendMsgWithAccounts(t *testing.T) { assert.Equal(t, sdk.CodeOK, res.Code, res.Log) } -//func TestWhatCoolMsg(t *testing.T) { -//bapp := newBasecoinApp() +func TestCoolMsg(t *testing.T) { + bapp := newBasecoinApp() -//// Construct genesis state -//// Construct some genesis bytes to reflect basecoin/types/AppAccount -//// Give 77 foocoin to the first key -//coins, err := sdk.ParseCoins("1icecold") -//require.Nil(t, err) -//baseAcc := auth.BaseAccount{ -//Address: addr1, -//Coins: coins, -//} -//acc1 := &types.AppAccount{baseAcc, "foobart"} + // Construct genesis state + // Construct some genesis bytes to reflect basecoin/types/AppAccount + coins := sdk.Coins{} + baseAcc := auth.BaseAccount{ + Address: addr1, + Coins: coins, + } + acc1 := &types.AppAccount{baseAcc, "foobart"} -//// Construct genesis state -//genesisState := types.GenesisState{ -//Accounts: []*types.GenesisAccount{ -//types.NewGenesisAccount(acc1), -//}, -//} -//stateBytes, err := json.MarshalIndent(genesisState, "", "\t") -//require.Nil(t, err) + // Construct genesis state + genesisState := types.GenesisState{ + Accounts: []*types.GenesisAccount{ + types.NewGenesisAccount(acc1), + }, + } + stateBytes, err := json.MarshalIndent(genesisState, "", "\t") + require.Nil(t, err) -//// Initialize the chain (nil) -//vals := []abci.Validator{} -//bapp.InitChain(abci.RequestInitChain{vals, stateBytes}) -//bapp.Commit() + // Initialize the chain (nil) + vals := []abci.Validator{} + bapp.InitChain(abci.RequestInitChain{vals, stateBytes}) + bapp.Commit() -//// A checkTx context (true) -//ctxCheck := bapp.BaseApp.NewContext(true, abci.Header{}) -//res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1) -//assert.Equal(t, acc1, res1) + // A checkTx context (true) + ctxCheck := bapp.BaseApp.NewContext(true, abci.Header{}) + res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1) + assert.Equal(t, acc1, res1) -//// Sign the tx -//tx := sdk.NewStdTx(whatCoolMsg1, []sdk.StdSignature{{ -//PubKey: priv1.PubKey(), -//Signature: priv1.Sign(whatCoolMsg1.GetSignBytes()), -//}}) + // Sign the tx + tx := sdk.NewStdTx(coolMsg1, []sdk.StdSignature{{ + PubKey: priv1.PubKey(), + Signature: priv1.Sign(coolMsg1.GetSignBytes()), + }}) -//// Run a Check -//res := bapp.Check(tx) -//assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + // Run a Check + res := bapp.Check(tx) + assert.Equal(t, sdk.CodeOK, res.Code, res.Log) -//// Simulate a Block -//bapp.BeginBlock(abci.RequestBeginBlock{}) -//res = bapp.Deliver(tx) -//assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + // Simulate a Block + bapp.BeginBlock(abci.RequestBeginBlock{}) + res = bapp.Deliver(tx) + assert.Equal(t, sdk.CodeOK, res.Code, res.Log) -//// Check balances -//ctxDeliver := bapp.BaseApp.NewContext(false, abci.Header{}) -//res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr1) -//assert.Equal(t, "70icecold", fmt.Sprintf("%v", res2.GetCoins())) -//} + // Check balances + ctxDeliver := bapp.BaseApp.NewContext(false, abci.Header{}) + res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr1) + assert.Equal(t, "70icecold", fmt.Sprintf("%v", res2.GetCoins())) +} diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 4a2cdd6f4b..5985c9677f 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -58,11 +58,11 @@ func main() { )...) basecliCmd.AddCommand( client.PostCommands( - coolcmd.WhatCoolTxCmd(cdc), + coolcmd.CoolTxCmd(cdc), )...) basecliCmd.AddCommand( client.PostCommands( - coolcmd.SetWhatCoolTxCmd(cdc), + coolcmd.SetCoolTxCmd(cdc), )...) // add proxy, version and key info diff --git a/examples/basecoin/x/cool/commands/cooltx.go b/examples/basecoin/x/cool/commands/cooltx.go index c2a1acee97..fe5f6a625f 100644 --- a/examples/basecoin/x/cool/commands/cooltx.go +++ b/examples/basecoin/x/cool/commands/cooltx.go @@ -13,9 +13,9 @@ import ( ) // what cool transaction -func WhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { +func CoolTxCmd(cdc *wire.Codec) *cobra.Command { return &cobra.Command{ - Use: "whatcool [answer]", + Use: "cool [answer]", Short: "What's cooler than being cool?", RunE: func(cmd *cobra.Command, args []string) error { if len(args) != 1 || len(args[0]) == 0 { @@ -29,7 +29,7 @@ func WhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { } // create the message - msg := cool.NewWhatCoolMsg(from, args[0]) + msg := cool.NewCoolMsg(from, args[0]) // build and sign the transaction, then broadcast to Tendermint res, err := builder.SignBuildBroadcast(msg, cdc) @@ -44,9 +44,9 @@ func WhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { } // set what cool transaction -func SetWhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { +func SetCoolTxCmd(cdc *wire.Codec) *cobra.Command { return &cobra.Command{ - Use: "setwhatcool [answer]", + Use: "setcool [answer]", Short: "You're so cool, tell us what is cool!", RunE: func(cmd *cobra.Command, args []string) error { if len(args) != 1 || len(args[0]) == 0 { @@ -60,7 +60,7 @@ func SetWhatCoolTxCmd(cdc *wire.Codec) *cobra.Command { } // create the message - msg := cool.NewSetWhatCoolMsg(from, args[0]) + msg := cool.NewSetCoolMsg(from, args[0]) // build and sign the transaction, then broadcast to Tendermint res, err := builder.SignBuildBroadcast(msg, cdc) diff --git a/examples/basecoin/x/cool/handler.go b/examples/basecoin/x/cool/handler.go index 152ae2f339..72a2979bc0 100644 --- a/examples/basecoin/x/cool/handler.go +++ b/examples/basecoin/x/cool/handler.go @@ -22,10 +22,10 @@ import ( func NewHandler(ck bank.CoinKeeper, cm Mapper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { switch msg := msg.(type) { - case SetWhatCoolMsg: - return handleSetWhatCoolMsg(ctx, cm, msg) - case WhatCoolMsg: - return handleWhatCoolMsg(ctx, ck, cm, msg) + case SetCoolMsg: + return handleSetCoolMsg(ctx, cm, msg) + case CoolMsg: + return handleCoolMsg(ctx, ck, cm, msg) default: errMsg := fmt.Sprintf("Unrecognized cool Msg type: %v", reflect.TypeOf(msg).Name()) 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 -func handleSetWhatCoolMsg(ctx sdk.Context, cm Mapper, msg SetWhatCoolMsg) sdk.Result { - cm.SetWhatCool(ctx, msg.WhatCool) +// Handle CoolMsg This is the engine of your module +func handleSetCoolMsg(ctx sdk.Context, cm Mapper, msg SetCoolMsg) sdk.Result { + cm.SetCool(ctx, msg.Cool) return sdk.Result{} } -// Handle WhatCoolMsg This is the engine of your module -func handleWhatCoolMsg(ctx sdk.Context, ck bank.CoinKeeper, cm Mapper, msg WhatCoolMsg) sdk.Result { +// Handle CoolMsg This is the engine of your module +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 { diff --git a/examples/basecoin/x/cool/mapper.go b/examples/basecoin/x/cool/mapper.go index f437f8ca43..9c4d2667bb 100644 --- a/examples/basecoin/x/cool/mapper.go +++ b/examples/basecoin/x/cool/mapper.go @@ -14,17 +14,17 @@ func NewMapper(key sdk.StoreKey) Mapper { } // Key to knowing whats cool -var whatCoolKey = []byte("WhatsCoolKey") +var coolKey = []byte("WhatsCoolKey") // Implements sdk.AccountMapper. -func (am Mapper) GetWhatCool(ctx sdk.Context) string { +func (am Mapper) GetCool(ctx sdk.Context) string { store := ctx.KVStore(am.key) - bz := store.Get(whatCoolKey) + bz := store.Get(coolKey) return string(bz) } // 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.Set(whatCoolKey, []byte(whatscool)) + store.Set(coolKey, []byte(whatscool)) } diff --git a/examples/basecoin/x/cool/types.go b/examples/basecoin/x/cool/types.go index 872afe7f8a..7f80f968ad 100644 --- a/examples/basecoin/x/cool/types.go +++ b/examples/basecoin/x/cool/types.go @@ -10,46 +10,46 @@ import ( // A really cool msg type, these fields are can be entirely arbitrary and // custom to your message -type SetWhatCoolMsg struct { - Sender sdk.Address - WhatCool string +type SetCoolMsg struct { + Sender sdk.Address + Cool string } // New cool message -func NewSetWhatCoolMsg(sender sdk.Address, whatcool string) SetWhatCoolMsg { - return SetWhatCoolMsg{ - Sender: sender, - WhatCool: whatcool, +func NewSetCoolMsg(sender sdk.Address, cool string) SetCoolMsg { + return SetCoolMsg{ + Sender: sender, + Cool: cool, } } // enforce the msg type at compile time -var _ sdk.Msg = SetWhatCoolMsg{} +var _ sdk.Msg = SetCoolMsg{} // nolint -func (msg SetWhatCoolMsg) Type() string { return "cool" } -func (msg SetWhatCoolMsg) Get(key interface{}) (value interface{}) { return nil } -func (msg SetWhatCoolMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } -func (msg SetWhatCoolMsg) String() string { - return fmt.Sprintf("SetWhatCoolMsg{Sender: %v, WhatCool: %v}", msg.Sender, msg.WhatCool) +func (msg SetCoolMsg) Type() string { return "cool" } +func (msg SetCoolMsg) Get(key interface{}) (value interface{}) { return nil } +func (msg SetCoolMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } +func (msg SetCoolMsg) String() string { + return fmt.Sprintf("SetCoolMsg{Sender: %v, Cool: %v}", msg.Sender, msg.Cool) } // 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 { 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") } - if strings.Contains(msg.WhatCool, "warm") { + if strings.Contains(msg.Cool, "warm") { return sdk.ErrUnauthorized("").Trace("warm is not very cool") } return nil } // 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) if err != nil { panic(err) @@ -61,32 +61,32 @@ func (msg SetWhatCoolMsg) GetSignBytes() []byte { // A really cool msg type, these fields are can be entirely arbitrary and // custom to your message -type WhatCoolMsg struct { +type TestYourCoolnessMsg struct { Sender sdk.Address CoolerThanCool string } // New cool message -func NewWhatCoolMsg(sender sdk.Address, coolerthancool string) WhatCoolMsg { - return WhatCoolMsg{ +func NewTestYourCoolnessMsg(sender sdk.Address, coolerthancool string) TestYourCoolnessMsg { + return TestYourCoolnessMsg{ Sender: sender, CoolerThanCool: coolerthancool, } } // enforce the msg type at compile time -var _ sdk.Msg = WhatCoolMsg{} +var _ sdk.Msg = TestYourCoolnessMsg{} // nolint -func (msg WhatCoolMsg) Type() string { return "cool" } -func (msg WhatCoolMsg) Get(key interface{}) (value interface{}) { return nil } -func (msg WhatCoolMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } -func (msg WhatCoolMsg) String() string { - return fmt.Sprintf("WhatCoolMsg{Sender: %v, CoolerThanCool: %v}", msg.Sender, msg.CoolerThanCool) +func (msg TestYourCoolnessMsg) Type() string { return "cool" } +func (msg TestYourCoolnessMsg) Get(key interface{}) (value interface{}) { return nil } +func (msg TestYourCoolnessMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } +func (msg TestYourCoolnessMsg) String() string { + return fmt.Sprintf("TestYourCoolnessMsg{Sender: %v, CoolerThanCool: %v}", msg.Sender, msg.CoolerThanCool) } // 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 { 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 -func (msg WhatCoolMsg) GetSignBytes() []byte { +func (msg TestYourCoolnessMsg) GetSignBytes() []byte { b, err := json.Marshal(msg) if err != nil { panic(err) From e7777cc7ca936f3240be6115e8bae36fa2977466 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 13 Mar 2018 01:37:50 +0100 Subject: [PATCH 2/5] cool module renames --- examples/basecoin/app/app.go | 8 +-- examples/basecoin/app/app_test.go | 16 +++--- examples/basecoin/cmd/basecli/main.go | 2 +- examples/basecoin/x/cool/commands/cooltx.go | 6 +-- examples/basecoin/x/cool/handler.go | 18 +++---- examples/basecoin/x/cool/mapper.go | 2 +- examples/basecoin/x/cool/types.go | 56 ++++++++++----------- 7 files changed, 54 insertions(+), 54 deletions(-) diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index bb6308d8c0..77816ba40c 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -81,14 +81,14 @@ func MakeCodec() *wire.Codec { const msgTypeSend = 0x1 const msgTypeIssue = 0x2 - const msgTypeCool = 0x3 - const msgTypeSetCool = 0x4 + const msgTypeQuiz = 0x3 + const msgTypeSetTrend = 0x4 var _ = oldwire.RegisterInterface( struct{ sdk.Msg }{}, oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend}, oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue}, - oldwire.ConcreteType{cool.CoolMsg{}, msgTypeCool}, - oldwire.ConcreteType{cool.SetCoolMsg{}, msgTypeSetCool}, + oldwire.ConcreteType{cool.QuizMsg{}, msgTypeQuiz}, + oldwire.ConcreteType{cool.SetTrendMsg{}, msgTypeSetTrend}, ) const accTypeApp = 0x1 diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index dbc4e00543..0c8771626a 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -33,17 +33,17 @@ var ( Outputs: []bank.Output{bank.NewOutput(addr2, coins)}, } - coolMsg1 = cool.CoolMsg{ - Sender: addr1, - CoolerThanCool: "icecold", + coolMsg1 = cool.QuizMsg{ + Sender: addr1, + CoolAnswer: "icecold", } - coolMsg2 = cool.CoolMsg{ - Sender: addr1, - CoolerThanCool: "icecold", + coolMsg2 = cool.QuizMsg{ + Sender: addr1, + CoolAnswer: "icecold", } - setCoolMsg = cool.SetCoolMsg{ + setTrendMsg = cool.SetTrendMsg{ Sender: addr1, Cool: "goodbye", } @@ -65,7 +65,7 @@ func TestMsgs(t *testing.T) { }{ {sendMsg}, {coolMsg1}, - {setCoolMsg}, + {setTrendMsg}, } chainID := "" diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 5985c9677f..74f45dc7e6 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -62,7 +62,7 @@ func main() { )...) basecliCmd.AddCommand( client.PostCommands( - coolcmd.SetCoolTxCmd(cdc), + coolcmd.SetTrendTxCmd(cdc), )...) // add proxy, version and key info diff --git a/examples/basecoin/x/cool/commands/cooltx.go b/examples/basecoin/x/cool/commands/cooltx.go index fe5f6a625f..128307be8d 100644 --- a/examples/basecoin/x/cool/commands/cooltx.go +++ b/examples/basecoin/x/cool/commands/cooltx.go @@ -29,7 +29,7 @@ func CoolTxCmd(cdc *wire.Codec) *cobra.Command { } // create the message - msg := cool.NewCoolMsg(from, args[0]) + msg := cool.NewQuizMsg(from, args[0]) // build and sign the transaction, then broadcast to Tendermint res, err := builder.SignBuildBroadcast(msg, cdc) @@ -44,7 +44,7 @@ func CoolTxCmd(cdc *wire.Codec) *cobra.Command { } // set what cool transaction -func SetCoolTxCmd(cdc *wire.Codec) *cobra.Command { +func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command { return &cobra.Command{ Use: "setcool [answer]", Short: "You're so cool, tell us what is cool!", @@ -60,7 +60,7 @@ func SetCoolTxCmd(cdc *wire.Codec) *cobra.Command { } // create the message - msg := cool.NewSetCoolMsg(from, args[0]) + msg := cool.NewSetTrendMsg(from, args[0]) // build and sign the transaction, then broadcast to Tendermint res, err := builder.SignBuildBroadcast(msg, cdc) diff --git a/examples/basecoin/x/cool/handler.go b/examples/basecoin/x/cool/handler.go index 72a2979bc0..b8f3134fca 100644 --- a/examples/basecoin/x/cool/handler.go +++ b/examples/basecoin/x/cool/handler.go @@ -22,10 +22,10 @@ import ( func NewHandler(ck bank.CoinKeeper, cm Mapper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) sdk.Result { switch msg := msg.(type) { - case SetCoolMsg: - return handleSetCoolMsg(ctx, cm, msg) - case CoolMsg: - return handleCoolMsg(ctx, ck, cm, msg) + case SetTrendMsg: + return handleSetTrendMsg(ctx, cm, msg) + case QuizMsg: + return handleQuizMsg(ctx, ck, cm, msg) default: errMsg := fmt.Sprintf("Unrecognized cool Msg type: %v", reflect.TypeOf(msg).Name()) return sdk.ErrUnknownRequest(errMsg).Result() @@ -34,22 +34,22 @@ func NewHandler(ck bank.CoinKeeper, cm Mapper) sdk.Handler { } // Handle CoolMsg This is the engine of your module -func handleSetCoolMsg(ctx sdk.Context, cm Mapper, msg SetCoolMsg) sdk.Result { - cm.SetCool(ctx, msg.Cool) +func handleSetTrendMsg(ctx sdk.Context, cm Mapper, msg SetTrendMsg) sdk.Result { + cm.SetTrend(ctx, msg.Cool) return sdk.Result{} } // Handle CoolMsg This is the engine of your module -func handleCoolMsg(ctx sdk.Context, ck bank.CoinKeeper, cm Mapper, msg CoolMsg) sdk.Result { +func handleQuizMsg(ctx sdk.Context, ck bank.CoinKeeper, cm Mapper, msg QuizMsg) sdk.Result { whatsCool := cm.GetCool(ctx) // set default if nothing is set //if whatsCool == "" { - //cm.SetCool(ctx, "icecold") + //cm.SetTrend(ctx, "icecold") //} - if msg.CoolerThanCool == whatsCool { + if msg.CoolAnswer == whatsCool { bonusCoins := sdk.Coins{{whatsCool, 69}} _, err := ck.AddCoins(ctx, msg.Sender, bonusCoins) diff --git a/examples/basecoin/x/cool/mapper.go b/examples/basecoin/x/cool/mapper.go index 9c4d2667bb..7122465891 100644 --- a/examples/basecoin/x/cool/mapper.go +++ b/examples/basecoin/x/cool/mapper.go @@ -24,7 +24,7 @@ func (am Mapper) GetCool(ctx sdk.Context) string { } // Implements sdk.AccountMapper. -func (am Mapper) SetCool(ctx sdk.Context, whatscool string) { +func (am Mapper) SetTrend(ctx sdk.Context, whatscool string) { store := ctx.KVStore(am.key) store.Set(coolKey, []byte(whatscool)) } diff --git a/examples/basecoin/x/cool/types.go b/examples/basecoin/x/cool/types.go index 7f80f968ad..f721bfa19b 100644 --- a/examples/basecoin/x/cool/types.go +++ b/examples/basecoin/x/cool/types.go @@ -10,32 +10,32 @@ import ( // A really cool msg type, these fields are can be entirely arbitrary and // custom to your message -type SetCoolMsg struct { +type SetTrendMsg struct { Sender sdk.Address Cool string } // New cool message -func NewSetCoolMsg(sender sdk.Address, cool string) SetCoolMsg { - return SetCoolMsg{ +func NewSetTrendMsg(sender sdk.Address, cool string) SetTrendMsg { + return SetTrendMsg{ Sender: sender, Cool: cool, } } // enforce the msg type at compile time -var _ sdk.Msg = SetCoolMsg{} +var _ sdk.Msg = SetTrendMsg{} // nolint -func (msg SetCoolMsg) Type() string { return "cool" } -func (msg SetCoolMsg) Get(key interface{}) (value interface{}) { return nil } -func (msg SetCoolMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } -func (msg SetCoolMsg) String() string { - return fmt.Sprintf("SetCoolMsg{Sender: %v, Cool: %v}", msg.Sender, msg.Cool) +func (msg SetTrendMsg) Type() string { return "cool" } +func (msg SetTrendMsg) Get(key interface{}) (value interface{}) { return nil } +func (msg SetTrendMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } +func (msg SetTrendMsg) String() string { + return fmt.Sprintf("SetTrendMsg{Sender: %v, Cool: %v}", msg.Sender, msg.Cool) } // Validate Basic is used to quickly disqualify obviously invalid messages quickly -func (msg SetCoolMsg) ValidateBasic() sdk.Error { +func (msg SetTrendMsg) ValidateBasic() sdk.Error { if len(msg.Sender) == 0 { return sdk.ErrUnrecognizedAddress(msg.Sender).Trace("") } @@ -49,7 +49,7 @@ func (msg SetCoolMsg) ValidateBasic() sdk.Error { } // Get the bytes for the message signer to sign on -func (msg SetCoolMsg) GetSignBytes() []byte { +func (msg SetTrendMsg) GetSignBytes() []byte { b, err := json.Marshal(msg) if err != nil { panic(err) @@ -59,34 +59,34 @@ func (msg SetCoolMsg) GetSignBytes() []byte { //_______________________________________________________________________ -// A really cool msg type, these fields are can be entirely arbitrary and -// custom to your message -type TestYourCoolnessMsg struct { - Sender sdk.Address - CoolerThanCool string +// A message type to quiz how cool you are. these fields are can be entirely +// arbitrary and custom to your message +type QuizMsg struct { + Sender sdk.Address + CoolAnswer string } // New cool message -func NewTestYourCoolnessMsg(sender sdk.Address, coolerthancool string) TestYourCoolnessMsg { - return TestYourCoolnessMsg{ - Sender: sender, - CoolerThanCool: coolerthancool, +func NewQuizMsg(sender sdk.Address, coolerthancool string) QuizMsg { + return QuizMsg{ + Sender: sender, + CoolAnswer: coolerthancool, } } // enforce the msg type at compile time -var _ sdk.Msg = TestYourCoolnessMsg{} +var _ sdk.Msg = QuizMsg{} // nolint -func (msg TestYourCoolnessMsg) Type() string { return "cool" } -func (msg TestYourCoolnessMsg) Get(key interface{}) (value interface{}) { return nil } -func (msg TestYourCoolnessMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } -func (msg TestYourCoolnessMsg) String() string { - return fmt.Sprintf("TestYourCoolnessMsg{Sender: %v, CoolerThanCool: %v}", msg.Sender, msg.CoolerThanCool) +func (msg QuizMsg) Type() string { return "cool" } +func (msg QuizMsg) Get(key interface{}) (value interface{}) { return nil } +func (msg QuizMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} } +func (msg QuizMsg) String() string { + return fmt.Sprintf("QuizMsg{Sender: %v, CoolAnswer: %v}", msg.Sender, msg.CoolAnswer) } // Validate Basic is used to quickly disqualify obviously invalid messages quickly -func (msg TestYourCoolnessMsg) ValidateBasic() sdk.Error { +func (msg QuizMsg) ValidateBasic() sdk.Error { if len(msg.Sender) == 0 { return sdk.ErrUnrecognizedAddress(msg.Sender).Trace("") } @@ -94,7 +94,7 @@ func (msg TestYourCoolnessMsg) ValidateBasic() sdk.Error { } // Get the bytes for the message signer to sign on -func (msg TestYourCoolnessMsg) GetSignBytes() []byte { +func (msg QuizMsg) GetSignBytes() []byte { b, err := json.Marshal(msg) if err != nil { panic(err) From 29cd41e5e452b77bc88ea2aeb68d32b93b829763 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 13 Mar 2018 02:43:29 +0100 Subject: [PATCH 3/5] basecoin app_test is cool now --- examples/basecoin/app/app_test.go | 67 +++++++++++++++---- examples/basecoin/cmd/basecli/main.go | 2 +- .../x/cool/commands/{cooltx.go => tx.go} | 6 +- examples/basecoin/x/cool/handler.go | 4 +- 4 files changed, 59 insertions(+), 20 deletions(-) rename examples/basecoin/x/cool/commands/{cooltx.go => tx.go} (93%) diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 0c8771626a..7704372546 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -33,19 +33,29 @@ var ( Outputs: []bank.Output{bank.NewOutput(addr2, coins)}, } - coolMsg1 = cool.QuizMsg{ + quizMsg1 = cool.QuizMsg{ Sender: addr1, CoolAnswer: "icecold", } - coolMsg2 = cool.QuizMsg{ + quizMsg2 = cool.QuizMsg{ Sender: addr1, - CoolAnswer: "icecold", + CoolAnswer: "badvibesonly", } - setTrendMsg = cool.SetTrendMsg{ + setTrendMsg1 = cool.SetTrendMsg{ Sender: addr1, - Cool: "goodbye", + Cool: "icecold", + } + + setTrendMsg2 = cool.SetTrendMsg{ + Sender: addr1, + Cool: "badvibesonly", + } + + setTrendMsg3 = cool.SetTrendMsg{ + Sender: addr1, + Cool: "warmandkind", } ) @@ -64,8 +74,8 @@ func TestMsgs(t *testing.T) { msg sdk.Msg }{ {sendMsg}, - {coolMsg1}, - {setTrendMsg}, + {quizMsg1}, + {setTrendMsg1}, } chainID := "" @@ -209,7 +219,7 @@ func TestSendMsgWithAccounts(t *testing.T) { assert.Equal(t, sdk.CodeOK, res.Code, res.Log) } -func TestCoolMsg(t *testing.T) { +func TestQuizMsg(t *testing.T) { bapp := newBasecoinApp() // Construct genesis state @@ -240,23 +250,52 @@ func TestCoolMsg(t *testing.T) { res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1) assert.Equal(t, acc1, res1) + // Set the trend, submit a really cool quiz and check for reward + SignCheckDeliver(t, bapp, setTrendMsg1, 0, true) + SignCheckDeliver(t, bapp, quizMsg1, 1, true) + CheckBalance(t, bapp, "69icecold") + SignCheckDeliver(t, bapp, quizMsg2, 2, true) // result without reward + CheckBalance(t, bapp, "69icecold") + SignCheckDeliver(t, bapp, quizMsg1, 3, true) + CheckBalance(t, bapp, "138icecold") + SignCheckDeliver(t, bapp, setTrendMsg2, 4, true) // reset the trend + SignCheckDeliver(t, bapp, quizMsg1, 5, true) // the same answer will nolonger do! + CheckBalance(t, bapp, "138icecold") + SignCheckDeliver(t, bapp, quizMsg2, 6, true) // earlier answer now relavent again + CheckBalance(t, bapp, "69badvibesonly,138icecold") + SignCheckDeliver(t, bapp, setTrendMsg3, 7, false) // expect to fail to set the trend to something which is not cool + +} + +func SignCheckDeliver(t *testing.T, bapp *BasecoinApp, msg sdk.Msg, seq int64, expPass bool) { + // Sign the tx - tx := sdk.NewStdTx(coolMsg1, []sdk.StdSignature{{ + tx := sdk.NewStdTx(msg, []sdk.StdSignature{{ PubKey: priv1.PubKey(), - Signature: priv1.Sign(coolMsg1.GetSignBytes()), + Signature: priv1.Sign(msg.GetSignBytes()), + Sequence: seq, }}) // Run a Check res := bapp.Check(tx) - assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + if expPass { + require.Equal(t, sdk.CodeOK, res.Code, res.Log) + } else { + require.NotEqual(t, sdk.CodeOK, res.Code, res.Log) + } // Simulate a Block bapp.BeginBlock(abci.RequestBeginBlock{}) res = bapp.Deliver(tx) - assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + if expPass { + require.Equal(t, sdk.CodeOK, res.Code, res.Log) + } else { + require.NotEqual(t, sdk.CodeOK, res.Code, res.Log) + } +} - // Check balances +func CheckBalance(t *testing.T, bapp *BasecoinApp, balExpected string) { ctxDeliver := bapp.BaseApp.NewContext(false, abci.Header{}) res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr1) - assert.Equal(t, "70icecold", fmt.Sprintf("%v", res2.GetCoins())) + assert.Equal(t, balExpected, fmt.Sprintf("%v", res2.GetCoins())) } diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 74f45dc7e6..1827b4d98e 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -58,7 +58,7 @@ func main() { )...) basecliCmd.AddCommand( client.PostCommands( - coolcmd.CoolTxCmd(cdc), + coolcmd.QuizTxCmd(cdc), )...) basecliCmd.AddCommand( client.PostCommands( diff --git a/examples/basecoin/x/cool/commands/cooltx.go b/examples/basecoin/x/cool/commands/tx.go similarity index 93% rename from examples/basecoin/x/cool/commands/cooltx.go rename to examples/basecoin/x/cool/commands/tx.go index 128307be8d..2b16546806 100644 --- a/examples/basecoin/x/cool/commands/cooltx.go +++ b/examples/basecoin/x/cool/commands/tx.go @@ -12,8 +12,8 @@ import ( "github.com/cosmos/cosmos-sdk/examples/basecoin/x/cool" ) -// what cool transaction -func CoolTxCmd(cdc *wire.Codec) *cobra.Command { +// take the coolness quiz transaction +func QuizTxCmd(cdc *wire.Codec) *cobra.Command { return &cobra.Command{ Use: "cool [answer]", Short: "What's cooler than being cool?", @@ -43,7 +43,7 @@ func CoolTxCmd(cdc *wire.Codec) *cobra.Command { } } -// set what cool transaction +// set a new cool trend transaction func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command { return &cobra.Command{ Use: "setcool [answer]", diff --git a/examples/basecoin/x/cool/handler.go b/examples/basecoin/x/cool/handler.go index b8f3134fca..968f15c9a3 100644 --- a/examples/basecoin/x/cool/handler.go +++ b/examples/basecoin/x/cool/handler.go @@ -33,13 +33,13 @@ func NewHandler(ck bank.CoinKeeper, cm Mapper) sdk.Handler { } } -// Handle CoolMsg This is the engine of your module +// Handle QuizMsg This is the engine of your module func handleSetTrendMsg(ctx sdk.Context, cm Mapper, msg SetTrendMsg) sdk.Result { cm.SetTrend(ctx, msg.Cool) return sdk.Result{} } -// Handle CoolMsg This is the engine of your module +// Handle QuizMsg This is the engine of your module func handleQuizMsg(ctx sdk.Context, ck bank.CoinKeeper, cm Mapper, msg QuizMsg) sdk.Result { whatsCool := cm.GetCool(ctx) From eea4e8d1512e6a958638b9af04b2b52692bbd51f Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Tue, 13 Mar 2018 02:49:02 +0100 Subject: [PATCH 4/5] cleanup --- examples/basecoin/x/cool/handler.go | 12 +++--------- examples/basecoin/x/cool/mapper.go | 12 ++++++------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/examples/basecoin/x/cool/handler.go b/examples/basecoin/x/cool/handler.go index 968f15c9a3..c0aae11bba 100644 --- a/examples/basecoin/x/cool/handler.go +++ b/examples/basecoin/x/cool/handler.go @@ -42,16 +42,10 @@ func handleSetTrendMsg(ctx sdk.Context, cm Mapper, msg SetTrendMsg) sdk.Result { // Handle QuizMsg This is the engine of your module func handleQuizMsg(ctx sdk.Context, ck bank.CoinKeeper, cm Mapper, msg QuizMsg) sdk.Result { - whatsCool := cm.GetCool(ctx) + currentTrend := cm.GetTrend(ctx) - // set default if nothing is set - //if whatsCool == "" { - //cm.SetTrend(ctx, "icecold") - //} - - if msg.CoolAnswer == whatsCool { - - bonusCoins := sdk.Coins{{whatsCool, 69}} + if msg.CoolAnswer == currentTrend { + bonusCoins := sdk.Coins{{currentTrend, 69}} _, err := ck.AddCoins(ctx, msg.Sender, bonusCoins) if err != nil { return err.Result() diff --git a/examples/basecoin/x/cool/mapper.go b/examples/basecoin/x/cool/mapper.go index 7122465891..2e0a791fa6 100644 --- a/examples/basecoin/x/cool/mapper.go +++ b/examples/basecoin/x/cool/mapper.go @@ -13,18 +13,18 @@ func NewMapper(key sdk.StoreKey) Mapper { return Mapper{key} } -// Key to knowing whats cool -var coolKey = []byte("WhatsCoolKey") +// Key to knowing the trend on the streets! +var trendKey = []byte("TrendKey") // Implements sdk.AccountMapper. -func (am Mapper) GetCool(ctx sdk.Context) string { +func (am Mapper) GetTrend(ctx sdk.Context) string { store := ctx.KVStore(am.key) - bz := store.Get(coolKey) + bz := store.Get(trendKey) return string(bz) } // Implements sdk.AccountMapper. -func (am Mapper) SetTrend(ctx sdk.Context, whatscool string) { +func (am Mapper) SetTrend(ctx sdk.Context, newTrend string) { store := ctx.KVStore(am.key) - store.Set(coolKey, []byte(whatscool)) + store.Set(trendKey, []byte(newTrend)) } From 8f03b181c7678d15a401bc39a27c14af174c1007 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 13 Mar 2018 11:54:22 +0100 Subject: [PATCH 5/5] examples/basecoin/app: fix test by using StdSignDoc --- examples/basecoin/app/app_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 7704372546..c2ef3e4547 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -23,6 +23,8 @@ import ( // Construct some global addrs and txs for tests. var ( + chainID = "" // TODO + priv1 = crypto.GenPrivKeyEd25519() addr1 = priv1.PubKey().Address() addr2 = crypto.GenPrivKeyEd25519().PubKey().Address() @@ -78,7 +80,6 @@ func TestMsgs(t *testing.T) { {setTrendMsg1}, } - chainID := "" sequences := []int64{0} for i, m := range msgs { sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, m.msg)) @@ -178,7 +179,6 @@ func TestSendMsgWithAccounts(t *testing.T) { assert.Equal(t, acc1, res1) // Sign the tx - chainID := "" // TODO: InitChain should get the ChainID sequences := []int64{0} sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, sendMsg)) tx := sdk.NewStdTx(sendMsg, []sdk.StdSignature{{ @@ -272,7 +272,7 @@ func SignCheckDeliver(t *testing.T, bapp *BasecoinApp, msg sdk.Msg, seq int64, e // Sign the tx tx := sdk.NewStdTx(msg, []sdk.StdSignature{{ PubKey: priv1.PubKey(), - Signature: priv1.Sign(msg.GetSignBytes()), + Signature: priv1.Sign(sdk.StdSignBytes(chainID, []int64{seq}, msg)), Sequence: seq, }})