Skip to content

Commit

Permalink
feat(tests): convert calldatacopy test (ethereum#1056)
Browse files Browse the repository at this point in the history
* convert calldatacopy from yml

* adding gas to tx gets everything passing

* review updates

* Update tests/frontier/opcodes/test_calldatacopy.py

* Update docs/CHANGELOG.md

---------

Co-authored-by: winsvega <dimitry@ethereum.org>
Co-authored-by: Mario Vega <marioevz@gmail.com>
  • Loading branch information
3 people authored and fselmo committed Jan 24, 2025
1 parent 32a44b6 commit 3ea186e
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
3 changes: 3 additions & 0 deletions converted-ethereum-tests.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
([#1056](https://github.com/ethereum/execution-spec-tests/pull/1056))
GeneralStateTests/VMTests/vmTests/calldatacopy.json

([#748](https://github.com/ethereum/execution-spec-tests/pull/748))
GeneralStateTests/stBadOpcode/badOpcodes.json
GeneralStateTests/stBugs/evmBytecode.json
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Release tarball changes:
- 🔀 Update EIP-7002 according to [spec updates](https://github.com/ethereum/EIPs/pull/9119) ([#1024](https://github.com/ethereum/execution-spec-tests/pull/1024)).
- 🔀 Update EIP-2935 according to [spec updates](https://github.com/ethereum/EIPs/pull/9144) ([#1046](https://github.com/ethereum/execution-spec-tests/pull/1046))
-[EIP-7691](https://eips.ethereum.org/EIPS/eip-7691) Blob throughput increase tests by parametrization of existing EIP-4844 tests ([#1023](https://github.com/ethereum/execution-spec-tests/pull/1023), [#1082](https://github.com/ethereum/execution-spec-tests/pull/1082))
- ✨ Port [calldatacopy test](https://github.com/ethereum/tests/blob/ae4791077e8fcf716136e70fe8392f1a1f1495fb/src/GeneralStateTestsFiller/VMTests/vmTests/calldatacopyFiller.yml) ([#1056](https://github.com/ethereum/execution-spec-tests/pull/1056)).

### 🛠️ Framework

Expand Down
180 changes: 180 additions & 0 deletions tests/frontier/opcodes/test_calldatacopy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
"""test `CALLDATACOPY` opcode."""

import pytest

from ethereum_test_forks import Byzantium, Fork
from ethereum_test_tools import Account, Alloc, Bytecode, StateTestFiller, Transaction
from ethereum_test_tools.vm.opcode import Opcodes as Op


@pytest.mark.parametrize(
"code,tx_data,code_address_storage,to_address_storage",
[
(
(
Op.CALLDATACOPY(dest_offset=0, offset=1, size=2)
+ Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0))
+ Op.RETURN(offset=0, size=Op.MSIZE)
),
b"\x00",
Account(
storage={0x00: 0x3456000000000000000000000000000000000000000000000000000000000000}
),
Account(
storage={0x00: 0x3456000000000000000000000000000000000000000000000000000000000000}
),
),
(
(
Op.CALLDATACOPY(dest_offset=0, offset=1, size=1)
+ Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0))
+ Op.RETURN(offset=0, size=Op.MSIZE)
),
b"\x01",
Account(
storage={0x00: 0x3400000000000000000000000000000000000000000000000000000000000000},
),
Account(
storage={0x00: 0x3400000000000000000000000000000000000000000000000000000000000000},
),
),
(
(
Op.CALLDATACOPY(dest_offset=0, offset=1, size=0)
+ Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0))
+ Op.RETURN(offset=0, size=Op.MSIZE)
),
b"\x02",
Account(
storage={0x00: 0x00},
),
Account(
storage={0x00: 0x00},
),
),
(
(
Op.CALLDATACOPY(dest_offset=0, offset=0, size=0)
+ Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0))
+ Op.RETURN(offset=0, size=Op.MSIZE)
),
b"\x03",
Account(
storage={0x00: 0x00},
),
Account(
storage={0x00: 0x00},
),
),
(
(
Op.CALLDATACOPY(
dest_offset=0,
offset=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA,
size=0xFF,
)
+ Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0))
+ Op.RETURN(offset=0, size=Op.MSIZE)
),
b"\x04",
Account(storage={0x00: 0x00}),
Account(storage={0x00: 0x00}),
),
(
(
Op.CALLDATACOPY(
dest_offset=0,
offset=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA,
size=0x9,
)
+ Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0))
+ Op.RETURN(offset=0, size=Op.MSIZE)
),
b"\x05",
Account(storage={0x00: 0x00}),
Account(storage={0x00: 0x00}),
),
(
(Op.SSTORE(key=0x1, value=0x1) + Op.PUSH1[0x1] + Op.PUSH1[0x2] + Op.CALLDATACOPY),
b"\x10",
Account(storage={0x01: 0x00}),
None,
),
(
(
Op.JUMP(pc=0x5)
+ Op.JUMPDEST
+ Op.STOP
+ Op.JUMPDEST
+ Op.MSTORE8(offset=0x1F, value=0x42)
+ Op.CALLDATACOPY(dest_offset=0x1F, offset=0x0, size=0x103)
+ Op.MLOAD(offset=0x0)
+ Op.DUP1
+ Op.PUSH1[0x60]
+ Op.JUMPI(pc=0x3, condition=Op.EQ)
+ Op.SSTORE(key=0xFF, value=0xBADC0FFEE)
),
b"\x11",
Account(storage={0xFF: 0xBADC0FFEE}),
None,
),
],
ids=[
"cdc 0 1 2",
"cdc 0 1 1",
"cdc 0 1 0",
"cdc 0 0 0",
"cdc 0 neg6 ff",
"cdc 0 neg6 9",
"underflow",
"sec",
],
)
def test_calldatacopy(
state_test: StateTestFiller,
code: Bytecode,
fork: Fork,
tx_data: bytes,
pre: Alloc,
code_address_storage: Account,
to_address_storage: Account | None,
):
"""
Test `CALLDATACOPY` opcode.
Based on https://github.com/ethereum/tests/blob/ae4791077e8fcf716136e70fe8392f1a1f1495fb/src/GeneralStateTestsFiller/VMTests/vmTests/calldatacopyFiller.ym
"""
code_address = pre.deploy_contract(code)
to = pre.deploy_contract(
code=(
Op.MSTORE(offset=0x0, value=0x1234567890ABCDEF01234567890ABCDEF0)
+ Op.CALL(
gas=Op.SUB(Op.GAS(), 0x100),
address=code_address,
value=0x0,
args_offset=0xF,
args_size=0x10,
ret_offset=0x20,
ret_size=0x40,
)
+ Op.POP
+ Op.SSTORE(key=0x0, value=Op.MLOAD(offset=0x20))
+ Op.SSTORE(key=0x1, value=Op.MLOAD(offset=0x40))
+ Op.STOP
),
)

tx = Transaction(
data=tx_data,
gas_limit=100_000,
gas_price=0x0A,
protected=fork >= Byzantium,
sender=pre.fund_eoa(),
to=to,
value=0x01,
)
if to_address_storage:
post = {code_address: code_address_storage, to: to_address_storage}
else:
post = {code_address: code_address_storage}
state_test(pre=pre, post=post, tx=tx)

0 comments on commit 3ea186e

Please sign in to comment.