|
1 | 1 | import base64
|
2 | 2 | import json
|
3 |
| -import subprocess |
4 |
| -from pathlib import Path |
5 |
| -from typing import NamedTuple |
6 | 3 |
|
7 | 4 | import pytest
|
8 |
| -from pystarport import ports |
9 | 5 |
|
10 |
| -from .network import Chainmain, Cronos, Hermes, setup_custom_cronos |
| 6 | +from .ibc_utils import RATIO, assert_ready, get_balance, prepare, prepare_network |
11 | 7 | from .utils import (
|
12 | 8 | ADDRS,
|
13 | 9 | CONTRACTS,
|
14 | 10 | deploy_contract,
|
15 | 11 | eth_to_bech32,
|
16 | 12 | send_transaction,
|
17 |
| - supervisorctl, |
18 | 13 | wait_for_fn,
|
19 | 14 | wait_for_new_blocks,
|
20 |
| - wait_for_port, |
21 | 15 | )
|
22 | 16 |
|
23 | 17 |
|
24 |
| -class IBCNetwork(NamedTuple): |
25 |
| - cronos: Cronos |
26 |
| - chainmain: Chainmain |
27 |
| - hermes: Hermes |
28 |
| - |
29 |
| - |
30 | 18 | @pytest.fixture(scope="module")
|
31 | 19 | def ibc(request, tmp_path_factory):
|
32 |
| - "start-cronos" |
33 |
| - path = tmp_path_factory.mktemp("ibc") |
34 |
| - gen = setup_custom_cronos( |
35 |
| - path, 26700, Path(__file__).parent / "configs/ibc.jsonnet" |
36 |
| - ) |
37 |
| - cronos = next(gen) |
38 |
| - try: |
39 |
| - chainmain = Chainmain(cronos.base_dir.parent / "chainmain-1") |
40 |
| - hermes = Hermes(cronos.base_dir.parent / "relayer.toml") |
41 |
| - # wait for grpc ready |
42 |
| - wait_for_port(ports.grpc_port(chainmain.base_port(0))) # chainmain grpc |
43 |
| - wait_for_port(ports.grpc_port(cronos.base_port(0))) # cronos grpc |
44 |
| - subprocess.check_call( |
45 |
| - [ |
46 |
| - "hermes", |
47 |
| - "-c", |
48 |
| - hermes.configpath, |
49 |
| - "create", |
50 |
| - "channel", |
51 |
| - "cronos_777-1", |
52 |
| - "chainmain-1", |
53 |
| - "--port-a", |
54 |
| - "transfer", |
55 |
| - "--port-b", |
56 |
| - "transfer", |
57 |
| - ] |
58 |
| - ) |
59 |
| - supervisorctl(cronos.base_dir / "../tasks.ini", "start", "relayer-demo") |
60 |
| - wait_for_port(hermes.port) |
61 |
| - yield IBCNetwork(cronos, chainmain, hermes) |
62 |
| - finally: |
63 |
| - try: |
64 |
| - next(gen) |
65 |
| - except StopIteration: |
66 |
| - pass |
67 |
| - |
68 |
| - |
69 |
| -def get_balance(chain, addr, denom): |
70 |
| - return chain.cosmos_cli().balance(addr, denom) |
| 20 | + "prepare-network" |
| 21 | + name = "ibc" |
| 22 | + path = tmp_path_factory.mktemp(name) |
| 23 | + network = prepare_network(path, name) |
| 24 | + yield from network |
71 | 25 |
|
72 | 26 |
|
73 | 27 | def get_balances(chain, addr):
|
74 | 28 | return chain.cosmos_cli().balances(addr)
|
75 | 29 |
|
76 | 30 |
|
77 | 31 | def test_ibc(ibc):
|
78 |
| - "test sending basecro from crypto-org chain to cronos" |
79 |
| - # wait for hermes |
80 |
| - output = subprocess.getoutput( |
81 |
| - f"curl -s -X GET 'http://127.0.0.1:{ibc.hermes.port}/state' | jq" |
82 |
| - ) |
83 |
| - assert json.loads(output)["status"] == "success" |
84 |
| - |
85 |
| - my_ibc0 = "chainmain-1" |
86 |
| - my_ibc1 = "cronos_777-1" |
87 |
| - my_channel = "channel-0" |
88 |
| - my_config = ibc.hermes.configpath |
89 |
| - # signer2 |
90 |
| - coin_receiver = eth_to_bech32(ADDRS["signer2"]) |
91 |
| - src_amount = 10 |
92 |
| - dst_amount = src_amount * (10**10) # the decimal places difference |
93 |
| - src_denom = "basecro" |
| 32 | + src_amount = prepare(ibc) |
| 33 | + dst_amount = src_amount * RATIO # the decimal places difference |
94 | 34 | dst_denom = "basetcro"
|
95 |
| - # dstchainid srcchainid srcportid srchannelid |
96 |
| - # chainmain-1 -> cronos_777-1 |
97 |
| - cmd = ( |
98 |
| - f"hermes -c {my_config} tx raw ft-transfer " |
99 |
| - f"{my_ibc1} {my_ibc0} transfer {my_channel} {src_amount} " |
100 |
| - f"-o 1000 -n 1 -d {src_denom} -r {coin_receiver} -k relayer" |
101 |
| - ) |
102 |
| - subprocess.run(cmd, check=True, shell=True) |
103 |
| - dstaddr = f"{coin_receiver}" |
104 |
| - olddstbalance = get_balance(ibc.cronos, dstaddr, dst_denom) |
105 |
| - newdstbalance = 0 |
| 35 | + dst_addr = eth_to_bech32(ADDRS["signer2"]) |
| 36 | + old_dst_balance = get_balance(ibc.cronos, dst_addr, dst_denom) |
| 37 | + |
| 38 | + new_dst_balance = 0 |
106 | 39 |
|
107 | 40 | def check_balance_change():
|
108 |
| - nonlocal newdstbalance |
109 |
| - newdstbalance = get_balance(ibc.cronos, dstaddr, dst_denom) |
110 |
| - return newdstbalance != olddstbalance |
| 41 | + nonlocal new_dst_balance |
| 42 | + new_dst_balance = get_balance(ibc.cronos, dst_addr, dst_denom) |
| 43 | + return new_dst_balance != old_dst_balance |
111 | 44 |
|
112 |
| - wait_for_fn("check balance change", check_balance_change) |
113 |
| - expectedbalance = olddstbalance + dst_amount |
114 |
| - assert expectedbalance == newdstbalance |
| 45 | + wait_for_fn("balance change", check_balance_change) |
| 46 | + assert old_dst_balance + dst_amount == new_dst_balance |
115 | 47 |
|
116 | 48 |
|
117 | 49 | def test_cronos_transfer_tokens(ibc):
|
118 | 50 | """
|
119 | 51 | test sending basetcro from cronos to crypto-org-chain using cli transfer_tokens.
|
120 | 52 | depends on `test_ibc` to send the original coins.
|
121 | 53 | """
|
122 |
| - output = subprocess.getoutput( |
123 |
| - f"curl -s -X GET 'http://127.0.0.1:{ibc.hermes.port}/state' | jq" |
124 |
| - ) |
125 |
| - assert json.loads(output)["status"] == "success" |
126 |
| - |
127 |
| - coin_receiver = ibc.chainmain.cosmos_cli().address("signer2") |
| 54 | + assert_ready(ibc) |
| 55 | + dst_addr = ibc.chainmain.cosmos_cli().address("signer2") |
128 | 56 | dst_amount = 2
|
129 |
| - src_amount = dst_amount * (10**10) # the decimal places difference |
| 57 | + dst_denom = "basecro" |
| 58 | + cli = ibc.cronos.cosmos_cli() |
| 59 | + src_amount = dst_amount * RATIO # the decimal places difference |
| 60 | + src_addr = cli.address("signer2") |
| 61 | + src_denom = "basetcro" |
130 | 62 |
|
131 | 63 | # case 1: use cronos cli
|
132 |
| - oldbalance = get_balance(ibc.chainmain, coin_receiver, "basecro") |
| 64 | + old_src_balance = get_balance(ibc.cronos, src_addr, src_denom) |
| 65 | + old_dst_balance = get_balance(ibc.chainmain, dst_addr, dst_denom) |
| 66 | + rsp = cli.transfer_tokens( |
| 67 | + src_addr, |
| 68 | + dst_addr, |
| 69 | + f"{src_amount}{src_denom}", |
| 70 | + ) |
| 71 | + assert rsp["code"] == 0, rsp["raw_log"] |
| 72 | + |
| 73 | + new_dst_balance = 0 |
| 74 | + |
| 75 | + def check_balance_change(): |
| 76 | + nonlocal new_dst_balance |
| 77 | + new_dst_balance = get_balance(ibc.chainmain, dst_addr, dst_denom) |
| 78 | + return old_dst_balance != new_dst_balance |
| 79 | + |
| 80 | + wait_for_fn("balance change", check_balance_change) |
| 81 | + assert old_dst_balance + dst_amount == new_dst_balance |
| 82 | + new_src_balance = get_balance(ibc.cronos, src_addr, src_denom) |
| 83 | + assert old_src_balance - src_amount == new_src_balance |
| 84 | + |
| 85 | + |
| 86 | +def test_cronos_transfer_tokens_acknowledgement_error(ibc): |
| 87 | + """ |
| 88 | + test sending basetcro from cronos to crypto-org-chain using cli transfer_tokens |
| 89 | + with invalid receiver for acknowledgement error. |
| 90 | + depends on `test_ibc` to send the original coins. |
| 91 | + """ |
| 92 | + assert_ready(ibc) |
| 93 | + dst_addr = "invalid_address" |
| 94 | + dst_amount = 2 |
133 | 95 | cli = ibc.cronos.cosmos_cli()
|
| 96 | + src_amount = dst_amount * RATIO # the decimal places difference |
| 97 | + src_addr = cli.address("signer2") |
| 98 | + src_denom = "basetcro" |
| 99 | + |
| 100 | + old_src_balance = get_balance(ibc.cronos, src_addr, src_denom) |
134 | 101 | rsp = cli.transfer_tokens(
|
135 |
| - cli.address("signer2"), |
136 |
| - coin_receiver, |
137 |
| - f"{src_amount}basetcro", |
| 102 | + src_addr, |
| 103 | + dst_addr, |
| 104 | + f"{src_amount}{src_denom}", |
138 | 105 | )
|
139 | 106 | assert rsp["code"] == 0, rsp["raw_log"]
|
140 | 107 |
|
141 |
| - newbalance = 0 |
| 108 | + new_src_balance = 0 |
142 | 109 |
|
143 | 110 | def check_balance_change():
|
144 |
| - nonlocal newbalance |
145 |
| - newbalance = get_balance(ibc.chainmain, coin_receiver, "basecro") |
146 |
| - return oldbalance != newbalance |
| 111 | + nonlocal new_src_balance |
| 112 | + new_src_balance = get_balance(ibc.cronos, src_addr, src_denom) |
| 113 | + return old_src_balance == new_src_balance |
147 | 114 |
|
148 |
| - wait_for_fn("check balance change", check_balance_change) |
149 |
| - assert oldbalance + dst_amount == newbalance |
| 115 | + wait_for_fn("balance no change", check_balance_change) |
| 116 | + new_src_balance = get_balance(ibc.cronos, src_addr, src_denom) |
150 | 117 |
|
151 | 118 |
|
152 | 119 | def test_cro_bridge_contract(ibc):
|
153 | 120 | """
|
154 | 121 | test sending basetcro from cronos to crypto-org-chain using CroBridge contract.
|
155 | 122 | depends on `test_ibc` to send the original coins.
|
156 | 123 | """
|
157 |
| - coin_receiver = ibc.chainmain.cosmos_cli().address("signer2") |
| 124 | + dst_addr = ibc.chainmain.cosmos_cli().address("signer2") |
158 | 125 | dst_amount = 2
|
159 |
| - src_amount = dst_amount * (10**10) # the decimal places difference |
160 |
| - oldbalance = get_balance(ibc.chainmain, coin_receiver, "basecro") |
| 126 | + dst_denom = "basecro" |
| 127 | + src_amount = dst_amount * RATIO # the decimal places difference |
| 128 | + old_dst_balance = get_balance(ibc.chainmain, dst_addr, dst_denom) |
161 | 129 |
|
162 | 130 | # case 2: use CroBridge contract
|
163 | 131 | w3 = ibc.cronos.w3
|
164 | 132 | contract = deploy_contract(w3, CONTRACTS["CroBridge"])
|
165 |
| - tx = contract.functions.send_cro_to_crypto_org(coin_receiver).buildTransaction( |
| 133 | + tx = contract.functions.send_cro_to_crypto_org(dst_addr).buildTransaction( |
166 | 134 | {"from": ADDRS["signer2"], "value": src_amount}
|
167 | 135 | )
|
168 | 136 | receipt = send_transaction(w3, tx)
|
169 | 137 | assert receipt.status == 1
|
170 | 138 |
|
171 |
| - newbalance = 0 |
| 139 | + new_dst_balance = 0 |
172 | 140 |
|
173 | 141 | def check_balance_change():
|
174 |
| - nonlocal newbalance |
175 |
| - newbalance = get_balance(ibc.chainmain, coin_receiver, "basecro") |
176 |
| - return oldbalance != newbalance |
| 142 | + nonlocal new_dst_balance |
| 143 | + new_dst_balance = get_balance(ibc.chainmain, dst_addr, dst_denom) |
| 144 | + return old_dst_balance != new_dst_balance |
177 | 145 |
|
178 | 146 | wait_for_fn("check balance change", check_balance_change)
|
179 |
| - assert oldbalance + dst_amount == newbalance |
| 147 | + assert old_dst_balance + dst_amount == new_dst_balance |
180 | 148 |
|
181 | 149 |
|
182 | 150 | def test_ica(ibc, tmp_path):
|
@@ -271,11 +239,7 @@ def test_cronos_transfer_source_tokens(ibc):
|
271 | 239 | """
|
272 | 240 | test sending crc20 tokens originated from cronos to crypto-org-chain
|
273 | 241 | """
|
274 |
| - output = subprocess.getoutput( |
275 |
| - f"curl -s -X GET 'http://127.0.0.1:{ibc.hermes.port}/state' | jq" |
276 |
| - ) |
277 |
| - assert json.loads(output)["status"] == "success" |
278 |
| - |
| 242 | + assert_ready(ibc) |
279 | 243 | # deploy crc21 contract
|
280 | 244 | w3 = ibc.cronos.w3
|
281 | 245 | contract = deploy_contract(w3, CONTRACTS["TestERC21Source"])
|
|
0 commit comments