Skip to content

test_delegatecall_no_additional_floor_cost()

Documentation for tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestNestedContractCalls::test_delegatecall_no_additional_floor_cost@c74f1a67.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py::TestNestedContractCalls::test_delegatecall_no_additional_floor_cost --fork Amsterdam

Test that nested contract calls don't trigger additional floor costs.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
class TestNestedContractCalls:
    """Test that nested contract calls don't trigger additional floor costs."""

    @pytest.fixture
    def sender(self, pre: Alloc) -> Address:
        """Create sender account."""
        return pre.fund_eoa()

    @pytest.fixture
    def contract_b(self, pre: Alloc) -> Address:
        """
        Deploy Contract B that receives calldata and stores something.

        This contract will be called by Contract A with calldata.
        """
        # Simply store a value to show it was executed
        code = Op.PUSH1(42) + Op.PUSH1(0) + Op.SSTORE + Op.STOP
        return pre.deploy_contract(code)

    @pytest.fixture
    def contract_a(self, pre: Alloc, contract_b: Address) -> Address:
        """
        Deploy Contract A that calls Contract B with calldata.

        This contract performs a CALL to contract_b with some calldata.
        """
        # Prepare call to contract_b with 100 bytes of data
        # CALL(gas, address, value, argsOffset, argsSize, retOffset, retSize)
        code = (
            # Store some data in memory to pass as calldata
            Op.PUSH1(100)  # Size
            + Op.PUSH1(0)  # Offset
            + Op.PUSH1(0xFF)  # Value to fill
            + Op.MSTORE
            +
            # Perform CALL
            Op.PUSH1(0)  # retSize
            + Op.PUSH1(0)  # retOffset
            + Op.PUSH1(100)  # argsSize (100 bytes)
            + Op.PUSH1(0)  # argsOffset
            + Op.PUSH1(0)  # value
            + Op.PUSH20(contract_b.hex())  # address
            + Op.GAS  # gas (all remaining)
            + Op.CALL
            + Op.STOP
        )
        return pre.deploy_contract(code)

    def test_nested_call_no_additional_floor_cost(
        self,
        state_test: StateTestFiller,
        pre: Alloc,
        sender: Address,
        contract_a: Address,
        contract_b: Address,
        fork: Fork,
    ) -> None:
        """
        Verify only the transaction's calldata affects floor cost.

        Internal CALL operations with calldata don't trigger additional
        floor costs.
        """
        # Transaction calldata (sent to contract_a)
        tx_calldata = Bytes(b"\x01" * 200)

        # Calculate floor cost based ONLY on transaction calldata
        floor_cost_calculator = fork.transaction_data_floor_cost_calculator()
        floor_cost = floor_cost_calculator(data=tx_calldata)

        intrinsic_cost_calculator = (
            fork.transaction_intrinsic_cost_calculator()
        )
        intrinsic_cost = intrinsic_cost_calculator(
            calldata=tx_calldata,
            contract_creation=False,
            access_list=None,
            authorization_list_or_count=None,
        )

        # The floor cost should only consider the transaction's calldata,
        # not the calldata passed in the internal CALL
        tokens_tx = len(tx_calldata) * 4  # All non-zero bytes
        gas_costs = fork.gas_costs()
        expected_floor_cost = gas_costs.TX_BASE + (
            tokens_tx * gas_costs.TX_DATA_TOKEN_FLOOR
        )
        # EIP-2780 anchors the floor on the decomposed intrinsic base;
        # the tx targets a contract, adding the recipient-access charge.
        expected_floor_cost += gas_costs.COLD_ACCOUNT_ACCESS
        assert floor_cost == expected_floor_cost

        tx = Transaction(
            sender=sender,
            to=contract_a,
            data=tx_calldata,
            gas_limit=intrinsic_cost + 500000,  # Add execution gas buffer
        )

        state_test(
            pre=pre,
            post={
                contract_b: {
                    "storage": {0: 42},  # Verify contract_b was executed
                }
            },
            tx=tx,
        )

    def test_delegatecall_no_additional_floor_cost(
        self,
        state_test: StateTestFiller,
        pre: Alloc,
        sender: Address,
        fork: Fork,
    ) -> None:
        """
        Verify DELEGATECALL operations don't trigger additional floor costs.
        """
        # Contract that will be delegatecalled
        delegate_code = Op.PUSH1(99) + Op.PUSH1(0) + Op.SSTORE + Op.STOP
        delegate_contract = pre.deploy_contract(delegate_code)

        # Contract that performs DELEGATECALL
        # DELEGATECALL(gas, address, argsOffset, argsSize, retOffset, retSize)
        caller_code = (
            Op.PUSH1(0)  # retSize
            + Op.PUSH1(0)  # retOffset
            + Op.PUSH1(64)  # argsSize
            + Op.PUSH1(0)  # argsOffset
            + Op.PUSH20(delegate_contract.hex())  # address
            + Op.GAS  # gas
            + Op.DELEGATECALL
            + Op.STOP
        )
        caller_contract = pre.deploy_contract(caller_code, storage={})

        # Transaction calldata
        tx_calldata = Bytes(b"\x01" * 150)

        intrinsic_cost_calculator = (
            fork.transaction_intrinsic_cost_calculator()
        )
        intrinsic_cost = intrinsic_cost_calculator(
            calldata=tx_calldata,
            contract_creation=False,
            access_list=None,
            authorization_list_or_count=None,
        )

        tx = Transaction(
            sender=sender,
            to=caller_contract,
            data=tx_calldata,
            gas_limit=intrinsic_cost + 500000,
        )

        state_test(
            pre=pre,
            post={
                caller_contract: {
                    "storage": {
                        0: 99
                    },  # DELEGATECALL executes in caller's context
                }
            },
            tx=tx,
        )

test_nested_call_no_additional_floor_cost(state_test, pre, sender, contract_a, contract_b, fork)

Verify only the transaction's calldata affects floor cost.

Internal CALL operations with calldata don't trigger additional floor costs.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
def test_nested_call_no_additional_floor_cost(
    self,
    state_test: StateTestFiller,
    pre: Alloc,
    sender: Address,
    contract_a: Address,
    contract_b: Address,
    fork: Fork,
) -> None:
    """
    Verify only the transaction's calldata affects floor cost.

    Internal CALL operations with calldata don't trigger additional
    floor costs.
    """
    # Transaction calldata (sent to contract_a)
    tx_calldata = Bytes(b"\x01" * 200)

    # Calculate floor cost based ONLY on transaction calldata
    floor_cost_calculator = fork.transaction_data_floor_cost_calculator()
    floor_cost = floor_cost_calculator(data=tx_calldata)

    intrinsic_cost_calculator = (
        fork.transaction_intrinsic_cost_calculator()
    )
    intrinsic_cost = intrinsic_cost_calculator(
        calldata=tx_calldata,
        contract_creation=False,
        access_list=None,
        authorization_list_or_count=None,
    )

    # The floor cost should only consider the transaction's calldata,
    # not the calldata passed in the internal CALL
    tokens_tx = len(tx_calldata) * 4  # All non-zero bytes
    gas_costs = fork.gas_costs()
    expected_floor_cost = gas_costs.TX_BASE + (
        tokens_tx * gas_costs.TX_DATA_TOKEN_FLOOR
    )
    # EIP-2780 anchors the floor on the decomposed intrinsic base;
    # the tx targets a contract, adding the recipient-access charge.
    expected_floor_cost += gas_costs.COLD_ACCOUNT_ACCESS
    assert floor_cost == expected_floor_cost

    tx = Transaction(
        sender=sender,
        to=contract_a,
        data=tx_calldata,
        gas_limit=intrinsic_cost + 500000,  # Add execution gas buffer
    )

    state_test(
        pre=pre,
        post={
            contract_b: {
                "storage": {0: 42},  # Verify contract_b was executed
            }
        },
        tx=tx,
    )

test_delegatecall_no_additional_floor_cost(state_test, pre, sender, fork)

Verify DELEGATECALL operations don't trigger additional floor costs.

Source code in tests/amsterdam/eip7976_increase_calldata_floor_cost/test_additional_coverage.py
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
def test_delegatecall_no_additional_floor_cost(
    self,
    state_test: StateTestFiller,
    pre: Alloc,
    sender: Address,
    fork: Fork,
) -> None:
    """
    Verify DELEGATECALL operations don't trigger additional floor costs.
    """
    # Contract that will be delegatecalled
    delegate_code = Op.PUSH1(99) + Op.PUSH1(0) + Op.SSTORE + Op.STOP
    delegate_contract = pre.deploy_contract(delegate_code)

    # Contract that performs DELEGATECALL
    # DELEGATECALL(gas, address, argsOffset, argsSize, retOffset, retSize)
    caller_code = (
        Op.PUSH1(0)  # retSize
        + Op.PUSH1(0)  # retOffset
        + Op.PUSH1(64)  # argsSize
        + Op.PUSH1(0)  # argsOffset
        + Op.PUSH20(delegate_contract.hex())  # address
        + Op.GAS  # gas
        + Op.DELEGATECALL
        + Op.STOP
    )
    caller_contract = pre.deploy_contract(caller_code, storage={})

    # Transaction calldata
    tx_calldata = Bytes(b"\x01" * 150)

    intrinsic_cost_calculator = (
        fork.transaction_intrinsic_cost_calculator()
    )
    intrinsic_cost = intrinsic_cost_calculator(
        calldata=tx_calldata,
        contract_creation=False,
        access_list=None,
        authorization_list_or_count=None,
    )

    tx = Transaction(
        sender=sender,
        to=caller_contract,
        data=tx_calldata,
        gas_limit=intrinsic_cost + 500000,
    )

    state_test(
        pre=pre,
        post={
            caller_contract: {
                "storage": {
                    0: 99
                },  # DELEGATECALL executes in caller's context
            }
        },
        tx=tx,
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.