From 643290323ac45c81a20012c16a80605d2e46facf Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 27 May 2024 16:26:53 +0800 Subject: [PATCH] Problem: multi txs with more validators is not tested in e2ee --- .../configs/more_validators.jsonnet | 24 ++++++++ integration_tests/test_e2ee.py | 28 +-------- integration_tests/test_e2ee_evm.py | 61 +++++++++++++++++++ integration_tests/utils.py | 30 +++++++++ 4 files changed, 116 insertions(+), 27 deletions(-) create mode 100644 integration_tests/configs/more_validators.jsonnet create mode 100644 integration_tests/test_e2ee_evm.py diff --git a/integration_tests/configs/more_validators.jsonnet b/integration_tests/configs/more_validators.jsonnet new file mode 100644 index 0000000000..33cb029f3a --- /dev/null +++ b/integration_tests/configs/more_validators.jsonnet @@ -0,0 +1,24 @@ +local config = import 'default.jsonnet'; + +config { + 'cronos_777-1'+: { + config+: { + mempool: { + recheck: false, + }, + }, + validators+: [{ + coins: '1000000000000000000stake,10000000000000000000000basetcro', + staked: '1000000000000000000stake', + client_config: { + 'broadcast-mode': 'sync', + }, + }, { + coins: '1000000000000000000stake,10000000000000000000000basetcro', + staked: '1000000000000000000stake', + client_config: { + 'broadcast-mode': 'sync', + }, + }], + }, +} diff --git a/integration_tests/test_e2ee.py b/integration_tests/test_e2ee.py index 6abc44e61e..f2243ef945 100644 --- a/integration_tests/test_e2ee.py +++ b/integration_tests/test_e2ee.py @@ -1,9 +1,7 @@ -import json - import pytest from .network import Cronos -from .utils import wait_for_new_blocks +from .utils import encrypt_to_validators, gen_validator_identity, wait_for_new_blocks def test_register(cronos: Cronos): @@ -15,20 +13,6 @@ def test_register(cronos: Cronos): assert not cli.query_e2ee_key(cli.address("validator")) -def gen_validator_identity(cronos: Cronos): - for i in range(len(cronos.config["validators"])): - cli = cronos.cosmos_cli(i) - if cli.query_e2ee_key(cli.address("validator")): - return - pubkey = cli.keygen() - cli.register_e2ee_key(pubkey, _from="validator") - assert cli.query_e2ee_key(cli.address("validator")) == pubkey - - cronos.supervisorctl("restart", f"cronos_777-1-node{i}") - - wait_for_new_blocks(cronos.cosmos_cli(), 1) - - def test_encrypt_decrypt(cronos): gen_validator_identity(cronos) @@ -62,16 +46,6 @@ def test_encrypt_decrypt(cronos): assert cli1.decrypt(cipherfile) == content -def encrypt_to_validators(cli, content): - blocklist = json.dumps(content) - plainfile = cli.data_dir / "plaintext" - plainfile.write_text(blocklist) - cipherfile = cli.data_dir / "ciphertext" - cli.encrypt_to_validators(plainfile, output=cipherfile) - rsp = cli.store_blocklist(cipherfile, _from="validator") - assert rsp["code"] == 0, rsp["raw_log"] - - def test_block_list(cronos): gen_validator_identity(cronos) cli = cronos.cosmos_cli() diff --git a/integration_tests/test_e2ee_evm.py b/integration_tests/test_e2ee_evm.py new file mode 100644 index 0000000000..91f9cc34fd --- /dev/null +++ b/integration_tests/test_e2ee_evm.py @@ -0,0 +1,61 @@ +from pathlib import Path + +import pytest +from eth_utils import to_checksum_address +from pystarport import ports + +from .network import setup_custom_cronos +from .utils import ( + ADDRS, + bech32_to_eth, + encrypt_to_validators, + gen_validator_identity, + get_unconfirmed_txs, + wait_for_new_blocks, +) + + +@pytest.fixture(scope="module") +def custom_cronos(tmp_path_factory): + path = tmp_path_factory.mktemp("more_validators") + yield from setup_custom_cronos( + path, 26800, Path(__file__).parent / "configs/more_validators.jsonnet" + ) + + +def test_block_list_evm(custom_cronos): + gen_validator_identity(custom_cronos) + cli = custom_cronos.cosmos_cli() + user = cli.address("signer2") + # set blocklist + encrypt_to_validators(cli, {"addresses": [user]}) + u = to_checksum_address(bech32_to_eth(user)) + w3 = custom_cronos.w3 + n = w3.eth.get_transaction_count(u) + tx = { + "from": u, + "to": ADDRS["community"], + "value": 1, + } + tx0 = tx | {"nonce": n} + tx1 = tx | {"nonce": n + 1} + base_port = custom_cronos.base_port(0) + p = ports.rpc_port(base_port) + assert not get_unconfirmed_txs(p) + txhash = w3.eth.send_transaction(tx0).hex() + txhash1 = w3.eth.send_transaction(tx1).hex() + nonce = int(cli.query_account(user)["base_account"]["sequence"]) + + r = get_unconfirmed_txs(p) + assert len(r) == 2 + # clear blocklist + encrypt_to_validators(cli, {}) + + # the blocked tx should be unblocked now + wait_for_new_blocks(cli, 1) + last = int(cli.query_account(user)["base_account"]["sequence"]) + assert nonce + 2 == last + assert not get_unconfirmed_txs(p) + + for txhash in [txhash, txhash1]: + assert len(cli.tx_search(f"ethereum_tx.ethereumTxHash='{txhash}'")["txs"]) == 1 diff --git a/integration_tests/utils.py b/integration_tests/utils.py index 8febe73595..5349d1a88d 100644 --- a/integration_tests/utils.py +++ b/integration_tests/utils.py @@ -755,3 +755,33 @@ def get_send_enable(port): url = f"http://127.0.0.1:{port}/cosmos/bank/v1beta1/params" raw = requests.get(url).json() return raw["params"]["send_enabled"] + + +def gen_validator_identity(cronos): + for i in range(len(cronos.config["validators"])): + cli = cronos.cosmos_cli(i) + if cli.query_e2ee_key(cli.address("validator")): + return + pubkey = cli.keygen() + cli.register_e2ee_key(pubkey, _from="validator") + assert cli.query_e2ee_key(cli.address("validator")) == pubkey + + cronos.supervisorctl("restart", f"cronos_777-1-node{i}") + + wait_for_new_blocks(cronos.cosmos_cli(), 1) + + +def encrypt_to_validators(cli, content): + blocklist = json.dumps(content) + plainfile = cli.data_dir / "plaintext" + plainfile.write_text(blocklist) + cipherfile = cli.data_dir / "ciphertext" + cli.encrypt_to_validators(plainfile, output=cipherfile) + rsp = cli.store_blocklist(cipherfile, _from="validator") + assert rsp["code"] == 0, rsp["raw_log"] + + +def get_unconfirmed_txs(port): + url = f"http://127.0.0.1:{port}/unconfirmed_txs" + raw = requests.get(url).json() + return raw["result"]["txs"]