Skip to content

test_scenarios()

Documentation for tests/frontier/scenarios/test_scenarios.py::test_scenarios@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/frontier/scenarios/test_scenarios.py::test_scenarios --fork Amsterdam

Test a given operation in different scenarios. Verify that its return value is equal to the expected result for every scenario that is valid for the given fork.

Note: Don't use pytest parametrize for scenario production, because scenarios will be complex. Generate one test file for [each operation] * [each scenario] to save space. As well as operations will be complex too.

Source code in tests/frontier/scenarios/test_scenarios.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/Templates/DiffPlaces/templateGen.js",
        "https://github.com/ethereum/tests/blob/v13.3/src/Templates/DiffPlaces/createDiffPlacesTests.sh",
        "https://github.com/ethereum/tests/blob/v13.3/src/Templates/DiffPlaces/createBadOpcodeTest.sh",
        "https://github.com/ethereum/tests/blob/v13.3/src/Templates/DiffPlaces/createAllBadOpcodeTests.sh",
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stSelfBalance/diffPlacesFiller.yml",
    ],
    pr=["https://github.com/ethereum/execution-spec-tests/pull/808"],
    coverage_missed_reason=(
        "Original test pre-sets storage of some of the deployed accounts."
    ),
)
@pytest.mark.valid_from("Frontier")
@pytest.mark.parametrize(
    # select program to debug ("program_id","scenario_name")
    # program=""
    # select all programs scenario_name=""
    # select all scenarios
    #
    # Example:
    # [ScenarioDebug(program_id=ProgramSstoreSload().id,
    # scenario_name="scenario_CALL_CALL")]
    "debug",
    [
        ScenarioDebug(
            program_id="",
            scenario_name="",
        )
    ],
    ids=["debug"],
)
@pytest.mark.parametrize(
    "test_program",
    [
        ProgramSstoreSload(),
        ProgramTstoreTload(),
        ProgramLogs(),
        ProgramSuicide(),
        ProgramInvalidOpcode(),
        ProgramAddress(),
        ProgramBalance(),
        ProgramOrigin(),
        ProgramCaller(),
        ProgramCallValue(),
        ProgramCallDataLoad(),
        ProgramCallDataSize(),
        ProgramCallDataCopy(),
        ProgramCodeCopyCodeSize(),
        ProgramGasPrice(),
        ProgramExtCodeCopyExtCodeSize(),
        ProgramReturnDataSize(),
        ProgramReturnDataCopy(),
        ProgramExtCodehash(),
        ProgramBlockhash(),
        ProgramCoinbase(),
        ProgramTimestamp(),
        ProgramNumber(),
        ProgramDifficultyRandao(),
        ProgramGasLimit(),
        ProgramChainid(),
        ProgramSelfbalance(),
        ProgramBasefee(),
        ProgramBlobhash(),
        ProgramBlobBaseFee(),
        ProgramTload(),
        ProgramMcopy(),
        ProgramPush0(),
        ProgramAllFrontierOpcodes(),
    ],
)
@pytest.mark.slow()
def test_scenarios(
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    pre: Alloc,
    debug: ScenarioDebug,
    test_program: ScenarioTestProgram,
    scenarios: List[Scenario],
) -> None:
    """
    Test a given operation in different scenarios. Verify that its return value
    is equal to the expected result for every scenario that is valid for the
    given fork.

    Note: Don't use pytest parametrize for scenario production, because
    scenarios will be complex. Generate one test file for [each operation] *
    [each scenario] to save space. As well as operations will be complex too.
    """
    tx_env = Environment()
    tx_origin: Address = pre.fund_eoa()

    tests: int = 0
    blocks: List[Block] = []
    post: dict = {}
    for scenario in scenarios:
        if debug.scenario_name and scenario.name != debug.scenario_name:
            continue

        if debug.program_id:
            if test_program.id != debug.program_id:
                continue
        tests = tests + 1

        post_storage = Storage()
        result_slot = post_storage.store_next(
            1, hint=f"runner result {scenario.name}"
        )

        tx_max_gas = 1_000_000
        if test_program.id == ProgramInvalidOpcode().id:
            tx_max_gas = 10_000_000 if fork.is_eip_enabled(8037) else 7_000_000
        if scenario.category == "double_call_combinations":
            tx_max_gas *= 2

        tx_gasprice: int = 10
        exec_env = ExecutionEnvironment(
            fork=fork,
            origin=tx_origin,
            gasprice=tx_gasprice,
            timestamp=tx_env.timestamp,  # we can't know timestamp before head,
            # use gas hash
            number=len(blocks) + 1,
            gaslimit=tx_env.gas_limit,
            coinbase=tx_env.fee_recipient,
        )

        def make_result(
            scenario: Scenario, exec_env: ExecutionEnvironment, post: Storage
        ) -> int:
            """Make Scenario post result."""
            if scenario.halts:
                return post.store_next(0, hint=scenario.name)
            else:
                return post.store_next(
                    test_program.result().translate_result(
                        scenario.env, exec_env
                    ),
                    hint=scenario.name,
                )

        runner_contract = pre.deploy_contract(
            code=Op.MSTORE(0, 0)
            + Op.CALL(tx_max_gas, scenario.code, 0, 0, 0, 0, 32)
            + Op.SSTORE(
                make_result(scenario, exec_env, post_storage), Op.MLOAD(0)
            )
            + Op.SSTORE(result_slot, 1),
            storage={
                result_slot: 0xFFFF,
            },
        )

        tx = Transaction(
            sender=tx_origin,
            gas_limit=tx_max_gas + 100_000,
            gas_price=tx_gasprice,
            to=runner_contract,
            data=bytes.fromhex("11223344"),
            value=0,
            protected=False,
        )

        post[runner_contract] = Account(storage=post_storage)
        blocks.append(Block(txs=[tx], expected_post_state=post))

    if tests > 0:
        blockchain_test(
            pre=pre,
            blocks=blocks,
            post=post,
        )

Parametrized Test Cases

This test generates 34 parametrized test cases across 14 forks.