Skip to content

Commit

Permalink
test ica ctrl with memo
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Dec 5, 2023
1 parent ecdba9e commit acaeab2
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 21 deletions.
23 changes: 23 additions & 0 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,29 @@ def icaauth_submit_tx(self, connid, tx, timeout_duration="1h", **kwargs):
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp

def ica_ctrl_send_tx(self, connid, tx, **kwargs):
default_kwargs = {
"home": self.data_dir,
"node": self.node_rpc,
"chain_id": self.chain_id,
"keyring_backend": "test",
}
rsp = json.loads(
self.raw(
"tx",
"ica",
"controller",
"send-tx",
connid,
tx,
"-y",
**(default_kwargs | kwargs),
)
)
if rsp["code"] == 0:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp

def ica_query_account(self, connid, owner, **kwargs):
default_kwargs = {
"node": self.node_rpc,
Expand Down
17 changes: 17 additions & 0 deletions integration_tests/ibc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,23 @@ def check_tx():
assert raised


def register_acc(cli, connid):
print("register ica account")
rsp = cli.icaauth_register_account(
connid, from_="signer2", gas="400000", fees="100000000basetcro"
)
_, channel_id = assert_channel_open_init(rsp)
wait_for_check_channel_ready(cli, connid, channel_id)

print("query ica account")
ica_address = cli.ica_query_account(
connid,
cli.address("signer2"),
)["interchain_account_address"]
print("ica address", ica_address, "channel_id", channel_id)
return ica_address, channel_id


def funds_ica(cli, adr):
# initial balance of interchain account should be zero
assert cli.balance(adr) == 0
Expand Down
22 changes: 3 additions & 19 deletions integration_tests/test_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from pystarport import cluster

from .ibc_utils import (
assert_channel_open_init,
funds_ica,
gen_send_msg,
prepare_network,
register_acc,
wait_for_check_channel_ready,
wait_for_check_tx,
)
Expand All @@ -31,23 +31,7 @@ def test_ica(ibc, tmp_path):
connid = "connection-0"
cli_host = ibc.chainmain.cosmos_cli()
cli_controller = ibc.cronos.cosmos_cli()

def register_acc():
print("register ica account")
rsp = cli_controller.icaauth_register_account(
connid, from_="signer2", gas="400000", fees="100000000basetcro"
)
_, channel_id = assert_channel_open_init(rsp)
wait_for_check_channel_ready(cli_controller, connid, channel_id)

print("query ica account")
ica_address = cli_controller.ica_query_account(
connid, cli_controller.address("signer2")
)["interchain_account_address"]
print("ica address", ica_address, "channel_id", channel_id)
return ica_address, channel_id

ica_address, channel_id = register_acc()
ica_address, channel_id = register_acc(cli_controller, connid)
balance = funds_ica(cli_host, ica_address)
num_txs = len(cli_host.query_all_txs(ica_address)["txs"])
to = cli_host.address("signer2")
Expand Down Expand Up @@ -90,7 +74,7 @@ def submit_msgs(msg_num, timeout_in_s=no_timeout, gas="200000"):
assert cli_host.balance(ica_address, denom=denom) == balance
wait_for_check_channel_ready(cli_controller, connid, channel_id, "STATE_CLOSED")
# reopen ica account after channel get closed
ica_address2, channel_id2 = register_acc()
ica_address2, channel_id2 = register_acc(cli_controller, connid)
assert ica_address2 == ica_address, ica_address2
assert channel_id2 != channel_id, channel_id2
# submit normal txs should work
Expand Down
78 changes: 78 additions & 0 deletions integration_tests/test_ica_ctrl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import json

import pytest
from pystarport import cluster

from .ibc_utils import (
deploy_contract,
funds_ica,
gen_send_msg,
parse_events_rpc,
prepare_network,
register_acc,
wait_for_check_tx,
)
from .utils import CONTRACTS, wait_for_fn


@pytest.fixture(scope="module")
def ibc(request, tmp_path_factory):
"prepare-network"
name = "ibc_rly"
path = tmp_path_factory.mktemp(name)
yield from prepare_network(
path,
name,
incentivized=False,
connection_only=True,
relayer=cluster.Relayer.RLY.value,
)


def test_cb(ibc):
connid = "connection-0"
cli_host = ibc.chainmain.cosmos_cli()
cli_controller = ibc.cronos.cosmos_cli()
ica_address, _ = register_acc(cli_controller, connid)
funds_ica(cli_host, ica_address)
num_txs = len(cli_host.query_all_txs(ica_address)["txs"])
to = cli_host.address("signer2")
amount = 1000
denom = "basecro"
jsonfile = CONTRACTS["TestICA"]
tcontract = deploy_contract(ibc.cronos.w3, jsonfile)
memo = {"src_callback": {"address": tcontract.address}}

def generated_tx_packet(msg_num):
# generate a transaction to send to host chain
m = gen_send_msg(ica_address, to, denom, amount)
msgs = []
for i in range(msg_num):
msgs.append(m)
data = json.dumps(msgs)
packet = cli_controller.ica_generate_packet_data(data, json.dumps(memo))
return packet

def send_tx(msg_num, gas="200000"):
generated_tx = json.dumps(generated_tx_packet(msg_num))
# submit transaction on host chain on behalf of interchain account
rsp = cli_controller.ica_ctrl_send_tx(
connid,
generated_tx,
gas=gas,
from_="signer2",
)
assert rsp["code"] == 0, rsp["raw_log"]
wait_for_check_tx(cli_host, ica_address, num_txs)

msg_num = 10
send_tx(msg_num)

def check_for_ack():
criteria = "message.action=/ibc.core.channel.v1.MsgAcknowledgement"
return cli_controller.tx_search(criteria)["txs"]

txs = wait_for_fn("ack change", check_for_ack)
events = parse_events_rpc(txs[0]["events"])
err = events.get("ibc_src_callback")["callback_error"]
assert "sender is not authenticated" in err, err
3 changes: 1 addition & 2 deletions integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ def contract_path(name, filename):
def wait_for_fn(name, fn, *, timeout=240, interval=1):
for i in range(int(timeout / interval)):
result = fn()
print("check", name, result)
if result:
break
return result
time.sleep(interval)
else:
raise TimeoutError(f"wait for {name} timeout")
Expand Down

0 comments on commit acaeab2

Please sign in to comment.