Skip to content

test_value_contract_creation_tx()

Documentation for tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_transactions.py::test_value_contract_creation_tx@a9abd46e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_transactions.py::test_value_contract_creation_tx --fork Amsterdam

Test value moving contract creation transactions.

When the init code succeeds, the contract is deployed with the transferred value and the receipt's gas_used equals the intrinsic plus the execution gas.

When the init code reverts, the deploy is rolled back: no code is set, the value transfer is reversed, and the intrinsic NEW_ACCOUNT state-gas charge is refilled to the reservoir. Under the default zero state-gas reservoir, the refill cancels the spilled-to-regular portion of the intrinsic exactly, so the sender pays only the regular portion of the intrinsic plus the few EVM gas units spent before the revert.

Source code in tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_value_moving_transactions.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
@pytest.mark.parametrize(
    "value",
    [
        pytest.param(0, id="zero_value"),
        pytest.param(1, id="non-zero_value"),
    ],
)
@pytest.mark.parametrize(
    "tx_reverts",
    [
        pytest.param(False, id="success"),
        pytest.param(True, id="init_reverts"),
    ],
)
def test_value_contract_creation_tx(
    fork: Fork,
    pre: Alloc,
    state_test: StateTestFiller,
    tx_reverts: bool,
    value: int,
) -> None:
    """
    Test value moving contract creation transactions.

    When the init code succeeds, the contract is deployed with the
    transferred value and the receipt's ``gas_used`` equals the
    intrinsic plus the execution gas.

    When the init code reverts, the deploy is rolled back: no code is
    set, the value transfer is reversed, and the intrinsic
    ``NEW_ACCOUNT`` state-gas charge is refilled to the reservoir.
    Under the default zero state-gas reservoir, the refill cancels
    the spilled-to-regular portion of the intrinsic exactly, so the
    sender pays only the regular portion of the intrinsic plus the
    few EVM gas units spent before the revert.
    """
    sender_initial_balance = 10**18
    sender = pre.fund_eoa(sender_initial_balance)

    code_to_deploy = Op.STOP
    if tx_reverts:
        # ``PUSH1 0 PUSH1 0 REVERT`` -- aborts immediately, so no
        # code is deployed.
        call_data = Op.REVERT(0, 0)
    else:
        call_data = Initcode(deploy_code=code_to_deploy)
    execution_gas = call_data.gas_cost(fork)

    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        calldata=call_data,
        contract_creation=True,
        sends_value=bool(value),
        return_cost_deducted_prior_execution=True,
    )

    if tx_reverts:
        # The ``NEW_ACCOUNT`` state portion of the intrinsic is
        # refilled to the reservoir on revert, so it does not appear
        # on the receipt.
        new_account_refund = fork.transaction_intrinsic_state_gas(
            contract_creation=True,
        )
        gas_used = intrinsic_gas + execution_gas - new_account_refund
        # A tiny init code can leave the decomposed calldata floor above
        # the regular gas actually consumed; gas_used then pins to the
        # floor, which EIP-2780 anchors on the create intrinsic base.
        gas_used = max(
            gas_used,
            fork.transaction_data_floor_cost_calculator()(
                data=call_data,
                contract_creation=True,
                sends_value=bool(value),
            ),
        )
        # Value transfer rolled back.
        sender_value_delta = 0
        expected_target = None
    else:
        gas_used = intrinsic_gas + execution_gas
        sender_value_delta = value
        expected_target = Account(code=code_to_deploy, balance=value)

    expected_target_address = compute_create_address(address=sender, nonce=0)

    if value > 0 and not tx_reverts:
        expected_logs = [transfer_log(sender, expected_target_address, value)]
    else:
        expected_logs = []

    gas_price = 1_000_000_000
    gas_limit = intrinsic_gas + execution_gas + 1000

    tx = Transaction(
        sender=sender,
        to=None,
        value=value,
        data=call_data,
        gas_limit=gas_limit,
        gas_price=gas_price,
        expected_receipt=TransactionReceipt(logs=expected_logs),
    )

    sender_final_balance = (
        sender_initial_balance - sender_value_delta - gas_used * gas_price
    )

    post = {
        sender: Account(nonce=1, balance=sender_final_balance),
        expected_target_address: expected_target,
    }

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.