From 47deeceea2032fced90390f14aaa88750f4dd0a3 Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 13 Aug 2021 17:55:16 +0200 Subject: [PATCH 1/6] test: adding test for TrySendTx --- .../keeper/handshake_test.go | 2 +- .../keeper/relay_test.go | 71 +++++++++++++++++++ .../apps/27-interchain-accounts/types/keys.go | 2 +- testing/app.go | 2 +- 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 modules/apps/27-interchain-accounts/keeper/relay_test.go diff --git a/modules/apps/27-interchain-accounts/keeper/handshake_test.go b/modules/apps/27-interchain-accounts/keeper/handshake_test.go index 279cc2109e2..35f09372f76 100644 --- a/modules/apps/27-interchain-accounts/keeper/handshake_test.go +++ b/modules/apps/27-interchain-accounts/keeper/handshake_test.go @@ -278,7 +278,7 @@ func (suite *KeeperTestSuite) TestOnChanOpenConfirm() { tc.malleate() // explicitly change fields in channel and testChannel - err = suite.chainB.GetSimApp().ICAKeeper.OnChanOpenConfirm(suite.chainA.GetContext(), + err = suite.chainB.GetSimApp().ICAKeeper.OnChanOpenConfirm(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) if tc.expPass { diff --git a/modules/apps/27-interchain-accounts/keeper/relay_test.go b/modules/apps/27-interchain-accounts/keeper/relay_test.go new file mode 100644 index 00000000000..e77dfc8e447 --- /dev/null +++ b/modules/apps/27-interchain-accounts/keeper/relay_test.go @@ -0,0 +1,71 @@ +package keeper_test + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + ibctesting "github.com/cosmos/ibc-go/testing" +) + +func (suite *KeeperTestSuite) TestTrySendTx() { + var ( + path *ibctesting.Path + msg interface{} + portID string + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", func() { + amount, _ := sdk.ParseCoinsNormalized("100stake") + interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount} + portID = path.EndpointA.ChannelConfig.PortID + }, true, + }, + { + "active channel not found", func() { + amount, _ := sdk.ParseCoinsNormalized("100stake") + interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount} + portID = "incorrect portID" + }, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + path = NewICAPath(suite.chainA, suite.chainB) + owner := "owner" + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, owner) + suite.Require().NoError(err) + + err = path.EndpointB.ChanOpenTry() + suite.Require().NoError(err) + + err = path.EndpointA.ChanOpenAck() + suite.Require().NoError(err) + + err = suite.chainB.GetSimApp().ICAKeeper.OnChanOpenConfirm(suite.chainA.GetContext(), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + suite.Require().NoError(err) + + tc.malleate() + _, err = suite.chainA.GetSimApp().ICAKeeper.TrySendTx(suite.chainA.GetContext(), portID, msg) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} diff --git a/modules/apps/27-interchain-accounts/types/keys.go b/modules/apps/27-interchain-accounts/types/keys.go index 321bfcee458..909813ec46d 100644 --- a/modules/apps/27-interchain-accounts/types/keys.go +++ b/modules/apps/27-interchain-accounts/types/keys.go @@ -3,7 +3,7 @@ package types import "fmt" const ( - // ModuleName defines the IBC transfer name + // ModuleName defines the Interchain Account module name ModuleName = "interchainaccounts" // Version defines the current version the IBC tranfer diff --git a/testing/app.go b/testing/app.go index f14178b9d53..a50d87d7868 100644 --- a/testing/app.go +++ b/testing/app.go @@ -89,8 +89,8 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs } validators = append(validators, validator) delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) - } + // set validators and delegations stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) From 3f3ae8bf4c5198c8f24e45aa8a328403dc800c75 Mon Sep 17 00:00:00 2001 From: Sean King Date: Mon, 16 Aug 2021 13:43:30 +0200 Subject: [PATCH 2/6] test: adding tests for data check --- .../apps/27-interchain-accounts/keeper/relay_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules/apps/27-interchain-accounts/keeper/relay_test.go b/modules/apps/27-interchain-accounts/keeper/relay_test.go index e77dfc8e447..7d922c159b7 100644 --- a/modules/apps/27-interchain-accounts/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/keeper/relay_test.go @@ -34,6 +34,18 @@ func (suite *KeeperTestSuite) TestTrySendTx() { portID = "incorrect portID" }, false, }, + { + "data is nil", func() { + msg = nil + portID = path.EndpointA.ChannelConfig.PortID + }, false, + }, + { + "data is not an SDK message", func() { + msg = "not an sdk message" + portID = path.EndpointA.ChannelConfig.PortID + }, false, + }, } for _, tc := range testCases { From b5c9ed9911c8b1ee1fbb3e7639159239e814decb Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 10 Sep 2021 17:18:40 +0200 Subject: [PATCH 3/6] test: adding check for SendTx with []sdk.Message --- .../apps/27-interchain-accounts/keeper/relay_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/modules/apps/27-interchain-accounts/keeper/relay_test.go b/modules/apps/27-interchain-accounts/keeper/relay_test.go index 7d922c159b7..89808696c4c 100644 --- a/modules/apps/27-interchain-accounts/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/keeper/relay_test.go @@ -26,6 +26,16 @@ func (suite *KeeperTestSuite) TestTrySendTx() { portID = path.EndpointA.ChannelConfig.PortID }, true, }, + { + "success with []sdk.Message", func() { + amount, _ := sdk.ParseCoinsNormalized("100stake") + interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + msg1 := &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount} + msg2 := &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount} + msg = []sdk.Msg{msg1, msg2} + portID = path.EndpointA.ChannelConfig.PortID + }, true, + }, { "active channel not found", func() { amount, _ := sdk.ParseCoinsNormalized("100stake") From 931ecb769c4a9d2fd44e3137c138ceb8ffbaa53b Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 10 Sep 2021 17:24:03 +0200 Subject: [PATCH 4/6] chore: seperate imports --- modules/apps/27-interchain-accounts/keeper/relay.go | 1 - modules/apps/27-interchain-accounts/keeper/relay_test.go | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/keeper/relay.go b/modules/apps/27-interchain-accounts/keeper/relay.go index 5aa0ddda8be..baf6009b2e1 100644 --- a/modules/apps/27-interchain-accounts/keeper/relay.go +++ b/modules/apps/27-interchain-accounts/keeper/relay.go @@ -40,7 +40,6 @@ func (k Keeper) createOutgoingPacket( destinationChannel string, data interface{}, ) ([]byte, error) { - if data == nil { return []byte{}, types.ErrInvalidOutgoingData } diff --git a/modules/apps/27-interchain-accounts/keeper/relay_test.go b/modules/apps/27-interchain-accounts/keeper/relay_test.go index 89808696c4c..bcd1c07c506 100644 --- a/modules/apps/27-interchain-accounts/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/keeper/relay_test.go @@ -3,6 +3,7 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + ibctesting "github.com/cosmos/ibc-go/testing" ) From 862ec0814902984c486355128c363b2a2ae48170 Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 10 Sep 2021 17:31:51 +0200 Subject: [PATCH 5/6] test: add helper function for creating ICA path --- .../keeper/keeper_test.go | 21 +++++++++++++++++++ .../keeper/relay_test.go | 12 +---------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/modules/apps/27-interchain-accounts/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/keeper/keeper_test.go index d3cc61c8d6a..7064d6ebc07 100644 --- a/modules/apps/27-interchain-accounts/keeper/keeper_test.go +++ b/modules/apps/27-interchain-accounts/keeper/keeper_test.go @@ -127,3 +127,24 @@ func (suite *KeeperTestSuite) TestSetInterchainAccountAddress() { suite.Require().True(found) suite.Require().Equal(expectedAddr, retrievedAddr) } + +func (suite *KeeperTestSuite) SetupICAPath(path *ibctesting.Path, owner string) error { + if err := InitInterchainAccount(path.EndpointA, owner); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenTry(); err != nil { + return err + } + + if err := path.EndpointA.ChanOpenAck(); err != nil { + return err + } + + if err := suite.chainB.GetSimApp().ICAKeeper.OnChanOpenConfirm(suite.chainA.GetContext(), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID); err != nil { + return err + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/keeper/relay_test.go b/modules/apps/27-interchain-accounts/keeper/relay_test.go index bcd1c07c506..31dad75f1fa 100644 --- a/modules/apps/27-interchain-accounts/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/keeper/relay_test.go @@ -68,17 +68,7 @@ func (suite *KeeperTestSuite) TestTrySendTx() { owner := "owner" suite.coordinator.SetupConnections(path) - err := InitInterchainAccount(path.EndpointA, owner) - suite.Require().NoError(err) - - err = path.EndpointB.ChanOpenTry() - suite.Require().NoError(err) - - err = path.EndpointA.ChanOpenAck() - suite.Require().NoError(err) - - err = suite.chainB.GetSimApp().ICAKeeper.OnChanOpenConfirm(suite.chainA.GetContext(), - path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + err := suite.SetupICAPath(path, owner) suite.Require().NoError(err) tc.malleate() From 1fccda122578a0a53f833bb109a2a556c872c489 Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 10 Sep 2021 17:44:54 +0200 Subject: [PATCH 6/6] test: adding cases for incorrect outgoing data & channel does not exist --- .../keeper/relay_test.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/apps/27-interchain-accounts/keeper/relay_test.go b/modules/apps/27-interchain-accounts/keeper/relay_test.go index 31dad75f1fa..66c3b22d51a 100644 --- a/modules/apps/27-interchain-accounts/keeper/relay_test.go +++ b/modules/apps/27-interchain-accounts/keeper/relay_test.go @@ -24,7 +24,6 @@ func (suite *KeeperTestSuite) TestTrySendTx() { amount, _ := sdk.ParseCoinsNormalized("100stake") interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount} - portID = path.EndpointA.ChannelConfig.PortID }, true, }, { @@ -34,9 +33,13 @@ func (suite *KeeperTestSuite) TestTrySendTx() { msg1 := &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount} msg2 := &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount} msg = []sdk.Msg{msg1, msg2} - portID = path.EndpointA.ChannelConfig.PortID }, true, }, + { + "incorrect outgoing data", func() { + msg = []byte{} + }, false, + }, { "active channel not found", func() { amount, _ := sdk.ParseCoinsNormalized("100stake") @@ -45,16 +48,22 @@ func (suite *KeeperTestSuite) TestTrySendTx() { portID = "incorrect portID" }, false, }, + { + "channel does not exist", func() { + amount, _ := sdk.ParseCoinsNormalized("100stake") + interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount} + suite.chainA.GetSimApp().ICAKeeper.SetActiveChannel(suite.chainA.GetContext(), portID, "channel-100") + }, false, + }, { "data is nil", func() { msg = nil - portID = path.EndpointA.ChannelConfig.PortID }, false, }, { "data is not an SDK message", func() { msg = "not an sdk message" - portID = path.EndpointA.ChannelConfig.PortID }, false, }, } @@ -70,7 +79,7 @@ func (suite *KeeperTestSuite) TestTrySendTx() { err := suite.SetupICAPath(path, owner) suite.Require().NoError(err) - + portID = path.EndpointA.ChannelConfig.PortID tc.malleate() _, err = suite.chainA.GetSimApp().ICAKeeper.TrySendTx(suite.chainA.GetContext(), portID, msg)