Skip to content

test_eof_calls_msg_depth()

Documentation for tests/osaka/eip7692_eof_v1/eip7069_extcall/test_calls.py::test_eof_calls_msg_depth@83970623.

Generate fixtures for these test cases for Osaka with:

fill -v tests/osaka/eip7692_eof_v1/eip7069_extcall/test_calls.py::test_eof_calls_msg_depth --fork Osaka

Test EOF contracts calls handle msg depth limit correctly (1024). NOTE: due to block gas limit and the 63/64th rule this limit is unlikely to be hit on mainnet.

Source code in tests/osaka/eip7692_eof_v1/eip7069_extcall/test_calls.py
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
@pytest.mark.parametrize(
    "opcode",
    [
        Op.EXTCALL,
        Op.EXTDELEGATECALL,
        Op.EXTSTATICCALL,
    ],
)
def test_eof_calls_msg_depth(
    state_test: StateTestFiller,
    pre: Alloc,
    sender: EOA,
    opcode: Op,
):
    """
    Test EOF contracts calls handle msg depth limit correctly (1024).
    NOTE: due to block gas limit and the 63/64th rule this limit is unlikely to be hit
          on mainnet.
    """
    # Not a precise gas_limit formula, but enough to exclude risk of gas causing the failure.
    gas_limit = int(200000 * (64 / 63) ** 1024)
    env = Environment(gas_limit=gas_limit)

    # Flow of the test:
    # `callee_code` is recursively calling itself, passing msg depth as calldata
    # (kept with the `MSTORE(0, ADD(...))`). When maximum msg depth is reached
    # the call fails and starts returning. The deep-most frame returns:
    #   - current reached msg depth (expected to be the maximum 1024), with the
    #     `MSTORE(32, ADD(...))`
    #   - the respective return code of the EXT*CALL (expected to be 1 - light failure), with the
    #     `MSTORE(64, NOOP)`. Note the `NOOP` is just to appease the `Op.MSTORE` call, the return
    #     code value is actually coming from the `Op.DUP1`
    # When unwinding the msg call stack, the intermediate frames return whatever the deeper callee
    # returned with the `RETURNDATACOPY` instruction.

    # Memory offsets layout:
    # - 0  - input - msg depth
    # - 32 - output - msg depth
    # - 64 - output - call result
    returndatacopy_block = Op.RETURNDATACOPY(32, 0, 64) + Op.RETURN(32, 64)
    deep_most_result_block = (
        Op.MSTORE(32, Op.ADD(Op.CALLDATALOAD(0), 1)) + Op.MSTORE(64, Op.NOOP) + Op.RETURN(32, 64)
    )
    rjump_offset = len(returndatacopy_block)

    callee_code = Container.Code(
        # current stack depth in memory bytes 0-31
        Op.MSTORE(0, Op.ADD(Op.CALLDATALOAD(0), 1))
        # pass it along deeper as calldata
        + opcode(address=Op.ADDRESS, args_size=32)
        # duplicate return code for the `returndatacopy_block` below
        + Op.DUP1
        # if return code was:
        #  - 1, we're in the deep-most frame, `deep_most_result_block` returns the actual result
        #  - 0, we're in an intermediate frame, `returndatacopy_block` only passes on the result
        + Op.RJUMPI[rjump_offset]
        + returndatacopy_block
        + deep_most_result_block
    )

    callee_address = pre.deploy_contract(callee_code)

    calling_contract_address = pre.deploy_contract(
        Container.Code(
            Op.MSTORE(0, Op.CALLDATALOAD(0))
            + Op.EXTCALL(address=callee_address, args_size=32)
            + Op.SSTORE(slot_max_depth, Op.RETURNDATALOAD(0))
            + Op.SSTORE(slot_call_result, Op.RETURNDATALOAD(32))
            + Op.SSTORE(slot_code_worked, value_code_worked)
            + Op.STOP
        )
    )
    tx = Transaction(
        sender=sender,
        to=Address(calling_contract_address),
        gas_limit=gas_limit,
    )

    calling_storage = {
        slot_max_depth: 1024,
        slot_code_worked: value_code_worked,
        slot_call_result: EXTCALL_REVERT,
    }

    post = {
        calling_contract_address: Account(storage=calling_storage),
    }

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

Parametrized Test Cases

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

Test ID (Abbreviated) opcode
...fork_Osaka-blockchain_test-opcode_EXTCALL EXTCALL
...fork_Osaka-blockchain_test-opcode_EXTDELEGATECALL EXTDELEGATECALL
...fork_Osaka-blockchain_test-opcode_EXTSTATICCALL EXTSTATICCALL
...fork_Osaka-state_test-opcode_EXTCALL EXTCALL
...fork_Osaka-state_test-opcode_EXTDELEGATECALL EXTDELEGATECALL
...fork_Osaka-state_test-opcode_EXTSTATICCALL EXTSTATICCALL