From 4a1b76a572de108037fdd912001d59ddbde77068 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 14 Oct 2024 11:42:03 +0800 Subject: [PATCH] test incentive --- CHANGELOG.md | 1 + integration_tests/ibc_utils.py | 148 +++++++++++++++++++++++++++++++-- integration_tests/test_ibc.py | 9 +- nix/sources.json | 8 +- x/nft/client/cli/cli_test.go | 2 - 5 files changed, 153 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad439e781..e2f0d178d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [#1061](https://github.com/crypto-org-chain/chain-main/pull/1061) Integrate sdk 0.50. - [#1068](https://github.com/crypto-org-chain/chain-main/pull/1068) Upgrade ibc-go to `v8.3.2` and remove icaauth module. - [#1084](https://github.com/crypto-org-chain/chain-main/pull/1084) Add MsgModuleQuerySafe in allowed messages for ica host param. +- [#1085](https://github.com/crypto-org-chain/chain-main/pull/1085) Upgrade ibc-go to `v8.5.1`. *Dec 6, 2023* diff --git a/integration_tests/ibc_utils.py b/integration_tests/ibc_utils.py index ee15453c6..d36985ae9 100644 --- a/integration_tests/ibc_utils.py +++ b/integration_tests/ibc_utils.py @@ -31,14 +31,28 @@ def search_target(query, key, chains): return results +def register_fee_payee(src_chain, dst_chain): + rsp = dst_chain.register_counterparty_payee( + "transfer", + "channel-0", + dst_chain.address("relayer"), + src_chain.address("relayer"), + from_="relayer", + fees="1basecro", + ) + assert rsp["code"] == 0, rsp["raw_log"] + + def start_and_wait_relayer( cluster, port="transfer", chains=["ibc-0", "ibc-1"], start_relaying=True, init_relayer=True, + incentivized=False, ): relayer = wait_relayer_ready(cluster) + version = {"fee_version": "ics29-1", "app_version": "ics20-1"} if init_relayer: # create connection and channel subprocess.run( @@ -56,13 +70,23 @@ def start_and_wait_relayer( chains[1], "--new-client-connection", "--yes", - ], + ] + + ( + [ + "--channel-version", + json.dumps(version), + ] + if incentivized + else [] + ), check=True, ) # start relaying if start_relaying: cluster[chains[0]].supervisor.startProcess("relayer-demo") + if incentivized: + register_fee_payee(cluster[chains[0]], cluster[chains[1]]) query = relayer + ["query", "channels", "--chain"] return search_target(query, "channel", chains) @@ -73,13 +97,14 @@ def ibc_transfer_flow(cluster, src_channel, dst_channel): raw = cluster["ibc-0"].cosmos_cli().raw denom = "basecro" amt = 10000 - origin = 10000000000 + origin0 = 10000000000 + origin1 = 10000000000 - 1 addr_0 = cluster["ibc-0"].address("relayer") addr_1 = cluster["ibc-1"].address("relayer") - assert cluster["ibc-0"].balance(addr_0) == origin - assert cluster["ibc-1"].balance(addr_1) == origin + assert cluster["ibc-0"].balance(addr_0) == origin0 + assert cluster["ibc-1"].balance(addr_1) == origin1 # do a transfer from ibc-0 to ibc-1 rsp = cluster["ibc-0"].ibc_transfer( @@ -87,7 +112,7 @@ def ibc_transfer_flow(cluster, src_channel, dst_channel): ) assert rsp["code"] == 0, rsp["raw_log"] # sender balance decreased - assert cluster["ibc-0"].balance(addr_0) == origin - amt + assert cluster["ibc-0"].balance(addr_0) == origin0 - amt denom_hash = ( hashlib.sha256(f"transfer/{dst_channel}/{denom}".encode()).hexdigest().upper() ) @@ -132,5 +157,114 @@ def check_balance_change(): wait_for_fn("balance change", check_balance_change) # both accounts return to normal - for i, cli in enumerate(cluster.values()): - assert cli.balance(cli.address("relayer"), denom=denom) == origin + assert cluster["ibc-0"].balance(addr_0, denom=denom) == origin0 + assert cluster["ibc-1"].balance(addr_1, denom=denom) == origin1 + + +def find_log_event_attrs(events, ev_type, cond=None): + for ev in events: + if ev["type"] == ev_type: + attrs = {attr["key"]: attr["value"] for attr in ev["attributes"]} + if cond is None or cond(attrs): + return attrs + return None + + +def ibc_incentivized_transfer(cluster): + chains = [cluster["ibc-0"].cosmos_cli(), cluster["ibc-1"].cosmos_cli()] + receiver = chains[1].address("signer") + sender = chains[0].address("signer2") + relayer = chains[0].address("relayer") + amount = 1000 + fee_denom = "ibcfee" + base_denom = "basecro" + old_amt_fee = chains[0].balance(relayer, fee_denom) + old_amt_sender_fee = chains[0].balance(sender, fee_denom) + old_amt_sender_base = chains[0].balance(sender, base_denom) + old_amt_receiver_base = chains[1].balance(receiver, "basecro") + current = chains[1].balances(receiver) + assert old_amt_sender_base == 200000000000 + assert old_amt_receiver_base == 20000000000 + src_channel = "channel-0" + dst_channel = "channel-0" + rsp = chains[0].ibc_transfer( + sender, + receiver, + f"{amount}{base_denom}", + src_channel, + 1, + fees="0basecro", + ) + assert rsp["code"] == 0, rsp["raw_log"] + rsp = chains[0].event_query_tx_for(rsp["txhash"]) + + def cb(attrs): + return "packet_sequence" in attrs + + evt = find_log_event_attrs(rsp["events"], "send_packet", cb) + print("packet event", evt) + packet_seq = int(evt["packet_sequence"]) + fee = f"10{fee_denom}" + rsp = chains[0].pay_packet_fee( + "transfer", + src_channel, + packet_seq, + recv_fee=fee, + ack_fee=fee, + timeout_fee=fee, + from_=sender, + ) + assert rsp["code"] == 0, rsp["raw_log"] + # fee is locked + current = chains[0].balance(sender, fee_denom) + # https://github.com/cosmos/ibc-go/pull/5571 + assert current == old_amt_sender_fee - 20, current + + # wait for relayer receive the fee + def check_fee(): + amt = chains[0].balance(relayer, fee_denom) + if amt > old_amt_fee: + assert amt == old_amt_fee + 20, amt + return True + else: + return False + + wait_for_fn("wait for relayer to receive the fee", check_fee) + + # timeout fee is refunded + actual = chains[0].balances(sender) + assert actual == [ + {"denom": base_denom, "amount": f"{old_amt_sender_base - amount}"}, + {"denom": fee_denom, "amount": f"{old_amt_sender_fee - 20}"}, + ], actual + path = f"transfer/{dst_channel}/{base_denom}" + denom_hash = hashlib.sha256(path.encode()).hexdigest().upper() + denom_trace = chains[0].ibc_denom_trace(path, cluster["ibc-1"].node_rpc(0)) + assert denom_trace == {"path": f"transfer/{dst_channel}", "base_denom": base_denom} + + current = chains[1].balances(receiver) + assert current == [ + {"denom": "basecro", "amount": f"{old_amt_receiver_base}"}, + {"denom": f"ibc/{denom_hash}", "amount": f"{amount}"}, + ], current + # transfer back + fee_amount = 100000000 + rsp = chains[1].ibc_transfer( + receiver, + sender, + f"{amount}ibc/{denom_hash}", + dst_channel, + 1, + fees=f"{fee_amount}basecro", + ) + assert rsp["code"] == 0, rsp["raw_log"] + + def check_balance_change(): + return chains[0].balance(sender, base_denom) != old_amt_sender_base - amount + + wait_for_fn("balance change", check_balance_change) + actual = chains[0].balance(sender, base_denom) + assert actual == old_amt_sender_base, actual + current = chains[1].balance(receiver, "basecro") + assert current == old_amt_receiver_base - fee_amount + return amount, packet_seq diff --git a/integration_tests/test_ibc.py b/integration_tests/test_ibc.py index 9e860bb9e..80eda5bc4 100644 --- a/integration_tests/test_ibc.py +++ b/integration_tests/test_ibc.py @@ -8,7 +8,11 @@ import pytest import yaml -from .ibc_utils import ibc_transfer_flow, start_and_wait_relayer +from .ibc_utils import ( + ibc_incentivized_transfer, + ibc_transfer_flow, + start_and_wait_relayer, +) from .utils import cluster_fixture pytestmark = pytest.mark.ibc @@ -25,8 +29,9 @@ def cluster(worker_index, pytestconfig, tmp_path_factory): def test_ibc(cluster): - src_channel, dst_channel = start_and_wait_relayer(cluster) + src_channel, dst_channel = start_and_wait_relayer(cluster, incentivized=True) ibc_transfer_flow(cluster, src_channel, dst_channel) + ibc_incentivized_transfer(cluster) @pytest.mark.skip(reason="chain-id change don't has effect") diff --git a/nix/sources.json b/nix/sources.json index 646933515..5d97e4594 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -24,15 +24,15 @@ "url_template": "https://github.com///archive/.tar.gz" }, "ibc-rs": { - "branch": "v1.9.0", + "branch": "v1.10.3", "description": "IBC modules and relayer - Formal specifications and Rust implementation", "homepage": "", "owner": "informalsystems", "repo": "ibc-rs", - "rev": "a026d661a74b93ffdefdc9f431007b1a6b4e95d0", - "sha256": "134h0s80laifd0ln1lx9ljcmq8pxpi5s7xmf1q5bz1mrrwmpmk9q", + "rev": "e26d356abe23be2d401d1ba3d6ebb2768f77ebd5", + "sha256": "1jzhlgx2jj1pfs6v7bvcijwrhmlgvm15mxc4mmidnnm123nww7yb", "type": "tarball", - "url": "https://github.com/informalsystems/hermes/archive/a026d661a74b93ffdefdc9f431007b1a6b4e95d0.tar.gz", + "url": "https://github.com/informalsystems/hermes/archive/e26d356abe23be2d401d1ba3d6ebb2768f77ebd5.tar.gz", "url_template": "https://github.com///archive/.tar.gz" }, "nixpkgs": { diff --git a/x/nft/client/cli/cli_test.go b/x/nft/client/cli/cli_test.go index 6fec524a2..827f170b6 100644 --- a/x/nft/client/cli/cli_test.go +++ b/x/nft/client/cli/cli_test.go @@ -65,8 +65,6 @@ func (s *IntegrationTestSuite) eventQueryTxFor(val *network.Validator, hash stri bz, err := clitestutil.ExecTestCLICmd(val.ClientCtx, rpc.QueryEventForTxCmd(), []string{hash, fmt.Sprintf("--%s=json", flags.FlagOutput)}) s.Require().NoError(err) respType := proto.Message(&sdk.TxResponse{}) - fmt.Println("mm-bz", bz.String()) - fmt.Println("mm-respType", respType) s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType), bz.String()) return respType.(*sdk.TxResponse) }