From 2d53b46a4b9c51e664ab263960e1606773c4bd8f Mon Sep 17 00:00:00 2001 From: Matt Hauff Date: Tue, 28 Jan 2025 08:39:27 -0700 Subject: [PATCH] [CHIA-2264] Add config constant support to `chia db validate` (#19182) Add config constant support to `chia db validate` --- chia/_tests/core/test_db_validation.py | 24 ++++++++++++++++++------ chia/cmds/db_validate_func.py | 20 +++++++++++++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/chia/_tests/core/test_db_validation.py b/chia/_tests/core/test_db_validation.py index 568f13bb1e84..01123eb7948f 100644 --- a/chia/_tests/core/test_db_validation.py +++ b/chia/_tests/core/test_db_validation.py @@ -4,6 +4,7 @@ import sqlite3 from contextlib import closing from pathlib import Path +from typing import Any import pytest @@ -69,13 +70,18 @@ def add_block( ) +default_config: dict[str, Any] = { + "full_node": {"selected_network": "local", "network_overrides": {"constants": {"local": {}}}} +} + + def test_db_validate_wrong_version() -> None: with TempFile() as db_file: with closing(sqlite3.connect(db_file)) as conn: make_version(conn, 3) with pytest.raises(RuntimeError) as execinfo: - validate_v2(db_file, validate_blocks=False) + validate_v2(db_file, config=default_config, validate_blocks=False) assert "Database has the wrong version (3 expected 2)" in str(execinfo.value) @@ -85,7 +91,7 @@ def test_db_validate_missing_peak_table() -> None: make_version(conn, 2) with pytest.raises(RuntimeError) as execinfo: - validate_v2(db_file, validate_blocks=False) + validate_v2(db_file, config=default_config, validate_blocks=False) assert "Database is missing current_peak table" in str(execinfo.value) @@ -98,7 +104,7 @@ def test_db_validate_missing_peak_block() -> None: make_block_table(conn) with pytest.raises(RuntimeError) as execinfo: - validate_v2(db_file, validate_blocks=False) + validate_v2(db_file, config=default_config, validate_blocks=False) assert "Database is missing the peak block" in str(execinfo.value) @@ -122,10 +128,10 @@ def test_db_validate_in_main_chain(invalid_in_chain: bool) -> None: if invalid_in_chain: with pytest.raises(RuntimeError) as execinfo: - validate_v2(db_file, validate_blocks=False) + validate_v2(db_file, config=default_config, validate_blocks=False) assert " (height: 96) is orphaned, but in_main_chain is set" in str(execinfo.value) else: - validate_v2(db_file, validate_blocks=False) + validate_v2(db_file, config=default_config, validate_blocks=False) async def make_db(db_file: Path, blocks: list[FullBlock]) -> None: @@ -158,5 +164,11 @@ async def test_db_validate_default_1000_blocks(default_1000_blocks: list[FullBlo # we expect everything to be valid except this is a test chain, so it # doesn't have the correct genesis challenge with pytest.raises(RuntimeError) as execinfo: - validate_v2(db_file, validate_blocks=True) + validate_v2(db_file, config=default_config, validate_blocks=True) assert "Blockchain has invalid genesis challenge" in str(execinfo.value) + + new_config = default_config.copy() + new_config["full_node"]["network_overrides"]["constants"]["local"]["AGG_SIG_ME_ADDITIONAL_DATA"] = ( + default_1000_blocks[0].foliage.prev_block_hash.hex() + ) + validate_v2(db_file, config=new_config, validate_blocks=True) diff --git a/chia/cmds/db_validate_func.py b/chia/cmds/db_validate_func.py index 2358095b4c73..058bf7bc18e7 100644 --- a/chia/cmds/db_validate_func.py +++ b/chia/cmds/db_validate_func.py @@ -4,6 +4,7 @@ from typing import Any, Optional from chia.consensus.block_record import BlockRecord +from chia.consensus.constants import replace_str_to_bytes from chia.consensus.default_constants import DEFAULT_CONSTANTS from chia.types.blockchain_format.sized_bytes import bytes32 from chia.types.full_block import FullBlock @@ -17,19 +18,20 @@ def db_validate_func( *, validate_blocks: bool, ) -> None: + config: dict[str, Any] = load_config(root_path, "config.yaml") if in_db_path is None: - config: dict[str, Any] = load_config(root_path, "config.yaml")["full_node"] - selected_network: str = config["selected_network"] - db_pattern: str = config["database_path"] + full_node_config = config["full_node"] + selected_network: str = full_node_config["selected_network"] + db_pattern: str = full_node_config["database_path"] db_path_replaced: str = db_pattern.replace("CHALLENGE", selected_network) in_db_path = path_from_root(root_path, db_path_replaced) - validate_v2(in_db_path, validate_blocks=validate_blocks) + validate_v2(in_db_path, config=config, validate_blocks=validate_blocks) print(f"\n\nDATABASE IS VALID: {in_db_path}\n") -def validate_v2(in_path: Path, *, validate_blocks: bool) -> None: +def validate_v2(in_path: Path, *, config: dict[str, Any], validate_blocks: bool) -> None: import sqlite3 from contextlib import closing @@ -174,10 +176,14 @@ def validate_v2(in_path: Path, *, validate_blocks: bool) -> None: # make sure the prev_hash pointer of block height 0 is the genesis # challenge - if next_hash != DEFAULT_CONSTANTS.AGG_SIG_ME_ADDITIONAL_DATA: + service_config = config["full_node"] + network_id = service_config["selected_network"] + overrides = service_config["network_overrides"]["constants"][network_id] + updated_constants = replace_str_to_bytes(DEFAULT_CONSTANTS, **overrides) + if next_hash != updated_constants.AGG_SIG_ME_ADDITIONAL_DATA: raise RuntimeError( f"Blockchain has invalid genesis challenge {next_hash}, expected " - f"{DEFAULT_CONSTANTS.AGG_SIG_ME_ADDITIONAL_DATA.hex()}" + f"{updated_constants.AGG_SIG_ME_ADDITIONAL_DATA.hex()}" ) if num_orphans > 0: