Skip to content

Commit e64fb25

Browse files
authored
test: adding tests for OnRecvPacket (#412)
* test: adding tests for OnRecvPacket * test: adding further test cases for onRecvPacket
1 parent e7cf4d3 commit e64fb25

File tree

2 files changed

+137
-14
lines changed

2 files changed

+137
-14
lines changed

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,7 @@ func (k Keeper) createOutgoingPacket(
4444
return []byte{}, types.ErrInvalidOutgoingData
4545
}
4646

47-
var msgs []sdk.Msg
48-
49-
switch data := data.(type) {
50-
case []sdk.Msg:
51-
msgs = data
52-
case sdk.Msg:
53-
msgs = []sdk.Msg{data}
54-
default:
55-
return []byte{}, types.ErrInvalidOutgoingData
56-
}
57-
58-
txBytes, err := k.SerializeCosmosTx(k.cdc, msgs)
47+
txBytes, err := k.SerializeCosmosTx(k.cdc, data)
5948
if err != nil {
6049
return []byte{}, sdkerrors.Wrap(err, "invalid packet data or codec")
6150
}

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

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package keeper_test
22

33
import (
4+
"fmt"
5+
46
sdk "github.com/cosmos/cosmos-sdk/types"
57
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
68

9+
"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"
10+
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
11+
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
12+
713
ibctesting "github.com/cosmos/ibc-go/testing"
814
)
915

@@ -74,13 +80,15 @@ func (suite *KeeperTestSuite) TestTrySendTx() {
7480
suite.Run(tc.name, func() {
7581
suite.SetupTest() // reset
7682
path = NewICAPath(suite.chainA, suite.chainB)
77-
owner := TestOwnerAddress
7883
suite.coordinator.SetupConnections(path)
7984

80-
err := suite.SetupICAPath(path, owner)
85+
err := suite.SetupICAPath(path, TestOwnerAddress)
8186
suite.Require().NoError(err)
87+
8288
portID = path.EndpointA.ChannelConfig.PortID
89+
8390
tc.malleate()
91+
8492
_, err = suite.chainA.GetSimApp().ICAKeeper.TrySendTx(suite.chainA.GetContext(), portID, msg)
8593

8694
if tc.expPass {
@@ -91,3 +99,129 @@ func (suite *KeeperTestSuite) TestTrySendTx() {
9199
})
92100
}
93101
}
102+
103+
func (suite *KeeperTestSuite) TestOnRecvPacket() {
104+
var (
105+
path *ibctesting.Path
106+
msg sdk.Msg
107+
txBytes []byte
108+
packetData []byte
109+
sourcePort string
110+
)
111+
112+
testCases := []struct {
113+
msg string
114+
malleate func()
115+
expPass bool
116+
}{
117+
{
118+
"Interchain account successfully executes banktypes.MsgSend", func() {
119+
// build MsgSend
120+
amount, _ := sdk.ParseCoinsNormalized("100stake")
121+
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
122+
msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
123+
// build packet data
124+
txBytes, err := suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, msg)
125+
suite.Require().NoError(err)
126+
127+
data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
128+
Data: txBytes}
129+
packetData = data.GetBytes()
130+
}, true,
131+
},
132+
{
133+
"Cannot deserialize txBytes", func() {
134+
txBytes = []byte("invalid tx bytes")
135+
data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
136+
Data: txBytes}
137+
packetData = data.GetBytes()
138+
}, false,
139+
},
140+
{
141+
"Cannot deserialize txBytes: invalid IBCTxRaw", func() {
142+
txBody := []byte("invalid tx body")
143+
txRaw := &types.IBCTxRaw{
144+
BodyBytes: txBody,
145+
}
146+
147+
txBytes = suite.chainB.Codec.MustMarshal(txRaw)
148+
data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
149+
Data: txBytes}
150+
packetData = data.GetBytes()
151+
}, false,
152+
},
153+
{
154+
"Invalid packet type", func() {
155+
txBytes = []byte{}
156+
// Type here is an ENUM
157+
// Valid type is types.EXECUTE_TX
158+
data := types.IBCAccountPacketData{Type: 100,
159+
Data: txBytes}
160+
packetData = data.GetBytes()
161+
}, false,
162+
},
163+
{
164+
"Cannot unmarshal interchain account packet data into types.IBCAccountPacketData", func() {
165+
packetData = []byte{}
166+
}, false,
167+
},
168+
{
169+
"Unauthorised: Interchain account not found for given source portID", func() {
170+
sourcePort = "invalid-port-id"
171+
}, false,
172+
},
173+
{
174+
"Unauthorised: Signer of message is not the interchain account associated with sourcePortID", func() {
175+
// build MsgSend
176+
amount, _ := sdk.ParseCoinsNormalized("100stake")
177+
// Incorrect FromAddress
178+
msg = &banktypes.MsgSend{FromAddress: suite.chainB.SenderAccount.GetAddress().String(), ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
179+
// build packet data
180+
txBytes, err := suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, msg)
181+
suite.Require().NoError(err)
182+
data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
183+
Data: txBytes}
184+
packetData = data.GetBytes()
185+
}, false,
186+
},
187+
}
188+
189+
for _, tc := range testCases {
190+
tc := tc
191+
192+
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
193+
suite.SetupTest() // reset
194+
195+
path = NewICAPath(suite.chainA, suite.chainB)
196+
suite.coordinator.SetupConnections(path)
197+
err := suite.SetupICAPath(path, TestOwnerAddress)
198+
suite.Require().NoError(err)
199+
200+
// send 100stake to interchain account wallet
201+
amount, _ := sdk.ParseCoinsNormalized("100stake")
202+
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
203+
bankMsg := &banktypes.MsgSend{FromAddress: suite.chainB.SenderAccount.GetAddress().String(), ToAddress: interchainAccountAddr, Amount: amount}
204+
205+
_, err = suite.chainB.SendMsgs(bankMsg)
206+
suite.Require().NoError(err)
207+
208+
// valid source port
209+
sourcePort = path.EndpointA.ChannelConfig.PortID
210+
211+
// malleate packetData for test cases
212+
tc.malleate()
213+
214+
seq := uint64(1)
215+
packet := channeltypes.NewPacket(packetData, seq, sourcePort, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0)
216+
217+
// Pass it in here
218+
err = suite.chainB.GetSimApp().ICAKeeper.OnRecvPacket(suite.chainB.GetContext(), packet)
219+
220+
if tc.expPass {
221+
suite.Require().NoError(err)
222+
} else {
223+
suite.Require().Error(err)
224+
}
225+
})
226+
}
227+
}

0 commit comments

Comments
 (0)