Skip to content

Commit e8d0dde

Browse files
authored
ICA: Adding tests for relay.go (#337)
* 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
1 parent d37f2ec commit e8d0dde

File tree

6 files changed

+117
-4
lines changed

6 files changed

+117
-4
lines changed

modules/apps/27-interchain-accounts/keeper/handshake_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (suite *KeeperTestSuite) TestOnChanOpenConfirm() {
278278

279279
tc.malleate() // explicitly change fields in channel and testChannel
280280

281-
err = suite.chainB.GetSimApp().ICAKeeper.OnChanOpenConfirm(suite.chainA.GetContext(),
281+
err = suite.chainB.GetSimApp().ICAKeeper.OnChanOpenConfirm(suite.chainB.GetContext(),
282282
path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
283283

284284
if tc.expPass {

modules/apps/27-interchain-accounts/keeper/keeper_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,24 @@ func (suite *KeeperTestSuite) TestSetInterchainAccountAddress() {
127127
suite.Require().True(found)
128128
suite.Require().Equal(expectedAddr, retrievedAddr)
129129
}
130+
131+
func (suite *KeeperTestSuite) SetupICAPath(path *ibctesting.Path, owner string) error {
132+
if err := InitInterchainAccount(path.EndpointA, owner); err != nil {
133+
return err
134+
}
135+
136+
if err := path.EndpointB.ChanOpenTry(); err != nil {
137+
return err
138+
}
139+
140+
if err := path.EndpointA.ChanOpenAck(); err != nil {
141+
return err
142+
}
143+
144+
if err := suite.chainB.GetSimApp().ICAKeeper.OnChanOpenConfirm(suite.chainA.GetContext(),
145+
path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID); err != nil {
146+
return err
147+
}
148+
149+
return nil
150+
}

modules/apps/27-interchain-accounts/keeper/relay.go

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func (k Keeper) createOutgoingPacket(
4040
destinationChannel string,
4141
data interface{},
4242
) ([]byte, error) {
43-
4443
if data == nil {
4544
return []byte{}, types.ErrInvalidOutgoingData
4645
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package keeper_test
2+
3+
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
5+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
6+
7+
ibctesting "github.com/cosmos/ibc-go/testing"
8+
)
9+
10+
func (suite *KeeperTestSuite) TestTrySendTx() {
11+
var (
12+
path *ibctesting.Path
13+
msg interface{}
14+
portID string
15+
)
16+
17+
testCases := []struct {
18+
name string
19+
malleate func()
20+
expPass bool
21+
}{
22+
{
23+
"success", func() {
24+
amount, _ := sdk.ParseCoinsNormalized("100stake")
25+
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
26+
msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
27+
}, true,
28+
},
29+
{
30+
"success with []sdk.Message", func() {
31+
amount, _ := sdk.ParseCoinsNormalized("100stake")
32+
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
33+
msg1 := &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
34+
msg2 := &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
35+
msg = []sdk.Msg{msg1, msg2}
36+
}, true,
37+
},
38+
{
39+
"incorrect outgoing data", func() {
40+
msg = []byte{}
41+
}, false,
42+
},
43+
{
44+
"active channel not found", func() {
45+
amount, _ := sdk.ParseCoinsNormalized("100stake")
46+
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
47+
msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
48+
portID = "incorrect portID"
49+
}, false,
50+
},
51+
{
52+
"channel does not exist", func() {
53+
amount, _ := sdk.ParseCoinsNormalized("100stake")
54+
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
55+
msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
56+
suite.chainA.GetSimApp().ICAKeeper.SetActiveChannel(suite.chainA.GetContext(), portID, "channel-100")
57+
}, false,
58+
},
59+
{
60+
"data is nil", func() {
61+
msg = nil
62+
}, false,
63+
},
64+
{
65+
"data is not an SDK message", func() {
66+
msg = "not an sdk message"
67+
}, false,
68+
},
69+
}
70+
71+
for _, tc := range testCases {
72+
tc := tc
73+
74+
suite.Run(tc.name, func() {
75+
suite.SetupTest() // reset
76+
path = NewICAPath(suite.chainA, suite.chainB)
77+
owner := "owner"
78+
suite.coordinator.SetupConnections(path)
79+
80+
err := suite.SetupICAPath(path, owner)
81+
suite.Require().NoError(err)
82+
portID = path.EndpointA.ChannelConfig.PortID
83+
tc.malleate()
84+
_, err = suite.chainA.GetSimApp().ICAKeeper.TrySendTx(suite.chainA.GetContext(), portID, msg)
85+
86+
if tc.expPass {
87+
suite.Require().NoError(err)
88+
} else {
89+
suite.Require().Error(err)
90+
}
91+
})
92+
}
93+
}

modules/apps/27-interchain-accounts/types/keys.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package types
33
import "fmt"
44

55
const (
6-
// ModuleName defines the IBC transfer name
6+
// ModuleName defines the Interchain Account module name
77
ModuleName = "interchainaccounts"
88

99
// Version defines the current version the IBC tranfer

testing/app.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
8989
}
9090
validators = append(validators, validator)
9191
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))
92-
9392
}
93+
9494
// set validators and delegations
9595
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
9696
genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis)

0 commit comments

Comments
 (0)