-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathtest_ica_precompile.py
481 lines (449 loc) · 14.3 KB
/
test_ica_precompile.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import base64
import json
from enum import Enum
import pytest
from eth_utils import keccak
from pystarport import cluster
from web3.datastructures import AttributeDict
from .ibc_utils import (
Status,
funds_ica,
gen_query_balance_packet,
gen_send_msg,
get_next_channel,
prepare_network,
wait_for_check_channel_ready,
wait_for_check_tx,
wait_for_status_change,
)
from .utils import (
ADDRS,
CONTRACT_ABIS,
CONTRACTS,
KEYS,
deploy_contract,
eth_to_bech32,
send_transaction,
wait_for_fn,
)
pytestmark = pytest.mark.ica
CONTRACT = "0x0000000000000000000000000000000000000066"
connid = "connection-0"
no_timeout = 300000000000
denom = "basecro"
validator = "validator"
amt = 1000
class Ordering(Enum):
NONE = 0
UNORDERED = 1
ORDERED = 2
@pytest.fixture(scope="module")
def ibc(request, tmp_path_factory):
"prepare-network"
name = "ibc_rly_evm"
path = tmp_path_factory.mktemp(name)
yield from prepare_network(
path,
name,
incentivized=False,
connection_only=True,
relayer=cluster.Relayer.RLY.value,
need_relayer_caller=True,
)
def register_acc(
cli,
w3,
register,
query,
data,
channel_id,
order,
signer="signer2",
expect_status=1,
contract_addr=None,
):
addr = contract_addr if contract_addr else ADDRS[signer]
keys = KEYS[signer]
print(f"register ica account with {channel_id}")
tx = register(connid, "", order).build_transaction(data)
receipt = send_transaction(w3, tx, keys)
assert receipt.status == expect_status
owner = eth_to_bech32(addr)
if expect_status == 1:
wait_for_check_channel_ready(cli, connid, channel_id)
res = cli.ica_query_account(connid, owner)
ica_address = res["address"]
print("query ica account", ica_address)
res = query(connid, addr).call()
assert ica_address == res, res
return ica_address
def submit_msgs(
ibc,
func,
data,
ica_address,
add_delegate,
expected_seq,
event,
channel_id,
timeout=no_timeout,
amount=amt,
need_wait=True,
msg_num=2,
with_channel_id=True,
signer="signer2",
):
cli_host = ibc.chainmain.cosmos_cli()
cli_controller = ibc.cronos.cosmos_cli()
w3 = ibc.cronos.w3
to = cli_host.address(validator)
# generate msgs send to host chain
m = gen_send_msg(ica_address, to, denom, amount)
msgs = []
diff_amt = 0
for i in range(msg_num):
msgs.append(m)
diff_amt += amount
if add_delegate:
to = cli_host.address(validator, bech="val")
# generate msg delegate to host chain
amt1 = 100
msgs.append(
{
"@type": "/cosmos.staking.v1beta1.MsgDelegate",
"delegator_address": ica_address,
"validator_address": to,
"amount": {"denom": denom, "amount": f"{amt1}"},
}
)
diff_amt += amt1
generated_packet = cli_controller.ica_generate_packet_data(json.dumps(msgs))
num_txs = len(cli_host.query_all_txs(ica_address)["txs"])
str = base64.b64decode(generated_packet["data"])
# submit transaction on host chain on behalf of interchain account
if with_channel_id:
tx = func(connid, channel_id, str, timeout).build_transaction(data)
else:
tx = func(connid, str, timeout).build_transaction(data)
receipt = send_transaction(w3, tx, KEYS[signer])
assert receipt.status == 1
if timeout < no_timeout:
timeout_in_s = timeout / 1e9 + 1
print(f"wait for {timeout_in_s}s")
wait_for_check_tx(cli_host, ica_address, num_txs, timeout_in_s)
else:
logs = event.get_logs()
assert len(logs) > 0
assert logs[0].args == AttributeDict(
{
"packetSrcChannel": keccak(text=channel_id),
"seq": expected_seq,
}
)
if need_wait:
wait_for_check_tx(cli_host, ica_address, num_txs)
return str, diff_amt
@pytest.mark.parametrize("order", [Ordering.ORDERED.value, Ordering.UNORDERED.value])
def test_call(ibc, order):
signer = "signer2" if order == Ordering.ORDERED.value else "community"
cli_host = ibc.chainmain.cosmos_cli()
cli_controller = ibc.cronos.cosmos_cli()
w3 = ibc.cronos.w3
contract_info = json.loads(CONTRACT_ABIS["IICAModule"].read_text())
contract = w3.eth.contract(address=CONTRACT, abi=contract_info)
data = {"from": ADDRS[signer]}
channel_id = get_next_channel(cli_controller, connid)
ica_address = register_acc(
cli_controller,
w3,
contract.functions.registerAccount,
contract.functions.queryAccount,
data,
channel_id,
order,
signer=signer,
)
balance = funds_ica(cli_host, ica_address, signer=signer)
expected_seq = 1
_, diff = submit_msgs(
ibc,
contract.functions.submitMsgs,
data,
ica_address,
False,
expected_seq,
contract.events.SubmitMsgsResult,
channel_id,
with_channel_id=False,
signer=signer,
)
balance -= diff
assert cli_host.balance(ica_address, denom=denom) == balance
expected_seq += 1
_, diff = submit_msgs(
ibc,
contract.functions.submitMsgs,
data,
ica_address,
True,
expected_seq,
contract.events.SubmitMsgsResult,
channel_id,
with_channel_id=False,
signer=signer,
)
balance -= diff
assert cli_host.balance(ica_address, denom=denom) == balance
def wait_for_packet_log(start, event, channel_id, seq, status):
print("wait for log arrive", seq, status)
expected = AttributeDict(
{
"packetSrcChannel": keccak(text=channel_id),
"seq": seq,
"status": status,
}
)
def check_log():
logs = event.get_logs(fromBlock=start)
return len(logs) > 0 and logs[-1].args == expected
wait_for_fn("packet log", check_log)
@pytest.mark.parametrize("order", [Ordering.ORDERED.value, Ordering.UNORDERED.value])
def test_sc_call(ibc, order):
cli_host = ibc.chainmain.cosmos_cli()
cli_controller = ibc.cronos.cosmos_cli()
w3 = ibc.cronos.w3
contract_info = json.loads(CONTRACT_ABIS["IICAModule"].read_text())
contract = w3.eth.contract(address=CONTRACT, abi=contract_info)
jsonfile = CONTRACTS["TestICA"]
tcontract = deploy_contract(w3, jsonfile)
contract_addr = tcontract.address
signer = "signer2" if order == Ordering.ORDERED.value else "community"
addr = ADDRS[signer]
keys = KEYS[signer]
default_gas = 500000
data = {"from": addr, "gas": default_gas}
channel_id = get_next_channel(cli_controller, connid)
ica_address = register_acc(
cli_controller,
w3,
tcontract.functions.callRegister,
contract.functions.queryAccount,
data,
channel_id,
order,
signer=signer,
contract_addr=contract_addr,
)
assert tcontract.caller.getAccount() == addr
assert (
tcontract.functions.callQueryAccount(connid, contract_addr).call()
== ica_address
)
# register from another user should fail
signer1 = "signer1"
data = {"from": ADDRS[signer1], "gas": default_gas}
version = ""
tx = tcontract.functions.callRegister(
connid,
version,
order,
).build_transaction(data)
res = send_transaction(w3, tx, KEYS[signer1])
assert res.status == 0
assert tcontract.caller.getAccount() == addr
assert (
tcontract.functions.delegateQueryAccount(connid, contract_addr).call()
== ica_address
)
assert (
tcontract.functions.staticQueryAccount(connid, contract_addr).call()
== ica_address
)
# readonly call should fail
def register_ro(func):
tx = func(connid, version, order).build_transaction(data)
assert send_transaction(w3, tx, keys).status == 0
register_ro(tcontract.functions.delegateRegister)
register_ro(tcontract.functions.staticRegister)
# readonly call should fail
def submit_msgs_ro(func, str):
tx = func(connid, str, no_timeout).build_transaction(data)
assert send_transaction(w3, tx, keys).status == 0
expected_seq = 1
packet_event = tcontract.events.OnPacketResult
start = w3.eth.get_block_number()
str, diff = submit_msgs(
ibc,
tcontract.functions.callSubmitMsgs,
data,
ica_address,
False,
expected_seq,
contract.events.SubmitMsgsResult,
channel_id,
need_wait=False,
signer=signer,
)
submit_msgs_ro(tcontract.functions.delegateSubmitMsgs, str)
submit_msgs_ro(tcontract.functions.staticSubmitMsgs, str)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.FAIL
wait_for_packet_log(start, packet_event, channel_id, last_seq, status)
expected_seq += 1
balance = funds_ica(cli_host, ica_address, signer=signer)
start = w3.eth.get_block_number()
str, diff = submit_msgs(
ibc,
tcontract.functions.callSubmitMsgs,
data,
ica_address,
False,
expected_seq,
contract.events.SubmitMsgsResult,
channel_id,
signer=signer,
)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.SUCCESS
wait_for_packet_log(start, packet_event, channel_id, last_seq, status)
balance -= diff
assert cli_host.balance(ica_address, denom=denom) == balance
expected_seq += 1
start = w3.eth.get_block_number()
str, diff = submit_msgs(
ibc,
tcontract.functions.callSubmitMsgs,
data,
ica_address,
True,
expected_seq,
contract.events.SubmitMsgsResult,
channel_id,
signer=signer,
)
submit_msgs_ro(tcontract.functions.delegateSubmitMsgs, str)
submit_msgs_ro(tcontract.functions.staticSubmitMsgs, str)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.SUCCESS
wait_for_packet_log(start, packet_event, channel_id, last_seq, status)
balance -= diff
assert cli_host.balance(ica_address, denom=denom) == balance
expected_seq += 1
start = w3.eth.get_block_number()
# balance should not change on fail
submit_msgs(
ibc,
tcontract.functions.callSubmitMsgs,
data,
ica_address,
False,
expected_seq,
contract.events.SubmitMsgsResult,
channel_id,
amount=100000001,
need_wait=False,
signer=signer,
)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.FAIL
wait_for_packet_log(start, packet_event, channel_id, last_seq, status)
assert cli_host.balance(ica_address, denom=denom) == balance
# balance should not change on timeout
expected_seq += 1
start = w3.eth.get_block_number()
timeout = 5000000000
data["gas"] = 800000
submit_msgs(
ibc,
tcontract.functions.callSubmitMsgs,
data,
ica_address,
False,
expected_seq,
contract.events.SubmitMsgsResult,
channel_id,
timeout,
msg_num=100,
signer=signer,
)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.FAIL
wait_for_packet_log(start, packet_event, channel_id, last_seq, status)
assert cli_host.balance(ica_address, denom=denom) == balance
if order == Ordering.UNORDERED.value:
channel_id2 = get_next_channel(cli_controller, connid)
register_acc(
cli_controller,
w3,
tcontract.functions.callRegister,
contract.functions.queryAccount,
data,
channel_id2,
order,
expect_status=0,
signer=signer,
contract_addr=contract_addr,
)
channel_id2 = channel_id
expected_seq += 1
else:
wait_for_check_channel_ready(cli_controller, connid, channel_id, "STATE_CLOSED")
data["gas"] = default_gas
channel_id2 = get_next_channel(cli_controller, connid)
ica_address2 = register_acc(
cli_controller,
w3,
tcontract.functions.callRegister,
contract.functions.queryAccount,
data,
channel_id2,
order,
signer=signer,
contract_addr=contract_addr,
)
assert channel_id2 != channel_id, channel_id2
assert ica_address2 == ica_address, ica_address2
expected_seq = 1
start = w3.eth.get_block_number()
str, diff = submit_msgs(
ibc,
tcontract.functions.callSubmitMsgs,
data,
ica_address,
False,
expected_seq,
contract.events.SubmitMsgsResult,
channel_id2,
signer=signer,
)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, channel_id2, last_seq)
status = tcontract.caller.getStatus(channel_id2, last_seq)
assert expected_seq == last_seq
assert status == Status.SUCCESS
# wait for ack to add log from call evm
wait_for_packet_log(start, packet_event, channel_id2, last_seq, status)
balance -= diff
assert cli_host.balance(ica_address, denom=denom) == balance
packet = gen_query_balance_packet(cli_controller, ica_address)
str = base64.b64decode(packet["data"])
tx = tcontract.functions.callSubmitMsgs(
connid, channel_id, str, no_timeout
).build_transaction(data)
receipt = send_transaction(w3, tx, KEYS[signer])
assert receipt.status == 1