From 2101dd67c6c233ed4ebc3602b934ede753d2f006 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Thu, 7 Sep 2023 09:09:25 +0800 Subject: [PATCH] Problem: start with exported genesis is not tested (backport #1140) (#1139) * add edit_ini_sections * Problem: start with exported genesis is not tested --- integration_tests/cosmoscli.py | 2 +- integration_tests/test_upgrade.py | 51 +++++++++++++---------- integration_tests/test_upgrade_gravity.py | 42 +++++++++---------- integration_tests/utils.py | 14 +++++++ 4 files changed, 63 insertions(+), 46 deletions(-) diff --git a/integration_tests/cosmoscli.py b/integration_tests/cosmoscli.py index e1ccd90992..b8ab038cd9 100644 --- a/integration_tests/cosmoscli.py +++ b/integration_tests/cosmoscli.py @@ -876,7 +876,7 @@ def export(self): return self.raw("export", home=self.data_dir) def unsaferesetall(self): - return self.raw("unsafe-reset-all") + return self.raw("tendermint", "unsafe-reset-all") def create_nft(self, from_addr, denomid, denomname, schema, fees): return json.loads( diff --git a/integration_tests/test_upgrade.py b/integration_tests/test_upgrade.py index d2f8c9cf89..84fec8a5fb 100644 --- a/integration_tests/test_upgrade.py +++ b/integration_tests/test_upgrade.py @@ -1,7 +1,6 @@ -import configparser import json -import re import subprocess +from datetime import datetime, timedelta from pathlib import Path import pytest @@ -14,10 +13,12 @@ ADDRS, CONTRACTS, deploy_contract, + edit_ini_sections, parse_events, send_transaction, wait_for_block, wait_for_block_time, + wait_for_new_blocks, wait_for_port, ) @@ -37,28 +38,20 @@ def post_init(path, base_port, config): prepare cosmovisor for each node """ chain_id = "cronos_777-1" - cfg = json.loads((path / chain_id / "config.json").read_text()) + data = path / chain_id + cfg = json.loads((data / "config.json").read_text()) for i, _ in enumerate(cfg["validators"]): - home = path / chain_id / f"node{i}" + home = data / f"node{i}" init_cosmovisor(home) - # patch supervisord ini config - ini_path = path / chain_id / SUPERVISOR_CONFIG_FILE - ini = configparser.RawConfigParser() - ini.read(ini_path) - reg = re.compile(rf"^program:{chain_id}-node(\d+)") - for section in ini.sections(): - m = reg.match(section) - if m: - i = m.group(1) - ini[section].update( - { - "command": f"cosmovisor start --home %(here)s/node{i}", - "environment": f"DAEMON_NAME=cronosd,DAEMON_HOME=%(here)s/node{i}", - } - ) - with ini_path.open("w") as fp: - ini.write(fp) + edit_ini_sections( + chain_id, + data / SUPERVISOR_CONFIG_FILE, + lambda i, _: { + "command": f"cosmovisor start --home %(here)s/node{i}", + "environment": f"DAEMON_NAME=cronosd,DAEMON_HOME=%(here)s/node{i}", + }, + ) @pytest.fixture(scope="module") @@ -99,7 +92,7 @@ def test_cosmovisor_upgrade(custom_cronos: Cronos, tmp_path_factory): custom_cronos.supervisorctl("start", "cronos_777-1-node0", "cronos_777-1-node1") wait_for_port(ports.evmrpc_port(custom_cronos.base_port(0))) - + wait_for_new_blocks(cli, 1) height = cli.block_height() target_height = height + 15 print("upgrade height", target_height) @@ -210,3 +203,17 @@ def test_cosmovisor_upgrade(custom_cronos: Cronos, tmp_path_factory): json.dump(cli.migrate_cronos_genesis(cronos_version, str(file_path1)), fp) fp.flush() print(cli.validate_genesis(str(file_path2))) + + # update the genesis time = current time + 5 secs + newtime = datetime.utcnow() + timedelta(seconds=5) + newtime = newtime.replace(tzinfo=None).isoformat("T") + "Z" + config = custom_cronos.config + config["genesis-time"] = newtime + for i, _ in enumerate(config["validators"]): + genesis = json.load(open(file_path2)) + genesis["genesis_time"] = config.get("genesis-time") + file = custom_cronos.cosmos_cli(i).data_dir / "config/genesis.json" + file.write_text(json.dumps(genesis)) + custom_cronos.supervisorctl("start", "cronos_777-1-node0", "cronos_777-1-node1") + wait_for_new_blocks(custom_cronos.cosmos_cli(), 1) + custom_cronos.supervisorctl("stop", "all") diff --git a/integration_tests/test_upgrade_gravity.py b/integration_tests/test_upgrade_gravity.py index f29b39da02..3ddf737ce8 100644 --- a/integration_tests/test_upgrade_gravity.py +++ b/integration_tests/test_upgrade_gravity.py @@ -1,6 +1,4 @@ -import configparser import json -import re import subprocess from pathlib import Path @@ -10,7 +8,13 @@ from pystarport.cluster import SUPERVISOR_CONFIG_FILE from .network import Cronos, setup_custom_cronos -from .utils import parse_events, wait_for_block, wait_for_block_time, wait_for_port +from .utils import ( + edit_ini_sections, + parse_events, + wait_for_block, + wait_for_block_time, + wait_for_port, +) def init_cosmovisor(home): @@ -28,29 +32,21 @@ def post_init(path, base_port, config): prepare cosmovisor for each node """ chain_id = "cronos_777-1" - cfg = json.loads((path / chain_id / "config.json").read_text()) + data = path / chain_id + cfg = json.loads((data / "config.json").read_text()) for i, _ in enumerate(cfg["validators"]): - home = path / chain_id / f"node{i}" + home = data / f"node{i}" init_cosmovisor(home) - # patch supervisord ini config - ini_path = path / chain_id / SUPERVISOR_CONFIG_FILE - ini = configparser.RawConfigParser() - ini.read(ini_path) - reg = re.compile(rf"^program:{chain_id}-node(\d+)") - for section in ini.sections(): - m = reg.match(section) - if m: - i = m.group(1) - ini[section].update( - { - "command": f"cosmovisor start --home %(here)s/node{i}" - f" --trace --unsafe-experimental", - "environment": f"DAEMON_NAME=cronosd,DAEMON_HOME=%(here)s/node{i}", - } - ) - with ini_path.open("w") as fp: - ini.write(fp) + edit_ini_sections( + chain_id, + data / SUPERVISOR_CONFIG_FILE, + lambda i, _: { + "command": f"cosmovisor start --home %(here)s/node{i}" + f" --trace --unsafe-experimental", + "environment": f"DAEMON_NAME=cronosd,DAEMON_HOME=%(here)s/node{i}", + }, + ) @pytest.fixture(scope="module") diff --git a/integration_tests/utils.py b/integration_tests/utils.py index 6df869c722..f8ae412b8e 100644 --- a/integration_tests/utils.py +++ b/integration_tests/utils.py @@ -268,6 +268,20 @@ def add_ini_sections(inipath, sections): ini.write(fp) +def edit_ini_sections(chain_id, ini_path, callback): + ini = configparser.RawConfigParser() + ini.read(ini_path) + reg = re.compile(rf"^program:{chain_id}-node(\d+)") + for section in ini.sections(): + m = reg.match(section) + if m: + i = m.group(1) + old = ini[section] + ini[section].update(callback(i, old)) + with ini_path.open("w") as fp: + ini.write(fp) + + def supervisorctl(inipath, *args): return subprocess.check_output( (sys.executable, "-msupervisor.supervisorctl", "-c", inipath, *args),