From 148b61beffce9bde7a93b6c0481149b3b0640b41 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Thu, 21 Mar 2024 16:22:48 +0800 Subject: [PATCH] Problem: multi ibc transfer is not tested (#1359) * Problem: multi ibc transfer is not tested * Apply suggestions from code review --- integration_tests/configs/ibc.jsonnet | 14 ++++- integration_tests/cosmoscli.py | 20 ++++++- integration_tests/ibc_utils.py | 77 +++++++++++++++++++++++++++ integration_tests/test_ibc_rly.py | 5 ++ scripts/.env | 2 +- 5 files changed, 115 insertions(+), 3 deletions(-) diff --git a/integration_tests/configs/ibc.jsonnet b/integration_tests/configs/ibc.jsonnet index 74d93476bd..d63860e654 100644 --- a/integration_tests/configs/ibc.jsonnet +++ b/integration_tests/configs/ibc.jsonnet @@ -7,7 +7,13 @@ config { key_name: 'signer1', accounts: super.accounts[:std.length(super.accounts) - 1] + [super.accounts[std.length(super.accounts) - 1] { coins: super.coins + ',100000000000ibcfee', - }], + }] + [ + { + name: 'user' + i, + coins: '30000000000000000000000basetcro', + } + for i in std.range(1, 20) + ], 'app-config'+: { 'index-events': super['index-events'] + ['message.action'], }, @@ -76,6 +82,12 @@ config { coins: '10000000000000cro', mnemonic: '${SIGNER2_MNEMONIC}', }, + ] + [ + { + name: 'user' + i, + coins: '10000000000000cro', + } + for i in std.range(1, 20) ], genesis: { app_state: { diff --git a/integration_tests/cosmoscli.py b/integration_tests/cosmoscli.py index 10843800ca..ee99f19de2 100644 --- a/integration_tests/cosmoscli.py +++ b/integration_tests/cosmoscli.py @@ -838,12 +838,14 @@ def ibc_transfer( amount, channel, # src channel target_version, # chain version number of target chain + event_query_tx_for=False, **kwargs, ): default_kwargs = { "home": self.data_dir, + "broadcast_mode": "sync", } - return json.loads( + rsp = json.loads( self.raw( "tx", "ibc-transfer", @@ -864,6 +866,9 @@ def ibc_transfer( **(default_kwargs | kwargs), ) ) + if rsp["code"] == 0 and event_query_tx_for: + rsp = self.event_query_tx_for(rsp["txhash"]) + return rsp def ibc_escrow_address(self, port, channel): res = self.raw( @@ -877,6 +882,19 @@ def ibc_escrow_address(self, port, channel): ).decode("utf-8") return re.sub(r"\n", "", res) + def ibc_denom_trace(self, path, node): + denom_hash = hashlib.sha256(path.encode()).hexdigest().upper() + return json.loads( + self.raw( + "query", + "ibc-transfer", + "denom-trace", + denom_hash, + node=node, + output="json", + ) + )["denom_trace"] + def export(self): return self.raw("export", home=self.data_dir) diff --git a/integration_tests/ibc_utils.py b/integration_tests/ibc_utils.py index baf9450df8..c6c0f32f30 100644 --- a/integration_tests/ibc_utils.py +++ b/integration_tests/ibc_utils.py @@ -331,6 +331,83 @@ def get_balances(chain, addr): return chain.cosmos_cli().balances(addr) +def ibc_multi_transfer(ibc): + chains = [ibc.cronos.cosmos_cli(), ibc.chainmain.cosmos_cli()] + users = [f"user{i}" for i in range(1, 21)] + addrs0 = [chains[0].address(user) for user in users] + addrs1 = [chains[1].address(user) for user in users] + denom0 = "basetcro" + denom1 = "basecro" + channel0 = "channel-0" + channel1 = "channel-0" + old_balance0 = 30000000000000000000000 + old_balance1 = 1000000000000000000000 + path = f"transfer/{channel1}/{denom0}" + denom_hash = hashlib.sha256(path.encode()).hexdigest().upper() + amount = 1000 + expected = [ + {"denom": denom1, "amount": f"{old_balance1}"}, + {"denom": f"ibc/{denom_hash}", "amount": f"{amount}"}, + ] + + for i, _ in enumerate(users): + rsp = chains[0].ibc_transfer( + addrs0[i], + addrs1[i], + f"{amount}{denom0}", + channel0, + 1, + fees=f"1000{denom1}", + event_query_tx_for=True, + ) + assert rsp["code"] == 0, rsp["raw_log"] + balance = chains[1].balance(addrs1[i], denom1) + assert balance == old_balance1, balance + balance = chains[0].balance(addrs0[i], denom0) + assert balance == old_balance0 - amount, balance + + def assert_trace_balance(addr): + balance = chains[1].balances(addr) + if len(balance) > 1: + assert balance == expected, balance + return True + else: + return False + + denom_trace = chains[0].ibc_denom_trace(path, ibc.chainmain.node_rpc(0)) + assert denom_trace == {"path": f"transfer/{channel1}", "base_denom": denom0} + for i, _ in enumerate(users): + wait_for_fn("assert balance", lambda: assert_trace_balance(addrs1[i])) + + # chainmain-1 -> cronos_777-1 + amt = amount // 2 + + def assert_balance(addr): + balance = chains[0].balance(addr, denom0) + if balance > old_balance0 - amount: + assert balance == old_balance0 - amt, balance + return True + else: + return False + + for _ in range(0, 2): + for i, _ in enumerate(users): + rsp = chains[1].ibc_transfer( + addrs1[i], + addrs0[i], + f"{amt}ibc/{denom_hash}", + channel1, + 1, + fees=f"100000000{denom1}", + ) + assert rsp["code"] == 0, rsp["raw_log"] + + for i, _ in enumerate(users): + wait_for_fn("assert balance", lambda: assert_balance(addrs0[i])) + + old_balance0 += amt + + def ibc_incentivized_transfer(ibc): chains = [ibc.cronos.cosmos_cli(), ibc.chainmain.cosmos_cli()] receiver = chains[1].address("signer2") diff --git a/integration_tests/test_ibc_rly.py b/integration_tests/test_ibc_rly.py index d4ced56e3a..73b624d196 100644 --- a/integration_tests/test_ibc_rly.py +++ b/integration_tests/test_ibc_rly.py @@ -14,6 +14,7 @@ hermes_transfer, ibc_denom, ibc_incentivized_transfer, + ibc_multi_transfer, prepare_network, ) from .utils import ( @@ -368,3 +369,7 @@ def test_cronos_transfer_source_tokens(ibc): def test_cronos_transfer_source_tokens_with_proxy(ibc): assert_transfer_source_tokens_topics(ibc, cronos_transfer_source_tokens_with_proxy) + + +def test_ibc_multi(ibc): + ibc_multi_transfer(ibc) diff --git a/scripts/.env b/scripts/.env index a30eeaa351..647508dd10 100644 --- a/scripts/.env +++ b/scripts/.env @@ -6,4 +6,4 @@ export SIGNER1_MNEMONIC="shed crumble dismiss loyal latin million oblige gesture export SIGNER2_MNEMONIC="night renew tonight dinner shaft scheme domain oppose echo summer broccoli agent face guitar surface belt veteran siren poem alcohol menu custom crunch index" export SIGNER3_MNEMONIC="step endless survey brand topic warrior merry boat metal throw tag recycle pitch animal drill jar hero library arm swift kitten proof acoustic chef" export CRONOS_ADMIN="crc12luku6uxehhak02py4rcz65zu0swh7wjsrw0pp" -export IBC_CRO_DENOM="ibc/6411AE2ADA1E73DB59DB151A8988F9B7D5E7E233D8414DB6817F8F1A01611F86" +export IBC_CRO_DENOM="ibc/6411AE2ADA1E73DB59DB151A8988F9B7D5E7E233D8414DB6817F8F1A01611F86" \ No newline at end of file