Skip to content

feat(tests): converting calldataload and calldatasize tests #1236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions converted-ethereum-tests.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
([#1236](https://github.com/ethereum/execution-spec-tests/pull/1236))
GeneralStateTests/VMTests/vmTests/calldataload.json
GeneralStateTests/VMTests/vmTests/calldatasize.json

([#1056](https://github.com/ethereum/execution-spec-tests/pull/1056))
GeneralStateTests/VMTests/vmTests/calldatacopy.json

Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ consume cache --help
- ✨ Add EIP-7702 incorrect-rlp-encoding tests ([#1347](https://github.com/ethereum/execution-spec-tests/pull/1347)).
- ✨ Add EIP-2935 tests for all call opcodes ([#1379](https://github.com/ethereum/execution-spec-tests/pull/1379)).
- ✨ Add more tests for EIP-7702: max-fee-per-gas verification, delegation-designation as initcode tests ([#1372](https://github.com/ethereum/execution-spec-tests/pull/1372)).
- ✨ Port [calldataload](https://github.com/ethereum/tests/blob/ae4791077e8fcf716136e70fe8392f1a1f1495fb/src/GeneralStateTestsFiller/VMTests/vmTests/calldatacopyFiller.yml) and [calldatasize](https://github.com/ethereum/tests/blob/81862e4848585a438d64f911a19b3825f0f4cd95/src/GeneralStateTestsFiller/VMTests/vmTests/calldatasizeFiller.yml) tests ([#1236](https://github.com/ethereum/execution-spec-tests/pull/1236)).

## [v4.1.0](https://github.com/ethereum/execution-spec-tests/releases/tag/v4.1.0) - 2025-03-11

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

import pytest

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


@pytest.mark.parametrize(
"calldata,calldata_offset,expected_storage",
[
(
b"\x25\x60",
0x0,
0x2560000000000000000000000000000000000000000000000000000000000000,
),
(
b"\xff" * 32 + b"\x23",
0x1,
0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF23,
),
(
bytes.fromhex("123456789ABCDEF00000000000000000000000000000000000000000000000000024"),
0x5,
0xBCDEF00000000000000000000000000000000000000000000000000024000000,
),
],
ids=[
"two_bytes",
"word_n_byte",
"34_bytes",
],
)
def test_calldataload(
state_test: StateTestFiller,
calldata: bytes,
calldata_offset: int,
fork: Fork,
pre: Alloc,
expected_storage: Account,
):
"""
Test `CALLDATALOAD` opcode.

Based on https://github.com/ethereum/tests/blob/ae4791077e8fcf716136e70fe8392f1a1f1495fb/src/GeneralStateTestsFiller/VMTests/vmTests/calldatacopyFiller.yml
"""
address_a = pre.deploy_contract(
(Op.PUSH1[calldata_offset] + Op.CALLDATALOAD + Op.PUSH1[0x0] + Op.SSTORE + Op.STOP),
)

address_b = pre.deploy_contract(
Om.MSTORE(calldata, 0x0)
+ Op.CALL(
gas=Op.SUB(Op.GAS(), 0x100),
address=address_a,
value=0x0,
args_offset=0x0,
args_size=len(calldata),
ret_offset=0x0,
ret_size=0x0,
)
)

to = pre.deploy_contract(
code=(
Op.ADD(0x1000, Op.CALLDATALOAD(offset=0x4))
+ Op.CALL(
gas=Op.SUB(Op.GAS(), 0x100),
address=address_b,
value=0x0,
args_offset=0x0,
args_size=0x0,
ret_offset=0x0,
ret_size=0x0,
)
+ Op.STOP
),
)

tx = Transaction(
data=calldata,
gas_limit=100_000,
protected=fork >= Byzantium,
sender=pre.fund_eoa(),
to=to,
)
post = {
address_a: Account(storage={0x00: expected_storage}),
}
state_test(pre=pre, post=post, tx=tx)
50 changes: 50 additions & 0 deletions tests/frontier/opcodes/test_calldatasize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""test `CALLDATASIZE` opcode."""

import pytest

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


@pytest.mark.parametrize(
"args_size",
[0, 2, 16, 33, 257],
)
def test_calldataload(
state_test: StateTestFiller,
fork: Fork,
args_size: int,
pre: Alloc,
):
"""
Test `CALLDATASIZE` opcode.

Based on https://github.com/ethereum/tests/blob/81862e4848585a438d64f911a19b3825f0f4cd95/src/GeneralStateTestsFiller/VMTests/vmTests/calldatasizeFiller.yml
"""
address = pre.deploy_contract(Op.SSTORE(key=0x0, value=Op.CALLDATASIZE))

to = pre.deploy_contract(
code=(
Om.MSTORE(b"\x01" * args_size, 0x0)
+ Op.CALL(
gas=Op.SUB(Op.GAS(), 0x100),
address=address,
value=0x0,
args_offset=0x0,
args_size=args_size,
ret_offset=0x0,
ret_size=0x0,
)
)
)

tx = Transaction(
gas_limit=100_000,
protected=fork >= Byzantium,
sender=pre.fund_eoa(),
to=to,
)
post = {address: Account(storage={0x00: args_size})}
state_test(pre=pre, post=post, tx=tx)
3 changes: 3 additions & 0 deletions tests/homestead/coverage/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_coverage(
+ Op.PUSH2(0x0102)
+ Op.PUSH3(0x010203)
+ Op.PUSH4(0x01020304)
+ Op.PUSH32(0x0101010101010101010101010101010101010101010101010101010101010101)
+ Op.MSTORE8(0x00, 0x01)
+ Op.ADD(0x02, 0x03)
+ Op.POP(0x01)
# lllc tests insert codecopy when using lll(seq())
+ Op.CODECOPY(0, 16, 4),
Expand Down
Loading