1
1
package keeper_test
2
2
3
3
import (
4
+ "fmt"
5
+
4
6
sdk "github.com/cosmos/cosmos-sdk/types"
5
7
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
6
8
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
+
7
13
ibctesting "github.com/cosmos/ibc-go/testing"
8
14
)
9
15
@@ -74,13 +80,15 @@ func (suite *KeeperTestSuite) TestTrySendTx() {
74
80
suite .Run (tc .name , func () {
75
81
suite .SetupTest () // reset
76
82
path = NewICAPath (suite .chainA , suite .chainB )
77
- owner := TestOwnerAddress
78
83
suite .coordinator .SetupConnections (path )
79
84
80
- err := suite .SetupICAPath (path , owner )
85
+ err := suite .SetupICAPath (path , TestOwnerAddress )
81
86
suite .Require ().NoError (err )
87
+
82
88
portID = path .EndpointA .ChannelConfig .PortID
89
+
83
90
tc .malleate ()
91
+
84
92
_ , err = suite .chainA .GetSimApp ().ICAKeeper .TrySendTx (suite .chainA .GetContext (), portID , msg )
85
93
86
94
if tc .expPass {
@@ -91,3 +99,129 @@ func (suite *KeeperTestSuite) TestTrySendTx() {
91
99
})
92
100
}
93
101
}
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