|
| 1 | +""" |
| 2 | +abstract: Test [EIP-7623: Increase calldata cost](https://eips.ethereum.org/EIPS/eip-7623) |
| 3 | + Test applied refunds for [EIP-7623: Increase calldata cost](https://eips.ethereum.org/EIPS/eip-7623). |
| 4 | +""" # noqa: E501 |
| 5 | + |
| 6 | +from enum import Enum, Flag, auto |
| 7 | +from typing import Dict |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from ethereum_test_forks import Fork, Prague |
| 12 | +from ethereum_test_tools import ( |
| 13 | + Address, |
| 14 | + Alloc, |
| 15 | + Bytecode, |
| 16 | + StateTestFiller, |
| 17 | + Transaction, |
| 18 | + TransactionReceipt, |
| 19 | +) |
| 20 | +from ethereum_test_tools import Opcodes as Op |
| 21 | + |
| 22 | +from .helpers import DataTestType |
| 23 | +from .spec import ref_spec_7623 |
| 24 | + |
| 25 | +REFERENCE_SPEC_GIT_PATH = ref_spec_7623.git_path |
| 26 | +REFERENCE_SPEC_VERSION = ref_spec_7623.version |
| 27 | + |
| 28 | +ENABLE_FORK = Prague |
| 29 | +pytestmark = [pytest.mark.valid_from(str(ENABLE_FORK))] |
| 30 | + |
| 31 | + |
| 32 | +class RefundTestType(Enum): |
| 33 | + """Refund test type.""" |
| 34 | + |
| 35 | + EXECUTION_GAS_MINUS_REFUND_GREATER_THAN_DATA_FLOOR = 0 |
| 36 | + """ |
| 37 | + The execution gas minus the refund is greater than the data floor, hence the execution gas cost |
| 38 | + is charged. |
| 39 | + """ |
| 40 | + EXECUTION_GAS_MINUS_REFUND_LESS_THAN_DATA_FLOOR = 1 |
| 41 | + """ |
| 42 | + The execution gas minus the refund is less than the data floor, hence the data floor cost is |
| 43 | + charged. |
| 44 | + """ |
| 45 | + |
| 46 | + |
| 47 | +class RefundType(Flag): |
| 48 | + """Refund type.""" |
| 49 | + |
| 50 | + STORAGE_CLEAR = auto() |
| 51 | + """The storage is cleared from a non-zero value.""" |
| 52 | + |
| 53 | + AUTHORIZATION_EXISTING_AUTHORITY = auto() |
| 54 | + """The authorization list contains an authorization where the authority exists in the state.""" |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def data_test_type() -> DataTestType: |
| 59 | + """Return data test type.""" |
| 60 | + return DataTestType.FLOOR_GAS_COST_GREATER_THAN_INTRINSIC_GAS |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def authorization_refund(refund_type: RefundType) -> int: |
| 65 | + """Modify fixture from conftest to automatically read the refund_type information.""" |
| 66 | + return RefundType.AUTHORIZATION_EXISTING_AUTHORITY in refund_type |
| 67 | + |
| 68 | + |
| 69 | +@pytest.fixture |
| 70 | +def ty(refund_type: RefundType) -> int: |
| 71 | + """Modify fixture from conftest to automatically read the refund_type information.""" |
| 72 | + if RefundType.AUTHORIZATION_EXISTING_AUTHORITY in refund_type: |
| 73 | + return 4 |
| 74 | + return 2 |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +def max_refund(fork: Fork, refund_type: RefundType) -> int: |
| 79 | + """Return the max refund gas of the transaction.""" |
| 80 | + gas_costs = fork.gas_costs() |
| 81 | + max_refund = gas_costs.R_STORAGE_CLEAR if RefundType.STORAGE_CLEAR in refund_type else 0 |
| 82 | + max_refund += ( |
| 83 | + gas_costs.R_AUTHORIZATION_EXISTING_AUTHORITY |
| 84 | + if RefundType.AUTHORIZATION_EXISTING_AUTHORITY in refund_type |
| 85 | + else 0 |
| 86 | + ) |
| 87 | + return max_refund |
| 88 | + |
| 89 | + |
| 90 | +@pytest.fixture |
| 91 | +def prefix_code_gas(fork: Fork, refund_type: RefundType) -> int: |
| 92 | + """Return the minimum execution gas cost due to the refund type.""" |
| 93 | + if RefundType.STORAGE_CLEAR in refund_type: |
| 94 | + # Minimum code to generate a storage clear is Op.SSTORE(0, 0). |
| 95 | + gas_costs = fork.gas_costs() |
| 96 | + return gas_costs.G_COLD_SLOAD + gas_costs.G_STORAGE_RESET + (gas_costs.G_VERY_LOW * 2) |
| 97 | + return 0 |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture |
| 101 | +def prefix_code(refund_type: RefundType) -> Bytecode: |
| 102 | + """Return the minimum execution gas cost due to the refund type.""" |
| 103 | + if RefundType.STORAGE_CLEAR in refund_type: |
| 104 | + # Clear the storage to trigger a refund. |
| 105 | + return Op.SSTORE(0, 0) |
| 106 | + return Bytecode() |
| 107 | + |
| 108 | + |
| 109 | +@pytest.fixture |
| 110 | +def code_storage(refund_type: RefundType) -> Dict: |
| 111 | + """Return the minimum execution gas cost due to the refund type.""" |
| 112 | + if RefundType.STORAGE_CLEAR in refund_type: |
| 113 | + # Pre-set the storage to be cleared. |
| 114 | + return {0: 1} |
| 115 | + return {} |
| 116 | + |
| 117 | + |
| 118 | +@pytest.fixture |
| 119 | +def execution_gas_used( |
| 120 | + tx_intrinsic_gas_cost_before_execution: int, |
| 121 | + tx_floor_data_cost: int, |
| 122 | + max_refund: int, |
| 123 | + prefix_code_gas: int, |
| 124 | + refund_test_type: RefundTestType, |
| 125 | +) -> int: |
| 126 | + """ |
| 127 | + Return the amount of gas that needs to be consumed by the execution. |
| 128 | +
|
| 129 | + This gas amount is on top of the transaction intrinsic gas cost. |
| 130 | +
|
| 131 | + Since intrinsic_gas_data_floor_minimum_delta is zero for all test cases, if this value is zero |
| 132 | + it will result in the refund being applied to the execution gas cost and the resulting amount |
| 133 | + being always below the floor data cost. |
| 134 | + """ |
| 135 | + |
| 136 | + def execution_gas_cost(execution_gas: int) -> int: |
| 137 | + total_gas_used = tx_intrinsic_gas_cost_before_execution + execution_gas |
| 138 | + return total_gas_used - min(max_refund, total_gas_used // 5) |
| 139 | + |
| 140 | + execution_gas = prefix_code_gas |
| 141 | + |
| 142 | + assert execution_gas_cost(execution_gas) < tx_floor_data_cost, "tx_floor_data_cost is too low" |
| 143 | + |
| 144 | + # Dumb for-loop to find the execution gas cost that will result in the expected refund. |
| 145 | + while execution_gas_cost(execution_gas) < tx_floor_data_cost: |
| 146 | + execution_gas += 1 |
| 147 | + if refund_test_type == RefundTestType.EXECUTION_GAS_MINUS_REFUND_GREATER_THAN_DATA_FLOOR: |
| 148 | + return execution_gas |
| 149 | + elif refund_test_type == RefundTestType.EXECUTION_GAS_MINUS_REFUND_LESS_THAN_DATA_FLOOR: |
| 150 | + return execution_gas - 1 |
| 151 | + |
| 152 | + raise ValueError("Invalid refund test type") |
| 153 | + |
| 154 | + |
| 155 | +@pytest.fixture |
| 156 | +def refund( |
| 157 | + tx_intrinsic_gas_cost_before_execution: int, |
| 158 | + execution_gas_used: int, |
| 159 | + max_refund: int, |
| 160 | +) -> int: |
| 161 | + """Return the refund gas of the transaction.""" |
| 162 | + total_gas_used = tx_intrinsic_gas_cost_before_execution + execution_gas_used |
| 163 | + return min(max_refund, total_gas_used // 5) |
| 164 | + |
| 165 | + |
| 166 | +@pytest.fixture |
| 167 | +def to( |
| 168 | + pre: Alloc, |
| 169 | + execution_gas_used: int, |
| 170 | + prefix_code: Bytecode, |
| 171 | + prefix_code_gas: int, |
| 172 | + code_storage: Dict, |
| 173 | +) -> Address | None: |
| 174 | + """Return a contract that consumes the expected execution gas.""" |
| 175 | + return pre.deploy_contract( |
| 176 | + prefix_code + (Op.JUMPDEST * (execution_gas_used - prefix_code_gas)) + Op.STOP, |
| 177 | + storage=code_storage, |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +@pytest.fixture |
| 182 | +def tx_gas_limit( |
| 183 | + tx_intrinsic_gas_cost_including_floor_data_cost: int, |
| 184 | + tx_intrinsic_gas_cost_before_execution: int, |
| 185 | + execution_gas_used: int, |
| 186 | +) -> int: |
| 187 | + """ |
| 188 | + Gas limit for the transaction. |
| 189 | +
|
| 190 | + The gas delta is added to the intrinsic gas cost to generate different test scenarios. |
| 191 | + """ |
| 192 | + tx_gas_limit = tx_intrinsic_gas_cost_before_execution + execution_gas_used |
| 193 | + assert tx_gas_limit >= tx_intrinsic_gas_cost_including_floor_data_cost |
| 194 | + return tx_gas_limit |
| 195 | + |
| 196 | + |
| 197 | +@pytest.mark.parametrize( |
| 198 | + "refund_test_type", |
| 199 | + [ |
| 200 | + RefundTestType.EXECUTION_GAS_MINUS_REFUND_GREATER_THAN_DATA_FLOOR, |
| 201 | + RefundTestType.EXECUTION_GAS_MINUS_REFUND_LESS_THAN_DATA_FLOOR, |
| 202 | + ], |
| 203 | +) |
| 204 | +@pytest.mark.parametrize( |
| 205 | + "refund_type", |
| 206 | + [ |
| 207 | + RefundType.STORAGE_CLEAR, |
| 208 | + RefundType.STORAGE_CLEAR | RefundType.AUTHORIZATION_EXISTING_AUTHORITY, |
| 209 | + RefundType.AUTHORIZATION_EXISTING_AUTHORITY, |
| 210 | + ], |
| 211 | +) |
| 212 | +def test_gas_refunds_from_data_floor( |
| 213 | + state_test: StateTestFiller, |
| 214 | + pre: Alloc, |
| 215 | + tx: Transaction, |
| 216 | + tx_floor_data_cost: int, |
| 217 | + tx_intrinsic_gas_cost_before_execution: int, |
| 218 | + execution_gas_cost: int, |
| 219 | + refund: int, |
| 220 | + refund_test_type: RefundTestType, |
| 221 | +) -> None: |
| 222 | + """Test gas refunds deducted from the execution gas cost and not the data floor.""" |
| 223 | + gas_used = tx_intrinsic_gas_cost_before_execution + execution_gas_cost - refund |
| 224 | + if refund_test_type == RefundTestType.EXECUTION_GAS_MINUS_REFUND_LESS_THAN_DATA_FLOOR: |
| 225 | + assert gas_used < tx_floor_data_cost |
| 226 | + elif refund_test_type == RefundTestType.EXECUTION_GAS_MINUS_REFUND_GREATER_THAN_DATA_FLOOR: |
| 227 | + assert gas_used >= tx_floor_data_cost |
| 228 | + else: |
| 229 | + raise ValueError("Invalid refund test type") |
| 230 | + if gas_used < tx_floor_data_cost: |
| 231 | + gas_used = tx_floor_data_cost |
| 232 | + tx.expected_receipt = TransactionReceipt(gas_used=gas_used) |
| 233 | + state_test( |
| 234 | + pre=pre, |
| 235 | + post={ |
| 236 | + tx.to: { |
| 237 | + "storage": {0: 0}, # Verify storage was cleared |
| 238 | + } |
| 239 | + }, |
| 240 | + tx=tx, |
| 241 | + ) |
0 commit comments