Skip to content

Commit

Permalink
Problem: icaauth route is not registered correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Sep 13, 2023
1 parent 4f11310 commit 23a7e51
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 159 deletions.
108 changes: 108 additions & 0 deletions integration_tests/ibc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
deploy_contract,
eth_to_bech32,
parse_events,
parse_events_rpc,
send_transaction,
setup_token_mapping,
wait_for_fn,
Expand Down Expand Up @@ -181,6 +182,58 @@ def hermes_transfer(ibc):
return src_amount


def find_duplicate(attributes):
res = set()
key = attributes[0]["key"]
for attribute in attributes:
if attribute["key"] == key:
value0 = attribute["value"]
elif attribute["key"] == "amount":
amount = attribute["value"]
value_pair = f"{value0}:{amount}"
if value_pair in res:
return value_pair
res.add(value_pair)
return None


def ibc_transfer_with_hermes(ibc):
src_amount = hermes_transfer(ibc)
dst_amount = src_amount * RATIO # the decimal places difference
dst_denom = "basetcro"
dst_addr = eth_to_bech32(ADDRS["signer2"])
old_dst_balance = get_balance(ibc.cronos, dst_addr, dst_denom)

new_dst_balance = 0

def check_balance_change():
nonlocal new_dst_balance
new_dst_balance = get_balance(ibc.cronos, dst_addr, dst_denom)
return new_dst_balance != old_dst_balance

wait_for_fn("balance change", check_balance_change)
assert old_dst_balance + dst_amount == new_dst_balance

# assert that the relayer transactions do enables the dynamic fee extension option.
cli = ibc.cronos.cosmos_cli()
criteria = "message.action=/ibc.core.channel.v1.MsgChannelOpenInit"
tx = cli.tx_search(criteria)["txs"][0]
events = parse_events_rpc(tx["events"])
fee = int(events["tx"]["fee"].removesuffix(dst_denom))
gas = int(tx["gas_wanted"])
# the effective fee is decided by the max_priority_fee (base fee is zero)
# rather than the normal gas price
assert fee == gas * 1000000

# check duplicate OnRecvPacket events
criteria = "message.action=/ibc.core.channel.v1.MsgRecvPacket"
tx = cli.tx_search(criteria)["txs"][0]
events = tx["logs"][1]["events"]
for event in events:
dup = find_duplicate(event["attributes"])
assert not dup, f"duplicate {dup} in {event['type']}"


def get_balance(chain, addr, denom):
balance = chain.cosmos_cli().balance(addr, denom)
print("balance", balance, addr, denom)
Expand Down Expand Up @@ -467,3 +520,58 @@ def check_contract_balance_change():
wait_for_fn("check contract balance change", check_contract_balance_change)
assert cronos_balance_after_send == amount
return amount, contract.address


def wait_for_check_channel_ready(cli, connid, channel_id):
print("wait for channel ready")

def check_channel_ready():
channels = cli.ibc_query_channels(connid)["channels"]
try:
state = next(
channel["state"]
for channel in channels
if channel["channel_id"] == channel_id
)
except StopIteration:
return False
return state == "STATE_OPEN"

wait_for_fn("channel ready", check_channel_ready)


def wait_for_check_tx(cli, adr, num_txs):
print("wait for tx arrive")

def check_tx():
current = len(cli.query_all_txs(adr)["txs"])
print("current", current)
return current > num_txs

wait_for_fn("transfer tx", check_tx)


def funds_ica(cli, adr):
# initial balance of interchain account should be zero
assert cli.balance(adr) == 0

# send some funds to interchain account
rsp = cli.transfer("signer2", adr, "1cro", gas_prices="1000000basecro")
assert rsp["code"] == 0, rsp["raw_log"]
wait_for_new_blocks(cli, 1)

# check if the funds are received in interchain account
assert cli.balance(adr, denom="basecro") == 100000000


def generate_ica_packet(cli, ica_address, to):
# generate a transaction to send to host chain
generated_tx_msg = {
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": ica_address,
"to_address": to,
"amount": [{"denom": "basecro", "amount": "50000000"}],
}
str = json.dumps(generated_tx_msg)
generated_packet = cli.ica_generate_packet_data(str)
return json.dumps(generated_packet)
161 changes: 3 additions & 158 deletions integration_tests/test_ibc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

import pytest

from .ibc_utils import (
Expand All @@ -8,20 +6,11 @@
cronos_transfer_source_tokens,
cronos_transfer_source_tokens_with_proxy,
get_balance,
hermes_transfer,
ibc_incentivized_transfer,
ibc_transfer_with_hermes,
prepare_network,
)
from .utils import (
ADDRS,
CONTRACTS,
deploy_contract,
eth_to_bech32,
parse_events_rpc,
send_transaction,
wait_for_fn,
wait_for_new_blocks,
)
from .utils import ADDRS, CONTRACTS, deploy_contract, send_transaction, wait_for_fn


@pytest.fixture(scope="module", params=[True, False])
Expand All @@ -34,63 +23,11 @@ def ibc(request, tmp_path_factory):
yield from network


def get_balances(chain, addr):
return chain.cosmos_cli().balances(addr)


def find_duplicate(attributes):
res = set()
key = attributes[0]["key"]
for attribute in attributes:
if attribute["key"] == key:
value0 = attribute["value"]
elif attribute["key"] == "amount":
amount = attribute["value"]
value_pair = f"{value0}:{amount}"
if value_pair in res:
return value_pair
res.add(value_pair)
return None


def test_ibc_transfer_with_hermes(ibc):
"""
test ibc transfer tokens with hermes cli
"""
src_amount = hermes_transfer(ibc)
dst_amount = src_amount * RATIO # the decimal places difference
dst_denom = "basetcro"
dst_addr = eth_to_bech32(ADDRS["signer2"])
old_dst_balance = get_balance(ibc.cronos, dst_addr, dst_denom)

new_dst_balance = 0

def check_balance_change():
nonlocal new_dst_balance
new_dst_balance = get_balance(ibc.cronos, dst_addr, dst_denom)
return new_dst_balance != old_dst_balance

wait_for_fn("balance change", check_balance_change)
assert old_dst_balance + dst_amount == new_dst_balance

# assert that the relayer transactions do enables the dynamic fee extension option.
cli = ibc.cronos.cosmos_cli()
criteria = "message.action=/ibc.core.channel.v1.MsgChannelOpenInit"
tx = cli.tx_search(criteria)["txs"][0]
events = parse_events_rpc(tx["events"])
fee = int(events["tx"]["fee"].removesuffix("basetcro"))
gas = int(tx["gas_wanted"])
# the effective fee is decided by the max_priority_fee (base fee is zero)
# rather than the normal gas price
assert fee == gas * 1000000

# check duplicate OnRecvPacket events
criteria = "message.action=/ibc.core.channel.v1.MsgRecvPacket"
tx = cli.tx_search(criteria)["txs"][0]
events = tx["logs"][1]["events"]
for event in events:
dup = find_duplicate(event["attributes"])
assert not dup, f"duplicate {dup} in {event['type']}"
ibc_transfer_with_hermes(ibc)


def test_ibc_incentivized_transfer(ibc):
Expand Down Expand Up @@ -215,95 +152,3 @@ def test_cronos_transfer_source_tokens_with_proxy(ibc):
"""
assert_ready(ibc)
cronos_transfer_source_tokens_with_proxy(ibc)


def test_ica(ibc, tmp_path):
connid = "connection-0"
cli_host = ibc.chainmain.cosmos_cli()
cli_controller = ibc.cronos.cosmos_cli()

print("register ica account")
rsp = cli_controller.ica_register_account(
connid, from_="signer2", gas="400000", fees="100000000basetcro"
)
assert rsp["code"] == 0, rsp["raw_log"]
port_id, channel_id = next(
(
evt["attributes"][0]["value"],
evt["attributes"][1]["value"],
)
for evt in rsp["events"]
if evt["type"] == "channel_open_init"
)
print("port-id", port_id, "channel-id", channel_id)

print("wait for ica channel ready")

def check_channel_ready():
channels = cli_controller.ibc_query_channels(connid)["channels"]
try:
state = next(
channel["state"]
for channel in channels
if channel["channel_id"] == channel_id
)
except StopIteration:
return False
return state == "STATE_OPEN"

wait_for_fn("channel ready", check_channel_ready)

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

# initial balance of interchain account should be zero
assert cli_host.balance(ica_address) == 0

# send some funds to interchain account
rsp = cli_host.transfer("signer2", ica_address, "1cro", gas_prices="1000000basecro")
assert rsp["code"] == 0, rsp["raw_log"]
wait_for_new_blocks(cli_host, 1)

# check if the funds are received in interchain account
assert cli_host.balance(ica_address, denom="basecro") == 100000000

# generate a transaction to send to host chain
generated_tx = tmp_path / "generated_tx.txt"
generated_tx_msg = {
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": ica_address,
"to_address": cli_host.address("signer2"),
"amount": [{"denom": "basecro", "amount": "50000000"}],
}
str = json.dumps(generated_tx_msg)
generated_packet = cli_controller.ica_generate_packet_data(str)
print(generated_packet)
generated_tx.write_text(json.dumps(generated_packet))

num_txs = len(cli_host.query_all_txs(ica_address)["txs"])

# submit transaction on host chain on behalf of interchain account
rsp = cli_controller.ica_submit_tx(
connid,
generated_tx,
from_="signer2",
)
assert rsp["code"] == 0, rsp["raw_log"]
packet_seq = next(
int(evt["attributes"][4]["value"])
for evt in rsp["events"]
if evt["type"] == "send_packet"
)
print("packet sequence", packet_seq)

def check_ica_tx():
return len(cli_host.query_all_txs(ica_address)["txs"]) > num_txs

print("wait for ica tx arrive")
wait_for_fn("ica transfer tx", check_ica_tx)

# check if the funds are reduced in interchain account
assert cli_host.balance(ica_address, denom="basecro") == 50000000
72 changes: 72 additions & 0 deletions integration_tests/test_ica.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest

from .ibc_utils import (
funds_ica,
generate_ica_packet,
ibc_transfer_with_hermes,
prepare_network,
wait_for_check_channel_ready,
wait_for_check_tx,
)


@pytest.fixture(scope="module")
def ibc(request, tmp_path_factory):
"prepare-network"
name = "ibc"
path = tmp_path_factory.mktemp(name)
network = prepare_network(path, name, True)
yield from network


def test_ibc_transfer_with_hermes(ibc):
ibc_transfer_with_hermes(ibc)


def test_ica(ibc):
connid = "connection-0"
cli_host = ibc.chainmain.cosmos_cli()
cli_controller = ibc.cronos.cosmos_cli()

print("register ica account")
rsp = cli_controller.ica_register_account(
connid, from_="signer2", gas="400000", fees="100000000basetcro"
)
assert rsp["code"] == 0, rsp["raw_log"]
port_id, channel_id = next(
(
evt["attributes"][0]["value"],
evt["attributes"][1]["value"],
)
for evt in rsp["events"]
if evt["type"] == "channel_open_init"
)
print("port-id", port_id, "channel-id", channel_id)

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)

funds_ica(cli_host, ica_address)
num_txs = len(cli_host.query_all_txs(ica_address)["txs"])
str = generate_ica_packet(cli_controller, ica_address, cli_host.address("signer2"))
# submit transaction on host chain on behalf of interchain account
rsp = cli_controller.ica_submit_tx(
connid,
str,
from_="signer2",
)
assert rsp["code"] == 0, rsp["raw_log"]
packet_seq = next(
int(evt["attributes"][4]["value"])
for evt in rsp["events"]
if evt["type"] == "send_packet"
)
print("packet sequence", packet_seq)
wait_for_check_tx(cli_host, ica_address, num_txs)
# check if the funds are reduced in interchain account
assert cli_host.balance(ica_address, denom="basecro") == 50000000
Loading

0 comments on commit 23a7e51

Please sign in to comment.