forked from crypto-org-chain/chain-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ica.py
159 lines (125 loc) · 4.47 KB
/
test_ica.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
import json
import subprocess
import time
from pathlib import Path
import pytest
from pystarport import ports
from .utils import cluster_fixture, wait_for_block, wait_for_port
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/ica.yaml",
worker_index,
tmp_path_factory.mktemp("data"),
)
def start_and_wait_relayer(cluster):
for cli in cluster.values():
for i in range(cli.nodes_len()):
wait_for_port(ports.grpc_port(cli.base_port(i)))
for cli in cluster.values():
# wait for at least 3 blocks, because
# "proof queries at height <= 2 are not supported"
wait_for_block(cli, 3)
# all clusters share the same root data directory
data_root = next(iter(cluster.values())).data_root
relayer = ["hermes", "-j", "-c", data_root / "relayer.toml"]
# create connection
subprocess.run(
relayer
+ [
"create",
"connection",
"ica-controller-1",
"ica-host-1",
],
check=True,
)
# start relaying
cluster["ica-controller-1"].supervisor.startProcess("relayer-demo")
rsp = json.loads(
subprocess.check_output(relayer + ["query", "connections", "ica-controller-1"])
)
controller_connection = rsp["result"][0]
rsp = json.loads(
subprocess.check_output(relayer + ["query", "connections", "ica-host-1"])
)
host_connection = rsp["result"][0]
return controller_connection, host_connection
def test_ica(cluster, tmp_path):
controller_connection, host_connection = start_and_wait_relayer(cluster)
# call chain-maind directly
cli_controller = cluster["ica-controller-1"].cosmos_cli()
cli_host = cluster["ica-host-1"].cosmos_cli()
addr_controller = cluster["ica-controller-1"].address("signer")
addr_host = cluster["ica-host-1"].address("signer")
# create interchain account
rsp = json.loads(
cli_controller.raw(
"tx",
"icaauth",
"register-account",
controller_connection,
"-y",
from_="signer",
home=cli_controller.data_dir,
node=cli_controller.node_rpc,
keyring_backend="test",
chain_id=cli_controller.chain_id,
)
)
assert rsp["code"] == 0, rsp["raw_log"]
# FIXME more stable way to wait for relaying
time.sleep(20)
# get interchain account address
ica_address = json.loads(
cli_controller.raw(
"query",
"icaauth",
"interchain-account-address",
controller_connection,
addr_controller,
output="json",
node=cli_controller.node_rpc,
chain_id=cli_controller.chain_id,
)
)["interchainAccountAddress"]
# initial balance of interchain account should be zero
assert cli_host.balance(ica_address) == 0
# send some funds to interchain account
cli_host.transfer("signer", ica_address, "1cro")
# check if the funds are received in interchain account
assert cli_host.balance(ica_address) == 100000000
# generate a transaction to send to host chain
generated_tx = tmp_path / "generated_tx.txt"
generated_tx_msg = cli_host.transfer(
ica_address, addr_host, "0.5cro", generate_only=True
)
print(json.dumps(generated_tx_msg))
with open(generated_tx, "w") as opened_file:
json.dump(generated_tx_msg, opened_file)
num_txs = len(cli_host.query_all_txs(ica_address)["txs"])
# submit transaction on host chain on behalf of interchain account
rsp = json.loads(
cli_controller.raw(
"tx",
"icaauth",
"submit-tx",
controller_connection,
generated_tx,
"-y",
from_="signer",
home=cli_controller.data_dir,
node=cli_controller.node_rpc,
keyring_backend="test",
chain_id=cli_controller.chain_id,
)
)
assert rsp["code"] == 0, rsp["raw_log"]
# FIXME more stable way to wait for relaying
time.sleep(20)
# check if the transaction is submitted
assert len(cli_host.query_all_txs(ica_address)["txs"]) == num_txs + 1
# check if the funds are reduced in interchain account
assert cli_host.balance(ica_address) == 50000000