From 12a17f6996cc613ef653a8c5f067d67d0853aa3e Mon Sep 17 00:00:00 2001 From: mossid Date: Sat, 31 Mar 2018 19:40:40 +0200 Subject: [PATCH 1/5] merge commit --- examples/basecoin/app/app_test.go | 280 +++++++++++++++++++----- examples/basecoin/types/account_test.go | 36 +++ 2 files changed, 261 insertions(+), 55 deletions(-) create mode 100644 examples/basecoin/types/account_test.go diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 0d916b054e02..00a295724b5c 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -26,20 +26,55 @@ import ( var ( chainID = "" // TODO - priv1 = crypto.GenPrivKeyEd25519() - addr1 = priv1.PubKey().Address() - addr2 = crypto.GenPrivKeyEd25519().PubKey().Address() - coins = sdk.Coins{{"foocoin", 10}} - fee = sdk.StdFee{ + accName = "foobart" + + priv1 = crypto.GenPrivKeyEd25519() + addr1 = priv1.PubKey().Address() + priv2 = crypto.GenPrivKeyEd25519() + addr2 = priv2.PubKey().Address() + addr3 = crypto.GenPrivKeyEd25519().PubKey().Address() + priv4 = crypto.GenPrivKeyEd25519() + addr4 = priv4.PubKey().Address() + coins = sdk.Coins{{"foocoin", 10}} + halfCoins = sdk.Coins{{"foocoin", 5}} + fee = sdk.StdFee{ sdk.Coins{{"foocoin", 0}}, 0, } - sendMsg = bank.SendMsg{ + sendMsg1 = bank.SendMsg{ Inputs: []bank.Input{bank.NewInput(addr1, coins)}, Outputs: []bank.Output{bank.NewOutput(addr2, coins)}, } + sendMsg2 = bank.SendMsg{ + Inputs: []bank.Input{bank.NewInput(addr1, coins)}, + Outputs: []bank.Output{ + bank.NewOutput(addr2, halfCoins), + bank.NewOutput(addr3, halfCoins), + }, + } + + sendMsg3 = bank.SendMsg{ + Inputs: []bank.Input{ + bank.NewInput(addr1, coins), + bank.NewInput(addr4, coins), + }, + Outputs: []bank.Output{ + bank.NewOutput(addr2, coins), + bank.NewOutput(addr3, coins), + }, + } + + sendMsg4 = bank.SendMsg{ + Inputs: []bank.Input{ + bank.NewInput(addr2, coins), + }, + Outputs: []bank.Output{ + bank.NewOutput(addr1, coins), + }, + } + quizMsg1 = cool.QuizMsg{ Sender: addr1, CoolAnswer: "icecold", @@ -72,6 +107,29 @@ func newBasecoinApp() *BasecoinApp { return NewBasecoinApp(logger, db) } +func setGenesisAccounts(bapp *BasecoinApp, accs ...auth.BaseAccount) error { + genaccs := make([]*types.GenesisAccount, len(accs)) + for i, acc := range accs { + genaccs[i] = types.NewGenesisAccount(&types.AppAccount{acc, accName}) + } + + genesisState := types.GenesisState{ + Accounts: genaccs, + } + + stateBytes, err := json.MarshalIndent(genesisState, "", "\t") + if err != nil { + return err + } + + // Initialize the chain + vals := []abci.Validator{} + bapp.InitChain(abci.RequestInitChain{vals, stateBytes}) + bapp.Commit() + + return nil +} + //_______________________________________________________________________ func TestMsgs(t *testing.T) { @@ -80,7 +138,7 @@ func TestMsgs(t *testing.T) { msgs := []struct { msg sdk.Msg }{ - {sendMsg}, + {sendMsg1}, {quizMsg1}, {setTrendMsg1}, } @@ -125,26 +183,13 @@ func TestGenesis(t *testing.T) { Address: addr, Coins: coins, } - acc := &types.AppAccount{baseAcc, "foobart"} - - genesisState := map[string]interface{}{ - "accounts": []*types.GenesisAccount{ - types.NewGenesisAccount(acc), - }, - "cool": map[string]string{ - "trend": "ice-cold", - }, - } - stateBytes, err := json.MarshalIndent(genesisState, "", "\t") - - vals := []abci.Validator{} - bapp.InitChain(abci.RequestInitChain{vals, stateBytes}) - bapp.Commit() + err = setGenesisAccounts(bapp, baseAcc) + assert.Nil(t, err) // A checkTx context ctx := bapp.BaseApp.NewContext(true, abci.Header{}) res1 := bapp.accountMapper.GetAccount(ctx, baseAcc.Address) - assert.Equal(t, acc, res1) + assert.Equal(t, baseAcc, res1.(*types.AppAccount).BaseAccount) /* // reload app and ensure the account is still there bapp = NewBasecoinApp(logger, db) @@ -165,34 +210,20 @@ func TestSendMsgWithAccounts(t *testing.T) { Address: addr1, Coins: coins, } - acc1 := &types.AppAccount{baseAcc, "foobart"} // Construct genesis state - genesisState := map[string]interface{}{ - "accounts": []*types.GenesisAccount{ - types.NewGenesisAccount(acc1), - }, - "cool": map[string]string{ - "trend": "ice-cold", - }, - } - stateBytes, err := json.MarshalIndent(genesisState, "", "\t") - require.Nil(t, err) - - // Initialize the chain - vals := []abci.Validator{} - bapp.InitChain(abci.RequestInitChain{vals, stateBytes}) - bapp.Commit() + err = setGenesisAccounts(bapp, baseAcc) + assert.Nil(t, err) // A checkTx context (true) ctxCheck := bapp.BaseApp.NewContext(true, abci.Header{}) res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1) - assert.Equal(t, acc1, res1) + assert.Equal(t, baseAcc, res1.(*types.AppAccount).BaseAccount) // Sign the tx sequences := []int64{0} - sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, sendMsg)) - tx := sdk.NewStdTx(sendMsg, fee, []sdk.StdSignature{{ + sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, sendMsg1)) + tx := sdk.NewStdTx(sendMsg1, fee, []sdk.StdSignature{{ PubKey: priv1.PubKey(), Signature: sig, }}) @@ -230,6 +261,156 @@ func TestSendMsgWithAccounts(t *testing.T) { assert.Equal(t, sdk.CodeOK, res.Code, res.Log) } +func TestSendMsgMultipleOut(t *testing.T) { + bapp := newBasecoinApp() + + genCoins, err := sdk.ParseCoins("42foocoin") + require.Nil(t, err) + + acc1 := auth.BaseAccount{ + Address: addr1, + Coins: genCoins, + } + + acc2 := auth.BaseAccount{ + Address: addr2, + Coins: genCoins, + } + + err = setGenesisAccounts(bapp, acc1, acc2) + assert.Nil(t, err) + + sequences := []int64{0} + sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, sendMsg2)) + tx := sdk.NewStdTx(sendMsg2, fee, []sdk.StdSignature{{ + PubKey: priv1.PubKey(), + Signature: sig, + }}) + + // Simulate a Block + bapp.BeginBlock(abci.RequestBeginBlock{}) + res := bapp.Deliver(tx) + assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + + // Check balances + ctx := bapp.BaseApp.NewContext(false, abci.Header{}) + acc := bapp.accountMapper.GetAccount(ctx, addr1) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "32foocoin") + acc = bapp.accountMapper.GetAccount(ctx, addr2) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "47foocoin") + acc = bapp.accountMapper.GetAccount(ctx, addr3) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "5foocoin") +} + +func TestSengMsgMultipleInOut(t *testing.T) { + bapp := newBasecoinApp() + + genCoins, err := sdk.ParseCoins("42foocoin") + require.Nil(t, err) + + acc1 := auth.BaseAccount{ + Address: addr1, + Coins: genCoins, + } + + acc2 := auth.BaseAccount{ + Address: addr2, + Coins: genCoins, + } + + acc4 := auth.BaseAccount{ + Address: addr4, + Coins: genCoins, + } + + err = setGenesisAccounts(bapp, acc1, acc2, acc4) + assert.Nil(t, err) + + sequences := []int64{0, 0} + signbz := sdk.StdSignBytes(chainID, sequences, fee, sendMsg3) + sig1 := priv1.Sign(signbz) + sig4 := priv4.Sign(signbz) + tx := sdk.NewStdTx(sendMsg3, fee, []sdk.StdSignature{ + { + PubKey: priv1.PubKey(), + Signature: sig1, + }, + { + PubKey: priv4.PubKey(), + Signature: sig4, + }, + }) + + // Simulate a Block + bapp.BeginBlock(abci.RequestBeginBlock{}) + res := bapp.Deliver(tx) + assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + + // Check balances + ctx := bapp.BaseApp.NewContext(false, abci.Header{}) + acc := bapp.accountMapper.GetAccount(ctx, addr1) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "32foocoin") + acc = bapp.accountMapper.GetAccount(ctx, addr4) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "32foocoin") + acc = bapp.accountMapper.GetAccount(ctx, addr2) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "52foocoin") + acc = bapp.accountMapper.GetAccount(ctx, addr3) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "10foocoin") + +} + +func TestSendMsgDependent(t *testing.T) { + bapp := newBasecoinApp() + + genCoins, err := sdk.ParseCoins("42foocoin") + require.Nil(t, err) + + acc1 := auth.BaseAccount{ + Address: addr1, + Coins: genCoins, + } + + err = setGenesisAccounts(bapp, acc1) + assert.Nil(t, err) + + sequences := []int64{0} + + // Simulate a block + signbz := sdk.StdSignBytes(chainID, sequences, fee, sendMsg1) + sig1 := priv1.Sign(signbz) + tx := sdk.NewStdTx(sendMsg1, fee, []sdk.StdSignature{{ + PubKey: priv1.PubKey(), + Signature: sig1, + }}) + + bapp.BeginBlock(abci.RequestBeginBlock{}) + res := bapp.Deliver(tx) + assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + + // Check balances + ctx := bapp.BaseApp.NewContext(false, abci.Header{}) + acc := bapp.accountMapper.GetAccount(ctx, addr1) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "32foocoin") + acc = bapp.accountMapper.GetAccount(ctx, addr2) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "10foocoin") + + // Simulate a Block + signbz = sdk.StdSignBytes(chainID, sequences, fee, sendMsg4) + sig2 := priv2.Sign(signbz) + tx = sdk.NewStdTx(sendMsg4, fee, []sdk.StdSignature{{ + PubKey: priv2.PubKey(), + Signature: sig2, + }}) + + res = bapp.Deliver(tx) + assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + + // Check balances + ctx = bapp.BaseApp.NewContext(false, abci.Header{}) + acc = bapp.accountMapper.GetAccount(ctx, addr1) + assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "42foocoin") +} + func TestQuizMsg(t *testing.T) { bapp := newBasecoinApp() @@ -287,24 +468,13 @@ func TestHandler(t *testing.T) { sourceChain := "source-chain" destChain := "dest-chain" - vals := []abci.Validator{} baseAcc := auth.BaseAccount{ Address: addr1, Coins: coins, } acc1 := &types.AppAccount{baseAcc, "foobart"} - genesisState := map[string]interface{}{ - "accounts": []*types.GenesisAccount{ - types.NewGenesisAccount(acc1), - }, - "cool": map[string]string{ - "trend": "ice-cold", - }, - } - stateBytes, err := json.MarshalIndent(genesisState, "", "\t") - require.Nil(t, err) - bapp.InitChain(abci.RequestInitChain{vals, stateBytes}) - bapp.Commit() + err := setGenesisAccounts(bapp, baseAcc) + assert.Nil(t, err) // A checkTx context (true) ctxCheck := bapp.BaseApp.NewContext(true, abci.Header{}) diff --git a/examples/basecoin/types/account_test.go b/examples/basecoin/types/account_test.go new file mode 100644 index 000000000000..2d03999c24d7 --- /dev/null +++ b/examples/basecoin/types/account_test.go @@ -0,0 +1,36 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + sdk "github.com/cosmos/cosmos-sdk/types" + wire "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" +) + +func TestAppAccount(t *testing.T) { + cdc := wire.NewCodec() + addr := sdk.Address([]byte("address")) + acc := &AppAccount{ + BaseAccount: auth.BaseAccount{ + Address: addr, + Coins: sdk.Coins{}, + }, + Name: "", + } + + bz, err := cdc.MarshalBinary(acc) + assert.Nil(t, err) + + decode := GetAccountDecoder(cdc) + res, err := decode(bz) + assert.Nil(t, err) + assert.Equal(t, acc, res) + + name := t.Name() + acc.SetName(name) + accname := acc.GetName() + assert.Equal(t, name, accname) +} From d80e6540c29fe9302d27441419ce1a7d6e5fd5e8 Mon Sep 17 00:00:00 2001 From: mossid Date: Sat, 31 Mar 2018 19:42:06 +0200 Subject: [PATCH 2/5] merge commit --- examples/basecoin/app/app_test.go | 167 ++++++++++-------------------- 1 file changed, 57 insertions(+), 110 deletions(-) diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 00a295724b5c..8d808d89bbeb 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -143,29 +143,9 @@ func TestMsgs(t *testing.T) { {setTrendMsg1}, } - sequences := []int64{0} for i, m := range msgs { - sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, m.msg)) - tx := sdk.NewStdTx(m.msg, fee, []sdk.StdSignature{{ - PubKey: priv1.PubKey(), - Signature: sig, - }}) - - // just marshal/unmarshal! - cdc := MakeCodec() - txBytes, err := cdc.MarshalBinary(tx) - require.NoError(t, err, "i: %v", i) - - // Run a Check - cres := bapp.CheckTx(txBytes) - assert.Equal(t, sdk.CodeUnknownAddress, - sdk.CodeType(cres.Code), "i: %v, log: %v", i, cres.Log) - - // Simulate a Block - bapp.BeginBlock(abci.RequestBeginBlock{}) - dres := bapp.DeliverTx(txBytes) - assert.Equal(t, sdk.CodeUnknownAddress, - sdk.CodeType(dres.Code), "i: %v, log: %v", i, dres.Log) + // Run a CheckDeliver + SignCheckDeliver(t, bapp, m.msg, int64(i), false, priv1) } } @@ -220,45 +200,25 @@ func TestSendMsgWithAccounts(t *testing.T) { res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1) assert.Equal(t, baseAcc, res1.(*types.AppAccount).BaseAccount) - // Sign the tx - sequences := []int64{0} - sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, sendMsg1)) - tx := sdk.NewStdTx(sendMsg1, fee, []sdk.StdSignature{{ - PubKey: priv1.PubKey(), - Signature: sig, - }}) - - // 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) + // Run a CheckDeliver + SignCheckDeliver(t, bapp, sendMsg1, 0, true, priv1) // Check balances - ctxDeliver := bapp.BaseApp.NewContext(false, abci.Header{}) - res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr1) - res3 := bapp.accountMapper.GetAccount(ctxDeliver, addr2) - assert.Equal(t, fmt.Sprintf("%v", res2.GetCoins()), "67foocoin") - assert.Equal(t, fmt.Sprintf("%v", res3.GetCoins()), "10foocoin") + CheckBalance(t, bapp, addr1, "67foocoin") + CheckBalance(t, bapp, addr2, "10foocoin") // Delivering again should cause replay error - res = bapp.Deliver(tx) - assert.Equal(t, sdk.CodeInvalidSequence, res.Code, res.Log) + SignCheckDeliver(t, bapp, sendMsg1, 0, false, priv1) // bumping the txnonce number without resigning should be an auth error + tx := genTx(sendMsg1, 0, priv1) tx.Signatures[0].Sequence = 1 - res = bapp.Deliver(tx) + res := bapp.Deliver(tx) + assert.Equal(t, sdk.CodeUnauthorized, res.Code, res.Log) // resigning the tx with the bumped sequence should work - sequences = []int64{1} - sig = priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, tx.Msg)) - tx.Signatures[0].Signature = sig - res = bapp.Deliver(tx) - assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + SignCheckDeliver(t, bapp, sendMsg1, 1, true, priv1) } func TestSendMsgMultipleOut(t *testing.T) { @@ -280,26 +240,13 @@ func TestSendMsgMultipleOut(t *testing.T) { err = setGenesisAccounts(bapp, acc1, acc2) assert.Nil(t, err) - sequences := []int64{0} - sig := priv1.Sign(sdk.StdSignBytes(chainID, sequences, fee, sendMsg2)) - tx := sdk.NewStdTx(sendMsg2, fee, []sdk.StdSignature{{ - PubKey: priv1.PubKey(), - Signature: sig, - }}) - // Simulate a Block - bapp.BeginBlock(abci.RequestBeginBlock{}) - res := bapp.Deliver(tx) - assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + SignCheckDeliver(t, bapp, sendMsg2, 0, true, priv1) // Check balances - ctx := bapp.BaseApp.NewContext(false, abci.Header{}) - acc := bapp.accountMapper.GetAccount(ctx, addr1) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "32foocoin") - acc = bapp.accountMapper.GetAccount(ctx, addr2) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "47foocoin") - acc = bapp.accountMapper.GetAccount(ctx, addr3) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "5foocoin") + CheckBalance(t, bapp, addr1, "32foocoin") + CheckBalance(t, bapp, addr2, "47foocoin") + CheckBalance(t, bapp, addr3, "5foocoin") } func TestSengMsgMultipleInOut(t *testing.T) { @@ -347,16 +294,10 @@ func TestSengMsgMultipleInOut(t *testing.T) { assert.Equal(t, sdk.CodeOK, res.Code, res.Log) // Check balances - ctx := bapp.BaseApp.NewContext(false, abci.Header{}) - acc := bapp.accountMapper.GetAccount(ctx, addr1) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "32foocoin") - acc = bapp.accountMapper.GetAccount(ctx, addr4) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "32foocoin") - acc = bapp.accountMapper.GetAccount(ctx, addr2) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "52foocoin") - acc = bapp.accountMapper.GetAccount(ctx, addr3) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "10foocoin") - + CheckBalance(t, bapp, addr1, "32foocoin") + CheckBalance(t, bapp, addr4, "32foocoin") + CheckBalance(t, bapp, addr2, "52foocoin") + CheckBalance(t, bapp, addr3, "10foocoin") } func TestSendMsgDependent(t *testing.T) { @@ -406,9 +347,7 @@ func TestSendMsgDependent(t *testing.T) { assert.Equal(t, sdk.CodeOK, res.Code, res.Log) // Check balances - ctx = bapp.BaseApp.NewContext(false, abci.Header{}) - acc = bapp.accountMapper.GetAccount(ctx, addr1) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "42foocoin") + CheckBalance(t, bapp, addr1, "42foocoin") } func TestQuizMsg(t *testing.T) { @@ -446,19 +385,19 @@ func TestQuizMsg(t *testing.T) { 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, false) // 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, false) // 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 + SignCheckDeliver(t, bapp, setTrendMsg1, 0, true, priv1) + SignCheckDeliver(t, bapp, quizMsg1, 1, true, priv1) + CheckBalance(t, bapp, addr1, "69icecold") + SignCheckDeliver(t, bapp, quizMsg2, 2, true, priv1) // result without reward + CheckBalance(t, bapp, addr1, "69icecold") + SignCheckDeliver(t, bapp, quizMsg1, 3, true, priv1) + CheckBalance(t, bapp, addr1, "138icecold") + SignCheckDeliver(t, bapp, setTrendMsg2, 4, true, priv1) // reset the trend + SignCheckDeliver(t, bapp, quizMsg1, 5, true, priv1) // the same answer will nolonger do! + CheckBalance(t, bapp, addr1, "138icecold") + SignCheckDeliver(t, bapp, quizMsg2, 6, true, priv1) // earlier answer now relavent again + CheckBalance(t, bapp, addr1, "69badvibesonly,138icecold") + SignCheckDeliver(t, bapp, setTrendMsg3, 7, false, priv1) // expect to fail to set the trend to something which is not cool } @@ -499,24 +438,32 @@ func TestHandler(t *testing.T) { Sequence: 0, } - SignCheckDeliver(t, bapp, transferMsg, 0, true) - CheckBalance(t, bapp, "") - SignCheckDeliver(t, bapp, transferMsg, 1, false) - SignCheckDeliver(t, bapp, receiveMsg, 2, true) - CheckBalance(t, bapp, "10foocoin") - SignCheckDeliver(t, bapp, receiveMsg, 3, false) + SignCheckDeliver(t, bapp, transferMsg, 0, true, priv1) + CheckBalance(t, bapp, addr1, "") + SignCheckDeliver(t, bapp, transferMsg, 1, false, priv1) + SignCheckDeliver(t, bapp, receiveMsg, 2, true, priv1) + CheckBalance(t, bapp, addr1, "10foocoin") + SignCheckDeliver(t, bapp, receiveMsg, 3, false, priv1) } -// TODO describe the use of this function -func SignCheckDeliver(t *testing.T, bapp *BasecoinApp, msg sdk.Msg, seq int64, expPass bool) { +func genTx(msg sdk.Msg, seq int64, priv ...crypto.PrivKeyEd25519) sdk.StdTx { + sigs := make([]sdk.StdSignature, len(priv)) + for i, p := range priv { + sigs[i] = sdk.StdSignature{ + PubKey: p.PubKey(), + Signature: p.Sign(sdk.StdSignBytes(chainID, []int64{seq}, fee, msg)), + Sequence: seq, + } + } - // Sign the tx - tx := sdk.NewStdTx(msg, fee, []sdk.StdSignature{{ - PubKey: priv1.PubKey(), - Signature: priv1.Sign(sdk.StdSignBytes(chainID, []int64{seq}, fee, msg)), - Sequence: seq, - }}) + return sdk.NewStdTx(msg, fee, sigs) + +} + +func SignCheckDeliver(t *testing.T, bapp *BasecoinApp, msg sdk.Msg, seq int64, expPass bool, priv ...crypto.PrivKeyEd25519) { + // Sign the tx + tx := genTx(msg, seq, priv...) // Run a Check res := bapp.Check(tx) if expPass { @@ -537,8 +484,8 @@ func SignCheckDeliver(t *testing.T, bapp *BasecoinApp, msg sdk.Msg, seq int64, e //bapp.Commit() } -func CheckBalance(t *testing.T, bapp *BasecoinApp, balExpected string) { +func CheckBalance(t *testing.T, bapp *BasecoinApp, addr sdk.Address, balExpected string) { ctxDeliver := bapp.BaseApp.NewContext(false, abci.Header{}) - res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr1) + res2 := bapp.accountMapper.GetAccount(ctxDeliver, addr) assert.Equal(t, balExpected, fmt.Sprintf("%v", res2.GetCoins())) } From f1d27e95ff9a6f121f7222ee9eb52fc83c01b4ff Mon Sep 17 00:00:00 2001 From: mossid Date: Sat, 31 Mar 2018 19:26:40 +0200 Subject: [PATCH 3/5] TestHandler -> TestIBCMsgs --- examples/basecoin/app/app_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 8d808d89bbeb..bc558417794d 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -401,7 +401,7 @@ func TestQuizMsg(t *testing.T) { } -func TestHandler(t *testing.T) { +func TestIBCMsgs(t *testing.T) { bapp := newBasecoinApp() sourceChain := "source-chain" From 549896128d61390fcbff638da79a31fd7b5c735b Mon Sep 17 00:00:00 2001 From: mossid Date: Sat, 31 Mar 2018 20:01:39 +0200 Subject: [PATCH 4/5] in progress --- examples/basecoin/app/app_test.go | 53 ++++--------------------------- 1 file changed, 7 insertions(+), 46 deletions(-) diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index bc558417794d..51dc301dd5ec 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -273,25 +273,8 @@ func TestSengMsgMultipleInOut(t *testing.T) { err = setGenesisAccounts(bapp, acc1, acc2, acc4) assert.Nil(t, err) - sequences := []int64{0, 0} - signbz := sdk.StdSignBytes(chainID, sequences, fee, sendMsg3) - sig1 := priv1.Sign(signbz) - sig4 := priv4.Sign(signbz) - tx := sdk.NewStdTx(sendMsg3, fee, []sdk.StdSignature{ - { - PubKey: priv1.PubKey(), - Signature: sig1, - }, - { - PubKey: priv4.PubKey(), - Signature: sig4, - }, - }) - - // Simulate a Block - bapp.BeginBlock(abci.RequestBeginBlock{}) - res := bapp.Deliver(tx) - assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + // CheckDeliver + SignCheckDeliver(t, bapp, sendMsg3, 0, true, priv1, priv4) // Check balances CheckBalance(t, bapp, addr1, "32foocoin") @@ -314,37 +297,15 @@ func TestSendMsgDependent(t *testing.T) { err = setGenesisAccounts(bapp, acc1) assert.Nil(t, err) - sequences := []int64{0} - - // Simulate a block - signbz := sdk.StdSignBytes(chainID, sequences, fee, sendMsg1) - sig1 := priv1.Sign(signbz) - tx := sdk.NewStdTx(sendMsg1, fee, []sdk.StdSignature{{ - PubKey: priv1.PubKey(), - Signature: sig1, - }}) - - bapp.BeginBlock(abci.RequestBeginBlock{}) - res := bapp.Deliver(tx) - assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + // CheckDeliver + SignCheckDeliver(t, bapp, sendMsg1, 0, true, priv1) // Check balances - ctx := bapp.BaseApp.NewContext(false, abci.Header{}) - acc := bapp.accountMapper.GetAccount(ctx, addr1) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "32foocoin") - acc = bapp.accountMapper.GetAccount(ctx, addr2) - assert.Equal(t, fmt.Sprintf("%v", acc.GetCoins()), "10foocoin") + CheckBalance(t, bapp, addr1, "32foocoin") + CheckBalance(t, bapp, addr2, "10foocoin") // Simulate a Block - signbz = sdk.StdSignBytes(chainID, sequences, fee, sendMsg4) - sig2 := priv2.Sign(signbz) - tx = sdk.NewStdTx(sendMsg4, fee, []sdk.StdSignature{{ - PubKey: priv2.PubKey(), - Signature: sig2, - }}) - - res = bapp.Deliver(tx) - assert.Equal(t, sdk.CodeOK, res.Code, res.Log) + SignCheckDeliver(t, bapp, sendMsg4, 0, true, priv2) // Check balances CheckBalance(t, bapp, addr1, "42foocoin") From 8af121a840023a89a87d9e9da55261db32fa13ea Mon Sep 17 00:00:00 2001 From: mossid Date: Sat, 31 Mar 2018 20:20:44 +0200 Subject: [PATCH 5/5] remove account_test --- examples/basecoin/types/account_test.go | 36 ------------------------- 1 file changed, 36 deletions(-) delete mode 100644 examples/basecoin/types/account_test.go diff --git a/examples/basecoin/types/account_test.go b/examples/basecoin/types/account_test.go deleted file mode 100644 index 2d03999c24d7..000000000000 --- a/examples/basecoin/types/account_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package types - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - sdk "github.com/cosmos/cosmos-sdk/types" - wire "github.com/cosmos/cosmos-sdk/wire" - "github.com/cosmos/cosmos-sdk/x/auth" -) - -func TestAppAccount(t *testing.T) { - cdc := wire.NewCodec() - addr := sdk.Address([]byte("address")) - acc := &AppAccount{ - BaseAccount: auth.BaseAccount{ - Address: addr, - Coins: sdk.Coins{}, - }, - Name: "", - } - - bz, err := cdc.MarshalBinary(acc) - assert.Nil(t, err) - - decode := GetAccountDecoder(cdc) - res, err := decode(bz) - assert.Nil(t, err) - assert.Equal(t, acc, res) - - name := t.Name() - acc.SetName(name) - accname := acc.GetName() - assert.Equal(t, name, accname) -}