Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: no stateful precompiled contract for ica #1163

Merged
merged 49 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
6cc52c4
register ica
mmsqe Sep 11, 2023
32101ba
send tx
mmsqe Sep 12, 2023
60a9ae4
test send tx
mmsqe Sep 12, 2023
288bde8
emit event
mmsqe Sep 12, 2023
41ef020
pass converter
mmsqe Sep 15, 2023
d042cf5
gen binding
mmsqe Sep 15, 2023
c5301b0
ica event
mmsqe Sep 15, 2023
3333640
choose default decorder
mmsqe Sep 18, 2023
4a2783e
Apply suggestions from code review
mmsqe Sep 18, 2023
fb417b4
emit res
mmsqe Sep 18, 2023
9a7a697
adjust emit
mmsqe Sep 18, 2023
766071f
pass encode proto msg
mmsqe Sep 18, 2023
158b138
fix lint
mmsqe Sep 18, 2023
6673875
Merge remote-tracking branch 'origin/main' into ica_precompile
mmsqe Sep 18, 2023
1cf427b
test
mmsqe Sep 18, 2023
af97a1a
add seq
mmsqe Sep 19, 2023
f6e7522
Revert "add seq"
mmsqe Sep 19, 2023
824e329
Revert "pass encode proto msg"
mmsqe Sep 19, 2023
806e529
Merge remote-tracking branch 'origin/main' into ica_precompile
mmsqe Sep 19, 2023
1ba0b09
avoid fork ibc-go
mmsqe Sep 19, 2023
5efb5ae
test timeout_duration
mmsqe Sep 20, 2023
942bc36
Update integration_tests/test_upgrade.py
mmsqe Sep 20, 2023
b5c12ca
Merge branch 'main' into ica_precompile
mmsqe Sep 20, 2023
cc98862
test ica rly
mmsqe Sep 20, 2023
f2b717d
cleanup
mmsqe Sep 20, 2023
99eedb9
Merge remote-tracking branch 'origin/main' into ica_precompile
mmsqe Sep 21, 2023
b7d26c1
test multi types
mmsqe Sep 21, 2023
b77834d
make proto-all
mmsqe Sep 21, 2023
93aafd0
Apply suggestions from code review
mmsqe Sep 21, 2023
dd806c0
test readonly call
mmsqe Sep 21, 2023
5912d0b
fix lint
mmsqe Sep 21, 2023
b393415
allow control by contract
mmsqe Sep 21, 2023
71bd37d
Merge remote-tracking branch 'origin/main' into ica_precompile
mmsqe Sep 21, 2023
b671ca6
pack output
mmsqe Sep 22, 2023
d4bee9b
point to log fix
mmsqe Sep 22, 2023
50923db
fix call ica
mmsqe Sep 22, 2023
96dc206
Merge remote-tracking branch 'origin/main' into ica_precompile
mmsqe Sep 24, 2023
acd7005
make use of binding
mmsqe Sep 24, 2023
06087b6
add last seq
mmsqe Sep 24, 2023
32c28eb
cleanup
mmsqe Sep 24, 2023
88a780e
seq as uint64
mmsqe Sep 24, 2023
94d6db0
Apply suggestions from code review
mmsqe Sep 25, 2023
2859b62
Merge remote-tracking branch 'origin/main' into ica_precompile
mmsqe Sep 25, 2023
60c2fb2
update rly
yihuang Sep 25, 2023
938d1bb
pass raw data into precompile
yihuang Sep 25, 2023
7189694
fix py lint
yihuang Sep 25, 2023
1fc78b1
fix packet data
mmsqe Sep 25, 2023
9ce4d3a
Merge remote-tracking branch 'origin/main' into ica_precompile
mmsqe Sep 25, 2023
7357456
fix resolve
mmsqe Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- (deps) [#1121](https://github.com/crypto-org-chain/cronos/pull/1121) Bump Cosmos-SDK to v0.47.5 and ibc-go to v7.2.0.
- [cronos#1014](https://github.com/crypto-org-chain/cronos/pull/1014) Support stateful precompiled contract for relayer.
- [cronos#1165](https://github.com/crypto-org-chain/cronos/pull/1165) Icaauth module is not adjusted correctly in ibc-go v7.2.0.
- [cronos#1163](https://github.com/crypto-org-chain/cronos/pull/1163) Support stateful precompiled contract for ica.
- [cronos#837](https://github.com/crypto-org-chain/cronos/pull/837) Support stateful precompiled contract for bank.

### Bug Fixes
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ func New(
[]vm.PrecompiledContract{
cronosprecompiles.NewBankContract(app.BankKeeper, appCodec),
cronosprecompiles.NewRelayerContract(app.IBCKeeper, appCodec),
cronosprecompiles.NewIcaContract(&app.ICAAuthKeeper, appCodec),
},
allKeys,
)
Expand Down
31 changes: 31 additions & 0 deletions integration_tests/contracts/contracts/TestICA.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.6;

contract TestICA {
address constant icaContract = 0x0000000000000000000000000000000000000066;

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

function nativeQueryAccount(string memory connectionID, address addr) public returns (bytes memory) {
(bool result, bytes memory data) = icaContract.call(abi.encodeWithSignature(
"queryAccount(string,address)",
connectionID, addr
));
require(result, "native call failed");
return data;
}

function nativeSubmitMsgs(string memory connectionID, string memory data) public {
(bool result,) = icaContract.call(abi.encodeWithSignature(
"submitMsgs(string,address,string,uint256)",
connectionID, msg.sender, data, 300000000000
));
require(result, "native call failed");
}
}
21 changes: 6 additions & 15 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,10 +1062,10 @@ def set_delegate_keys(self, val_addr, acc_addr, eth_addr, signature, **kwargs):
)

def query_gravity_params(self):
return json.loads(self.raw("query", "gravity", "params", home=self.data_dir))
return self.query_params("gravity")

def query_evm_params(self):
return json.loads(self.raw("query", "evm", "params", home=self.data_dir))
def query_params(self, module="cronos"):
return json.loads(self.raw("query", module, "params", home=self.data_dir))

def query_signer_set_txs(self):
return json.loads(
Expand Down Expand Up @@ -1275,7 +1275,7 @@ def icaauth_register_account(self, connid, **kwargs):
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp

def icaauth_submit_tx(self, connid, tx, **kwargs):
def icaauth_submit_tx(self, connid, tx, timeout_duration="1h", **kwargs):
default_kwargs = {
"home": self.data_dir,
"node": self.node_rpc,
Expand All @@ -1289,6 +1289,8 @@ def icaauth_submit_tx(self, connid, tx, **kwargs):
"submit-tx",
connid,
tx,
"--timeout-duration" if timeout_duration else None,
timeout_duration if timeout_duration else None,
"-y",
**(default_kwargs | kwargs),
)
Expand Down Expand Up @@ -1586,17 +1588,6 @@ def turn_bridge(self, enable, **kwargs):
)
)

def query_params(self):
"query cronos params"
return json.loads(
self.raw(
"query",
"cronos",
"params",
home=self.data_dir,
)
)

def evm_params(self, **kwargs):
default_kwargs = {
"node": self.node_rpc,
Expand Down
62 changes: 14 additions & 48 deletions integration_tests/test_ibc_rly.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import subprocess

import pytest
from eth_utils import abi, keccak, to_checksum_address
from eth_utils import keccak, to_checksum_address
from pystarport import cluster
from web3._utils.contracts import abi_to_signature, find_matching_event_abi
from web3._utils.events import get_event_data
from web3.datastructures import AttributeDict

from .ibc_utils import (
Expand All @@ -24,14 +22,17 @@
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,
)

CONTRACT = "0x0000000000000000000000000000000000000065"
method_map = None
contract_info = None
contract_info = json.loads(CONTRACT_ABIS["IRelayerModule"].read_text())
method_map = get_method_map(contract_info)
cronos_signer2 = ADDRS["signer2"]
src_amount = 10
src_denom = "basecro"
Expand Down Expand Up @@ -81,41 +82,6 @@ def rly_transfer(ibc):
subprocess.run(cmd, check=True, shell=True)


def get_method_map():
global contract_info
if contract_info is None:
contract_info = json.loads(CONTRACT_ABIS["IRelayerModule"].read_text())
global method_map
if method_map is None:
method_map = {}
for item in contract_info:
event_abi = find_matching_event_abi(contract_info, item["name"])
signature = abi_to_signature(event_abi)
key = f"0x{abi.event_signature_to_log_topic(signature).hex()}"
method_map[key] = signature
return method_map, contract_info


def get_topic_data(w3, log):
method_map, info = get_method_map()
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 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 @@ -245,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 @@ -262,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 @@ -278,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 @@ -305,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 @@ -335,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 @@ -351,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]
1 change: 1 addition & 0 deletions integration_tests/test_ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_ica(ibc, tmp_path):
rsp = cli_controller.icaauth_submit_tx(
connid,
generated_tx,
timeout_duration="2h",
from_="signer2",
)
assert rsp["code"] == 0, rsp["raw_log"]
Expand Down
94 changes: 94 additions & 0 deletions integration_tests/test_ica_precompile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import json
import re

import pytest
from web3.datastructures import AttributeDict

from .ibc_utils import (
funds_ica,
prepare_network,
wait_for_check_channel_ready,
wait_for_check_tx,
)
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):
"prepare-network"
name = "ibc"
path = tmp_path_factory.mktemp(name)
network = prepare_network(path, name, incentivized=False, connection_only=True)
yield from network


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)


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

w3 = ibc.cronos.w3
addr = ADDRS["signer2"]
keys = KEYS["signer2"]
jsonfile = CONTRACTS["TestICA"]
contract = deploy_contract(w3, jsonfile, (), keys)
data = {"from": addr, "gas": 200000}

print("register ica account from", contract.address)
tx = contract.functions.nativeRegister(connid).build_transaction(data)
receipt = send_transaction(w3, tx, keys)
assert receipt.status == 1
owner = eth_to_bech32(addr)
channel_id = "channel-0"
wait_for_check_channel_ready(cli_controller, connid, channel_id)
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)
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)
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
receipt = send_transaction(w3, tx, keys)
assert receipt.status == 1
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
3 changes: 3 additions & 0 deletions integration_tests/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def test_cosmovisor_upgrade(custom_cronos: Cronos, tmp_path_factory):
}
}

rsp = cli.query_params("icaauth")
assert rsp["params"]["min_timeout_duration"] == "3600s", rsp

# migrate to sdk v0.47
custom_cronos.supervisorctl("stop", "all")
sdk_version = "v0.47"
Expand Down
Loading
Loading