Skip to content

test_gas_usage()

Documentation for tests/shanghai/eip3860_initcode/test_initcode.py::TestContractCreationGasUsage::test_gas_usage@verkle@v0.0.6.

Generate fixtures for these test cases for Shanghai with:

Shanghai only:

fill -v tests/shanghai/eip3860_initcode/test_initcode.py::TestContractCreationGasUsage::test_gas_usage --fork=Shanghai --evm-bin=/path/to/evm-tool-dev-version

For all forks up to and including Shanghai:

fill -v tests/shanghai/eip3860_initcode/test_initcode.py::TestContractCreationGasUsage::test_gas_usage --until=Shanghai

Tests the following cases that verify the gas cost behavior of a contract creating transaction:

  1. Test with exact intrinsic gas minus one, contract create fails and tx is invalid.
  2. Test with exact intrinsic gas, contract create fails, but tx is valid.
  3. Test with exact execution gas minus one, contract create fails, but tx is valid.
  4. Test with exact execution gas, contract create succeeds.

Initcode must be within a valid EIP-3860 length.

Source code in tests/shanghai/eip3860_initcode/test_initcode.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
@pytest.mark.parametrize(
    "initcode",
    [
        INITCODE_ZEROS_MAX_LIMIT,
        INITCODE_ONES_MAX_LIMIT,
        EMPTY_INITCODE,
        SINGLE_BYTE_INITCODE,
        INITCODE_ZEROS_32_BYTES,
        INITCODE_ZEROS_33_BYTES,
        INITCODE_ZEROS_49120_BYTES,
        INITCODE_ZEROS_49121_BYTES,
    ],
    ids=get_initcode_name,
)
@pytest.mark.parametrize(
    "gas_test_case",
    [
        "too_little_intrinsic_gas",
        "exact_intrinsic_gas",
        "too_little_execution_gas",
        "exact_execution_gas",
    ],
    ids=lambda x: x,
)
class TestContractCreationGasUsage:
    """
    Tests the following cases that verify the gas cost behavior of a
    contract creating transaction:

    1. Test with exact intrinsic gas minus one, contract create fails
        and tx is invalid.
    2. Test with exact intrinsic gas, contract create fails,
        but tx is valid.
    3. Test with exact execution gas minus one, contract create fails,
        but tx is valid.
    4. Test with exact execution gas, contract create succeeds.

    Initcode must be within a valid EIP-3860 length.
    """

    @pytest.fixture
    def exact_intrinsic_gas(self, initcode: Initcode) -> int:
        """
        Calculates the intrinsic tx gas cost.
        """
        return calculate_create_tx_intrinsic_cost(initcode)

    @pytest.fixture
    def exact_execution_gas(self, initcode: Initcode) -> int:
        """
        Calculates the total execution gas cost.
        """
        return calculate_create_tx_execution_cost(initcode)

    @pytest.fixture
    def tx_error(self, gas_test_case: str) -> TransactionException | None:
        """
        Check that the transaction is invalid if too little intrinsic gas is
        specified, otherwise the tx is valid and succeeds.
        """
        if gas_test_case == "too_little_intrinsic_gas":
            return TransactionException.INTRINSIC_GAS_TOO_LOW
        return None

    @pytest.fixture
    def tx(
        self,
        sender: EOA,
        initcode: Initcode,
        gas_test_case: str,
        tx_error: TransactionException | None,
        exact_intrinsic_gas: int,
        exact_execution_gas: int,
    ) -> Transaction:
        """
        Implement the gas_test_case by setting the gas_limit of the tx
        appropriately and test whether the tx succeeds or fails with
        appropriate error.
        """
        if gas_test_case == "too_little_intrinsic_gas":
            gas_limit = exact_intrinsic_gas - 1
        elif gas_test_case == "exact_intrinsic_gas":
            gas_limit = exact_intrinsic_gas
        elif gas_test_case == "too_little_execution_gas":
            gas_limit = exact_execution_gas - 1
        elif gas_test_case == "exact_execution_gas":
            gas_limit = exact_execution_gas
        else:
            pytest.fail("Invalid gas test case provided.")

        return Transaction(
            nonce=0,
            to=None,
            data=initcode,
            gas_limit=gas_limit,
            gas_price=10,
            error=tx_error,
            sender=sender,
        )

    @pytest.fixture
    def post(
        self,
        sender: EOA,
        initcode: Initcode,
        gas_test_case: str,
        exact_intrinsic_gas: int,
        exact_execution_gas: int,
    ) -> Alloc:
        """
        Test that contract creation fails unless enough execution gas is
        provided.
        """
        create_contract_address = compute_create_address(
            address=sender,
            nonce=0,
        )
        if gas_test_case == "exact_intrinsic_gas" and exact_intrinsic_gas == exact_execution_gas:
            # Special scenario where the execution of the initcode and
            # gas cost to deploy are zero
            return Alloc({create_contract_address: Account(code=initcode.deploy_code)})
        elif gas_test_case == "exact_execution_gas":
            return Alloc({create_contract_address: Account(code=initcode.deploy_code)})
        return Alloc({create_contract_address: Account.NONEXISTENT})

    def test_gas_usage(
        self,
        state_test: StateTestFiller,
        env: Environment,
        pre: Alloc,
        post: Alloc,
        tx: Transaction,
        gas_test_case: str,
        initcode: Initcode,
        exact_intrinsic_gas: int,
        exact_execution_gas: int,
    ):
        """
        Test transaction and contract creation behavior for different gas
        limits.
        """
        if (gas_test_case == "too_little_execution_gas") and (
            exact_execution_gas == exact_intrinsic_gas
        ):
            pytest.skip(
                "Special case, the execution of the initcode and gas "
                "cost to deploy are zero: Then this test case is "
                "equivalent to that of 'test_exact_intrinsic_gas'."
            )

        state_test(
            env=env,
            pre=pre,
            post=post,
            tx=tx,
        )

test_gas_usage(state_test, env, pre, post, tx, gas_test_case, initcode, exact_intrinsic_gas, exact_execution_gas)

Test transaction and contract creation behavior for different gas limits.

Source code in tests/shanghai/eip3860_initcode/test_initcode.py
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
def test_gas_usage(
    self,
    state_test: StateTestFiller,
    env: Environment,
    pre: Alloc,
    post: Alloc,
    tx: Transaction,
    gas_test_case: str,
    initcode: Initcode,
    exact_intrinsic_gas: int,
    exact_execution_gas: int,
):
    """
    Test transaction and contract creation behavior for different gas
    limits.
    """
    if (gas_test_case == "too_little_execution_gas") and (
        exact_execution_gas == exact_intrinsic_gas
    ):
        pytest.skip(
            "Special case, the execution of the initcode and gas "
            "cost to deploy are zero: Then this test case is "
            "equivalent to that of 'test_exact_intrinsic_gas'."
        )

    state_test(
        env=env,
        pre=pre,
        post=post,
        tx=tx,
    )

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Skipped Parameters

For more concise readability, the table below does not list the following parameter values: fork, blockchain_test, state_test, state_test_only, eof_test, eof_state_test.

Test ID gas_test_case initcode
too_little_intrinsic_gas-max_size_zeros too_little_intrinsic_gas max_size_zeros
too_little_intrinsic_gas-max_size_ones too_little_intrinsic_gas max_size_ones
too_little_intrinsic_gas-empty too_little_intrinsic_gas empty
too_little_intrinsic_gas-single_byte too_little_intrinsic_gas single_byte
too_little_intrinsic_gas-32_bytes too_little_intrinsic_gas 32_bytes
too_little_intrinsic_gas-33_bytes too_little_intrinsic_gas 33_bytes
too_little_intrinsic_gas-49120_bytes too_little_intrinsic_gas 49120_bytes
too_little_intrinsic_gas-49121_bytes too_little_intrinsic_gas 49121_bytes
exact_intrinsic_gas-max_size_zeros exact_intrinsic_gas max_size_zeros
exact_intrinsic_gas-max_size_ones exact_intrinsic_gas max_size_ones
exact_intrinsic_gas-empty exact_intrinsic_gas empty
exact_intrinsic_gas-single_byte exact_intrinsic_gas single_byte
exact_intrinsic_gas-32_bytes exact_intrinsic_gas 32_bytes
exact_intrinsic_gas-33_bytes exact_intrinsic_gas 33_bytes
exact_intrinsic_gas-49120_bytes exact_intrinsic_gas 49120_bytes
exact_intrinsic_gas-49121_bytes exact_intrinsic_gas 49121_bytes
too_little_execution_gas-max_size_zeros too_little_execution_gas max_size_zeros
too_little_execution_gas-max_size_ones too_little_execution_gas max_size_ones
too_little_execution_gas-empty too_little_execution_gas empty
too_little_execution_gas-single_byte too_little_execution_gas single_byte
too_little_execution_gas-32_bytes too_little_execution_gas 32_bytes
too_little_execution_gas-33_bytes too_little_execution_gas 33_bytes
too_little_execution_gas-49120_bytes too_little_execution_gas 49120_bytes
too_little_execution_gas-49121_bytes too_little_execution_gas 49121_bytes
exact_execution_gas-max_size_zeros exact_execution_gas max_size_zeros
exact_execution_gas-max_size_ones exact_execution_gas max_size_ones
exact_execution_gas-empty exact_execution_gas empty
exact_execution_gas-single_byte exact_execution_gas single_byte
exact_execution_gas-32_bytes exact_execution_gas 32_bytes
exact_execution_gas-33_bytes exact_execution_gas 33_bytes
exact_execution_gas-49120_bytes exact_execution_gas 49120_bytes
exact_execution_gas-49121_bytes exact_execution_gas 49121_bytes
too_little_intrinsic_gas-max_size_zeros too_little_intrinsic_gas max_size_zeros
too_little_intrinsic_gas-max_size_ones too_little_intrinsic_gas max_size_ones
too_little_intrinsic_gas-empty too_little_intrinsic_gas empty
too_little_intrinsic_gas-single_byte too_little_intrinsic_gas single_byte
too_little_intrinsic_gas-32_bytes too_little_intrinsic_gas 32_bytes
too_little_intrinsic_gas-33_bytes too_little_intrinsic_gas 33_bytes
too_little_intrinsic_gas-49120_bytes too_little_intrinsic_gas 49120_bytes
too_little_intrinsic_gas-49121_bytes too_little_intrinsic_gas 49121_bytes
exact_intrinsic_gas-max_size_zeros exact_intrinsic_gas max_size_zeros
exact_intrinsic_gas-max_size_ones exact_intrinsic_gas max_size_ones
exact_intrinsic_gas-empty exact_intrinsic_gas empty
exact_intrinsic_gas-single_byte exact_intrinsic_gas single_byte
exact_intrinsic_gas-32_bytes exact_intrinsic_gas 32_bytes
exact_intrinsic_gas-33_bytes exact_intrinsic_gas 33_bytes
exact_intrinsic_gas-49120_bytes exact_intrinsic_gas 49120_bytes
exact_intrinsic_gas-49121_bytes exact_intrinsic_gas 49121_bytes
too_little_execution_gas-max_size_zeros too_little_execution_gas max_size_zeros
too_little_execution_gas-max_size_ones too_little_execution_gas max_size_ones
too_little_execution_gas-empty too_little_execution_gas empty
too_little_execution_gas-single_byte too_little_execution_gas single_byte
too_little_execution_gas-32_bytes too_little_execution_gas 32_bytes
too_little_execution_gas-33_bytes too_little_execution_gas 33_bytes
too_little_execution_gas-49120_bytes too_little_execution_gas 49120_bytes
too_little_execution_gas-49121_bytes too_little_execution_gas 49121_bytes
exact_execution_gas-max_size_zeros exact_execution_gas max_size_zeros
exact_execution_gas-max_size_ones exact_execution_gas max_size_ones
exact_execution_gas-empty exact_execution_gas empty
exact_execution_gas-single_byte exact_execution_gas single_byte
exact_execution_gas-32_bytes exact_execution_gas 32_bytes
exact_execution_gas-33_bytes exact_execution_gas 33_bytes
exact_execution_gas-49120_bytes exact_execution_gas 49120_bytes
exact_execution_gas-49121_bytes exact_execution_gas 49121_bytes