Skip to content

Commit

Permalink
ICA: Adding tests for relay.go (#337)
Browse files Browse the repository at this point in the history
* test: adding test for TrySendTx

* test: adding tests for data check

* test: adding check for SendTx with []sdk.Message

* chore: seperate imports

* test: add helper function for creating ICA path

* test: adding cases for incorrect outgoing data & channel does not exist
  • Loading branch information
seantking authored Sep 13, 2021
1 parent d37f2ec commit e8d0dde
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions modules/apps/27-interchain-accounts/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion modules/apps/27-interchain-accounts/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (k Keeper) createOutgoingPacket(
destinationChannel string,
data interface{},
) ([]byte, error) {

if data == nil {
return []byte{}, types.ErrInvalidOutgoingData
}
Expand Down
93 changes: 93 additions & 0 deletions modules/apps/27-interchain-accounts/keeper/relay_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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}
}, 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}
}, true,
},
{
"incorrect outgoing data", func() {
msg = []byte{}
}, false,
},
{
"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,
},
{
"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
}, false,
},
{
"data is not an SDK message", func() {
msg = "not an sdk message"
}, 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 := 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)

if tc.expPass {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
}
})
}
}
2 changes: 1 addition & 1 deletion modules/apps/27-interchain-accounts/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion testing/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e8d0dde

Please sign in to comment.