Skip to content

Commit

Permalink
fix(consume): genesis block hash exception bug & extra.
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-tb committed Aug 8, 2024
1 parent 05da2d9 commit 06a123d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/pytest_plugins/consume/hive_simulators/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import pprint
from typing import Dict, List, Tuple

from ethereum_test_fixtures.blockchain import FixtureHeader

Expand All @@ -12,36 +13,47 @@ class GenesisBlockMismatchException(Exception):
Definers a mismatch exception between the client and fixture genesis blockhash.
"""

def __init__(self, *, expected_header: FixtureHeader, got_header: FixtureHeader):
def __init__(self, *, expected_header: FixtureHeader, got_genesis_block: Dict[str, str]):
message = (
"Genesis block hash mismatch.\n\n"
f"Expected: {expected_header.block_hash}\n"
f" Got: {got_header.block_hash}."
f" Got: {got_genesis_block['hash']}."
)
differences, unexpected_fields = self.compare_models(
expected_header, FixtureHeader(**got_genesis_block)
)
differences = self.compare_models(expected_header, got_header)
if differences:
message += (
"\n\nGenesis block header field differences:\n"
f"{pprint.pformat(differences, indent=4)}"
)
elif unexpected_fields:
message += (
"\n\nUn-expected genesis block header fields from client:\n"
f"{pprint.pformat(unexpected_fields, indent=4)}"
"\nIs the fork configuration correct?"
)
else:
message += (
"There were no differences in the expected and received genesis block headers."
)
super().__init__(message)

@staticmethod
def compare_models(expected: FixtureHeader, got: FixtureHeader) -> dict:
def compare_models(expected: FixtureHeader, got: FixtureHeader) -> Tuple[Dict, List]:
"""
Compare two FixtureHeader model instances and return their differences.
"""
differences = {}
for (exp_name, exp_value), (_, got_value) in zip(expected, got):
unexpected_fields = []
for (exp_name, exp_value), (got_name, got_value) in zip(expected, got):
if "rlp" in exp_name or "fork" in exp_name: # ignore rlp as not verbose enough
continue
if exp_value is None or got_value is None or exp_value != got_value:
if exp_value != got_value:
differences[exp_name] = {
"expected ": str(exp_value),
"got (via rpc)": str(got_value),
}
return differences
if got_value is None:
unexpected_fields.append(got_name)
return differences, unexpected_fields
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Clients consume the genesis and RLP-encoded blocks from input files upon start-up.
"""

from ethereum_test_fixtures import BlockchainFixture, FixtureFormats
from ethereum_test_fixtures.blockchain import FixtureHeader
from ethereum_test_tools.rpc import EthRPC
from pytest_plugins.consume.hive_simulators.exceptions import GenesisBlockMismatchException

Expand All @@ -28,7 +28,7 @@ def test_via_rlp(
if genesis_block["hash"] != str(blockchain_fixture.genesis.block_hash):
raise GenesisBlockMismatchException(
expected_header=blockchain_fixture.genesis,
got_header=FixtureHeader(**genesis_block),
got_genesis_block=genesis_block,
)
with timing_data.time("Get latest block"):
block = eth_rpc.get_block_by_number("latest")
Expand Down

0 comments on commit 06a123d

Please sign in to comment.