|
| 1 | +"""abstract: Test identity precompile output size.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_tools import ( |
| 6 | + Account, |
| 7 | + Alloc, |
| 8 | + Bytecode, |
| 9 | + Environment, |
| 10 | + StateTestFiller, |
| 11 | + Transaction, |
| 12 | +) |
| 13 | +from ethereum_test_tools import Opcodes as Op |
| 14 | + |
| 15 | +REFERENCE_SPEC_GIT_PATH = "EIPS/eip-152.md" |
| 16 | +REFERENCE_SPEC_VERSION = "5510973b40973b6aa774f04c9caba823c8ff8460" |
| 17 | + |
| 18 | +IDENTITY_PRECOMPILE_ADDRESS = 0x04 |
| 19 | + |
| 20 | + |
| 21 | +class CallInputs: |
| 22 | + """Defines inputs to CALL for the Identity precompile.""" |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + gas=0x1F4, |
| 27 | + value=0x0, |
| 28 | + args_offset=0x0, |
| 29 | + args_size=0x20, |
| 30 | + ret_offset=0x0, |
| 31 | + ret_size=0x20, |
| 32 | + ): |
| 33 | + """Create a new instance with the provided values.""" |
| 34 | + self.gas = gas |
| 35 | + self.value = value |
| 36 | + self.args_offset = args_offset |
| 37 | + self.args_size = args_size |
| 38 | + self.ret_offset = ret_offset |
| 39 | + self.ret_size = ret_size |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.valid_from("Byzantium") |
| 43 | +@pytest.mark.parametrize( |
| 44 | + [ |
| 45 | + "inputs", |
| 46 | + "store_values_code", |
| 47 | + "stored_value_offset", |
| 48 | + "expected_stored_value", |
| 49 | + "expected_call_result", |
| 50 | + ], |
| 51 | + [ |
| 52 | + pytest.param(CallInputs(gas=0xFF), Op.MSTORE(0, 0x1), 0x0, 0x1, 0x1, id="identity_0"), |
| 53 | + pytest.param( |
| 54 | + CallInputs(args_size=0x0), |
| 55 | + Op.MSTORE(0, 0x0), |
| 56 | + 0x0, |
| 57 | + 0x0, |
| 58 | + 0x1, |
| 59 | + id="identity_1", |
| 60 | + ), |
| 61 | + pytest.param( |
| 62 | + CallInputs(gas=0x30D40, value=0x13, args_size=0x0), |
| 63 | + Bytecode(), |
| 64 | + 0x0, |
| 65 | + 0x0, |
| 66 | + 0x1, |
| 67 | + id="identity_1_nonzero", |
| 68 | + ), |
| 69 | + pytest.param( |
| 70 | + CallInputs(args_size=0x25), |
| 71 | + Op.MSTORE(0, 0xF34578907F), |
| 72 | + 0x0, |
| 73 | + 0xF34578907F, |
| 74 | + 0x1, |
| 75 | + id="identity_2", |
| 76 | + ), |
| 77 | + pytest.param( |
| 78 | + CallInputs(args_size=0x25), |
| 79 | + Op.MSTORE(0, 0xF34578907F), |
| 80 | + 0x0, |
| 81 | + 0xF34578907F, |
| 82 | + 0x1, |
| 83 | + id="identity_3", |
| 84 | + ), |
| 85 | + pytest.param( |
| 86 | + CallInputs(gas=0x64), |
| 87 | + Op.MSTORE(0, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), |
| 88 | + 0x0, |
| 89 | + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, |
| 90 | + 0x1, |
| 91 | + id="identity_4", |
| 92 | + ), |
| 93 | + pytest.param( |
| 94 | + CallInputs(gas=0x11), |
| 95 | + Op.MSTORE(0, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), |
| 96 | + 0x0, |
| 97 | + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, |
| 98 | + 0x0, |
| 99 | + id="identity_4_gas17", |
| 100 | + ), |
| 101 | + pytest.param( |
| 102 | + CallInputs(gas=0x12), |
| 103 | + Op.MSTORE(0, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), |
| 104 | + 0x0, |
| 105 | + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, |
| 106 | + 0x1, |
| 107 | + id="identity_4_gas18", |
| 108 | + ), |
| 109 | + pytest.param( |
| 110 | + CallInputs(gas=0x258, args_size=0xF4240), |
| 111 | + Op.MSTORE(0, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF), |
| 112 | + 0x0, |
| 113 | + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, |
| 114 | + 0xDEADBEEF, |
| 115 | + id="identity_5", |
| 116 | + ), |
| 117 | + pytest.param( |
| 118 | + CallInputs(gas=0x258, ret_size=0x40), |
| 119 | + Op.MSTORE(0, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) |
| 120 | + + Op.MSTORE(0x20, 0x1234), |
| 121 | + 0x20, |
| 122 | + 0x1234, |
| 123 | + 0x1, |
| 124 | + id="identity_6", |
| 125 | + ), |
| 126 | + ], |
| 127 | +) |
| 128 | +def test_call_identity_precompile( |
| 129 | + state_test: StateTestFiller, |
| 130 | + pre: Alloc, |
| 131 | + inputs: CallInputs, |
| 132 | + store_values_code: Bytecode, |
| 133 | + stored_value_offset: int, |
| 134 | + expected_stored_value: int, |
| 135 | + expected_call_result: int, |
| 136 | +): |
| 137 | + """Test identity precompile RETURNDATA is sized correctly based on the input size.""" |
| 138 | + env = Environment() |
| 139 | + |
| 140 | + account = pre.deploy_contract( |
| 141 | + store_values_code |
| 142 | + + Op.SSTORE( |
| 143 | + 2, |
| 144 | + Op.CALL( |
| 145 | + gas=inputs.gas, |
| 146 | + address=IDENTITY_PRECOMPILE_ADDRESS, |
| 147 | + value=inputs.value, |
| 148 | + args_offset=inputs.args_offset, |
| 149 | + args_size=inputs.args_size, |
| 150 | + ret_offset=inputs.ret_offset, |
| 151 | + ret_size=inputs.ret_size, |
| 152 | + ), |
| 153 | + ) |
| 154 | + + Op.SSTORE(0, Op.MLOAD(stored_value_offset)) |
| 155 | + + Op.STOP, |
| 156 | + storage={0: 0xDEADBEEF, 2: 0xDEADBEEF}, |
| 157 | + ) |
| 158 | + |
| 159 | + tx = Transaction( |
| 160 | + to=account, |
| 161 | + sender=pre.fund_eoa(), |
| 162 | + gas_limit=1_000_000, |
| 163 | + protected=True, |
| 164 | + ) |
| 165 | + |
| 166 | + post = {account: Account(storage={0: expected_stored_value, 2: expected_call_result})} |
| 167 | + |
| 168 | + state_test(env=env, pre=pre, post=post, tx=tx) |
0 commit comments