-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathtest_nft_transfer.py
544 lines (481 loc) · 13.5 KB
/
test_nft_transfer.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import json
import time
from pathlib import Path
import pytest
from .ibc_utils import start_and_wait_relayer
from .utils import cluster_fixture, find_log_event_attrs
pytestmark = pytest.mark.ibc
@pytest.fixture(scope="module")
def cluster(worker_index, pytestconfig, tmp_path_factory):
"override cluster fixture for this test module"
yield from cluster_fixture(
Path(__file__).parent / "configs/nft_transfer.jsonnet",
worker_index,
tmp_path_factory.mktemp("data"),
)
# This function tests nft transfer from source chain -> mid chain -> destination chain
# and all the way back to source
# chain following the same path
def test_nft_transfer(cluster):
src_channel, mid_src_channel = start_and_wait_relayer(cluster, "nft")
mid_dst_channel, dst_channel = start_and_wait_relayer(
cluster, "nft", ["ibc-1", "ibc-2"], False
)
assert src_channel == "channel-0", src_channel
assert mid_src_channel == "channel-0", mid_src_channel
# assert mid_dst_channel == "channel-1", mid_dst_channel
assert dst_channel == "channel-0", dst_channel
mid_dst_channel = "channel-1"
cli_src = cluster["ibc-0"].cosmos_cli()
cli_mid = cluster["ibc-1"].cosmos_cli()
cli_dst = cluster["ibc-2"].cosmos_cli()
addr_src = cluster["ibc-0"].address("relayer")
addr_mid = cluster["ibc-1"].address("relayer")
addr_dst = cluster["ibc-2"].address("relayer")
denomid = "testdenomid"
denomname = "testdenomname"
denomuri = "testdenomuri"
tokenid = "testtokenid"
tokenuri = "testtokenuri"
# mint nft on source chain
rsp = json.loads(
cli_src.raw(
"tx",
"nft",
"issue",
denomid,
"-y",
name=denomname,
uri=denomuri,
home=cli_src.data_dir,
from_=addr_src,
keyring_backend="test",
chain_id=cli_src.chain_id,
node=cli_src.node_rpc,
)
)
if rsp["code"] == 0:
rsp = cli_src.event_query_tx_for(rsp["txhash"])
ev = find_log_event_attrs(rsp["events"], "issue_denom")
assert ev == {
"denom_id": denomid,
"denom_name": denomname,
"creator": addr_src,
"msg_index": "0",
}, ev
rsp = json.loads(
cli_src.raw(
"tx",
"nft",
"mint",
denomid,
tokenid,
"-y",
uri=tokenuri,
recipient=addr_src,
home=cli_src.data_dir,
from_=addr_src,
keyring_backend="test",
chain_id=cli_src.chain_id,
node=cli_src.node_rpc,
)
)
if rsp["code"] == 0:
rsp = cli_src.event_query_tx_for(rsp["txhash"])
ev = find_log_event_attrs(rsp["events"], "message")
assert ev["action"] == "/chainmain.nft.v1.MsgMintNFT", ev
# nft transfer that's supposed to fail, exceeds max receiver length
rsp = json.loads(
cli_src.raw(
"tx",
"nft-transfer",
"transfer",
"nft",
src_channel,
"a" * 2049,
denomid,
tokenid,
"-y",
home=cli_src.data_dir,
from_=addr_src,
keyring_backend="test",
chain_id=cli_src.chain_id,
node=cli_src.node_rpc,
)
)
assert rsp["code"] != 0
assert "receiver length must be less than 2048" in rsp["raw_log"]
# transfer nft on mid-destination chain
rsp = json.loads(
cli_src.raw(
"tx",
"nft-transfer",
"transfer",
"nft",
src_channel,
addr_mid,
denomid,
tokenid,
"-y",
home=cli_src.data_dir,
from_=addr_src,
keyring_backend="test",
chain_id=cli_src.chain_id,
node=cli_src.node_rpc,
)
)
if rsp["code"] == 0:
rsp = cli_src.event_query_tx_for(rsp["txhash"])
assert rsp["code"] == 0, rsp["raw_log"]
# FIXME more stable way to wait for relaying
time.sleep(20)
# get class hash on mid chain
mid_class_hash = json.loads(
cli_mid.raw(
"query",
"nft-transfer",
"class-hash",
"nft/" + mid_src_channel + "/" + denomid,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
)
)["hash"]
# get class trace on mid chain
mid_class_trace = json.loads(
cli_mid.raw(
"query",
"nft-transfer",
"class-trace",
mid_class_hash,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
)
)["class_trace"]
assert mid_class_trace["base_class_id"] == denomid, mid_class_trace
assert mid_class_trace["path"] == "nft/" + mid_src_channel, mid_class_trace
mid_denom_id = "ibc/" + mid_class_hash
# query denom on mid chain
rsp = json.loads(
cli_mid.raw(
"query",
"nft",
"denom",
mid_denom_id,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
)
)
assert rsp["uri"] == denomuri, rsp["uri"]
# query nft on mid chain
rsp = json.loads(
cli_mid.raw(
"query",
"nft",
"token",
mid_denom_id,
tokenid,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
)
)
assert rsp["uri"] == tokenuri, rsp
assert rsp["owner"] == addr_mid, rsp
# query nft on source chain's escrow address
src_escrow_address = str(
cli_src.raw(
"query",
"nft-transfer",
"escrow-address",
"nft",
src_channel,
home=cli_src.data_dir,
node=cli_src.node_rpc,
output="json",
),
"UTF-8",
).strip()
rsp = json.loads(
cli_src.raw(
"query",
"nft",
"token",
denomid,
tokenid,
home=cli_src.data_dir,
node=cli_src.node_rpc,
output="json",
)
)
assert rsp["uri"] == tokenuri, rsp
assert rsp["owner"] == src_escrow_address, rsp
# transfer nft to destination chain
rsp = json.loads(
cli_mid.raw(
"tx",
"nft-transfer",
"transfer",
"nft",
mid_dst_channel,
addr_dst,
mid_denom_id,
tokenid,
"-y",
home=cli_mid.data_dir,
from_=addr_mid,
keyring_backend="test",
chain_id=cli_mid.chain_id,
node=cli_mid.node_rpc,
)
)
if rsp["code"] == 0:
rsp = cli_mid.event_query_tx_for(rsp["txhash"])
assert rsp["code"] == 0, rsp["raw_log"]
# FIXME more stable way to wait for relaying
time.sleep(20)
# get class hash on destination chain
dst_class_hash = json.loads(
cli_dst.raw(
"query",
"nft-transfer",
"class-hash",
"nft/" + dst_channel + "/nft/" + mid_src_channel + "/" + denomid,
home=cli_dst.data_dir,
node=cli_dst.node_rpc,
output="json",
)
)["hash"]
# get class trace on destination chain
dst_class_trace = json.loads(
cli_dst.raw(
"query",
"nft-transfer",
"class-trace",
dst_class_hash,
home=cli_dst.data_dir,
node=cli_dst.node_rpc,
output="json",
)
)["class_trace"]
assert dst_class_trace["base_class_id"] == denomid, dst_class_trace
assert (
dst_class_trace["path"] == "nft/" + dst_channel + "/nft/" + mid_src_channel
), dst_class_trace
dst_denom_id = "ibc/" + dst_class_hash
# query denom on destination chain
rsp = json.loads(
cli_dst.raw(
"query",
"nft",
"denom",
dst_denom_id,
home=cli_dst.data_dir,
node=cli_dst.node_rpc,
output="json",
)
)
assert rsp["uri"] == denomuri, rsp["uri"]
# query nft on destination chain
rsp = json.loads(
cli_dst.raw(
"query",
"nft",
"token",
dst_denom_id,
tokenid,
home=cli_dst.data_dir,
node=cli_dst.node_rpc,
output="json",
)
)
assert rsp["uri"] == tokenuri, rsp
assert rsp["owner"] == addr_dst, rsp
# quert nft on mid chain's escrow address
mid_escrow_address = str(
cli_mid.raw(
"query",
"nft-transfer",
"escrow-address",
"nft",
mid_dst_channel,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
),
"UTF-8",
).strip()
rsp = json.loads(
cli_mid.raw(
"query",
"nft",
"token",
mid_denom_id,
tokenid,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
)
)
assert rsp["uri"] == tokenuri, rsp
assert rsp["owner"] == mid_escrow_address, rsp
# transfer nft back to mid chain
rsp = json.loads(
cli_dst.raw(
"tx",
"nft-transfer",
"transfer",
"nft",
dst_channel,
addr_mid,
dst_denom_id,
tokenid,
"-y",
home=cli_dst.data_dir,
from_=addr_dst,
keyring_backend="test",
chain_id=cli_dst.chain_id,
node=cli_dst.node_rpc,
)
)
if rsp["code"] == 0:
rsp = cli_dst.event_query_tx_for(rsp["txhash"])
assert rsp["code"] == 0, rsp["raw_log"]
# FIXME more stable way to wait for relaying
time.sleep(20)
# nft should be burnt on destination chain
rsp = json.loads(
cli_dst.raw(
"query",
"nft",
"collection",
dst_denom_id,
home=cli_dst.data_dir,
node=cli_dst.node_rpc,
output="json",
)
)["collection"]
assert len(rsp["nfts"]) == 0, rsp
# query nft on mid chain
rsp = json.loads(
cli_mid.raw(
"query",
"nft",
"token",
mid_denom_id,
tokenid,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
)
)
assert rsp["uri"] == tokenuri, rsp
assert rsp["owner"] == addr_mid, rsp
# transfer nft back to source chain
rsp = json.loads(
cli_mid.raw(
"tx",
"nft-transfer",
"transfer",
"nft",
mid_src_channel,
addr_src,
mid_denom_id,
tokenid,
"-y",
home=cli_mid.data_dir,
from_=addr_mid,
keyring_backend="test",
chain_id=cli_mid.chain_id,
node=cli_mid.node_rpc,
)
)
if rsp["code"] == 0:
rsp = cli_mid.event_query_tx_for(rsp["txhash"])
assert rsp["code"] == 0, rsp["raw_log"]
# FIXME more stable way to wait for relaying
time.sleep(20)
# nft should be burnt on mid chain
rsp = json.loads(
cli_mid.raw(
"query",
"nft",
"collection",
mid_denom_id,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
)
)["collection"]
assert len(rsp["nfts"]) == 0, rsp
# query nft on source chain
rsp = json.loads(
cli_src.raw(
"query",
"nft",
"token",
denomid,
tokenid,
home=cli_src.data_dir,
node=cli_src.node_rpc,
output="json",
)
)
assert rsp["uri"] == tokenuri, rsp
assert rsp["owner"] == addr_src, rsp
# Test packet timeout
# transfer nft on mid chain (with very less timeout so that the packet times out)
rsp = json.loads(
cli_src.raw(
"tx",
"nft-transfer",
"transfer",
"nft",
src_channel,
addr_mid,
denomid,
tokenid,
"-y",
packet_timeout_height="0-1",
home=cli_src.data_dir,
from_=addr_src,
keyring_backend="test",
chain_id=cli_src.chain_id,
node=cli_src.node_rpc,
)
)
if rsp["code"] == 0:
rsp = cli_src.event_query_tx_for(rsp["txhash"])
assert rsp["code"] == 0, rsp["raw_log"]
# FIXME more stable way to wait for relaying
time.sleep(20)
# nft should be not be present on mid chain
rsp = json.loads(
cli_mid.raw(
"query",
"nft",
"collection",
mid_denom_id,
home=cli_mid.data_dir,
node=cli_mid.node_rpc,
output="json",
)
)["collection"]
assert len(rsp["nfts"]) == 0, rsp
# query nft on source chain (as the transfer should time out)
rsp = json.loads(
cli_src.raw(
"query",
"nft",
"token",
denomid,
tokenid,
home=cli_src.data_dir,
node=cli_src.node_rpc,
output="json",
)
)
assert rsp["uri"] == tokenuri, rsp
assert rsp["owner"] == addr_src, rsp