Skip to content

test_contract_creation()

Documentation for tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py::TestTransientStorageInContractCreation::test_contract_creation@21fb11c8.

Generate fixtures for these test cases for Prague with:

fill -v tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py::TestTransientStorageInContractCreation::test_contract_creation --fork Prague

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
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
220
221
222
223
224
225
@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:
        """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
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
220
221
222
223
224
225
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.

Test ID (Abbreviated) constructor_code deploy_code expected_storage opcode
...fork_Cancun-state_test-only_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Cancun-state_test-only_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Cancun-state_test-in_constructor_and_deployed_code-create {0: 0, 1: 0, 2: 1} CREATE
...fork_Cancun-state_test-in_constructor_and_deployed_code-create2 {0: 0, 1: 0, 2: 1} CREATE2
...fork_Cancun-state_test-across_constructor_and_deployed_code_v0-create {0: 0, 1: 1, 2: 1} CREATE
...fork_Cancun-state_test-across_constructor_and_deployed_code_v0-create2 {0: 0, 1: 1, 2: 1} CREATE2
...fork_Cancun-state_test-across_constructor_and_deployed_code_v1-create {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE
...fork_Cancun-state_test-across_constructor_and_deployed_code_v1-create2 {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE2
...fork_Cancun-state_test-no_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Cancun-state_test-no_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Cancun-blockchain_test_from_state_test-only_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Cancun-blockchain_test_from_state_test-only_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Cancun-blockchain_test_from_state_test-in_constructor_and_deployed_code-create {0: 0, 1: 0, 2: 1} CREATE
...fork_Cancun-blockchain_test_from_state_test-in_constructor_and_deployed_code-create2 {0: 0, 1: 0, 2: 1} CREATE2
...fork_Cancun-blockchain_test_from_state_test-across_constructor_and_deployed_code_v0-create {0: 0, 1: 1, 2: 1} CREATE
...fork_Cancun-blockchain_test_from_state_test-across_constructor_and_deployed_code_v0-create2 {0: 0, 1: 1, 2: 1} CREATE2
...fork_Cancun-blockchain_test_from_state_test-across_constructor_and_deployed_code_v1-create {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE
...fork_Cancun-blockchain_test_from_state_test-across_constructor_and_deployed_code_v1-create2 {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE2
...fork_Cancun-blockchain_test_from_state_test-no_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Cancun-blockchain_test_from_state_test-no_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Prague-state_test-only_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Prague-state_test-only_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Prague-state_test-in_constructor_and_deployed_code-create {0: 0, 1: 0, 2: 1} CREATE
...fork_Prague-state_test-in_constructor_and_deployed_code-create2 {0: 0, 1: 0, 2: 1} CREATE2
...fork_Prague-state_test-across_constructor_and_deployed_code_v0-create {0: 0, 1: 1, 2: 1} CREATE
...fork_Prague-state_test-across_constructor_and_deployed_code_v0-create2 {0: 0, 1: 1, 2: 1} CREATE2
...fork_Prague-state_test-across_constructor_and_deployed_code_v1-create {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE
...fork_Prague-state_test-across_constructor_and_deployed_code_v1-create2 {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE2
...fork_Prague-state_test-no_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Prague-state_test-no_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Prague-blockchain_test_from_state_test-only_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Prague-blockchain_test_from_state_test-only_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Prague-blockchain_test_from_state_test-in_constructor_and_deployed_code-create {0: 0, 1: 0, 2: 1} CREATE
...fork_Prague-blockchain_test_from_state_test-in_constructor_and_deployed_code-create2 {0: 0, 1: 0, 2: 1} CREATE2
...fork_Prague-blockchain_test_from_state_test-across_constructor_and_deployed_code_v0-create {0: 0, 1: 1, 2: 1} CREATE
...fork_Prague-blockchain_test_from_state_test-across_constructor_and_deployed_code_v0-create2 {0: 0, 1: 1, 2: 1} CREATE2
...fork_Prague-blockchain_test_from_state_test-across_constructor_and_deployed_code_v1-create {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE
...fork_Prague-blockchain_test_from_state_test-across_constructor_and_deployed_code_v1-create2 {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE2
...fork_Prague-blockchain_test_from_state_test-no_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Prague-blockchain_test_from_state_test-no_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Osaka-state_test-only_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Osaka-state_test-only_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Osaka-state_test-in_constructor_and_deployed_code-create {0: 0, 1: 0, 2: 1} CREATE
...fork_Osaka-state_test-in_constructor_and_deployed_code-create2 {0: 0, 1: 0, 2: 1} CREATE2
...fork_Osaka-state_test-across_constructor_and_deployed_code_v0-create {0: 0, 1: 1, 2: 1} CREATE
...fork_Osaka-state_test-across_constructor_and_deployed_code_v0-create2 {0: 0, 1: 1, 2: 1} CREATE2
...fork_Osaka-state_test-across_constructor_and_deployed_code_v1-create {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE
...fork_Osaka-state_test-across_constructor_and_deployed_code_v1-create2 {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE2
...fork_Osaka-state_test-no_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Osaka-state_test-no_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Osaka-blockchain_test_from_state_test-only_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Osaka-blockchain_test_from_state_test-only_constructor_code-create2 {0: 0, 1: 1} CREATE2
...fork_Osaka-blockchain_test_from_state_test-in_constructor_and_deployed_code-create {0: 0, 1: 0, 2: 1} CREATE
...fork_Osaka-blockchain_test_from_state_test-in_constructor_and_deployed_code-create2 {0: 0, 1: 0, 2: 1} CREATE2
...fork_Osaka-blockchain_test_from_state_test-across_constructor_and_deployed_code_v0-create {0: 0, 1: 1, 2: 1} CREATE
...fork_Osaka-blockchain_test_from_state_test-across_constructor_and_deployed_code_v0-create2 {0: 0, 1: 1, 2: 1} CREATE2
...fork_Osaka-blockchain_test_from_state_test-across_constructor_and_deployed_code_v1-create {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE
...fork_Osaka-blockchain_test_from_state_test-across_constructor_and_deployed_code_v1-create2 {0: 0, 1: 1, 2: 0, 3: 1, 4: 1} CREATE2
...fork_Osaka-blockchain_test_from_state_test-no_constructor_code-create {0: 0, 1: 1} CREATE
...fork_Osaka-blockchain_test_from_state_test-no_constructor_code-create2 {0: 0, 1: 1} CREATE2