Skip to content

Commit

Permalink
adjust emit
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Sep 18, 2023
1 parent 828403b commit 2247df5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 60 deletions.
8 changes: 2 additions & 6 deletions integration_tests/contracts/contracts/TestICA.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ pragma solidity ^0.6.6;

contract TestICA {
address constant icaContract = 0x0000000000000000000000000000000000000066;
event RegisterAccountResult(bytes data);
event SubmitMsgsResult(bytes data);

function nativeRegister(string memory connectionID) public {
(bool result, bytes memory res) = icaContract.call(abi.encodeWithSignature(
(bool result,) = icaContract.call(abi.encodeWithSignature(
"registerAccount(string,address,string)",
connectionID, msg.sender, ""
));
require(result, "native call failed");
emit RegisterAccountResult(res);
}

function nativeQueryAccount(string memory connectionID, address addr) public returns (bytes memory) {
Expand All @@ -25,11 +22,10 @@ contract TestICA {
}

function nativeSubmitMsgs(string memory connectionID, string memory data) public {
(bool result, bytes memory res) = icaContract.call(abi.encodeWithSignature(
(bool result,) = icaContract.call(abi.encodeWithSignature(
"submitMsgs(string,address,string,uint256)",
connectionID, msg.sender, data, 300000000000
));
require(result, "native call failed");
emit SubmitMsgsResult(res);
}
}
39 changes: 10 additions & 29 deletions integration_tests/test_ibc_rly.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import pytest
from eth_utils import keccak, to_checksum_address
from pystarport import cluster
from web3._utils.contracts import find_matching_event_abi
from web3._utils.events import get_event_data
from web3.datastructures import AttributeDict

from .ibc_utils import (
Expand All @@ -24,7 +22,9 @@
CONTRACT_ABIS,
bech32_to_eth,
eth_to_bech32,
get_logs_since,
get_method_map,
get_topic_data,
module_address,
wait_for_fn,
wait_for_new_blocks,
Expand Down Expand Up @@ -82,25 +82,6 @@ def rly_transfer(ibc):
subprocess.run(cmd, check=True, shell=True)


def get_topic_data(w3, log):
method = method_map[log.topics[0].hex()]
name = method.split("(")[0]
event_abi = find_matching_event_abi(contract_info, name)
event_data = get_event_data(w3.codec, event_abi, log)
return name, event_data.args


def get_logs(w3, start):
end = w3.eth.get_block_number()
return w3.eth.get_logs(
{
"fromBlock": start,
"toBlock": end,
"address": [CONTRACT],
}
)


def coin_received(receiver, amt, denom):
return {
"receiver": receiver,
Expand Down Expand Up @@ -230,7 +211,7 @@ def check_balance_change():

wait_for_fn("balance change", check_balance_change)
assert old_dst_balance + dst_amount == new_dst_balance
logs = get_logs(w3, start)
logs = get_logs_since(w3, CONTRACT, start)
relayer0 = ibc.chainmain.cosmos_cli().address("relayer")
relayer = to_checksum_address(bech32_to_eth(relayer0))
cronos_addr = module_address("cronos")
Expand All @@ -247,7 +228,7 @@ def check_balance_change():
]

for i, log in enumerate(logs):
method_name, args = get_topic_data(w3, log)
method_name, args = get_topic_data(w3, method_map, contract_info, log)
assert args == AttributeDict(expected[i]), [i, method_name]


Expand All @@ -263,7 +244,7 @@ def test_ibc_incentivized_transfer(ibc):
wait_for_new_blocks(cli, 1)
start = w3.eth.get_block_number()
amount = ibc_incentivized_transfer(ibc)
logs = get_logs(w3, start)
logs = get_logs_since(w3, CONTRACT, start)
fee_denom = "ibcfee"
fee = f"{src_amount}{fee_denom}"
transfer_denom = "transfer/channel-0/basetcro"
Expand All @@ -290,7 +271,7 @@ def test_ibc_incentivized_transfer(ibc):
]
assert len(logs) == len(expected)
for i, log in enumerate(logs):
method_name, args = get_topic_data(w3, log)
method_name, args = get_topic_data(w3, method_map, contract_info, log)
assert args == AttributeDict(expected[i]), [i, method_name]


Expand Down Expand Up @@ -320,13 +301,13 @@ def test_cronos_transfer_source_tokens(ibc):
w3 = ibc.cronos.w3
start = w3.eth.get_block_number()
amount, contract = cronos_transfer_source_tokens(ibc)
logs = get_logs(w3, start)
logs = get_logs_since(w3, CONTRACT, start)
escrow = get_escrow_address(cli, channel)
dst_adr = ibc.chainmain.cosmos_cli().address("signer2")
expected = get_transfer_source_tokens_topics(dst_adr, amount, contract, escrow)
assert len(logs) == len(expected)
for i, log in enumerate(logs):
method_name, args = get_topic_data(w3, log)
method_name, args = get_topic_data(w3, method_map, contract_info, log)
assert args == AttributeDict(expected[i]), [i, method_name]


Expand All @@ -336,11 +317,11 @@ def test_cronos_transfer_source_tokens_with_proxy(ibc):
w3 = ibc.cronos.w3
start = w3.eth.get_block_number()
amount, contract = cronos_transfer_source_tokens_with_proxy(ibc)
logs = get_logs(w3, start)
logs = get_logs_since(w3, CONTRACT, start)
escrow = get_escrow_address(cli, channel)
dst_adr = ibc.chainmain.cosmos_cli().address("signer2")
expected = get_transfer_source_tokens_topics(dst_adr, amount, contract, escrow)
assert len(logs) == len(expected)
for i, log in enumerate(logs):
method_name, args = get_topic_data(w3, log)
method_name, args = get_topic_data(w3, method_map, contract_info, log)
assert args == AttributeDict(expected[i]), [i, method_name]
55 changes: 30 additions & 25 deletions integration_tests/test_ica_precompile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import re

import pytest
from web3._utils.contracts import find_matching_event_abi
from web3._utils.events import get_event_data
from web3.datastructures import AttributeDict

from .ibc_utils import (
funds_ica,
Expand All @@ -14,13 +13,21 @@
)
from .utils import (
ADDRS,
CONTRACT_ABIS,
CONTRACTS,
KEYS,
deploy_contract,
eth_to_bech32,
get_logs_since,
get_method_map,
get_topic_data,
send_transaction,
)

CONTRACT = "0x0000000000000000000000000000000000000066"
contract_info = json.loads(CONTRACT_ABIS["IICAModule"].read_text())
method_map = get_method_map(contract_info)


@pytest.fixture(scope="module")
def ibc(request, tmp_path_factory):
Expand All @@ -31,15 +38,6 @@ def ibc(request, tmp_path_factory):
yield from network


def get_log_data(w3, method_map, info, logs):
for _, log in enumerate(logs):
method = method_map[log.topics[0].hex()]
name = method.split("(")[0]
event_abi = find_matching_event_abi(info, name)
event_data = get_event_data(w3.codec, event_abi, log)
return event_data["args"]["data"].decode("utf-8")


def test_call(ibc):
connid = "connection-0"
cli_host = ibc.chainmain.cosmos_cli()
Expand All @@ -52,34 +50,41 @@ def test_call(ibc):
contract = deploy_contract(w3, jsonfile, (), keys)
data = {"from": addr, "gas": 200000}

print("register ica account")
print("register ica account from", contract.address)
start = w3.eth.get_block_number()
tx = contract.functions.nativeRegister(connid).build_transaction(data)
receipt = send_transaction(w3, tx, keys)
assert receipt.status == 1
info = json.loads(jsonfile.read_text())["abi"]
method_map = get_method_map(info)
res = get_log_data(w3, method_map, info, receipt.logs)
res = re.sub(r"\n\t", "", res)
channel_id, port_id = res.split("\x128")
print("port-id", port_id, "channel-id", channel_id)
logs = get_logs_since(w3, CONTRACT, start)
owner = eth_to_bech32(addr)
channel_id = "channel-0"
port_id = f"icacontroller-{owner}"
expected = [{"channelId": channel_id, "portId": port_id}]
for i, log in enumerate(logs):
method_name, args = get_topic_data(w3, method_map, contract_info, log)
assert args == AttributeDict(expected[i]), [i, method_name]

wait_for_check_channel_ready(cli_controller, connid, channel_id)

print("query ica account")
res = cli_controller.ica_query_account(connid, owner)
ica_address = res["interchain_account_address"]
print("query ica account", ica_address)
res = contract.caller.nativeQueryAccount(connid, addr)
ica_address = re.sub(r"\n>", "", res.decode("utf-8"))
print("ica address", ica_address)
res = re.sub(r"\n>", "", res.decode("utf-8"))
assert ica_address == res, res

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"))
start = w3.eth.get_block_number()
# submit transaction on host chain on behalf of interchain account
tx = contract.functions.nativeSubmitMsgs(connid, str).build_transaction(data)
receipt = send_transaction(w3, tx, keys)
assert receipt.status == 1
res = get_log_data(w3, method_map, info, receipt.logs)
res = re.sub(r"\x08", "", res)
print("seq", res)
logs = get_logs_since(w3, CONTRACT, start)
expected = [{"seq": "1"}]
for i, log in enumerate(logs):
method_name, args = get_topic_data(w3, method_map, contract_info, log)
assert args == AttributeDict(expected[i]), [i, method_name]
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
21 changes: 21 additions & 0 deletions integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from hexbytes import HexBytes
from pystarport import ledger
from web3._utils.contracts import abi_to_signature, find_matching_event_abi
from web3._utils.events import get_event_data
from web3._utils.method_formatters import receipt_formatter
from web3._utils.transactions import fill_nonce, fill_transaction_defaults
from web3.datastructures import AttributeDict
Expand Down Expand Up @@ -82,6 +83,7 @@ def contract_path(name, filename):

CONTRACT_ABIS = {
"IRelayerModule": Path(__file__).parent.parent / "build/IRelayerModule.abi",
"IICAModule": Path(__file__).parent.parent / "build/IICAModule.abi",
}


Expand Down Expand Up @@ -694,3 +696,22 @@ def get_method_map(contract_info):
key = f"0x{abi.event_signature_to_log_topic(signature).hex()}"
method_map[key] = signature
return method_map


def get_topic_data(w3, method_map, contract_info, log):
method = method_map[log.topics[0].hex()]
name = method.split("(")[0]
event_abi = find_matching_event_abi(contract_info, name)
event_data = get_event_data(w3.codec, event_abi, log)
return name, event_data.args


def get_logs_since(w3, addr, start):
end = w3.eth.get_block_number()
return w3.eth.get_logs(
{
"fromBlock": start,
"toBlock": end,
"address": [addr],
}
)

0 comments on commit 2247df5

Please sign in to comment.