Skip to content

Commit

Permalink
[CHIA-2264] Add config constant support to chia db validate (#19182)
Browse files Browse the repository at this point in the history
Add config constant support to `chia db validate`
  • Loading branch information
Quexington authored Jan 28, 2025
1 parent 37df97e commit 2d53b46
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
24 changes: 18 additions & 6 deletions chia/_tests/core/test_db_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sqlite3
from contextlib import closing
from pathlib import Path
from typing import Any

import pytest

Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand All @@ -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)


Expand All @@ -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:
Expand Down Expand Up @@ -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)
20 changes: 13 additions & 7 deletions chia/cmds/db_validate_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2d53b46

Please sign in to comment.