Skip to content

Commit

Permalink
refactor(tests): Refactor requests usages, add more 7685 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Oct 9, 2024
1 parent 2e5a5de commit e2a1cbb
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 84 deletions.
16 changes: 13 additions & 3 deletions tests/prague/eip6110_deposits/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import pytest

from ethereum_test_tools import Alloc, Block, BlockException, Header, Transaction
from ethereum_test_forks import Fork
from ethereum_test_tools import Alloc, Block, BlockException, Header, Requests, Transaction

from .helpers import DepositInteractionBase, DepositRequest

Expand Down Expand Up @@ -61,6 +62,7 @@ def included_requests(

@pytest.fixture
def blocks(
fork: Fork,
included_requests: List[DepositRequest],
block_body_override_requests: List[DepositRequest] | None,
txs: List[Transaction],
Expand All @@ -71,9 +73,17 @@ def blocks(
Block(
txs=txs,
header_verify=Header(
requests_root=included_requests,
requests_hash=Requests(
*included_requests,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
),
),
requests=block_body_override_requests,
requests=Requests(
*block_body_override_requests,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
).requests_list
if block_body_override_requests
else None,
exception=exception,
)
]
19 changes: 16 additions & 3 deletions tests/prague/eip7002_el_triggerable_withdrawals/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import pytest

from ethereum_test_tools import Alloc, Block, Header
from ethereum_test_forks import Fork
from ethereum_test_tools import Alloc, Block, Header, Requests

from .helpers import WithdrawalRequest, WithdrawalRequestInteractionBase
from .spec import Spec
Expand Down Expand Up @@ -70,6 +71,7 @@ def included_requests(

@pytest.fixture
def blocks(
fork: Fork,
update_pre: None, # Fixture is used for its side effects
blocks_withdrawal_requests: List[List[WithdrawalRequestInteractionBase]],
included_requests: List[List[WithdrawalRequest]],
Expand All @@ -80,13 +82,24 @@ def blocks(
return [ # type: ignore
Block(
txs=sum((r.transactions() for r in block_requests), []),
header_verify=Header(requests_root=block_included_requests),
header_verify=Header(
requests_hash=Requests(
*block_included_requests,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
)
),
)
for block_requests, block_included_requests in zip_longest(
blocks_withdrawal_requests,
included_requests,
fillvalue=[],
)
] + [
Block(header_verify=Header(requests_root=[]))
Block(
header_verify=Header(
requests_hash=Requests(
max_request_type=fork.max_request_type(block_number=1, timestamp=1)
)
)
)
] # Add an empty block at the end to verify that no more withdrawal requests are included
2 changes: 1 addition & 1 deletion tests/prague/eip7002_el_triggerable_withdrawals/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ReferenceSpec:
version: str


ref_spec_7002 = ReferenceSpec("EIPS/eip-7002.md", "e5af719767e789c88c0e063406c6557c8f53cfba")
ref_spec_7002 = ReferenceSpec("EIPS/eip-7002.md", "9fe721f56f45bd5cf2d2958c0e6867aa81f82ebc")


# Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest

from ethereum_test_forks import Fork
from ethereum_test_tools import (
Address,
Alloc,
Expand All @@ -19,7 +20,7 @@
Macros,
)
from ethereum_test_tools import Opcodes as Op
from ethereum_test_tools import TestAddress, TestAddress2
from ethereum_test_tools import Requests, TestAddress, TestAddress2

from .helpers import (
WithdrawalRequest,
Expand Down Expand Up @@ -350,9 +351,11 @@
+ [
WithdrawalRequest(
validator_pubkey=Spec.MAX_WITHDRAWAL_REQUESTS_PER_BLOCK,
amount=Spec.MAX_AMOUNT - 1
if (Spec.MAX_WITHDRAWAL_REQUESTS_PER_BLOCK - 1) % 2 == 0
else 0,
amount=(
Spec.MAX_AMOUNT - 1
if (Spec.MAX_WITHDRAWAL_REQUESTS_PER_BLOCK - 1) % 2 == 0
else 0
),
fee=0,
)
],
Expand Down Expand Up @@ -678,6 +681,7 @@ def test_withdrawal_requests(
)
def test_withdrawal_requests_negative(
pre: Alloc,
fork: Fork,
blockchain_test: BlockchainTestFiller,
requests: List[WithdrawalRequestInteractionBase],
block_body_override_requests: List[WithdrawalRequest],
Expand Down Expand Up @@ -705,9 +709,19 @@ def test_withdrawal_requests_negative(
Block(
txs=sum((r.transactions() for r in requests), []),
header_verify=Header(
requests_root=included_requests,
requests_hash=Requests(
*included_requests,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
),
),
requests=(
Requests(
*block_body_override_requests,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
).requests_list
if block_body_override_requests
else None
),
requests=block_body_override_requests,
exception=exception,
)
],
Expand Down
19 changes: 16 additions & 3 deletions tests/prague/eip7251_consolidations/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import pytest

from ethereum_test_tools import Alloc, Block, Header
from ethereum_test_forks import Fork
from ethereum_test_tools import Alloc, Block, Header, Requests

from .helpers import ConsolidationRequest, ConsolidationRequestInteractionBase
from .spec import Spec
Expand Down Expand Up @@ -71,6 +72,7 @@ def included_requests(

@pytest.fixture
def blocks(
fork: Fork,
update_pre: None, # Fixture is used for its side effects
blocks_consolidation_requests: List[List[ConsolidationRequestInteractionBase]],
included_requests: List[List[ConsolidationRequest]],
Expand All @@ -81,13 +83,24 @@ def blocks(
return [ # type: ignore
Block(
txs=sum((r.transactions() for r in block_requests), []),
header_verify=Header(requests_root=block_included_requests),
header_verify=Header(
requests_hash=Requests(
*block_included_requests,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
)
),
)
for block_requests, block_included_requests in zip_longest(
blocks_consolidation_requests,
included_requests,
fillvalue=[],
)
] + [
Block(header_verify=Header(requests_root=[]))
Block(
header_verify=Header(
requests_hash=Requests(
max_request_type=fork.max_request_type(block_number=1, timestamp=1)
)
)
)
] # Add an empty block at the end to verify that no more consolidation requests are included
2 changes: 1 addition & 1 deletion tests/prague/eip7251_consolidations/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ReferenceSpec:
version: str


ref_spec_7251 = ReferenceSpec("EIPS/eip-7251.md", "e5af719767e789c88c0e063406c6557c8f53cfba")
ref_spec_7251 = ReferenceSpec("EIPS/eip-7251.md", "18af57e74e4e862da5cbb8140aeb24128088f4e2")


# Constants
Expand Down
18 changes: 15 additions & 3 deletions tests/prague/eip7251_consolidations/test_consolidations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest

from ethereum_test_forks import Fork
from ethereum_test_tools import (
Address,
Alloc,
Expand All @@ -19,7 +20,7 @@
Macros,
)
from ethereum_test_tools import Opcodes as Op
from ethereum_test_tools import TestAddress, TestAddress2
from ethereum_test_tools import Requests, TestAddress, TestAddress2

from .helpers import (
ConsolidationRequest,
Expand Down Expand Up @@ -731,6 +732,7 @@ def test_consolidation_requests(
)
def test_consolidation_requests_negative(
pre: Alloc,
fork: Fork,
blockchain_test: BlockchainTestFiller,
requests: List[ConsolidationRequestInteractionBase],
block_body_override_requests: List[ConsolidationRequest],
Expand Down Expand Up @@ -758,9 +760,19 @@ def test_consolidation_requests_negative(
Block(
txs=sum((r.transactions() for r in requests), []),
header_verify=Header(
requests_root=included_requests,
requests_hash=Requests(
*included_requests,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
),
),
requests=(
Requests(
*block_body_override_requests,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
).requests_list
if block_body_override_requests
else None
),
requests=block_body_override_requests,
exception=exception,
)
],
Expand Down
63 changes: 47 additions & 16 deletions tests/prague/eip7685_general_purpose_el_requests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
Fixtures for the EIP-7685 deposit tests.
"""

from typing import List
from typing import List, SupportsBytes

import pytest

from ethereum_test_tools import Alloc, Block, BlockException, Header
from ethereum_test_forks import Fork
from ethereum_test_tools import Alloc, Block, BlockException, Bytes, Header, Requests

from ..eip6110_deposits.helpers import DepositInteractionBase, DepositRequest
from ..eip7002_el_triggerable_withdrawals.helpers import (
Expand All @@ -20,13 +21,31 @@


@pytest.fixture
def block_body_override_requests() -> List[
DepositRequest | WithdrawalRequest | ConsolidationRequest
] | None:
def block_body_override_requests(
request: pytest.FixtureRequest,
) -> List[DepositRequest | WithdrawalRequest | ConsolidationRequest] | None:
"""List of requests that overwrite the requests in the header. None by default."""
if hasattr(request, "param"):
return request.param
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 correct_requests_hash_in_header() -> bool:
"""
Whether to include the correct requests hash in the header so the calculated
block hash is correct, even though the requests in the new payload parameters might
be wrong.
"""
return False


@pytest.fixture
def exception() -> BlockException | None:
"""Block exception expected by the tests. None by default."""
Expand All @@ -35,41 +54,53 @@ def exception() -> BlockException | None:

@pytest.fixture
def blocks(
fork: Fork,
pre: Alloc,
requests: List[
DepositInteractionBase
| WithdrawalRequestInteractionBase
| ConsolidationRequestInteractionBase
],
block_body_override_requests: List[DepositRequest | WithdrawalRequest | ConsolidationRequest]
| None,
block_body_override_requests: (List[Bytes | SupportsBytes] | None),
block_body_extra_requests: List[SupportsBytes],
correct_requests_hash_in_header: bool,
exception: BlockException | None,
) -> List[Block]:
"""List of blocks that comprise the test."""
included_deposit_requests = []
included_withdrawal_requests = []
included_consolidation_requests = []
valid_requests_list: List[DepositRequest | WithdrawalRequest | ConsolidationRequest] = []
# Single block therefore base fee
withdrawal_request_fee = 1
consolidation_request_fee = 1
for r in requests:
r.update_pre(pre)
if isinstance(r, DepositInteractionBase):
included_deposit_requests += r.valid_requests(10**18)
valid_requests_list += r.valid_requests(10**18)
elif isinstance(r, WithdrawalRequestInteractionBase):
included_withdrawal_requests += r.valid_requests(withdrawal_request_fee)
valid_requests_list += r.valid_requests(withdrawal_request_fee)
elif isinstance(r, ConsolidationRequestInteractionBase):
included_consolidation_requests += r.valid_requests(consolidation_request_fee)
valid_requests_list += r.valid_requests(consolidation_request_fee)

valid_requests = Requests(
*valid_requests_list,
max_request_type=fork.max_request_type(block_number=1, timestamp=1),
)

if block_body_override_requests is None and block_body_extra_requests is not None:
block_body_override_requests = valid_requests.requests_list + block_body_extra_requests

rlp_modifier: Header | None = None
if correct_requests_hash_in_header:
rlp_modifier = Header(
requests_hash=valid_requests,
)
return [
Block(
txs=sum((r.transactions() for r in requests), []),
header_verify=Header(
requests_root=included_deposit_requests
+ included_withdrawal_requests
+ included_consolidation_requests,
requests_hash=valid_requests,
),
requests=block_body_override_requests,
exception=exception,
rlp_modifier=rlp_modifier,
)
]
Loading

0 comments on commit e2a1cbb

Please sign in to comment.