Skip to content

Commit

Permalink
src/tools: Refactor & clean up of state_test.
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-tb committed Oct 4, 2023
1 parent a33ef95 commit a035e3d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 145 deletions.
40 changes: 12 additions & 28 deletions src/ethereum_test_tools/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,6 @@ class Environment:
to_json=True,
),
)

base_fee: Optional[NumberConvertible] = field(
default=None,
json_encoder=JSONEncoder.Field(
Expand All @@ -961,7 +960,6 @@ class Environment:
cast_type=Number,
),
)

parent_timestamp: Optional[NumberConvertible] = field(
default=None,
json_encoder=JSONEncoder.Field(
Expand Down Expand Up @@ -1033,7 +1031,7 @@ class Environment:
),
)
extra_data: Optional[BytesConvertible] = field(
default=None,
default=b"\x00",
json_encoder=JSONEncoder.Field(
skip=True,
),
Expand Down Expand Up @@ -2028,7 +2026,6 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="parentHash"),
)

ommers_hash: Hash = header_field(
source=HeaderFieldSource(
parse_type=Hash,
Expand All @@ -2037,47 +2034,41 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="uncleHash"),
)

coinbase: Address = header_field(
source=HeaderFieldSource(
parse_type=Address,
source_environment="coinbase",
),
json_encoder=JSONEncoder.Field(),
)

state_root: Hash = header_field(
source=HeaderFieldSource(
parse_type=Hash,
source_transition_tool="stateRoot",
),
json_encoder=JSONEncoder.Field(name="stateRoot"),
)

transactions_root: Hash = header_field(
source=HeaderFieldSource(
parse_type=Hash,
source_transition_tool="txRoot",
),
json_encoder=JSONEncoder.Field(name="transactionsTrie"),
)

receipt_root: Hash = header_field(
source=HeaderFieldSource(
parse_type=Hash,
source_transition_tool="receiptsRoot",
),
json_encoder=JSONEncoder.Field(name="receiptTrie"),
)

bloom: Bloom = header_field(
source=HeaderFieldSource(
parse_type=Bloom,
source_transition_tool="logsBloom",
),
json_encoder=JSONEncoder.Field(),
)

difficulty: int = header_field(
source=HeaderFieldSource(
parse_type=Number,
Expand All @@ -2087,39 +2078,34 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(cast_type=ZeroPaddedHexNumber),
)

number: int = header_field(
source=HeaderFieldSource(
parse_type=Number,
source_environment="number",
),
json_encoder=JSONEncoder.Field(cast_type=ZeroPaddedHexNumber),
)

gas_limit: int = header_field(
source=HeaderFieldSource(
parse_type=Number,
source_environment="gas_limit",
),
json_encoder=JSONEncoder.Field(name="gasLimit", cast_type=ZeroPaddedHexNumber),
)

gas_used: int = header_field(
source=HeaderFieldSource(
parse_type=Number,
source_transition_tool="gasUsed",
),
json_encoder=JSONEncoder.Field(name="gasUsed", cast_type=ZeroPaddedHexNumber),
)

timestamp: int = header_field(
source=HeaderFieldSource(
parse_type=Number,
source_environment="timestamp",
),
json_encoder=JSONEncoder.Field(cast_type=ZeroPaddedHexNumber),
)

extra_data: Bytes = header_field(
source=HeaderFieldSource(
parse_type=Bytes,
Expand All @@ -2128,7 +2114,6 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="extraData"),
)

mix_digest: Hash = header_field(
source=HeaderFieldSource(
parse_type=Hash,
Expand All @@ -2137,15 +2122,13 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="mixHash"),
)

nonce: HeaderNonce = header_field(
source=HeaderFieldSource(
parse_type=HeaderNonce,
default=b"",
),
json_encoder=JSONEncoder.Field(),
)

base_fee: Optional[int] = header_field(
default=None,
source=HeaderFieldSource(
Expand All @@ -2156,7 +2139,6 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="baseFeePerGas", cast_type=ZeroPaddedHexNumber),
)

withdrawals_root: Optional[Hash] = header_field(
default=None,
source=HeaderFieldSource(
Expand All @@ -2166,7 +2148,6 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="withdrawalsRoot"),
)

blob_gas_used: Optional[int] = header_field(
default=None,
source=HeaderFieldSource(
Expand All @@ -2176,7 +2157,6 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="blobGasUsed", cast_type=ZeroPaddedHexNumber),
)

excess_blob_gas: Optional[int] = header_field(
default=None,
source=HeaderFieldSource(
Expand All @@ -2186,7 +2166,6 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="excessBlobGas", cast_type=ZeroPaddedHexNumber),
)

beacon_root: Optional[Hash] = header_field(
default=None,
source=HeaderFieldSource(
Expand All @@ -2196,7 +2175,6 @@ class FixtureHeader:
),
json_encoder=JSONEncoder.Field(name="parentBeaconBlockRoot"),
)

hash: Optional[Hash] = header_field(
default=None,
source=HeaderFieldSource(
Expand Down Expand Up @@ -2638,6 +2616,14 @@ class FixtureBlock:
Representation of an Ethereum block within a test Fixture.
"""

@staticmethod
def _txs_encoder(txs: List[Transaction]) -> List[FixtureTransaction]:
return [FixtureTransaction.from_transaction(tx) for tx in txs]

@staticmethod
def _withdrawals_encoder(withdrawals: List[Withdrawal]) -> List[FixtureWithdrawal]:
return [FixtureWithdrawal.from_withdrawal(w) for w in withdrawals]

rlp: Bytes = field(
default=None,
json_encoder=JSONEncoder.Field(),
Expand Down Expand Up @@ -2666,7 +2652,7 @@ class FixtureBlock:
default=None,
json_encoder=JSONEncoder.Field(
name="transactions",
cast_type=lambda txs: [FixtureTransaction.from_transaction(tx) for tx in txs],
cast_type=_txs_encoder,
to_json=True,
),
)
Expand All @@ -2681,9 +2667,7 @@ class FixtureBlock:
default=None,
json_encoder=JSONEncoder.Field(
name="withdrawals",
cast_type=lambda withdrawals: [
FixtureWithdrawal.from_withdrawal(w) for w in withdrawals
],
cast_type=_withdrawals_encoder,
to_json=True,
),
)
Expand Down Expand Up @@ -2716,7 +2700,7 @@ class InvalidFixtureBlock:
@dataclass(kw_only=True)
class BaseFixture:
"""
Base Ethereum test fixture class.
Base Ethereum test fixture fields class.
"""

info: Dict[str, str] = field(
Expand Down
15 changes: 6 additions & 9 deletions src/ethereum_test_tools/filling/fill.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Filler object definitions.
Test filler definitions.
"""
from typing import List, Optional, Union

Expand All @@ -19,18 +19,15 @@ def fill_test(
eips: Optional[List[int]] = None,
) -> Optional[Union[Fixture, HiveFixture]]:
"""
Fills fixtures for the specified fork.
Fills default/hive fixture for the specified fork and test spec.
"""
t8n.reset_traces()
fixture: Union[Fixture, HiveFixture]
t8n.reset_traces()
if test_spec.base_test_config.enable_hive:
if fork.engine_new_payload_version() is not None:
fixture = test_spec.make_hive_fixture(t8n, fork, eips)
else: # pre Merge tests are not supported in Hive
# TODO: remove this logic. if hive enabled set --from to Merge
return None
if fork.engine_new_payload_version() is None:
return None # pre Merge tests are not supported in Hive
fixture = test_spec.make_hive_fixture(t8n, fork, eips)
else:
fixture = test_spec.make_fixture(t8n, fork, eips)
fixture.fill_info(t8n, spec)

return fixture
2 changes: 1 addition & 1 deletion src/ethereum_test_tools/spec/base_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Generic Ethereum test base class
Base test class and helper functions for Ethereum state and blockchain tests.
"""
from abc import abstractmethod
from dataclasses import dataclass, field
Expand Down
Loading

0 comments on commit a035e3d

Please sign in to comment.