Skip to content

test_nested_calls_reservoir_passing()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_nested_calls_reservoir_passing@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py::test_nested_calls_reservoir_passing --fork Amsterdam

Test reservoir passes through nested calls.

The reservoir is passed from A to B to C. C performs an SSTORE using the reservoir gas. After all calls return, A verifies success. C is handed only its execution cost, so one gas less halts it before the SSTORE and leaves the reservoir untouched.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_call.py
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
@pytest.mark.parametrize(
    "sufficient_gas",
    [
        pytest.param(False, id="insufficient_gas"),
        pytest.param(True, id="sufficient_gas"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_nested_calls_reservoir_passing(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    sufficient_gas: bool,
) -> None:
    """
    Test reservoir passes through nested calls.

    The reservoir is passed from A to B to C. C performs an SSTORE
    using the reservoir gas. After all calls return, A verifies
    success. C is handed only its execution cost, so one gas less
    halts it before the SSTORE and leaves the reservoir untouched.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    c_storage = Storage()
    c_code = Op.SSTORE(c_storage.store_next(1 if sufficient_gas else 0), 1)
    c = pre.deploy_contract(code=c_code)
    c_gas = c_code.execution_cost(fork)
    if not sufficient_gas:
        c_gas -= 1

    # Each hop forwards only execution gas; the reservoir rides along in
    # full, so C's SSTORE lands only if it reached the bottom frame. B
    # gets 64/63 of C's need because a frame may forward at most 63/64
    # of the gas it holds.
    b_code = Op.CALL(gas=c_gas, address=c)
    b = pre.deploy_contract(code=b_code)

    a_storage = Storage()
    call_slot = a_storage.store_next(1, "nested_call_succeeds")
    a_code = Op.SSTORE(
        call_slot,
        Op.CALL(address=b),
        # gas accounting
        original_value=1,
        current_value=1,
        new_value=1,
        key_warm=False,
    )
    a = pre.deploy_contract(code=a_code, storage={call_slot: 1})

    execution_gas = (
        fork.transaction_intrinsic_cost_calculator()()
        + a_code.execution_cost(fork)
        + b_code.execution_cost(fork)
        + c_gas
    )
    expected_gas_used = max(
        execution_gas, sstore_state_gas if sufficient_gas else 0
    )

    tx = Transaction(
        to=a,
        state_gas_reservoir=sstore_state_gas,
        sender=pre.fund_eoa(),
    )

    post = {
        a: Account(storage=a_storage),
        c: Account(storage=c_storage),
    }
    state_test(
        pre=pre,
        post=post,
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=expected_gas_used),
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.