Skip to content

Commit

Permalink
feat(tools): add a non-rlp field (rlp_decoded) to invalid blocks (e…
Browse files Browse the repository at this point in the history
…thereum#322)

---------

Co-authored-by: Dan <[email protected]>
  • Loading branch information
spencer-tb and danceratopz authored Oct 4, 2023
1 parent c659195 commit f810be2
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 19 deletions.
4 changes: 3 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Test fixtures for use by clients are available for each release on the [Github r

### 🛠️ Framework

### 🔧 Tools
- 🔀 Fixtures: Add a non-RLP format field (`rlp_decoded`) to invalid blocks ([#322](https://github.com/ethereum/execution-spec-tests/pull/322)).

### 🔧 EVM Tools

- ✨ Enable tracing support for `ethereum-spec-evm` ([#289](https://github.com/ethereum/execution-spec-tests/pull/289)).

Expand Down
2 changes: 1 addition & 1 deletion docs/changelog_section_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ The following can be copy-pasted into the `CHANGELOG.md` file for a new release.

### 🛠️ Framework

### 🔧 Tools
### 🔧 EVM Tools

### 📋 Misc
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
3 changes: 2 additions & 1 deletion src/ethereum_test_tools/spec/state_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
FixtureHeader,
Hash,
HeaderNonce,
InvalidFixtureBlock,
Number,
Transaction,
ZeroPaddedHexNumber,
Expand Down Expand Up @@ -128,7 +129,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 Down
2 changes: 0 additions & 2 deletions src/ethereum_test_tools/tests/test_filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ def test_fill_state_test(fork: Fork, expected_json_file: str, enable_hive: bool)

fixture_json = to_json(fixture)
remove_info(fixture_json)
with open("gen.json", "w") as fr:
json.dump(fixture_json, fr, indent=4)
assert fixture_json == expected


Expand Down
Loading

0 comments on commit f810be2

Please sign in to comment.