Skip to content

Test Tstorage Create Contexts

Documentation for tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py.

Generate fixtures for these test cases for Cancun with:

Cancun only:

fill -v tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py --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 --until=Cancun --evm-bin=/path/to/evm-tool-dev-version

abstract: Tests for EIP-1153: Transient Storage

Test cases for `TSTORE` and `TLOAD` opcode calls in contract initcode.

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.
Source code in tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
@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: bytes, constructor_code: bytes
    ) -> Optional[bytes]:
        initcode = Initcode(deploy_code=deploy_code, initcode_prefix=constructor_code).bytecode
        return initcode

    @pytest.fixture()
    def creator_contract_code(  # noqa: D102
        self,
        opcode: Op,
        create2_salt: int,
        created_contract_address: str,
    ) -> bytes:
        if opcode == Op.CREATE:
            create_call = Op.CREATE(0, 0, Op.CALLDATASIZE)
        elif opcode == Op.CREATE2:
            create_call = Op.CREATE2(0, 0, Op.CALLDATASIZE, create2_salt)
        else:
            raise Exception("Invalid opcode specified for test.")
        contract_call = Op.SSTORE(
            4, Op.CALL(Op.GAS(), Op.PUSH20(created_contract_address), 0, 0, 0, 0, 0)
        )
        return (
            Op.TSTORE(0, 0x0100)
            + Op.TSTORE(1, 0x0200)
            + Op.TSTORE(2, 0x0300)
            + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
            + create_call
            + contract_call
            # 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 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, opcode: Op, create2_salt: int, initcode: bytes
    ) -> str:
        if opcode == Op.CREATE:
            return compute_create_address(address=creator_address, nonce=1)
        if opcode == Op.CREATE2:
            return compute_create2_address(
                address=creator_address, salt=create2_salt, initcode=initcode
            )
        raise Exception("invalid opcode for generator")

    def test_contract_creation(
        self,
        state_test: StateTestFiller,
        creator_contract_code: str,
        created_contract_address: str,
        initcode: bytes,
        deploy_code: bytes,
        expected_creator_storage: dict,
        expected_storage: dict,
    ) -> None:
        """
        Test transient storage in contract creation contexts.
        """
        pre = {
            TestAddress: Account(balance=100_000_000_000_000),
            creator_address: Account(
                code=creator_contract_code,
                nonce=1,
            ),
        }

        tx = Transaction(
            nonce=0,
            to=creator_address,
            data=initcode,
            gas_limit=1_000_000_000_000,
            gas_price=10,
        )

        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,
            txs=[tx],
        )

test_contract_creation(state_test, creator_contract_code, 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
def test_contract_creation(
    self,
    state_test: StateTestFiller,
    creator_contract_code: str,
    created_contract_address: str,
    initcode: bytes,
    deploy_code: bytes,
    expected_creator_storage: dict,
    expected_storage: dict,
) -> None:
    """
    Test transient storage in contract creation contexts.
    """
    pre = {
        TestAddress: Account(balance=100_000_000_000_000),
        creator_address: Account(
            code=creator_contract_code,
            nonce=1,
        ),
    }

    tx = Transaction(
        nonce=0,
        to=creator_address,
        data=initcode,
        gas_limit=1_000_000_000_000,
        gas_price=10,
    )

    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,
        txs=[tx],
    )