Skip to content

Commit

Permalink
new(tests): EIP-7685: Add invalid request type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Sep 27, 2024
1 parent d6d6ddf commit 35a8be9
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 6 deletions.
23 changes: 17 additions & 6 deletions tests/prague/eip7685_general_purpose_el_requests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Fixtures for the EIP-7685 deposit tests.
"""

from typing import List
from typing import List, SupportsBytes

import pytest

Expand All @@ -27,6 +27,12 @@ def block_body_override_requests() -> List[
return None


@pytest.fixture
def block_body_extra_requests() -> List[SupportsBytes]:
"""List of requests that overwrite the requests in the header. None by default."""
return []


@pytest.fixture
def exception() -> BlockException | None:
"""Block exception expected by the tests. None by default."""
Expand All @@ -41,8 +47,11 @@ def blocks(
| WithdrawalRequestInteractionBase
| ConsolidationRequestInteractionBase
],
block_body_override_requests: List[DepositRequest | WithdrawalRequest | ConsolidationRequest]
block_body_override_requests: List[
DepositRequest | WithdrawalRequest | ConsolidationRequest | SupportsBytes
]
| None,
block_body_extra_requests: List[SupportsBytes],
exception: BlockException | None,
) -> List[Block]:
"""List of blocks that comprise the test."""
Expand All @@ -60,14 +69,16 @@ def blocks(
included_withdrawal_requests += r.valid_requests(withdrawal_request_fee)
elif isinstance(r, ConsolidationRequestInteractionBase):
included_consolidation_requests += r.valid_requests(consolidation_request_fee)

valid_requests = (
included_deposit_requests + included_withdrawal_requests + included_consolidation_requests
)
if block_body_override_requests is None and block_body_extra_requests is not None:
block_body_override_requests = valid_requests + block_body_extra_requests
return [
Block(
txs=sum((r.transactions() for r in requests), []),
header_verify=Header(
requests_hash=included_deposit_requests
+ included_withdrawal_requests
+ included_consolidation_requests,
requests_hash=valid_requests,
),
requests=block_body_override_requests,
exception=exception,
Expand Down
114 changes: 114 additions & 0 deletions tests/prague/eip7685_general_purpose_el_requests/test_request_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""
Test the request types that can be included in a block by the given fork.
"""
from typing import List

import pytest

from ethereum_test_exceptions import BlockException
from ethereum_test_forks import Fork
from ethereum_test_tools import Alloc, Block, BlockchainTestFiller, Environment

from ..eip6110_deposits.helpers import DepositInteractionBase, DepositRequest, DepositTransaction
from ..eip7002_el_triggerable_withdrawals.helpers import (
WithdrawalRequest,
WithdrawalRequestInteractionBase,
WithdrawalRequestTransaction,
)
from ..eip7251_consolidations.helpers import (
ConsolidationRequest,
ConsolidationRequestInteractionBase,
ConsolidationRequestTransaction,
)
from .spec import ref_spec_7685

REFERENCE_SPEC_GIT_PATH = ref_spec_7685.git_path
REFERENCE_SPEC_VERSION = ref_spec_7685.version

pytestmark = pytest.mark.valid_from("Prague")


@pytest.fixture
def block_body_extra_requests(fork: Fork, invalid_request_data: bytes) -> List[bytes]:
"""List of requests that overwrite the requests in the header. None by default."""
invalid_request_type = fork.max_request_type() + 1
return [bytes([invalid_request_type]) + invalid_request_data]


@pytest.fixture
def requests(
fork: Fork,
include_valid_requests: bool,
) -> List[
DepositInteractionBase | WithdrawalRequestInteractionBase | ConsolidationRequestInteractionBase
]:
"""List of valid requests that are added along with the invalid request."""
if not include_valid_requests:
return []
if fork.max_request_type() == 2:
return [
DepositTransaction(
requests=[
DepositRequest(
pubkey=1,
withdrawal_credentials=2,
amount=1_000_000_000,
signature=3,
index=0,
)
]
),
WithdrawalRequestTransaction(
requests=[
WithdrawalRequest(
validator_pubkey=1,
amount=0,
fee=1,
)
]
),
ConsolidationRequestTransaction(
requests=[
ConsolidationRequest(
source_pubkey=2,
target_pubkey=5,
fee=1,
)
]
),
]
raise NotImplementedError(f"Unsupported fork: {fork}")


@pytest.mark.parametrize(
"include_valid_requests",
[False, True],
)
@pytest.mark.parametrize(
"invalid_request_data",
[
pytest.param(b"", id="no_data"),
pytest.param(b"\0", id="single_byte"),
pytest.param(b"\0" * 32, id="32_bytes"),
],
)
@pytest.mark.parametrize(
"exception",
[
pytest.param(BlockException.INVALID_REQUESTS, id=""),
],
)
def test_invalid_request_type(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
blocks: List[Block],
):
"""
Test sending a block with an invalid request type.
"""
blockchain_test(
genesis_environment=Environment(),
pre=pre,
post={},
blocks=blocks,
)

0 comments on commit 35a8be9

Please sign in to comment.