Skip to content

Commit a44c402

Browse files
seantkingdamiannolan
authored andcommitted
test: adding further test cases for onRecvPacket
1 parent 22a18e3 commit a44c402

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
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: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ func (suite *KeeperTestSuite) TestTrySendTx() {
8080
suite.Run(tc.name, func() {
8181
suite.SetupTest() // reset
8282
path = NewICAPath(suite.chainA, suite.chainB)
83-
owner := TestOwnerAddress
8483
suite.coordinator.SetupConnections(path)
8584

86-
err := suite.SetupICAPath(path, owner)
85+
err := suite.SetupICAPath(path, TestOwnerAddress)
8786
suite.Require().NoError(err)
8887

8988
portID = path.EndpointA.ChannelConfig.PortID
89+
9090
tc.malleate()
9191

9292
_, err = suite.chainA.GetSimApp().ICAKeeper.TrySendTx(suite.chainA.GetContext(), portID, msg)
@@ -106,6 +106,7 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
106106
msg sdk.Msg
107107
txBytes []byte
108108
packetData []byte
109+
sourcePort string
109110
)
110111

111112
testCases := []struct {
@@ -119,6 +120,10 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
119120
amount, _ := sdk.ParseCoinsNormalized("100stake")
120121
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
121122
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+
122127
data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
123128
Data: txBytes}
124129
packetData = data.GetBytes()
@@ -146,18 +151,39 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
146151
}, false,
147152
},
148153
{
149-
"Wrong data type", func() {
154+
"Invalid packet type", func() {
150155
txBytes = []byte{}
156+
// Type here is an ENUM
157+
// Valid type is types.EXECUTE_TX
151158
data := types.IBCAccountPacketData{Type: 100,
152159
Data: txBytes}
153160
packetData = data.GetBytes()
154161
}, false,
155162
},
156163
{
157-
"Cannot unmarshal interchain account packet data", func() {
164+
"Cannot unmarshal interchain account packet data into types.IBCAccountPacketData", func() {
158165
packetData = []byte{}
159166
}, false,
160167
},
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+
},
161187
}
162188

163189
for _, tc := range testCases {
@@ -166,26 +192,27 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
166192
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
167193
suite.SetupTest() // reset
168194

169-
// setup interchain account
170-
owner := suite.chainA.SenderAccount.GetAddress().String()
171195
path = NewICAPath(suite.chainA, suite.chainB)
172196
suite.coordinator.SetupConnections(path)
173-
err := suite.SetupICAPath(path, owner)
197+
err := suite.SetupICAPath(path, TestOwnerAddress)
174198
suite.Require().NoError(err)
175199

176200
// send 100stake to interchain account wallet
177201
amount, _ := sdk.ParseCoinsNormalized("100stake")
178202
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
179203
bankMsg := &banktypes.MsgSend{FromAddress: suite.chainB.SenderAccount.GetAddress().String(), ToAddress: interchainAccountAddr, Amount: amount}
204+
180205
_, err = suite.chainB.SendMsgs(bankMsg)
206+
suite.Require().NoError(err)
181207

182-
txBytes, err = suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, msg)
183-
// Next we need to define the packet/data to pass into OnRecvPacket
184-
seq := uint64(1)
208+
// valid source port
209+
sourcePort = path.EndpointA.ChannelConfig.PortID
185210

211+
// malleate packetData for test cases
186212
tc.malleate()
187213

188-
packet := channeltypes.NewPacket(packetData, seq, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0)
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)
189216

190217
// Pass it in here
191218
err = suite.chainB.GetSimApp().ICAKeeper.OnRecvPacket(suite.chainB.GetContext(), packet)

0 commit comments

Comments
 (0)