|
| 1 | +""" |
| 2 | +Test the request types that can be included in a block by the given fork. |
| 3 | +""" |
| 4 | +from typing import List |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from ethereum_test_exceptions import BlockException |
| 9 | +from ethereum_test_forks import Fork |
| 10 | +from ethereum_test_tools import Alloc, Block, BlockchainTestFiller, Environment |
| 11 | + |
| 12 | +from ..eip6110_deposits.helpers import DepositInteractionBase, DepositRequest, DepositTransaction |
| 13 | +from ..eip7002_el_triggerable_withdrawals.helpers import ( |
| 14 | + WithdrawalRequest, |
| 15 | + WithdrawalRequestInteractionBase, |
| 16 | + WithdrawalRequestTransaction, |
| 17 | +) |
| 18 | +from ..eip7251_consolidations.helpers import ( |
| 19 | + ConsolidationRequest, |
| 20 | + ConsolidationRequestInteractionBase, |
| 21 | + ConsolidationRequestTransaction, |
| 22 | +) |
| 23 | +from .spec import ref_spec_7685 |
| 24 | + |
| 25 | +REFERENCE_SPEC_GIT_PATH = ref_spec_7685.git_path |
| 26 | +REFERENCE_SPEC_VERSION = ref_spec_7685.version |
| 27 | + |
| 28 | +pytestmark = pytest.mark.valid_from("Prague") |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def block_body_extra_requests(fork: Fork, invalid_request_data: bytes) -> List[bytes]: |
| 33 | + """List of requests that overwrite the requests in the header. None by default.""" |
| 34 | + invalid_request_type = fork.max_request_type() + 1 |
| 35 | + return [bytes([invalid_request_type]) + invalid_request_data] |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def requests( |
| 40 | + fork: Fork, |
| 41 | + include_valid_requests: bool, |
| 42 | +) -> List[ |
| 43 | + DepositInteractionBase | WithdrawalRequestInteractionBase | ConsolidationRequestInteractionBase |
| 44 | +]: |
| 45 | + """List of valid requests that are added along with the invalid request.""" |
| 46 | + if not include_valid_requests: |
| 47 | + return [] |
| 48 | + if fork.max_request_type() == 2: |
| 49 | + return [ |
| 50 | + DepositTransaction( |
| 51 | + requests=[ |
| 52 | + DepositRequest( |
| 53 | + pubkey=1, |
| 54 | + withdrawal_credentials=2, |
| 55 | + amount=1_000_000_000, |
| 56 | + signature=3, |
| 57 | + index=0, |
| 58 | + ) |
| 59 | + ] |
| 60 | + ), |
| 61 | + WithdrawalRequestTransaction( |
| 62 | + requests=[ |
| 63 | + WithdrawalRequest( |
| 64 | + validator_pubkey=1, |
| 65 | + amount=0, |
| 66 | + fee=1, |
| 67 | + ) |
| 68 | + ] |
| 69 | + ), |
| 70 | + ConsolidationRequestTransaction( |
| 71 | + requests=[ |
| 72 | + ConsolidationRequest( |
| 73 | + source_pubkey=2, |
| 74 | + target_pubkey=5, |
| 75 | + fee=1, |
| 76 | + ) |
| 77 | + ] |
| 78 | + ), |
| 79 | + ] |
| 80 | + raise NotImplementedError(f"Unsupported fork: {fork}") |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize( |
| 84 | + "include_valid_requests", |
| 85 | + [False, True], |
| 86 | +) |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "invalid_request_data", |
| 89 | + [ |
| 90 | + pytest.param(b"", id="no_data"), |
| 91 | + pytest.param(b"\0", id="single_byte"), |
| 92 | + pytest.param(b"\0" * 32, id="32_bytes"), |
| 93 | + ], |
| 94 | +) |
| 95 | +@pytest.mark.parametrize( |
| 96 | + "exception", |
| 97 | + [ |
| 98 | + pytest.param(BlockException.INVALID_REQUESTS, id=""), |
| 99 | + ], |
| 100 | +) |
| 101 | +def test_invalid_request_type( |
| 102 | + blockchain_test: BlockchainTestFiller, |
| 103 | + pre: Alloc, |
| 104 | + blocks: List[Block], |
| 105 | +): |
| 106 | + """ |
| 107 | + Test sending a block with an invalid request type. |
| 108 | + """ |
| 109 | + blockchain_test( |
| 110 | + genesis_environment=Environment(), |
| 111 | + pre=pre, |
| 112 | + post={}, |
| 113 | + blocks=blocks, |
| 114 | + ) |
0 commit comments