diff --git a/cmd/gaia/app/app_test.go b/cmd/gaia/app/app_test.go index 0fe1cfe5b9..1ec24d0179 100644 --- a/cmd/gaia/app/app_test.go +++ b/cmd/gaia/app/app_test.go @@ -12,7 +12,6 @@ import ( "github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" - "github.com/cosmos/cosmos-sdk/x/ibc" "github.com/cosmos/cosmos-sdk/x/stake" abci "github.com/tendermint/abci/types" @@ -46,43 +45,6 @@ var ( Inputs: []bank.Input{bank.NewInput(addr1, coins)}, Outputs: []bank.Output{bank.NewOutput(addr2, coins)}, } - - sendMsg2 = bank.MsgSend{ - Inputs: []bank.Input{bank.NewInput(addr1, coins)}, - Outputs: []bank.Output{ - bank.NewOutput(addr2, halfCoins), - bank.NewOutput(addr3, halfCoins), - }, - } - - sendMsg3 = bank.MsgSend{ - Inputs: []bank.Input{ - bank.NewInput(addr1, coins), - bank.NewInput(addr4, coins), - }, - Outputs: []bank.Output{ - bank.NewOutput(addr2, coins), - bank.NewOutput(addr3, coins), - }, - } - - sendMsg4 = bank.MsgSend{ - Inputs: []bank.Input{ - bank.NewInput(addr2, coins), - }, - Outputs: []bank.Output{ - bank.NewOutput(addr1, coins), - }, - } - - sendMsg5 = bank.MsgSend{ - Inputs: []bank.Input{ - bank.NewInput(addr1, manyCoins), - }, - Outputs: []bank.Output{ - bank.NewOutput(addr2, manyCoins), - }, - } ) func loggerAndDB() (log.Logger, dbm.DB) { @@ -167,181 +129,6 @@ func TestGenesis(t *testing.T) { assert.Equal(t, baseAcc, res1) } -func TestMsgSendWithAccounts(t *testing.T) { - gapp := newGaiaApp() - - // Construct some genesis bytes to reflect GaiaAccount - // Give 77 foocoin to the first key - coins, err := sdk.ParseCoins("77foocoin") - require.Nil(t, err) - baseAcc := &auth.BaseAccount{ - Address: addr1, - Coins: coins, - } - - // Construct genesis state - err = setGenesis(gapp, baseAcc) - require.Nil(t, err) - - // A checkTx context (true) - ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{}) - res1 := gapp.accountMapper.GetAccount(ctxCheck, addr1) - assert.Equal(t, baseAcc, res1.(*auth.BaseAccount)) - - // Run a CheckDeliver - SignCheckDeliver(t, gapp, sendMsg1, []int64{0}, true, priv1) - - // Check balances - CheckBalance(t, gapp, addr1, "67foocoin") - CheckBalance(t, gapp, addr2, "10foocoin") - - // Delivering again should cause replay error - SignCheckDeliver(t, gapp, sendMsg1, []int64{0}, false, priv1) - - // bumping the txnonce number without resigning should be an auth error - tx := genTx(sendMsg1, []int64{0}, priv1) - tx.Signatures[0].Sequence = 1 - res := gapp.Deliver(tx) - - assert.Equal(t, sdk.ToABCICode(sdk.CodespaceRoot, sdk.CodeUnauthorized), res.Code, res.Log) - - // resigning the tx with the bumped sequence should work - SignCheckDeliver(t, gapp, sendMsg1, []int64{1}, true, priv1) -} - -func TestMsgSendMultipleOut(t *testing.T) { - gapp := newGaiaApp() - - genCoins, err := sdk.ParseCoins("42foocoin") - require.Nil(t, err) - - acc1 := &auth.BaseAccount{ - Address: addr1, - Coins: genCoins, - } - - acc2 := &auth.BaseAccount{ - Address: addr2, - Coins: genCoins, - } - - err = setGenesis(gapp, acc1, acc2) - require.Nil(t, err) - - // Simulate a Block - SignCheckDeliver(t, gapp, sendMsg2, []int64{0}, true, priv1) - - // Check balances - CheckBalance(t, gapp, addr1, "32foocoin") - CheckBalance(t, gapp, addr2, "47foocoin") - CheckBalance(t, gapp, addr3, "5foocoin") -} - -func TestSengMsgMultipleInOut(t *testing.T) { - gapp := newGaiaApp() - - 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 = setGenesis(gapp, acc1, acc2, acc4) - assert.Nil(t, err) - - // CheckDeliver - SignCheckDeliver(t, gapp, sendMsg3, []int64{0, 0}, true, priv1, priv4) - - // Check balances - CheckBalance(t, gapp, addr1, "32foocoin") - CheckBalance(t, gapp, addr4, "32foocoin") - CheckBalance(t, gapp, addr2, "52foocoin") - CheckBalance(t, gapp, addr3, "10foocoin") -} - -func TestMsgSendDependent(t *testing.T) { - gapp := newGaiaApp() - - genCoins, err := sdk.ParseCoins("42foocoin") - require.Nil(t, err) - - acc1 := &auth.BaseAccount{ - Address: addr1, - Coins: genCoins, - } - - err = setGenesis(gapp, acc1) - require.Nil(t, err) - - // CheckDeliver - SignCheckDeliver(t, gapp, sendMsg1, []int64{0}, true, priv1) - - // Check balances - CheckBalance(t, gapp, addr1, "32foocoin") - CheckBalance(t, gapp, addr2, "10foocoin") - - // Simulate a Block - SignCheckDeliver(t, gapp, sendMsg4, []int64{0}, true, priv2) - - // Check balances - CheckBalance(t, gapp, addr1, "42foocoin") -} - -func TestIBCMsgs(t *testing.T) { - gapp := newGaiaApp() - - sourceChain := "source-chain" - destChain := "dest-chain" - - baseAcc := &auth.BaseAccount{ - Address: addr1, - Coins: coins, - } - - err := setGenesis(gapp, baseAcc) - require.Nil(t, err) - - // A checkTx context (true) - ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{}) - res1 := gapp.accountMapper.GetAccount(ctxCheck, addr1) - assert.Equal(t, baseAcc, res1) - - packet := ibc.IBCPacket{ - SrcAddr: addr1, - DestAddr: addr1, - Coins: coins, - SrcChain: sourceChain, - DestChain: destChain, - } - - transferMsg := ibc.IBCTransferMsg{ - IBCPacket: packet, - } - - receiveMsg := ibc.IBCReceiveMsg{ - IBCPacket: packet, - Relayer: addr1, - Sequence: 0, - } - - SignCheckDeliver(t, gapp, transferMsg, []int64{0}, true, priv1) - CheckBalance(t, gapp, addr1, "") - SignCheckDeliver(t, gapp, transferMsg, []int64{1}, false, priv1) - SignCheckDeliver(t, gapp, receiveMsg, []int64{2}, true, priv1) - CheckBalance(t, gapp, addr1, "10foocoin") - SignCheckDeliver(t, gapp, receiveMsg, []int64{3}, false, priv1) -} - func TestStakeMsgs(t *testing.T) { gapp := newGaiaApp() diff --git a/tests/mock/app.go b/tests/mock/app.go index f7b7a0a34c..6a39330055 100644 --- a/tests/mock/app.go +++ b/tests/mock/app.go @@ -39,6 +39,7 @@ func NewApp() *App { cdc := wire.NewCodec() sdk.RegisterWire(cdc) wire.RegisterCrypto(cdc) + auth.RegisterWire(cdc) // create your application object app := &App{ @@ -64,7 +65,7 @@ func NewApp() *App { } // complete the application setup after the routes have been registered -func (app App) CompleteSetup(t *testing.T, newKeys []*sdk.KVStoreKey) { +func (app *App) CompleteSetup(t *testing.T, newKeys []*sdk.KVStoreKey) { newKeys = append(newKeys, app.KeyMain) newKeys = append(newKeys, app.KeyAccount) @@ -74,7 +75,7 @@ func (app App) CompleteSetup(t *testing.T, newKeys []*sdk.KVStoreKey) { } // custom logic for initialization -func (app App) initChainer(ctx sdk.Context, _ abci.RequestInitChain) abci.ResponseInitChain { +func (app *App) initChainer(ctx sdk.Context, _ abci.RequestInitChain) abci.ResponseInitChain { // load the accounts for _, acc := range app.GenesisAccounts { diff --git a/x/bank/app_test.go b/x/bank/app_test.go index 3dcf37c220..ecbb661455 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -18,8 +18,6 @@ import ( var ( chainID = "" // TODO - accName = "foobart" - priv1 = crypto.GenPrivKeyEd25519() addr1 = priv1.PubKey().Address() priv2 = crypto.GenPrivKeyEd25519() @@ -30,7 +28,8 @@ var ( coins = sdk.Coins{{"foocoin", 10}} halfCoins = sdk.Coins{{"foocoin", 5}} manyCoins = sdk.Coins{{"foocoin", 1}, {"barcoin", 1}} - fee = auth.StdFee{ + + freeFee = auth.StdFee{ // no fees for a buncha gas sdk.Coins{{"foocoin", 0}}, 100000, } @@ -39,11 +38,47 @@ var ( Inputs: []Input{NewInput(addr1, coins)}, Outputs: []Output{NewOutput(addr2, coins)}, } + + sendMsg2 = MsgSend{ + Inputs: []Input{NewInput(addr1, coins)}, + Outputs: []Output{ + NewOutput(addr2, halfCoins), + NewOutput(addr3, halfCoins), + }, + } + + sendMsg3 = MsgSend{ + Inputs: []Input{ + NewInput(addr1, coins), + NewInput(addr4, coins), + }, + Outputs: []Output{ + NewOutput(addr2, coins), + NewOutput(addr3, coins), + }, + } + + sendMsg4 = MsgSend{ + Inputs: []Input{ + NewInput(addr2, coins), + }, + Outputs: []Output{ + NewOutput(addr1, coins), + }, + } + + sendMsg5 = MsgSend{ + Inputs: []Input{ + NewInput(addr1, manyCoins), + }, + Outputs: []Output{ + NewOutput(addr2, manyCoins), + }, + } ) -func TestMsgSendWithAccounts(t *testing.T) { - - // initialize the mock application +// initialize the mock application for this module +func getMockApp(t *testing.T) *mock.App { mapp := mock.NewApp() RegisterWire(mapp.Cdc) @@ -51,34 +86,33 @@ func TestMsgSendWithAccounts(t *testing.T) { mapp.Router().AddRoute("bank", NewHandler(coinKeeper)) mapp.CompleteSetup(t, []*sdk.KVStoreKey{}) + return mapp +} + +func TestMsgSendWithAccounts(t *testing.T) { + mapp := getMockApp(t) // Add an account at genesis - coins, err := sdk.ParseCoins("77foocoin") - require.Nil(t, err) - //acc := auth.NewAccountWithAddress(addr1) - //acc.SetCoins(coins) - //accs := []auth.Account{acc} - - baseAcc := &auth.BaseAccount{ + acc := &auth.BaseAccount{ Address: addr1, - Coins: coins, + Coins: sdk.Coins{{"foocoin", 67}}, } - baseAccs := []auth.Account{baseAcc} + accs := []auth.Account{acc} // Construct genesis state - mock.SetGenesis(mapp, baseAccs) + mock.SetGenesis(mapp, accs) // A checkTx context (true) ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{}) res1 := mapp.AccountMapper.GetAccount(ctxCheck, addr1) require.NotNil(t, res1) - assert.Equal(t, baseAcc, res1.(*auth.BaseAccount)) + assert.Equal(t, acc, res1.(*auth.BaseAccount)) // Run a CheckDeliver mock.SignCheckDeliver(t, mapp, sendMsg1, []int64{0}, true, priv1) // Check balances - mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 67}}) + mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 57}}) mock.CheckBalance(t, mapp, addr2, sdk.Coins{{"foocoin", 10}}) // Delivering again should cause replay error @@ -94,3 +128,82 @@ func TestMsgSendWithAccounts(t *testing.T) { // resigning the tx with the bumped sequence should work mock.SignCheckDeliver(t, mapp, sendMsg1, []int64{1}, true, priv1) } + +func TestMsgSendMultipleOut(t *testing.T) { + mapp := getMockApp(t) + + acc1 := &auth.BaseAccount{ + Address: addr1, + Coins: sdk.Coins{{"foocoin", 42}}, + } + + acc2 := &auth.BaseAccount{ + Address: addr2, + Coins: sdk.Coins{{"foocoin", 42}}, + } + accs := []auth.Account{acc1, acc2} + + mock.SetGenesis(mapp, accs) + + // Simulate a Block + mock.SignCheckDeliver(t, mapp, sendMsg2, []int64{0}, true, priv1) + + // Check balances + mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 32}}) + mock.CheckBalance(t, mapp, addr2, sdk.Coins{{"foocoin", 47}}) + mock.CheckBalance(t, mapp, addr3, sdk.Coins{{"foocoin", 5}}) +} + +func TestSengMsgMultipleInOut(t *testing.T) { + mapp := getMockApp(t) + + acc1 := &auth.BaseAccount{ + Address: addr1, + Coins: sdk.Coins{{"foocoin", 42}}, + } + acc2 := &auth.BaseAccount{ + Address: addr2, + Coins: sdk.Coins{{"foocoin", 42}}, + } + acc4 := &auth.BaseAccount{ + Address: addr4, + Coins: sdk.Coins{{"foocoin", 42}}, + } + accs := []auth.Account{acc1, acc2, acc4} + + mock.SetGenesis(mapp, accs) + + // CheckDeliver + mock.SignCheckDeliver(t, mapp, sendMsg3, []int64{0, 0}, true, priv1, priv4) + + // Check balances + mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 32}}) + mock.CheckBalance(t, mapp, addr4, sdk.Coins{{"foocoin", 32}}) + mock.CheckBalance(t, mapp, addr2, sdk.Coins{{"foocoin", 52}}) + mock.CheckBalance(t, mapp, addr3, sdk.Coins{{"foocoin", 10}}) +} + +func TestMsgSendDependent(t *testing.T) { + mapp := getMockApp(t) + + acc1 := &auth.BaseAccount{ + Address: addr1, + Coins: sdk.Coins{{"foocoin", 42}}, + } + accs := []auth.Account{acc1} + + mock.SetGenesis(mapp, accs) + + // CheckDeliver + mock.SignCheckDeliver(t, mapp, sendMsg1, []int64{0}, true, priv1) + + // Check balances + mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 32}}) + mock.CheckBalance(t, mapp, addr2, sdk.Coins{{"foocoin", 10}}) + + // Simulate a Block + mock.SignCheckDeliver(t, mapp, sendMsg4, []int64{0}, true, priv2) + + // Check balances + mock.CheckBalance(t, mapp, addr1, sdk.Coins{{"foocoin", 42}}) +} diff --git a/x/ibc/app_test.go b/x/ibc/app_test.go new file mode 100644 index 0000000000..0124e417e5 --- /dev/null +++ b/x/ibc/app_test.go @@ -0,0 +1,79 @@ +package ibc + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/cosmos/cosmos-sdk/tests/mock" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + + abci "github.com/tendermint/abci/types" + crypto "github.com/tendermint/go-crypto" +) + +// initialize the mock application for this module +func getMockApp(t *testing.T) *mock.App { + mapp := mock.NewApp() + + RegisterWire(mapp.Cdc) + keyIBC := sdk.NewKVStoreKey("ibc") + ibcMapper := NewMapper(mapp.Cdc, keyIBC, mapp.RegisterCodespace(DefaultCodespace)) + coinKeeper := bank.NewKeeper(mapp.AccountMapper) + mapp.Router().AddRoute("ibc", NewHandler(ibcMapper, coinKeeper)) + + mapp.CompleteSetup(t, []*sdk.KVStoreKey{keyIBC}) + return mapp +} + +func TestIBCMsgs(t *testing.T) { + gapp := getMockApp(t) + + sourceChain := "source-chain" + destChain := "dest-chain" + + priv1 := crypto.GenPrivKeyEd25519() + addr1 := priv1.PubKey().Address() + coins := sdk.Coins{{"foocoin", 10}} + var emptyCoins sdk.Coins + + acc := &auth.BaseAccount{ + Address: addr1, + Coins: coins, + } + accs := []auth.Account{acc} + + mock.SetGenesis(gapp, accs) + + // A checkTx context (true) + ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{}) + res1 := gapp.AccountMapper.GetAccount(ctxCheck, addr1) + assert.Equal(t, acc, res1) + + packet := IBCPacket{ + SrcAddr: addr1, + DestAddr: addr1, + Coins: coins, + SrcChain: sourceChain, + DestChain: destChain, + } + + transferMsg := IBCTransferMsg{ + IBCPacket: packet, + } + + receiveMsg := IBCReceiveMsg{ + IBCPacket: packet, + Relayer: addr1, + Sequence: 0, + } + + mock.SignCheckDeliver(t, gapp, transferMsg, []int64{0}, true, priv1) + mock.CheckBalance(t, gapp, addr1, emptyCoins) + mock.SignCheckDeliver(t, gapp, transferMsg, []int64{1}, false, priv1) + mock.SignCheckDeliver(t, gapp, receiveMsg, []int64{2}, true, priv1) + mock.CheckBalance(t, gapp, addr1, coins) + mock.SignCheckDeliver(t, gapp, receiveMsg, []int64{3}, false, priv1) +}