Skip to content

test_contract_creation()

Documentation for tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py::TestTransientStorageInContractCreation::test_contract_creation@verkle@v0.0.6.

Generate fixtures for these test cases for Cancun with:

Cancun only:

fill -v tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py::TestTransientStorageInContractCreation::test_contract_creation --fork=Cancun --evm-bin=/path/to/evm-tool-dev-version

For all forks up to and including Cancun:

fill -v tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py::TestTransientStorageInContractCreation::test_contract_creation --until=Cancun

Test transient storage in contract creation contexts:

  • TSTORE/TLOAD in initcode should not be able to access the creator's transient storage.
  • TSTORE/TLOAD in initcode should be able to access the created contract's transient storage.
  • TSTORE/TLOAD in creator contract should be able to use its own transient storage.
Source code in tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
@CreateOpcodeParams.parametrize()
@InitcodeTestCases.parametrize()
class TestTransientStorageInContractCreation:
    """
    Test transient storage in contract creation contexts:

    - TSTORE/TLOAD in initcode should not be able to access the creator's transient storage.
    - TSTORE/TLOAD in initcode should be able to access the created contract's transient
        storage.
    - TSTORE/TLOAD in creator contract should be able to use its own transient storage.
    """

    @pytest.fixture()
    def create2_salt(self) -> int:  # noqa: D102
        return 0xDEADBEEF

    @pytest.fixture()
    def initcode(  # noqa: D102
        self,
        deploy_code: Bytecode,
        constructor_code: Bytecode,
    ) -> Initcode:
        return Initcode(deploy_code=deploy_code, initcode_prefix=constructor_code)

    @pytest.fixture()
    def creator_contract_code(  # noqa: D102
        self,
        opcode: Op,
        create2_salt: int,
    ) -> Bytecode:
        return (
            Op.TSTORE(0, 0x0100)
            + Op.TSTORE(1, 0x0200)
            + Op.TSTORE(2, 0x0300)
            + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
            + Op.SSTORE(4, Op.CALL(address=opcode(size=Op.CALLDATASIZE, salt=create2_salt)))
            # Save the state of transient storage following call to storage; the transient
            # storage should not have been overwritten
            + Op.SSTORE(0, Op.TLOAD(0))
            + Op.SSTORE(1, Op.TLOAD(1))
            + Op.SSTORE(2, Op.TLOAD(2))
        )

    @pytest.fixture()
    def creator_address(self, pre: Alloc, creator_contract_code: Bytecode) -> Address:
        """The address that creates the contract with create/create2"""
        return pre.deploy_contract(creator_contract_code)

    @pytest.fixture()
    def expected_creator_storage(self) -> dict:  # noqa: D102
        return {0: 0x0100, 1: 0x0200, 2: 0x0300, 4: 0x0001}

    @pytest.fixture()
    def created_contract_address(  # noqa: D102
        self, creator_address: Address, opcode: Op, create2_salt: int, initcode: Initcode
    ) -> Address:
        return compute_create_address(
            address=creator_address,
            nonce=1,
            salt=create2_salt,
            initcode=initcode,
            opcode=opcode,
        )

    def test_contract_creation(
        self,
        state_test: StateTestFiller,
        pre: Alloc,
        creator_address: Address,
        created_contract_address: Address,
        initcode: Initcode,
        deploy_code: Bytecode,
        expected_creator_storage: dict,
        expected_storage: dict,
    ) -> None:
        """
        Test transient storage in contract creation contexts.
        """
        sender = pre.fund_eoa()

        tx = Transaction(
            sender=sender,
            to=creator_address,
            data=initcode,
            gas_limit=1_000_000,
        )

        post = {
            creator_address: Account(
                nonce=2,
                storage=expected_creator_storage,
            ),
            created_contract_address: Account(
                nonce=1,
                code=deploy_code,
                storage=expected_storage,
            ),
        }

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

test_contract_creation(state_test, pre, creator_address, created_contract_address, initcode, deploy_code, expected_creator_storage, expected_storage)

Test transient storage in contract creation contexts.

Source code in tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def test_contract_creation(
    self,
    state_test: StateTestFiller,
    pre: Alloc,
    creator_address: Address,
    created_contract_address: Address,
    initcode: Initcode,
    deploy_code: Bytecode,
    expected_creator_storage: dict,
    expected_storage: dict,
) -> None:
    """
    Test transient storage in contract creation contexts.
    """
    sender = pre.fund_eoa()

    tx = Transaction(
        sender=sender,
        to=creator_address,
        data=initcode,
        gas_limit=1_000_000,
    )

    post = {
        creator_address: Account(
            nonce=2,
            storage=expected_creator_storage,
        ),
        created_contract_address: Account(
            nonce=1,
            code=deploy_code,
            storage=expected_storage,
        ),
    }

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

Parametrized Test Cases

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

Skipped Parameters

For more concise readability, the table below does not list the following parameter values: fork, blockchain_test, state_test, state_test_only, eof_test, eof_state_test.

Test ID constructor_code deploy_code expected_storage opcode
only_constructor_code-create {0: 0, 1: 1} CREATE
only_constructor_code-create2 {0: 0, 1: 1} CREATE2
in_constructor_and_deployed_code-create {0: 0, 1: 0, 2: 1} CREATE
in_constructor_and_deployed_code-create2 {0: 0, 1: 0, 2: 1} CREATE2
across_constructor_and_deployed_code_v0-create {0: 0, 1: 1, 2: 1} CREATE
across_constructor_and_deployed_code_v0-create2 {0: 0, 1: 1, 2: 1} CREATE2
across_constructor_and_deployed_code_v1-create {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE
across_constructor_and_deployed_code_v1-create2 {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE2
no_constructor_code-create {0: 0, 1: 1} CREATE
no_constructor_code-create2 {0: 0, 1: 1} CREATE2
only_constructor_code-create {0: 0, 1: 1} CREATE
only_constructor_code-create2 {0: 0, 1: 1} CREATE2
in_constructor_and_deployed_code-create {0: 0, 1: 0, 2: 1} CREATE
in_constructor_and_deployed_code-create2 {0: 0, 1: 0, 2: 1} CREATE2
across_constructor_and_deployed_code_v0-create {0: 0, 1: 1, 2: 1} CREATE
across_constructor_and_deployed_code_v0-create2 {0: 0, 1: 1, 2: 1} CREATE2
across_constructor_and_deployed_code_v1-create {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE
across_constructor_and_deployed_code_v1-create2 {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE2
no_constructor_code-create {0: 0, 1: 1} CREATE
no_constructor_code-create2 {0: 0, 1: 1} CREATE2