|
| 1 | +"""abstract: Test identity precompile output size.""" |
| 2 | + |
| 3 | +from typing import Tuple |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ethereum_test_base_types.composite_types import Storage |
| 8 | +from ethereum_test_tools import ( |
| 9 | + Account, |
| 10 | + Alloc, |
| 11 | + Environment, |
| 12 | + StateTestFiller, |
| 13 | + Transaction, |
| 14 | +) |
| 15 | +from ethereum_test_tools import Opcodes as Op |
| 16 | + |
| 17 | +from .common import CallArgs, generate_identity_call_bytecode |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.ported_from( |
| 21 | + [ |
| 22 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentitiy_0Filler.json", |
| 23 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentitiy_1Filler.json", |
| 24 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentity_1_nonzeroValueFiller.json", |
| 25 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentity_2Filler.json", |
| 26 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentity_3Filler.json", |
| 27 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentity_4Filler.json", |
| 28 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentity_4_gas17Filler.json", |
| 29 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentity_4_gas18Filler.json", |
| 30 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentitiy_0Filler.json", |
| 31 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentitiy_1Filler.json", |
| 32 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentity_1_nonzeroValueFiller.json", |
| 33 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentity_2Filler.json", |
| 34 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentity_3Filler.json", |
| 35 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentity_4Filler.json", |
| 36 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentity_4_gas17Filler.json", |
| 37 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentity_4_gas18Filler.json", |
| 38 | + ], |
| 39 | + pr=["https://github.com/ethereum/execution-spec-tests/pull/1344"], |
| 40 | +) |
| 41 | +@pytest.mark.valid_from("Byzantium") |
| 42 | +@pytest.mark.parametrize("call_type", [Op.CALL, Op.CALLCODE]) |
| 43 | +@pytest.mark.parametrize( |
| 44 | + [ |
| 45 | + "call_args", |
| 46 | + "memory_values", |
| 47 | + "call_succeeds", |
| 48 | + ], |
| 49 | + [ |
| 50 | + pytest.param(CallArgs(gas=0xFF), (0x1,), True, id="identity_0"), |
| 51 | + pytest.param( |
| 52 | + CallArgs(args_size=0x0), |
| 53 | + (0x0,), |
| 54 | + True, |
| 55 | + id="identity_1", |
| 56 | + ), |
| 57 | + pytest.param( |
| 58 | + CallArgs(gas=0x30D40, value=0x1, args_size=0x0), |
| 59 | + None, |
| 60 | + False, |
| 61 | + id="identity_1_nonzerovalue", |
| 62 | + ), |
| 63 | + pytest.param( |
| 64 | + CallArgs(args_size=0x25), |
| 65 | + (0xF34578907F,), |
| 66 | + True, |
| 67 | + id="identity_2", |
| 68 | + ), |
| 69 | + pytest.param( |
| 70 | + CallArgs(args_size=0x25), |
| 71 | + (0xF34578907F,), |
| 72 | + True, |
| 73 | + id="identity_3", |
| 74 | + ), |
| 75 | + pytest.param( |
| 76 | + CallArgs(gas=0x64), |
| 77 | + (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,), |
| 78 | + True, |
| 79 | + id="identity_4", |
| 80 | + ), |
| 81 | + pytest.param( |
| 82 | + CallArgs(gas=0x11), |
| 83 | + (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,), |
| 84 | + False, |
| 85 | + id="identity_4_insufficient_gas", |
| 86 | + ), |
| 87 | + pytest.param( |
| 88 | + CallArgs(gas=0x12), |
| 89 | + (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,), |
| 90 | + True, |
| 91 | + id="identity_4_exact_gas", |
| 92 | + ), |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_call_identity_precompile( |
| 96 | + state_test: StateTestFiller, |
| 97 | + pre: Alloc, |
| 98 | + call_type: Op, |
| 99 | + call_args: CallArgs, |
| 100 | + memory_values: Tuple[int, ...], |
| 101 | + call_succeeds: bool, |
| 102 | + tx_gas_limit: int, |
| 103 | +): |
| 104 | + """Test identity precompile RETURNDATA is sized correctly based on the input size.""" |
| 105 | + env = Environment() |
| 106 | + storage = Storage() |
| 107 | + |
| 108 | + contract_bytecode = generate_identity_call_bytecode( |
| 109 | + storage, |
| 110 | + call_type, |
| 111 | + memory_values, |
| 112 | + call_args, |
| 113 | + call_succeeds, |
| 114 | + ) |
| 115 | + |
| 116 | + account = pre.deploy_contract( |
| 117 | + contract_bytecode, |
| 118 | + storage=storage.canary(), |
| 119 | + ) |
| 120 | + |
| 121 | + tx = Transaction( |
| 122 | + to=account, |
| 123 | + sender=pre.fund_eoa(), |
| 124 | + gas_limit=tx_gas_limit, |
| 125 | + ) |
| 126 | + |
| 127 | + post = {account: Account(storage=storage)} |
| 128 | + |
| 129 | + state_test(env=env, pre=pre, post=post, tx=tx) |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.ported_from( |
| 133 | + [ |
| 134 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CALLCODEIdentity_5Filler.json", |
| 135 | + "https://github.com/ethereum/tests/blob/v17.1/src/GeneralStateTestsFiller/stPreCompiledContracts2/CallIdentity_5Filler.json", |
| 136 | + ], |
| 137 | + pr=["https://github.com/ethereum/execution-spec-tests/pull/1344"], |
| 138 | +) |
| 139 | +@pytest.mark.valid_from("Byzantium") |
| 140 | +@pytest.mark.parametrize("call_type", [Op.CALL, Op.CALLCODE]) |
| 141 | +@pytest.mark.parametrize( |
| 142 | + [ |
| 143 | + "call_args", |
| 144 | + "memory_values", |
| 145 | + "call_succeeds", |
| 146 | + ], |
| 147 | + [ |
| 148 | + pytest.param( |
| 149 | + CallArgs(gas=0x258, args_size=0xF4240), |
| 150 | + (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,), |
| 151 | + False, |
| 152 | + id="identity_5", |
| 153 | + ), |
| 154 | + pytest.param( |
| 155 | + CallArgs(gas=0x258, ret_size=0x40), |
| 156 | + ( |
| 157 | + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, |
| 158 | + 0x1234, |
| 159 | + ), |
| 160 | + True, |
| 161 | + id="identity_6", |
| 162 | + ), |
| 163 | + ], |
| 164 | +) |
| 165 | +@pytest.mark.parametrize("tx_gas_limit", [10_000_000]) |
| 166 | +def test_call_identity_precompile_large_params( |
| 167 | + state_test: StateTestFiller, |
| 168 | + pre: Alloc, |
| 169 | + call_type: Op, |
| 170 | + call_args: CallArgs, |
| 171 | + memory_values: Tuple[int, ...], |
| 172 | + call_succeeds: bool, |
| 173 | + tx_gas_limit: int, |
| 174 | +): |
| 175 | + """Test identity precompile when out of gas occurs.""" |
| 176 | + env = Environment() |
| 177 | + storage = Storage() |
| 178 | + |
| 179 | + contract_bytecode = generate_identity_call_bytecode( |
| 180 | + storage, |
| 181 | + call_type, |
| 182 | + memory_values, |
| 183 | + call_args, |
| 184 | + call_succeeds, |
| 185 | + ) |
| 186 | + |
| 187 | + account = pre.deploy_contract( |
| 188 | + contract_bytecode, |
| 189 | + storage=storage.canary(), |
| 190 | + ) |
| 191 | + |
| 192 | + tx = Transaction( |
| 193 | + to=account, |
| 194 | + sender=pre.fund_eoa(), |
| 195 | + gas_limit=tx_gas_limit, |
| 196 | + ) |
| 197 | + |
| 198 | + post = {account: Account(storage=storage)} |
| 199 | + |
| 200 | + state_test(env=env, pre=pre, post=post, tx=tx) |
0 commit comments