Skip to content

Test Calldata

Documentation for tests/prague/eip7692_eof_v1/eip7069_extcall/test_calldata.py.

Generate fixtures for these test cases for Prague with:

Prague only:

fill -v tests/prague/eip7692_eof_v1/eip7069_extcall/test_calldata.py --fork=Prague --evm-bin=/path/to/evm-tool-dev-version
For all forks up to and including Prague:
fill -v tests/prague/eip7692_eof_v1/eip7069_extcall/test_calldata.py --until=Prague --evm-bin=/path/to/evm-tool-dev-version

Tests EIP-7069: Revamped CALL instructions

Tests for the RETURNDATALOAD instriction

test_extcalls_inputdata(state_test, pre, value, memory, offset, length)

Tests call data into EXT*CALL including multiple offset conditions.

Caller pushes data into memory, then calls the target. Target writes 64 bytes of call data to storage and a success byte.

Source code in tests/prague/eip7692_eof_v1/eip7069_extcall/test_calldata.py
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
@pytest.mark.parametrize("value", [0, 1])
@pytest.mark.parametrize(
    "memory",
    [
        b"",
        b"1234567890abcdef",
        b"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=-",
        b"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=-" * 4,
    ],
    ids=lambda x: "size_%d" % len(x),
)
@pytest.mark.parametrize("offset", [0, 8, 24, 80])
@pytest.mark.parametrize("length", [0, 8, 32, 48])
def test_extcalls_inputdata(
    state_test: StateTestFiller,
    pre: Alloc,
    value: int,
    memory: bytes,
    offset: int,
    length: int,
):
    """
    Tests call data into EXT*CALL including multiple offset conditions.

    Caller pushes data into memory, then calls the target.  Target writes 64 bytes of call data
    to storage and a success byte.
    """
    env = Environment()

    sender = pre.fund_eoa(10**18)

    address_returner = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_calldata_1, Op.CALLDATALOAD(0))
                    + Op.SSTORE(slot_calldata_2, Op.CALLDATALOAD(32))
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP
                ),
            ]
        ),
    )
    address_caller = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.DATACOPY(0, 0, len(memory))
                    + Op.SSTORE(
                        slot_call_status,
                        Op.EXTCALL(address_returner, offset, length, value),
                    )
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP
                ),
                Section.Data(data=memory),
            ]
        ),
        storage={slot_call_status: value_exceptional_abort_canary},
        balance=10**9,
    )

    calldata = memory[offset : offset + length]
    post = {
        address_returner: Account(
            storage={
                slot_code_worked: value_code_worked,
                slot_calldata_1: right_pad_32(calldata[0:32]),
                slot_calldata_2: right_pad_32(calldata[32:64]),
            }
        ),
        address_caller: Account(
            storage={
                slot_code_worked: value_code_worked,
                slot_call_status: EXTCALL_SUCCESS,
            }
        ),
    }

    tx = Transaction(to=address_caller, gas_limit=2_000_000, sender=sender)

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

test_extdelegatecall_inputdata(state_test, pre, memory, offset, length)

Tests call data into EXT*CALL including multiple offset conditions.

Caller pushes data into memory, then calls the target. Target writes 64 bytes of call data to storage and a success byte.

Source code in tests/prague/eip7692_eof_v1/eip7069_extcall/test_calldata.py
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
@pytest.mark.parametrize(
    "memory",
    [
        b"",
        b"1234567890abcdef",
        b"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=-",
        b"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=-" * 4,
    ],
    ids=lambda x: "size_%d" % len(x),
)
@pytest.mark.parametrize("offset", [0, 8, 24, 80])
@pytest.mark.parametrize("length", [0, 8, 32, 48])
def test_extdelegatecall_inputdata(
    state_test: StateTestFiller,
    pre: Alloc,
    memory: bytes,
    offset: int,
    length: int,
):
    """
    Tests call data into EXT*CALL including multiple offset conditions.

    Caller pushes data into memory, then calls the target.  Target writes 64 bytes of call data
    to storage and a success byte.
    """
    env = Environment()

    sender = pre.fund_eoa(10**18)

    address_returner = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_calldata_1, Op.CALLDATALOAD(0))
                    + Op.SSTORE(slot_calldata_2, Op.CALLDATALOAD(32))
                    + Op.SSTORE(slot_delegate_code_worked, value_code_worked)
                    + Op.STOP
                ),
            ]
        ),
    )
    address_caller = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.DATACOPY(0, 0, len(memory))
                    + Op.SSTORE(
                        slot_call_status,
                        Op.EXTDELEGATECALL(address_returner, offset, length),
                    )
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP
                ),
                Section.Data(data=memory),
            ]
        ),
        storage={slot_call_status: value_exceptional_abort_canary},
        balance=10**9,
    )

    calldata = memory[offset : offset + length]
    post = {
        address_returner: Account(storage={}),
        address_caller: Account(
            storage={
                slot_code_worked: value_code_worked,
                slot_delegate_code_worked: value_code_worked,
                slot_call_status: EXTCALL_SUCCESS,
                slot_calldata_1: right_pad_32(calldata[0:32]),
                slot_calldata_2: right_pad_32(calldata[32:64]),
            }
        ),
    }

    tx = Transaction(to=address_caller, gas_limit=2_000_000, sender=sender)

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

test_extstaticcall_inputdata(state_test, pre, memory, offset, length)

Tests call data into EXT*CALL including multiple offset conditions.

Caller pushes data into memory, then calls the target. Target writes 64 bytes of call data to storage and a success byte.

Source code in tests/prague/eip7692_eof_v1/eip7069_extcall/test_calldata.py
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
287
288
289
290
291
@pytest.mark.parametrize(
    "memory",
    [
        b"",
        b"1234567890abcdef",
        b"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=-",
        b"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=-" * 4,
    ],
    ids=lambda x: "size_%d" % len(x),
)
@pytest.mark.parametrize("offset", [0, 8, 24, 80])
@pytest.mark.parametrize("length", [0, 8, 32, 48])
def test_extstaticcall_inputdata(
    state_test: StateTestFiller,
    pre: Alloc,
    memory: bytes,
    offset: int,
    length: int,
):
    """
    Tests call data into EXT*CALL including multiple offset conditions.

    Caller pushes data into memory, then calls the target.  Target writes 64 bytes of call data
    to storage and a success byte.
    """
    env = Environment()

    sender = pre.fund_eoa(10**18)

    address_returner = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE) + Op.RETURN(0, Op.CALLDATASIZE)
                ),
            ]
        ),
    )
    address_caller = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.DATACOPY(0, 0, len(memory))
                    + Op.SSTORE(
                        slot_call_status,
                        Op.EXTSTATICCALL(address_returner, offset, length),
                    )
                    + Op.SSTORE(slot_calldata_1, Op.RETURNDATALOAD(0))
                    + Op.SSTORE(slot_calldata_2, Op.RETURNDATALOAD(32))
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP
                ),
                Section.Data(data=memory),
            ]
        ),
        storage={slot_call_status: value_exceptional_abort_canary},
        balance=10**9,
    )

    calldata = memory[offset : offset + length]
    post = {
        address_returner: Account(storage={}),
        address_caller: Account(
            storage={
                slot_code_worked: value_code_worked,
                slot_call_status: EXTCALL_SUCCESS,
                slot_calldata_1: right_pad_32(calldata[0:32]),
                slot_calldata_2: right_pad_32(calldata[32:64]),
            }
        ),
    }

    tx = Transaction(to=address_caller, gas_limit=2_000_000, sender=sender)

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

test_calldata_remains_after_subcall(state_test, pre, opcode)

Tests call data remains after a call to another contract.

Caller pushes data into memory, then calls the target. Target calls 3rd contract. 3rd contract returns. Target writes calldata to storage.

Source code in tests/prague/eip7692_eof_v1/eip7069_extcall/test_calldata.py
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
@pytest.mark.parametrize(
    "opcode",
    [
        Op.CALL,
        Op.CALLCODE,
        Op.DELEGATECALL,
        Op.STATICCALL,
        Op.EXTCALL,
        Op.EXTDELEGATECALL,
        Op.EXTSTATICCALL,
    ],
)
def test_calldata_remains_after_subcall(
    state_test: StateTestFiller,
    pre: Alloc,
    opcode: Op,
):
    """
    Tests call data remains after a call to another contract.

    Caller pushes data into memory, then calls the target.  Target calls 3rd contract. 3rd contract
    returns. Target writes calldata to storage.
    """
    env = Environment()

    sender = pre.fund_eoa(10**18)

    address_sub_called = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.SSTORE(slot_delegate_code_worked, value_code_worked) + Op.STOP
                )
            ]
        ),
        storage={slot_delegate_code_worked: value_exceptional_abort_canary},
    )
    called_code = (
        Op.MSTORE(0, value_calldata_2)
        + Op.SSTORE(slot_call_status, value_exceptional_abort_canary)
        + Op.SSTORE(slot_calldata_1, value_exceptional_abort_canary)
        + Op.SSTORE(slot_code_worked, value_exceptional_abort_canary)
        + Op.SSTORE(
            slot_call_status,
            opcode(
                address=address_sub_called,
                args_offset=0,
                args_size=size_calldata,
            ),
        )
        + Op.SSTORE(slot_calldata_1, Op.CALLDATALOAD(0))
        + Op.SSTORE(slot_code_worked, value_code_worked)
        + Op.STOP
    )
    match opcode:
        case Op.CALL | Op.CALLCODE | Op.DELEGATECALL | Op.STATICCALL:
            address_called = pre.deploy_contract(code=called_code)
        case Op.EXTCALL | Op.EXTDELEGATECALL | Op.EXTSTATICCALL:
            address_called = pre.deploy_contract(
                Container(
                    sections=[
                        Section.Code(code=called_code),
                    ]
                ),
            )
    address_caller = pre.deploy_contract(
        Container(
            sections=[
                Section.Code(
                    code=Op.MSTORE(0, value_calldata_1)
                    + Op.SSTORE(slot_calldata_1, value_exceptional_abort_canary)
                    + Op.SSTORE(slot_code_worked, value_exceptional_abort_canary)
                    + Op.SSTORE(
                        slot_call_status,
                        Op.EXTCALL(address_called, 0, size_calldata, 0),
                    )
                    + Op.SSTORE(slot_calldata_1, Op.RETURNDATALOAD(0))
                    + Op.SSTORE(slot_code_worked, value_code_worked)
                    + Op.STOP
                ),
            ]
        ),
        storage={slot_call_status: value_exceptional_abort_canary},
        balance=10**9,
    )

    match opcode:
        case Op.STATICCALL:
            called_storage = {
                slot_code_worked: value_code_worked,
                slot_call_status: CALL_FAILURE,
                slot_calldata_1: value_calldata_1,
            }
            sub_called_storage = {
                slot_delegate_code_worked: value_exceptional_abort_canary,
            }
        case Op.DELEGATECALL | Op.CALLCODE:
            called_storage = {
                slot_code_worked: value_code_worked,
                slot_delegate_code_worked: value_code_worked,
                slot_call_status: CALL_SUCCESS,
                slot_calldata_1: value_calldata_1,
            }
            sub_called_storage = {
                slot_delegate_code_worked: value_exceptional_abort_canary,
            }
        case Op.CALL:
            called_storage = {
                slot_code_worked: value_code_worked,
                slot_call_status: CALL_SUCCESS,
                slot_calldata_1: value_calldata_1,
            }
            sub_called_storage = {
                slot_delegate_code_worked: value_code_worked,
            }
        case Op.EXTSTATICCALL:
            called_storage = {
                slot_code_worked: value_code_worked,
                slot_call_status: EXTCALL_FAILED,
                slot_calldata_1: value_calldata_1,
            }
            sub_called_storage = {
                slot_delegate_code_worked: value_exceptional_abort_canary,
            }
        case Op.EXTDELEGATECALL:
            called_storage = {
                slot_code_worked: value_code_worked,
                slot_delegate_code_worked: value_code_worked,
                slot_call_status: EXTCALL_SUCCESS,
                slot_calldata_1: value_calldata_1,
            }
            sub_called_storage = {
                slot_delegate_code_worked: value_exceptional_abort_canary,
            }
        case Op.EXTCALL:
            called_storage = {
                slot_code_worked: value_code_worked,
                slot_call_status: EXTCALL_SUCCESS,
                slot_calldata_1: value_calldata_1,
            }
            sub_called_storage = {
                slot_delegate_code_worked: value_code_worked,
            }
        case _:
            raise ValueError(f"Unexpected opcode: {opcode}")

    post = {
        address_caller: Account(storage={slot_code_worked: value_code_worked}),
        address_called: Account(storage=called_storage),
        address_sub_called: Account(storage=sub_called_storage),
    }

    tx = Transaction(to=address_caller, gas_limit=4_000_000, sender=sender)

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