|
5 | 5 | import pytest
|
6 | 6 |
|
7 | 7 | from ethereum_test_forks import Fork, Osaka
|
8 |
| -from ethereum_test_tools import Account, Address, Alloc, Storage, Transaction |
| 8 | +from ethereum_test_tools import Account, Address, Alloc, Bytes, Storage, Transaction, keccak256 |
9 | 9 | from ethereum_test_tools.vm.opcode import Opcodes as Op
|
10 | 10 |
|
11 |
| -from .helpers import Vector |
| 11 | +from ...byzantium.eip198_modexp_precompile.helpers import ModExpInput |
12 | 12 | from .spec import Spec, Spec7883
|
13 | 13 |
|
14 | 14 |
|
| 15 | +@pytest.fixture |
| 16 | +def gas_old() -> int | None: |
| 17 | + """Get old gas cost from the test vector if any.""" |
| 18 | + return None |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def gas_new() -> int | None: |
| 23 | + """Get new gas cost from the test vector if any.""" |
| 24 | + return None |
| 25 | + |
| 26 | + |
15 | 27 | @pytest.fixture
|
16 | 28 | def call_opcode() -> Op:
|
17 |
| - """Return default call used to call the precompile.""" |
| 29 | + """Return call operation used to call the precompile.""" |
18 | 30 | return Op.CALL
|
19 | 31 |
|
20 | 32 |
|
21 | 33 | @pytest.fixture
|
22 |
| -def gas_measure_contract(pre: Alloc, call_opcode: Op, fork: Fork, vector: Vector) -> Address: |
23 |
| - """Deploys a contract that measures ModExp gas consumption.""" |
| 34 | +def call_contract_post_storage() -> Storage: |
| 35 | + """ |
| 36 | + Storage of the test contract after the transaction is executed. |
| 37 | + Note: Fixture `call_contract_code` fills the actual expected storage values. |
| 38 | + """ |
| 39 | + return Storage() |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def call_succeeds() -> bool: |
| 44 | + """ |
| 45 | + By default, depending on the expected output, we can deduce if the call is expected to succeed |
| 46 | + or fail. |
| 47 | + """ |
| 48 | + return True |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def gas_measure_contract( |
| 53 | + pre: Alloc, |
| 54 | + call_opcode: Op, |
| 55 | + fork: Fork, |
| 56 | + modexp_expected: bytes, |
| 57 | + precompile_gas: int, |
| 58 | + precompile_gas_modifier: int, |
| 59 | + call_contract_post_storage: Storage, |
| 60 | + call_succeeds: bool, |
| 61 | +) -> Address: |
| 62 | + """ |
| 63 | + Deploys a contract that measures ModExp gas consumption and execution result. |
| 64 | +
|
| 65 | + Always stored: |
| 66 | + storage[0]: precompile call success |
| 67 | + storage[1]: return data length from precompile |
| 68 | + Only if the precompile call succeeds: |
| 69 | + storage[2]: gas consumed by precompile |
| 70 | + storage[3]: hash of return data from precompile |
| 71 | + """ |
| 72 | + assert call_opcode in [Op.CALL, Op.CALLCODE, Op.DELEGATECALL, Op.STATICCALL] |
| 73 | + value = [0] if call_opcode in [Op.CALL, Op.CALLCODE] else [] |
| 74 | + |
24 | 75 | call_code = call_opcode(
|
25 |
| - address=Spec.MODEXP_ADDRESS, |
26 |
| - value=0, |
27 |
| - args_offset=0, |
28 |
| - args_size=Op.CALLDATASIZE, |
| 76 | + precompile_gas + precompile_gas_modifier, |
| 77 | + Spec.MODEXP_ADDRESS, |
| 78 | + *value, |
| 79 | + 0, |
| 80 | + Op.CALLDATASIZE(), |
| 81 | + 0, |
| 82 | + 0, |
29 | 83 | )
|
| 84 | + |
30 | 85 | gas_costs = fork.gas_costs()
|
31 | 86 | extra_gas = (
|
32 | 87 | gas_costs.G_WARM_ACCOUNT_ACCESS
|
33 |
| - + (gas_costs.G_VERY_LOW * (len(call_opcode.kwargs) - 2)) # type: ignore |
34 |
| - + (gas_costs.G_BASE * 3) |
| 88 | + + (gas_costs.G_VERY_LOW * (len(call_opcode.kwargs) - 1)) # type: ignore |
| 89 | + + gas_costs.G_BASE # CALLDATASIZE |
| 90 | + + gas_costs.G_BASE # GAS |
35 | 91 | )
|
36 |
| - measure_code = ( |
| 92 | + |
| 93 | + # Build the gas measurement contract code |
| 94 | + # Stack operations: |
| 95 | + # [gas_start] |
| 96 | + # [gas_start, call_result] |
| 97 | + # [gas_start, call_result, gas_end] |
| 98 | + # [gas_start, gas_end, call_result] |
| 99 | + call_result_measurement = Op.GAS + call_code + Op.GAS + Op.SWAP1 |
| 100 | + |
| 101 | + # Calculate gas consumed: gas_start - (gas_end + extra_gas) |
| 102 | + # Stack Operation: |
| 103 | + # [gas_start, gas_end] |
| 104 | + # [gas_start, gas_end, extra_gas] |
| 105 | + # [gas_start, gas_end + extra_gas] |
| 106 | + # [gas_end + extra_gas, gas_start] |
| 107 | + # [gas_consumed] |
| 108 | + gas_calculation = Op.PUSH2[extra_gas] + Op.ADD + Op.SWAP1 + Op.SUB |
| 109 | + |
| 110 | + code = ( |
37 | 111 | Op.CALLDATACOPY(dest_offset=0, offset=0, size=Op.CALLDATASIZE)
|
38 |
| - + Op.GAS # [gas_start] |
39 |
| - + call_code # [gas_start, call_result] |
40 |
| - + Op.GAS # [gas_start, call_result, gas_end] |
41 |
| - + Op.SWAP1 # [gas_start, gas_end, call_result] |
42 |
| - + Op.PUSH1[0] # [gas_start, gas_end, call_result, 0] |
43 |
| - + Op.SSTORE # [gas_start, gas_end] |
44 |
| - + Op.PUSH2[extra_gas] # [gas_start, gas_end, extra_gas] |
45 |
| - + Op.ADD # [gas_start, gas_end + extra_gas] |
46 |
| - + Op.SWAP1 # [gas_end + extra_gas, gas_start] |
47 |
| - + Op.SUB # [gas_start - (gas_end + extra_gas)] |
48 |
| - + Op.PUSH1[1] # [gas_start - (gas_end + extra_gas), 1] |
49 |
| - + Op.SSTORE # [] |
| 112 | + + Op.SSTORE(call_contract_post_storage.store_next(call_succeeds), call_result_measurement) |
| 113 | + + Op.SSTORE( |
| 114 | + call_contract_post_storage.store_next(len(modexp_expected)), |
| 115 | + Op.RETURNDATASIZE(), |
| 116 | + ) |
50 | 117 | )
|
51 |
| - measure_code += Op.SSTORE(2, Op.RETURNDATASIZE()) |
52 |
| - for i in range(len(vector.expected) // 32): |
53 |
| - measure_code += Op.RETURNDATACOPY(0, i * 32, 32) |
54 |
| - measure_code += Op.SSTORE(i + 3, Op.MLOAD(0)) |
55 |
| - measure_code += Op.STOP() |
56 |
| - return pre.deploy_contract(measure_code) |
| 118 | + |
| 119 | + if call_succeeds: |
| 120 | + code += Op.SSTORE(call_contract_post_storage.store_next(precompile_gas), gas_calculation) |
| 121 | + code += Op.RETURNDATACOPY(dest_offset=0, offset=0, size=Op.RETURNDATASIZE()) |
| 122 | + code += Op.SSTORE( |
| 123 | + call_contract_post_storage.store_next(keccak256(Bytes(modexp_expected))), |
| 124 | + Op.SHA3(0, Op.RETURNDATASIZE()), |
| 125 | + ) |
| 126 | + return pre.deploy_contract(code) |
57 | 127 |
|
58 | 128 |
|
59 | 129 | @pytest.fixture
|
60 |
| -def precompile_gas(fork: Fork, vector: Vector) -> int: |
| 130 | +def precompile_gas( |
| 131 | + fork: Fork, modexp_input: ModExpInput, gas_old: int | None, gas_new: int | None |
| 132 | +) -> int: |
61 | 133 | """Calculate gas cost for the ModExp precompile and verify it matches expected gas."""
|
62 | 134 | spec = Spec if fork < Osaka else Spec7883
|
63 |
| - expected_gas = vector.gas_old if fork < Osaka else vector.gas_new |
64 |
| - calculated_gas = spec.calculate_gas_cost( |
65 |
| - len(vector.input.base), |
66 |
| - len(vector.input.modulus), |
67 |
| - len(vector.input.exponent), |
68 |
| - vector.input.exponent, |
69 |
| - ) |
70 |
| - assert calculated_gas == expected_gas, ( |
71 |
| - f"Calculated gas {calculated_gas} != Vector gas {expected_gas}\n" |
72 |
| - f"Lengths: base: {hex(len(vector.input.base))} ({len(vector.input.base)}), " |
73 |
| - f"exponent: {hex(len(vector.input.exponent))} ({len(vector.input.exponent)}), " |
74 |
| - f"modulus: {hex(len(vector.input.modulus))} ({len(vector.input.modulus)})\n" |
75 |
| - f"Exponent: {vector.input.exponent} " |
76 |
| - f"({int.from_bytes(vector.input.exponent, byteorder='big')})" |
77 |
| - ) |
78 |
| - return calculated_gas |
| 135 | + try: |
| 136 | + calculated_gas = spec.calculate_gas_cost( |
| 137 | + len(modexp_input.base), |
| 138 | + len(modexp_input.modulus), |
| 139 | + len(modexp_input.exponent), |
| 140 | + modexp_input.exponent, |
| 141 | + ) |
| 142 | + if gas_old is not None and gas_new is not None: |
| 143 | + expected_gas = gas_old if fork < Osaka else gas_new |
| 144 | + assert calculated_gas == expected_gas, ( |
| 145 | + f"Calculated gas {calculated_gas} != Vector gas {expected_gas}\n" |
| 146 | + f"Lengths: base: {hex(len(modexp_input.base))} ({len(modexp_input.base)}), " |
| 147 | + f"exponent: {hex(len(modexp_input.exponent))} ({len(modexp_input.exponent)}), " |
| 148 | + f"modulus: {hex(len(modexp_input.modulus))} ({len(modexp_input.modulus)})\n" |
| 149 | + f"Exponent: {modexp_input.exponent} " |
| 150 | + f"({int.from_bytes(modexp_input.exponent, byteorder='big')})" |
| 151 | + ) |
| 152 | + return calculated_gas |
| 153 | + except Exception as e: |
| 154 | + print(f"Warning: Error calculating gas, using minimum: {e}") |
| 155 | + return 500 if fork >= Osaka else 200 |
| 156 | + |
| 157 | + |
| 158 | +@pytest.fixture |
| 159 | +def precompile_gas_modifier() -> int: |
| 160 | + """Return the gas modifier for the ModExp precompile.""" |
| 161 | + return 0 |
79 | 162 |
|
80 | 163 |
|
81 | 164 | @pytest.fixture
|
82 | 165 | def tx(
|
83 |
| - fork: Fork, |
84 | 166 | pre: Alloc,
|
85 | 167 | gas_measure_contract: Address,
|
86 |
| - vector: Vector, |
87 |
| - precompile_gas: int, |
| 168 | + modexp_input: ModExpInput, |
| 169 | + tx_gas_limit: int, |
88 | 170 | ) -> Transaction:
|
89 | 171 | """Transaction to measure gas consumption of the ModExp precompile."""
|
90 |
| - intrinsic_gas_cost_calc = fork.transaction_intrinsic_cost_calculator() |
91 |
| - intrinsic_gas_cost = intrinsic_gas_cost_calc(calldata=vector.input) |
92 |
| - memory_expansion_gas_calc = fork.memory_expansion_gas_calculator() |
93 |
| - memory_expansion_gas = memory_expansion_gas_calc(new_bytes=len(bytes(vector.input))) |
94 |
| - sstore_gas = fork.gas_costs().G_STORAGE_SET * (len(vector.expected) // 32) |
95 | 172 | return Transaction(
|
96 | 173 | sender=pre.fund_eoa(),
|
97 | 174 | to=gas_measure_contract,
|
98 |
| - data=vector.input, |
99 |
| - gas_limit=intrinsic_gas_cost |
| 175 | + data=bytes(modexp_input), |
| 176 | + gas_limit=tx_gas_limit, |
| 177 | + ) |
| 178 | + |
| 179 | + |
| 180 | +@pytest.fixture |
| 181 | +def tx_gas_limit( |
| 182 | + fork: Fork, modexp_expected: bytes, modexp_input: ModExpInput, precompile_gas: int |
| 183 | +) -> int: |
| 184 | + """Transaction gas limit used for the test (Can be overridden in the test).""" |
| 185 | + intrinsic_gas_cost_calculator = fork.transaction_intrinsic_cost_calculator() |
| 186 | + memory_expansion_gas_calculator = fork.memory_expansion_gas_calculator() |
| 187 | + sstore_gas = fork.gas_costs().G_STORAGE_SET * (len(modexp_expected) // 32) |
| 188 | + extra_gas = 100_000 |
| 189 | + |
| 190 | + total_gas = ( |
| 191 | + extra_gas |
| 192 | + + intrinsic_gas_cost_calculator(calldata=bytes(modexp_input)) |
| 193 | + + memory_expansion_gas_calculator(new_bytes=len(bytes(modexp_input))) |
100 | 194 | + precompile_gas
|
101 |
| - + memory_expansion_gas |
102 | 195 | + sstore_gas
|
103 |
| - + 100_000, |
104 | 196 | )
|
105 | 197 |
|
| 198 | + tx_gas_limit_cap = fork.transaction_gas_limit_cap() |
| 199 | + |
| 200 | + if tx_gas_limit_cap is not None: |
| 201 | + return min(tx_gas_limit_cap, total_gas) |
| 202 | + return total_gas |
| 203 | + |
106 | 204 |
|
107 | 205 | @pytest.fixture
|
108 | 206 | def post(
|
109 | 207 | gas_measure_contract: Address,
|
110 |
| - precompile_gas: int, |
111 |
| - vector: Vector, |
| 208 | + call_contract_post_storage: Storage, |
112 | 209 | ) -> Dict[Address, Account]:
|
113 | 210 | """Return expected post state with gas consumption check."""
|
114 |
| - storage = Storage() |
115 |
| - storage[0] = 1 |
116 |
| - storage[1] = precompile_gas |
117 |
| - storage[2] = len(vector.expected) |
118 |
| - for i in range(len(vector.expected) // 32): |
119 |
| - storage[i + 3] = vector.expected[i * 32 : (i + 1) * 32] |
120 |
| - return {gas_measure_contract: Account(storage=storage)} |
| 211 | + return { |
| 212 | + gas_measure_contract: Account(storage=call_contract_post_storage), |
| 213 | + } |
0 commit comments