Skip to content

test_eof_eofcreate_msg_depth()

Documentation for tests/osaka/eip7692_eof_v1/eip7620_eof_create/test_eofcreate_failures.py::test_eof_eofcreate_msg_depth@21fb11c8.

Generate fixtures for these test cases for Osaka with:

fill -v tests/osaka/eip7692_eof_v1/eip7620_eof_create/test_eofcreate_failures.py::test_eof_eofcreate_msg_depth --fork Osaka

Test EOFCREATE handles 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. NOTE: See tests/osaka/eip7692_eof_v1/eip7069_extcall/test_calls.py::test_eof_calls_msg_depth for more explanations and comments. Most notable deviation from that test is that here calls and EOFCREATEs alternate in order to reach the max depth. who_fails decides whether the failing depth 1024 will be on a call or on an EOFCREATE to happen.

Source code in tests/osaka/eip7692_eof_v1/eip7620_eof_create/test_eofcreate_failures.py
639
640
641
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
@pytest.mark.parametrize(
    "opcode",
    [
        Op.EXTCALL,
        Op.EXTDELEGATECALL,
    ],
)
@pytest.mark.parametrize(
    "who_fails",
    [magic_value_call, magic_value_create],
    ids=["call_fails", "create_fails"],
)
@pytest.mark.pre_alloc_modify
def test_eof_eofcreate_msg_depth(
    state_test: StateTestFiller,
    pre: Alloc,
    opcode: Op,
    who_fails: int,
):
    """
    Test EOFCREATE handles 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.
    NOTE: See `tests/osaka/eip7692_eof_v1/eip7069_extcall/test_calls.py::test_eof_calls_msg_depth`
          for more explanations and comments. Most notable deviation from that test is that here
          calls and `EOFCREATE`s alternate in order to reach the max depth. `who_fails` decides
          whether the failing depth 1024 will be on a call or on an `EOFCREATE` to happen.
    """
    # Not a precise gas_limit formula, but enough to exclude risk of gas causing the failure.
    gas_limit = int(20000000 * (64 / 63) ** 1024)
    env = Environment(gas_limit=gas_limit)
    sender = pre.fund_eoa()

    callee_address = Address(0x5000)

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

    callee_code = Container(
        sections=[
            Section.Code(
                Op.MSTORE(0, Op.ADD(Op.CALLDATALOAD(0), 1))
                + Op.MSTORE(96, magic_value_create)
                + Op.EOFCREATE[0](salt=Op.CALLDATALOAD(0), input_size=32)
                + Op.RETURNDATASIZE
                + Op.ISZERO
                + Op.RJUMPI[rjump_offset]
                + returndatacopy_block
                + deep_most_result_block
            ),
            Section.Container(
                Container.Code(
                    Op.MSTORE(0, Op.ADD(Op.CALLDATALOAD(0), 1))
                    + Op.MSTORE(96, magic_value_call)
                    + opcode(address=callee_address, args_size=32)
                    + Op.RETURNDATASIZE
                    + Op.ISZERO
                    + Op.RJUMPI[rjump_offset]
                    + returndatacopy_block
                    + deep_most_result_block
                )
            ),
        ]
    )

    pre.deploy_contract(callee_code, address=callee_address)

    calling_contract_address = pre.deploy_contract(
        Container.Code(
            Op.MSTORE(0, Op.CALLDATALOAD(0))
            + opcode(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_call_or_create, Op.RETURNDATALOAD(64))
            + Op.SSTORE(slot_code_worked, value_code_worked)
            + Op.STOP
        )
    )

    # Only bumps the msg call depth "register" and forwards to the `calling_contract_address`.
    # If it is used it makes the "failing" depth of 1024 to happen on EOFCREATE, instead of CALL.
    passthrough_address = pre.deploy_contract(
        Container.Code(
            Op.MSTORE(0, 1) + Op.EXTCALL(address=calling_contract_address, args_size=32) + Op.STOP
        )
    )

    tx = Transaction(
        sender=sender,
        to=calling_contract_address if who_fails == magic_value_call else passthrough_address,
        gas_limit=gas_limit,
        data="",
    )

    calling_storage = {
        slot_max_depth: 1024,
        slot_code_worked: value_code_worked,
        slot_call_result: EXTCALL_REVERT if who_fails == magic_value_call else EOFCREATE_FAILURE,
        slot_call_or_create: who_fails,
    }

    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) who_fails opcode
...fork_Osaka-state_test-call_fails-opcode_EXTCALL 51729 EXTCALL
...fork_Osaka-state_test-call_fails-opcode_EXTDELEGATECALL 51729 EXTDELEGATECALL
...fork_Osaka-state_test-create_fails-opcode_EXTCALL 3423791742 EXTCALL
...fork_Osaka-state_test-create_fails-opcode_EXTDELEGATECALL 3423791742 EXTDELEGATECALL
...fork_Osaka-blockchain_test_from_state_test-call_fails-opcode_EXTCALL 51729 EXTCALL
...fork_Osaka-blockchain_test_from_state_test-call_fails-opcode_EXTDELEGATECALL 51729 EXTDELEGATECALL
...fork_Osaka-blockchain_test_from_state_test-create_fails-opcode_EXTCALL 3423791742 EXTCALL
...fork_Osaka-blockchain_test_from_state_test-create_fails-opcode_EXTDELEGATECALL 3423791742 EXTDELEGATECALL