Skip to content

Commit

Permalink
src/tools: InvalidFixtureBlock type added.
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-tb committed Sep 26, 2023
1 parent bf714a3 commit 2458c85
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Test fixtures for use by clients are available for each release on the [Github r

### 🔧 Tools

- 🔀 Tools: Updated invalid blocks in fixtures to include non-RLP format ([#311](https://github.com/ethereum/execution-spec-tests/pull/311)).

### 📋 Misc

- ✨ Docs: Changelog updated post release ([#321](https://github.com/ethereum/execution-spec-tests/pull/321)).
Expand Down
2 changes: 2 additions & 0 deletions src/ethereum_test_tools/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
Header,
HeaderNonce,
HiveFixture,
InvalidFixtureBlock,
JSONEncoder,
Number,
Removable,
Expand Down Expand Up @@ -79,6 +80,7 @@
"HeaderNonce",
"HistoryStorageAddress",
"HiveFixture",
"InvalidFixtureBlock",
"JSONEncoder",
"Number",
"Removable",
Expand Down
27 changes: 26 additions & 1 deletion src/ethereum_test_tools/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2639,6 +2639,7 @@ class FixtureBlock:
"""

rlp: Bytes = field(
default=None,
json_encoder=JSONEncoder.Field(),
)
block_header: Optional[FixtureHeader] = field(
Expand Down Expand Up @@ -2688,6 +2689,30 @@ class FixtureBlock:
)


@dataclass(kw_only=True)
class InvalidFixtureBlock:
"""
Representation of an invalid Ethereum block within a test Fixture.
"""

rlp: Bytes = field(
json_encoder=JSONEncoder.Field(),
)
expected_exception: Optional[str] = field(
default=None,
json_encoder=JSONEncoder.Field(
name="expectException",
),
)
rlp_decoded: FixtureBlock = field(
default=None,
json_encoder=JSONEncoder.Field(
name="rlp_decoded",
to_json=True,
),
)


@dataclass(kw_only=True)
class BaseFixture:
"""
Expand Down Expand Up @@ -2763,7 +2788,7 @@ class Fixture(BaseFixture):
to_json=True,
),
)
blocks: Optional[List[FixtureBlock]] = field(
blocks: Optional[List[FixtureBlock | InvalidFixtureBlock]] = field(
default=None,
json_encoder=JSONEncoder.Field(
name="blocks",
Expand Down
3 changes: 2 additions & 1 deletion src/ethereum_test_tools/spec/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
FixtureEngineNewPayload,
FixtureHeader,
Hash,
InvalidFixtureBlock,
Transaction,
withdrawals_root,
)
Expand Down Expand Up @@ -129,7 +130,7 @@ def make_blocks(
chain_id: int = 1,
eips: Optional[List[int]] = None,
) -> Tuple[
Optional[List[FixtureBlock]],
Optional[List[FixtureBlock | InvalidFixtureBlock]],
Optional[List[Optional[FixtureEngineNewPayload]]],
Hash,
Dict[str, Any],
Expand Down
28 changes: 21 additions & 7 deletions src/ethereum_test_tools/spec/blockchain_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
FixtureHeader,
Hash,
HeaderNonce,
InvalidFixtureBlock,
Number,
ZeroPaddedHexNumber,
to_json,
Expand Down Expand Up @@ -121,7 +122,13 @@ def make_block(
previous_head: Hash,
chain_id=1,
eips: Optional[List[int]] = None,
) -> Tuple[FixtureBlock, Optional[FixtureEngineNewPayload], Environment, Dict[str, Any], Hash]:
) -> Tuple[
FixtureBlock | InvalidFixtureBlock,
Optional[FixtureEngineNewPayload],
Environment,
Dict[str, Any],
Hash,
]:
"""
Produces a block based on the previous environment and allocation.
If the block is an invalid block, the environment and allocation
Expand Down Expand Up @@ -244,10 +251,15 @@ def make_block(
)
else:
return (
FixtureBlock(
InvalidFixtureBlock(
rlp=rlp,
block_number=Number(header.number),
expected_exception=block.exception,
rlp_decoded=FixtureBlock(
block_header=header,
txs=txs,
ommers=[],
withdrawals=env.withdrawals,
),
),
fixture_payload,
previous_env,
Expand All @@ -256,7 +268,7 @@ def make_block(
)
else:
return (
FixtureBlock(
InvalidFixtureBlock(
rlp=Bytes(block.rlp),
expected_exception=block.exception,
),
Expand All @@ -275,7 +287,7 @@ def make_blocks(
chain_id=1,
eips: Optional[List[int]] = None,
) -> Tuple[
Optional[List[FixtureBlock]],
Optional[List[FixtureBlock | InvalidFixtureBlock]],
Optional[List[Optional[FixtureEngineNewPayload]]],
Hash,
Dict[str, Any],
Expand All @@ -288,7 +300,9 @@ def make_blocks(
"""
alloc = to_json(pre)
env = Environment.from_parent_header(genesis)
fixture_blocks: List[FixtureBlock] | None = [] if not self.hive_enabled else None
fixture_blocks: List[FixtureBlock | InvalidFixtureBlock] | None = (
[] if not self.hive_enabled else None
)

fixture_payloads: List[Optional[FixtureEngineNewPayload]] | None = (
[] if self.hive_enabled else None
Expand All @@ -312,7 +326,7 @@ def make_blocks(
fixture_blocks.append(fixture_block)
if self.hive_enabled and fixture_payloads is not None:
fixture_payloads.append(fixture_payload)
if block.exception is None:
if isinstance(fixture_block, FixtureBlock):
last_valid = fixture_block.block_header

if self.hive_enabled and last_valid:
Expand Down

0 comments on commit 2458c85

Please sign in to comment.