@pytest.mark.parametrize(
"initcode_name,gas_test_case",
[
pytest.param(
i,
g,
marks=(
[pytest.mark.exception_test]
if g == "too_little_intrinsic_gas"
else []
),
)
for i in [
"max_size_zeros",
"max_size_ones",
"empty",
"single_byte",
"32_bytes",
"33_bytes",
"max_size_minus_word",
"max_size_minus_word_plus_byte",
]
for g in [
"too_little_intrinsic_gas",
"exact_intrinsic_gas",
"too_little_execution_gas",
"exact_execution_gas",
]
if valid_gas_test_case(i, g)
],
)
class TestContractCreationGasUsage:
"""
Test the gas cost behavior of a contract creating transaction.
The following scenarios are tested:
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 tx_access_list(self) -> List[AccessList]:
"""
Return an access list to raise the intrinsic gas cost.
Upon EIP-7623 activation, we need to use an access list to raise the
intrinsic gas cost to be above the floor data cost.
"""
return [
AccessList(address=Address(i), storage_keys=[])
for i in range(1, 642)
]
@pytest.fixture
def exact_intrinsic_gas(
self, fork: Fork, initcode: Initcode, tx_access_list: List[AccessList]
) -> int:
"""
Calculate the intrinsic tx gas cost.
"""
tx_intrinsic_gas_cost_calculator = (
fork.transaction_intrinsic_cost_calculator()
)
assert tx_intrinsic_gas_cost_calculator(
calldata=initcode,
contract_creation=True,
access_list=tx_access_list,
) == tx_intrinsic_gas_cost_calculator(
calldata=initcode,
contract_creation=True,
access_list=tx_access_list,
return_cost_deducted_prior_execution=True,
)
return tx_intrinsic_gas_cost_calculator(
calldata=initcode,
contract_creation=True,
access_list=tx_access_list,
)
@pytest.fixture
def exact_execution_gas(
self, fork: Fork, exact_intrinsic_gas: int, initcode: Initcode
) -> int:
"""
Calculate total execution gas cost.
"""
return exact_intrinsic_gas + initcode.gas_cost(fork)
@pytest.fixture
def tx_error(self, gas_test_case: str) -> TransactionException | None:
"""
Return the transaction exception, or None, as expected.
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_access_list: List[AccessList],
tx_error: TransactionException | None,
exact_intrinsic_gas: int,
exact_execution_gas: int,
) -> Transaction:
"""
Return a tx with `gas_limit` corresponding to the `gas_test_case`.
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(
to=None,
access_list=tx_access_list,
data=initcode,
gas_limit=gas_limit,
error=tx_error,
sender=sender,
# The entire gas limit is expected to be consumed.
expected_receipt=TransactionReceipt(cumulative_gas_used=gas_limit),
)
@pytest.fixture
def post(
self,
sender: EOA,
initcode: Initcode,
gas_test_case: str,
exact_intrinsic_gas: int,
exact_execution_gas: int,
) -> Alloc:
"""
Test 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})
# Gated off under EIP-8037: state gas breaks the single-dimension
# intrinsic-gas equivalence asserted in `exact_intrinsic_gas`. The
# 2D-aware creation-gas metering is covered on Amsterdam by
# `test_create_tx_intrinsic_gas_boundary` and
# `test_max_initcode_size_gas_metering_via_create` in
# `eip8037_state_creation_gas_cost_increase/test_state_gas_create.py`.
@pytest.mark.inclusion_test
@pytest.mark.valid_before("EIP8037")
@pytest.mark.slow()
def test_gas_usage(
self,
state_test: StateTestFiller,
pre: Alloc,
post: Alloc,
tx: Transaction,
) -> None:
"""
Test transaction and contract creation using different gas limits.
"""
state_test(pre=pre, post=post, tx=tx)