Skip to content

test_eofcreate_context()

Documentation for tests/osaka/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py::test_eofcreate_context@b48d1dc8.

Generate fixtures for these test cases for Osaka with:

fill -v tests/osaka/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py::test_eofcreate_context --fork Osaka

Test EOFCREATE's initcode context instructions.

Source code in tests/osaka/eip7692_eof_v1/eip7620_eof_create/test_eofcreate.py
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
@pytest.mark.parametrize(
    ["destination_code", "expected_result"],
    [
        pytest.param(Op.ADDRESS, "destination"),
        pytest.param(Op.CALLER, "caller"),
        pytest.param(Op.CALLVALUE, "eofcreate_value"),
        pytest.param(Op.ORIGIN, "sender"),
        pytest.param(Op.SELFBALANCE, "selfbalance"),
        pytest.param(Op.BALANCE(Op.CALLER), "factorybalance"),
    ],
)
def test_eofcreate_context(
    state_test: StateTestFiller,
    pre: Alloc,
    destination_code: Bytecode,
    expected_result: str,
):
    """Test EOFCREATE's initcode context instructions."""
    env = Environment()
    sender = pre.fund_eoa()
    value = 0x1123
    eofcreate_value = 0x13

    initcode = Container(
        sections=[
            Section.Code(Op.SSTORE(slot_call_result, destination_code) + Op.RETURNCODE[0](0, 0)),
            Section.Container(smallest_runtime_subcontainer),
        ]
    )

    factory_contract = Container(
        sections=[
            Section.Code(
                Op.SSTORE(slot_code_worked, value_code_worked)
                + Op.EOFCREATE[0](value=eofcreate_value)
                + Op.STOP
            ),
            Section.Container(initcode),
        ]
    )
    factory_address = pre.deploy_contract(factory_contract)

    destination_contract_address = compute_eofcreate_address(factory_address, 0)

    tx = Transaction(sender=sender, to=factory_address, gas_limit=200_000, value=value)

    expected_bytes: Address | int
    if expected_result == "destination":
        expected_bytes = destination_contract_address
    elif expected_result == "caller":
        expected_bytes = factory_address
    elif expected_result == "sender":
        expected_bytes = sender
    elif expected_result == "eofcreate_value":
        expected_bytes = eofcreate_value
    elif expected_result == "selfbalance":
        expected_bytes = eofcreate_value
    elif expected_result == "factorybalance":
        # Factory receives value from sender and passes on eofcreate_value as endowment.
        expected_bytes = value - eofcreate_value
    else:
        raise TypeError("Unexpected expected_result", expected_result)

    calling_storage = {
        slot_code_worked: value_code_worked,
    }
    destination_contract_storage = {
        slot_call_result: expected_bytes,
    }

    post = {
        factory_address: Account(storage=calling_storage, balance=value - eofcreate_value),
        destination_contract_address: Account(
            storage=destination_contract_storage, balance=eofcreate_value
        ),
    }

    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) destination_code expected_result
...fork_Osaka-state_test-destination_code_ADDRESS-expected_result_destination ADDRESS destination
...fork_Osaka-state_test-destination_code_CALLER-expected_result_caller CALLER caller
...fork_Osaka-state_test-destination_code_CALLVALUE-expected_result_eofcreate_value CALLVALUE eofcreate_value
...fork_Osaka-state_test-destination_code_ORIGIN-expected_result_sender ORIGIN sender
...fork_Osaka-state_test-destination_code_SELFBALANCE-expected_result_selfbalance SELFBALANCE selfbalance
...fork_Osaka-state_test-destination_code_-expected_result_factorybalance factorybalance
...fork_Osaka-blockchain_test_from_state_test-destination_code_ADDRESS-expected_result_destination ADDRESS destination
...fork_Osaka-blockchain_test_from_state_test-destination_code_CALLER-expected_result_caller CALLER caller
...fork_Osaka-blockchain_test_from_state_test-destination_code_CALLVALUE-expected_result_eofcreate_value CALLVALUE eofcreate_value
...fork_Osaka-blockchain_test_from_state_test-destination_code_ORIGIN-expected_result_sender ORIGIN sender
...fork_Osaka-blockchain_test_from_state_test-destination_code_SELFBALANCE-expected_result_selfbalance SELFBALANCE selfbalance
...fork_Osaka-blockchain_test_from_state_test-destination_code_-expected_result_factorybalance factorybalance