Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@

import pytest

from execution_testing.test_types import Environment
from execution_testing.client_clis import (
ExecutionSpecsTransitionTool,
TransitionTool,
)
from execution_testing.fixtures import (
BlockchainEngineFixture,
BlockchainFixture,
)
from execution_testing.fixtures.file import Fixtures
from execution_testing.test_types import Environment

from ..filler import default_output_directory

# Path to the real benchmark conftest.py that we copy into testdirs.
Expand Down Expand Up @@ -1056,37 +1062,38 @@ def test_execution_witness_in_blockchain_fixture(
)
assert fixture_path.exists(), f"{fixture_path} does not exist"

with open(fixture_path, "r") as f:
fixture_data = json.load(f)
fixture_data = Fixtures.model_validate_json(fixture_path.read_text())

assert len(fixture_data) == 1, "Expected exactly one fixture"
fixture = next(iter(fixture_data.values()))
block = fixture["blocks"][0]
assert isinstance(fixture, BlockchainFixture)
block = fixture.blocks[0]

# executionWitness exists with non-empty state, codes, and headers
assert "executionWitness" in block
witness = block["executionWitness"]
assert len(witness["state"]) > 0, "executionWitness.state is empty"
assert len(witness["codes"]) > 0, "executionWitness.codes is empty"
assert len(witness["headers"]) > 0, "executionWitness.headers is empty"
witness = block.execution_witness
assert witness is not None
assert len(witness.state) > 0, "executionWitness.state is empty"
assert len(witness.codes) > 0, "executionWitness.codes is empty"
assert len(witness.headers) > 0, "executionWitness.headers is empty"

# statelessInputBytes is schema-prefixed guest input bytes.
sib = block.stateless_input_bytes
assert sib is not None and len(sib) > 0
from ethereum.forks.amsterdam.stateless_ssz import (
STATELESS_INPUT_SCHEMA_ID_BYTES,
)

# statelessInputBytes and statelessOutputBytes are non-empty hex strings
assert "statelessInputBytes" in block
sib = block["statelessInputBytes"]
assert isinstance(sib, str) and sib.startswith("0x") and len(sib) > 2
assert bytes(sib).startswith(STATELESS_INPUT_SCHEMA_ID_BYTES)

assert "statelessOutputBytes" in block
sob = block["statelessOutputBytes"]
assert isinstance(sob, str) and sob.startswith("0x") and len(sob) > 2
sob = block.stateless_output_bytes
assert sob is not None and len(sob) > 0

from ethereum.forks.amsterdam.stateless_host import (
deserialize_stateless_output,
)
from ethereum_types.bytes import Bytes as EthereumBytes

stateless_output = deserialize_stateless_output(
EthereumBytes(bytes.fromhex(sob[2:]))
)
stateless_output = deserialize_stateless_output(EthereumBytes(bytes(sob)))
assert stateless_output.successful_validation is True

engine_fixture_path = Path(
Expand All @@ -1097,22 +1104,24 @@ def test_execution_witness_in_blockchain_fixture(
f"{engine_fixture_path} does not exist"
)

with open(engine_fixture_path, "r") as f:
engine_fixture_data = json.load(f)
engine_fixture_data = Fixtures.model_validate_json(
engine_fixture_path.read_text()
)

assert len(engine_fixture_data) == 1, "Expected exactly one engine fixture"
engine_fixture = next(iter(engine_fixture_data.values()))
engine_payload = engine_fixture["engineNewPayloads"][0]
assert isinstance(engine_fixture, BlockchainEngineFixture)
engine_payload = engine_fixture.payloads[0]

assert "executionWitness" in engine_payload
engine_witness = engine_payload["executionWitness"]
assert len(engine_witness["state"]) > 0, (
engine_witness = engine_payload.execution_witness
assert engine_witness is not None
assert len(engine_witness.state) > 0, (
"engine executionWitness.state is empty"
)
assert len(engine_witness["codes"]) > 0, (
assert len(engine_witness.codes) > 0, (
"engine executionWitness.codes is empty"
)
assert len(engine_witness["headers"]) > 0, (
assert len(engine_witness.headers) > 0, (
"engine executionWitness.headers is empty"
)
assert engine_witness == witness
Expand Down
18 changes: 16 additions & 2 deletions src/ethereum/forks/amsterdam/stateless_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
verify_stateless_new_payload,
)
from .stateless_ssz import (
STATELESS_INPUT_SCHEMA_ID,
STATELESS_INPUT_SCHEMA_ID_SIZE,
SszStatelessInput,
ssz_to_stateless_input,
validation_result_to_ssz,
Expand All @@ -25,8 +27,20 @@ def serialize_stateless_output(


def deserialize_stateless_input(data: Bytes) -> StatelessInput:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crux of the PR.

"""Deserialize a StatelessInput from SSZ bytes."""
ssz_obj = SszStatelessInput.decode_bytes(data)
"""Deserialize a StatelessInput from schema-prefixed SSZ bytes."""
if len(data) < STATELESS_INPUT_SCHEMA_ID_SIZE:
raise ValueError("Stateless input is missing schema id")
schema_id = int.from_bytes(
data[:STATELESS_INPUT_SCHEMA_ID_SIZE],
"big",
)
if schema_id != STATELESS_INPUT_SCHEMA_ID:
raise ValueError(
f"Unsupported stateless input schema id: 0x{schema_id:04x}"
)
Comment on lines +37 to +40

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec, we don't have a single guest program entry point that supports multiple-forks. This is mainly related on how execution-specs is architected having full specs per fork in different folders.

In real EL guest programs which support multiple forks, they will probably have a switch here to act accordingly.

ssz_obj = SszStatelessInput.decode_bytes(
data[STATELESS_INPUT_SCHEMA_ID_SIZE:]
)
return ssz_to_stateless_input(ssz_obj)


Expand Down
7 changes: 5 additions & 2 deletions src/ethereum/forks/amsterdam/stateless_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
StatelessValidationResult,
)
from .stateless_ssz import (
STATELESS_INPUT_SCHEMA_ID_BYTES,
SszStatelessValidationResult,
ssz_to_validation_result,
stateless_input_to_ssz,
Expand All @@ -36,9 +37,11 @@
def serialize_stateless_input(
stateless_input: StatelessInput,
) -> Bytes:
"""Serialize a StatelessInput to SSZ-encoded bytes."""
"""Serialize a StatelessInput to schema-prefixed SSZ bytes."""
ssz_obj = stateless_input_to_ssz(stateless_input)
return Bytes(ssz_obj.encode_bytes())
return Bytes(
STATELESS_INPUT_SCHEMA_ID_BYTES + bytes(ssz_obj.encode_bytes())
)


def deserialize_stateless_output(data: Bytes) -> StatelessValidationResult:
Expand Down
8 changes: 8 additions & 0 deletions src/ethereum/forks/amsterdam/stateless_ssz.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
MAX_PUBLIC_KEYS = 2**20
MAX_BYTES_PER_PUBLIC_KEY = 65

# Amsterdam SSZ stateless input schema identifier.
STATELESS_INPUT_SCHEMA_ID = 0x0001
STATELESS_INPUT_SCHEMA_ID_SIZE = 2
STATELESS_INPUT_SCHEMA_ID_BYTES = STATELESS_INPUT_SCHEMA_ID.to_bytes(
STATELESS_INPUT_SCHEMA_ID_SIZE,
"big",
)


# --- SSZ Container types ---

Expand Down
31 changes: 31 additions & 0 deletions tests/json_loader/test_stateless_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
from typing import Tuple

import pytest
from ethereum_types.bytes import Bytes, Bytes32, Bytes48, Bytes96
from ethereum_types.numeric import U64, U256, Uint

Expand Down Expand Up @@ -31,6 +32,10 @@
deserialize_stateless_output,
serialize_stateless_input,
)
from ethereum.forks.amsterdam.stateless_ssz import (
STATELESS_INPUT_SCHEMA_ID_BYTES,
stateless_input_to_ssz,
)
from ethereum.state import Address, Root

_RNG = random.Random(0xDEADBEEF)
Expand Down Expand Up @@ -115,6 +120,7 @@ def test_roundtrip(self) -> None:
"""Encoding then decoding recovers the original StatelessInput."""
original = _make_stateless_input()
encoded = serialize_stateless_input(original)
assert encoded[:2] == STATELESS_INPUT_SCHEMA_ID_BYTES
recovered = deserialize_stateless_input(encoded)
assert recovered == original

Expand All @@ -136,6 +142,7 @@ def test_empty_witness(self) -> None:
public_keys=(),
)
encoded = serialize_stateless_input(original)
assert encoded[:2] == STATELESS_INPUT_SCHEMA_ID_BYTES
recovered = deserialize_stateless_input(encoded)
assert recovered == original

Expand Down Expand Up @@ -171,6 +178,30 @@ def test_empty_witness(self) -> None:
recovered = deserialize_stateless_input(encoded)
assert recovered == original

def test_empty_input_rejected(self) -> None:
"""Reject input that does not contain a schema id."""
with pytest.raises(ValueError, match="missing schema id"):
deserialize_stateless_input(Bytes(b""))

def test_one_byte_input_rejected(self) -> None:
"""Reject input that does not contain a full schema id."""
with pytest.raises(ValueError, match="missing schema id"):
deserialize_stateless_input(Bytes(b"\x01"))

def test_unknown_schema_id_rejected(self) -> None:
"""Reject input with a schema id other than Amsterdam's."""
encoded = serialize_stateless_input(_make_stateless_input())
with pytest.raises(ValueError, match="Unsupported stateless input"):
deserialize_stateless_input(Bytes(b"\x00\x02" + encoded[2:]))

def test_legacy_raw_ssz_input_rejected(self) -> None:
"""Reject unprefixed SSZ input bytes."""
original = _make_stateless_input()
raw_ssz = Bytes(stateless_input_to_ssz(original).encode_bytes())
assert raw_ssz[:2] != STATELESS_INPUT_SCHEMA_ID_BYTES
with pytest.raises(ValueError, match="Unsupported stateless input"):
deserialize_stateless_input(raw_ssz)


class TestSerializeStatelessOutput:
"""Test serialize_stateless_output."""
Expand Down
Loading