forked from CosmWasm/wasmvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibc_test.go
339 lines (292 loc) · 10.8 KB
/
ibc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package cosmwasm
import (
"encoding/json"
"io/ioutil"
"testing"
"github.com/CosmWasm/wasmvm/api"
"github.com/CosmWasm/wasmvm/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const IBC_TEST_CONTRACT = "./api/testdata/ibc_reflect.wasm"
func TestIBC(t *testing.T) {
vm := withVM(t)
wasm, err := ioutil.ReadFile(IBC_TEST_CONTRACT)
require.NoError(t, err)
checksum, err := vm.Create(wasm)
require.NoError(t, err)
code, err := vm.GetCode(checksum)
require.NoError(t, err)
require.Equal(t, WasmCode(wasm), code)
}
// IBCInstantiateMsg is the Go version of
// https://github.com/CosmWasm/cosmwasm/blob/v0.14.0-beta1/contracts/ibc-reflect/src/msg.rs#L9-L11
type IBCInstantiateMsg struct {
ReflectCodeID uint64 `json:"reflect_code_id"`
}
// IBCExecuteMsg is the Go version of
// https://github.com/CosmWasm/cosmwasm/blob/v0.14.0-beta1/contracts/ibc-reflect/src/msg.rs#L15
type IBCExecuteMsg struct {
InitCallback InitCallback `json:"init_callback"`
}
// InitCallback is the Go version of
// https://github.com/CosmWasm/cosmwasm/blob/v0.14.0-beta1/contracts/ibc-reflect/src/msg.rs#L17-L22
type InitCallback struct {
ID string `json:"id"`
ContractAddr string `json:"contract_addr"`
}
type IBCPacketMsg struct {
Dispatch *DispatchMsg `json:"dispatch,omitempty"`
}
type DispatchMsg struct {
Msgs []types.CosmosMsg `json:"msgs"`
}
type IBCQueryMsg struct {
ListAccounts *struct{} `json:"list_accounts,omitempty"`
}
type ListAccountsResponse struct {
Accounts []AccountInfo `json:"accounts"`
}
type AccountInfo struct {
Account string `json:"account"`
ChannelID string `json:"channel_id"`
}
// We just check if an error is returned or not
type AcknowledgeDispatch struct {
Err string `json:"error"`
}
func toBytes(t *testing.T, v interface{}) []byte {
bz, err := json.Marshal(v)
require.NoError(t, err)
return bz
}
const IBC_VERSION = "ibc-reflect-v1"
func TestIBCHandshake(t *testing.T) {
// code id of the reflect contract
const REFLECT_ID uint64 = 101
// channel id for handshake
const CHANNEL_ID = "channel-432"
vm := withVM(t)
checksum := createTestContract(t, vm, IBC_TEST_CONTRACT)
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
deserCost := types.UFraction{1, 1}
// instantiate it with this store
store := api.NewLookup(gasMeter1)
goapi := api.NewMockAPI()
balance := types.Coins{}
querier := api.DefaultQuerier(api.MOCK_CONTRACT_ADDR, balance)
// instantiate
env := api.MockEnv()
info := api.MockInfo("creator", nil)
init_msg := IBCInstantiateMsg{
ReflectCodeID: REFLECT_ID,
}
ires, _, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))
// channel open
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter2)
env = api.MockEnv()
openMsg := api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, IBC_VERSION)
ores, _, err := vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, &types.IBC3ChannelOpenResponse{Version: "ibc-reflect-v1"}, ores)
// channel connect
gasMeter3 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter3)
env = api.MockEnv()
// completes and dispatches message to create reflect contract
connectMsg := api.MockIBCChannelConnectAck(CHANNEL_ID, types.Ordered, IBC_VERSION)
res, _, err := vm.IBCChannelConnect(checksum, env, connectMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 1, len(res.Messages))
// check for the expected custom event
expected_events := []types.Event{{
Type: "ibc",
Attributes: []types.EventAttribute{{
Key: "channel",
Value: "connect",
}},
}}
require.Equal(t, expected_events, res.Events)
// make sure it read the balance properly and we got 250 atoms
dispatch := res.Messages[0].Msg
require.NotNil(t, dispatch.Wasm, "%#v", dispatch)
require.NotNil(t, dispatch.Wasm.Instantiate, "%#v", dispatch)
init := dispatch.Wasm.Instantiate
assert.Equal(t, REFLECT_ID, init.CodeID)
assert.Empty(t, init.Funds)
}
func TestIBCPacketDispatch(t *testing.T) {
// code id of the reflect contract
const REFLECT_ID uint64 = 77
// address of first reflect contract instance that we created
const REFLECT_ADDR = "reflect-acct-1"
// channel id for handshake
const CHANNEL_ID = "channel-234"
// setup
vm := withVM(t)
checksum := createTestContract(t, vm, IBC_TEST_CONTRACT)
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
deserCost := types.UFraction{1, 1}
// instantiate it with this store
store := api.NewLookup(gasMeter1)
goapi := api.NewMockAPI()
balance := types.Coins{}
querier := api.DefaultQuerier(api.MOCK_CONTRACT_ADDR, balance)
// instantiate
env := api.MockEnv()
info := api.MockInfo("creator", nil)
initMsg := IBCInstantiateMsg{
ReflectCodeID: REFLECT_ID,
}
_, _, err := vm.Instantiate(checksum, env, info, toBytes(t, initMsg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
// channel open
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter2)
openMsg := api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, IBC_VERSION)
ores, _, err := vm.IBCChannelOpen(checksum, env, openMsg, store, *goapi, querier, gasMeter2, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, &types.IBC3ChannelOpenResponse{Version: "ibc-reflect-v1"}, ores)
// channel connect
gasMeter3 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter3)
// completes and dispatches message to create reflect contract
connectMsg := api.MockIBCChannelConnectAck(CHANNEL_ID, types.Ordered, IBC_VERSION)
res, _, err := vm.IBCChannelConnect(checksum, env, connectMsg, store, *goapi, querier, gasMeter3, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 1, len(res.Messages))
id := res.Messages[0].ID
// mock reflect init callback (to store address)
gasMeter4 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter4)
reply := types.Reply{
ID: id,
Result: types.SubMsgResult{
Ok: &types.SubMsgResponse{
Events: types.Events{{
Type: "instantiate",
Attributes: types.EventAttributes{
{
Key: "_contract_address",
Value: REFLECT_ADDR,
},
},
}},
Data: nil,
},
},
}
_, _, err = vm.Reply(checksum, env, reply, store, *goapi, querier, gasMeter4, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
// ensure the channel is registered
queryMsg := IBCQueryMsg{
ListAccounts: &struct{}{},
}
qres, _, err := vm.Query(checksum, env, toBytes(t, queryMsg), store, *goapi, querier, gasMeter4, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
var accounts ListAccountsResponse
err = json.Unmarshal(qres, &accounts)
require.Equal(t, 1, len(accounts.Accounts))
require.Equal(t, CHANNEL_ID, accounts.Accounts[0].ChannelID)
require.Equal(t, REFLECT_ADDR, accounts.Accounts[0].Account)
// process message received on this channel
gasMeter5 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter5)
ibcMsg := IBCPacketMsg{
Dispatch: &DispatchMsg{
Msgs: []types.CosmosMsg{{
Bank: &types.BankMsg{Send: &types.SendMsg{
ToAddress: "my-friend",
Amount: types.Coins{types.NewCoin(12345678, "uatom")},
}},
}},
},
}
msg := api.MockIBCPacketReceive(CHANNEL_ID, toBytes(t, ibcMsg))
pr, _, err := vm.IBCPacketReceive(checksum, env, msg, store, *goapi, querier, gasMeter5, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
assert.NotNil(t, pr.Ok)
pres := pr.Ok
// assert app-level success
var ack AcknowledgeDispatch
err = json.Unmarshal(pres.Acknowledgement, &ack)
require.Empty(t, ack.Err)
// error on message from another channel
msg2 := api.MockIBCPacketReceive("no-such-channel", toBytes(t, ibcMsg))
pr2, _, err := vm.IBCPacketReceive(checksum, env, msg2, store, *goapi, querier, gasMeter5, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
assert.NotNil(t, pr.Ok)
pres2 := pr2.Ok
// assert app-level failure
var ack2 AcknowledgeDispatch
err = json.Unmarshal(pres2.Acknowledgement, &ack2)
require.Equal(t, "invalid packet: cosmwasm_std::addresses::Addr not found", ack2.Err)
// check for the expected custom event
expected_events := []types.Event{{
Type: "ibc",
Attributes: []types.EventAttribute{{
Key: "packet",
Value: "receive",
}},
}}
require.Equal(t, expected_events, pres2.Events)
}
func TestAnalyzeCode(t *testing.T) {
vm := withVM(t)
// Store non-IBC contract
wasm, err := ioutil.ReadFile(HACKATOM_TEST_CONTRACT)
require.NoError(t, err)
checksum, err := vm.Create(wasm)
require.NoError(t, err)
// and analyze
report, err := vm.AnalyzeCode(checksum)
require.NoError(t, err)
require.False(t, report.HasIBCEntryPoints)
require.Equal(t, "", report.RequiredFeatures)
// Store IBC contract
wasm2, err := ioutil.ReadFile(IBC_TEST_CONTRACT)
require.NoError(t, err)
checksum2, err := vm.Create(wasm2)
require.NoError(t, err)
// and analyze
report2, err := vm.AnalyzeCode(checksum2)
require.NoError(t, err)
require.True(t, report2.HasIBCEntryPoints)
require.Equal(t, "iterator,stargate", report2.RequiredFeatures)
}
func TestIBCMsgGetChannel(t *testing.T) {
const CHANNEL_ID = "channel-432"
msg1 := api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, "random-garbage")
msg2 := api.MockIBCChannelOpenTry(CHANNEL_ID, types.Ordered, "random-garbage")
msg3 := api.MockIBCChannelConnectAck(CHANNEL_ID, types.Ordered, "random-garbage")
msg4 := api.MockIBCChannelConnectConfirm(CHANNEL_ID, types.Ordered, "random-garbage")
msg5 := api.MockIBCChannelCloseInit(CHANNEL_ID, types.Ordered, "random-garbage")
msg6 := api.MockIBCChannelCloseConfirm(CHANNEL_ID, types.Ordered, "random-garbage")
require.Equal(t, msg1.GetChannel(), msg2.GetChannel())
require.Equal(t, msg1.GetChannel(), msg3.GetChannel())
require.Equal(t, msg1.GetChannel(), msg4.GetChannel())
require.Equal(t, msg1.GetChannel(), msg5.GetChannel())
require.Equal(t, msg1.GetChannel(), msg6.GetChannel())
require.Equal(t, msg1.GetChannel().Endpoint.ChannelID, CHANNEL_ID)
}
func TestIBCMsgGetCounterVersion(t *testing.T) {
const CHANNEL_ID = "channel-432"
const VERSION = "random-garbage"
msg1 := api.MockIBCChannelOpenInit(CHANNEL_ID, types.Ordered, VERSION)
v, ok := msg1.GetCounterVersion()
require.False(t, ok)
msg2 := api.MockIBCChannelOpenTry(CHANNEL_ID, types.Ordered, VERSION)
v, ok = msg2.GetCounterVersion()
require.True(t, ok)
require.Equal(t, VERSION, v)
msg3 := api.MockIBCChannelConnectAck(CHANNEL_ID, types.Ordered, VERSION)
v, ok = msg3.GetCounterVersion()
require.True(t, ok)
require.Equal(t, VERSION, v)
msg4 := api.MockIBCChannelConnectConfirm(CHANNEL_ID, types.Ordered, VERSION)
v, ok = msg4.GetCounterVersion()
require.False(t, ok)
}