From 6da43f8d0b5539f28fd063c61bc7a6a021303966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 12 Apr 2024 12:47:58 +0200 Subject: [PATCH] new(test): add tests for EOF/EIP-663 DUPN SWAPN --- .../eip663_dupn_swapn_exchange/__init__.py | 7 +++ .../eip663_dupn_swapn_exchange/conftest.py | 14 +++++ .../eip663_dupn_swapn_exchange/test_dupn.py | 56 ++++++++++++++++++ .../eip663_dupn_swapn_exchange/test_swapn.py | 57 +++++++++++++++++++ whitelist.txt | 2 + 5 files changed, 136 insertions(+) create mode 100644 tests/prague/eip663_dupn_swapn_exchange/__init__.py create mode 100644 tests/prague/eip663_dupn_swapn_exchange/conftest.py create mode 100644 tests/prague/eip663_dupn_swapn_exchange/test_dupn.py create mode 100644 tests/prague/eip663_dupn_swapn_exchange/test_swapn.py diff --git a/tests/prague/eip663_dupn_swapn_exchange/__init__.py b/tests/prague/eip663_dupn_swapn_exchange/__init__.py new file mode 100644 index 0000000000..63cdff3442 --- /dev/null +++ b/tests/prague/eip663_dupn_swapn_exchange/__init__.py @@ -0,0 +1,7 @@ +""" +abstract: Tests [EIP-663: SWAPN, DUPN and EXCHANGE instructions](https://eips.ethereum.org/EIPS/eip-663) + Tests for [EIP-663: SWAPN, DUPN and EXCHANGE instructions](https://eips.ethereum.org/EIPS/eip-663). +""" # noqa: E501 + +REFERENCE_SPEC_GIT_PATH = "EIPS/eip-663.md" +REFERENCE_SPEC_VERSION = "6aed382f86258f33603f5dc275956f739aaae096" diff --git a/tests/prague/eip663_dupn_swapn_exchange/conftest.py b/tests/prague/eip663_dupn_swapn_exchange/conftest.py new file mode 100644 index 0000000000..9c7b2de037 --- /dev/null +++ b/tests/prague/eip663_dupn_swapn_exchange/conftest.py @@ -0,0 +1,14 @@ +""" +Pytest fixtures for EIP-663 tests +""" +import pytest + +from ethereum_test_tools import Transaction + + +@pytest.fixture +def tx() -> Transaction: + """ + Produces the default Transaction. + """ + return Transaction(to=0xC0DE, gas_limit=10_000_000) diff --git a/tests/prague/eip663_dupn_swapn_exchange/test_dupn.py b/tests/prague/eip663_dupn_swapn_exchange/test_dupn.py new file mode 100644 index 0000000000..f928211b78 --- /dev/null +++ b/tests/prague/eip663_dupn_swapn_exchange/test_dupn.py @@ -0,0 +1,56 @@ +""" +abstract: Tests [EIP-663: SWAPN, DUPN and EXCHANGE instructions](https://eips.ethereum.org/EIPS/eip-663) + Tests for the DUPN instruction. +""" # noqa: E501 + +import pytest + +from ethereum_test_tools import Account, Environment, StateTestFiller, TestAddress, Transaction +from ethereum_test_tools.eof.v1 import Container, Section +from ethereum_test_tools.eof.v1.constants import NON_RETURNING_SECTION +from ethereum_test_tools.vm.opcode import Opcodes as Op + +from ..eip3540_eof_v1.spec import EOF_FORK_NAME +from . import REFERENCE_SPEC_GIT_PATH, REFERENCE_SPEC_VERSION + +REFERENCE_SPEC_GIT_PATH = REFERENCE_SPEC_GIT_PATH +REFERENCE_SPEC_VERSION = REFERENCE_SPEC_VERSION + + +@pytest.mark.valid_from(EOF_FORK_NAME) +def test_dupn_all_valid_immediates( + tx: Transaction, + state_test: StateTestFiller, +): + """ + Test case for all valid DUPN immediates. + """ + n = 256 + values = range(0xD00, 0xD00 + n) + + eof_code = Container( + sections=[ + Section.Code( + code=b"".join(Op.PUSH2(v) for v in values) + + b"".join(Op.DUPN(x) + Op.PUSH2(x) + Op.SSTORE for x in range(0, n)) + + Op.STOP, + code_inputs=0, + code_outputs=NON_RETURNING_SECTION, + max_stack_height=258, + ) + ], + ) + + pre = { + TestAddress: Account(balance=1_000_000_000), + tx.to: Account(code=eof_code), + } + + post = {tx.to: Account(storage=dict(zip(range(0, n), reversed(values))))} + + state_test( + env=Environment(), + pre=pre, + post=post, + tx=tx, + ) diff --git a/tests/prague/eip663_dupn_swapn_exchange/test_swapn.py b/tests/prague/eip663_dupn_swapn_exchange/test_swapn.py new file mode 100644 index 0000000000..81a5b5b671 --- /dev/null +++ b/tests/prague/eip663_dupn_swapn_exchange/test_swapn.py @@ -0,0 +1,57 @@ +""" +abstract: Tests [EIP-663: SWAPN, DUPN and EXCHANGE instructions](https://eips.ethereum.org/EIPS/eip-663) + Tests for the SWAPN instruction. +""" # noqa: E501 + +import pytest + +from ethereum_test_tools import Account, Environment, StateTestFiller, TestAddress, Transaction +from ethereum_test_tools.eof.v1 import Container, Section +from ethereum_test_tools.eof.v1.constants import NON_RETURNING_SECTION +from ethereum_test_tools.vm.opcode import Opcodes as Op + +from ..eip3540_eof_v1.spec import EOF_FORK_NAME +from . import REFERENCE_SPEC_GIT_PATH, REFERENCE_SPEC_VERSION + +REFERENCE_SPEC_GIT_PATH = REFERENCE_SPEC_GIT_PATH +REFERENCE_SPEC_VERSION = REFERENCE_SPEC_VERSION + + +@pytest.mark.valid_from(EOF_FORK_NAME) +def test_swapn_all_valid_immediates( + tx: Transaction, + state_test: StateTestFiller, +): + """ + Test case for all valid SWAPN immediates. + """ + n = 100 + values = range(0x500, 0x500 + 257) + + eof_code = Container( + sections=[ + Section.Code( + code=b"".join(Op.PUSH2(v) for v in values) + + b"".join(Op.SWAPN(0xFF - x) + Op.PUSH2(x) + Op.SSTORE for x in range(0, n)) + + Op.STOP, + code_inputs=0, + code_outputs=NON_RETURNING_SECTION, + max_stack_height=258, + ) + ], + ) + + pre = { + TestAddress: Account(balance=1_000_000_000), + tx.to: Account(code=eof_code), + } + + values_rotated = list(values[1:]) + [values[0]] + post = {tx.to: Account(storage=dict(zip(range(0, n), reversed(values_rotated))))} + + state_test( + env=Environment(), + pre=pre, + post=post, + tx=tx, + ) diff --git a/whitelist.txt b/whitelist.txt index c06e368730..7548303a42 100644 --- a/whitelist.txt +++ b/whitelist.txt @@ -89,6 +89,7 @@ dup dunder EEST eip +eip3540 eips EIPs endianness @@ -155,6 +156,7 @@ https hyperledger ignoreRevsFile img +immediates incrementing init initcode