From ab31dd7718c3fbfd50ffb0c2b103030fb62d640e Mon Sep 17 00:00:00 2001 From: Guruprasad Kamath Date: Thu, 27 Jun 2024 09:39:42 +0200 Subject: [PATCH] run available prague tests --- tests/prague/test_eof.py | 78 +++++++++++++++++++++++++++ tests/prague/test_rlp.py | 2 +- tests/prague/test_state_transition.py | 23 ++++---- 3 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 tests/prague/test_eof.py diff --git a/tests/prague/test_eof.py b/tests/prague/test_eof.py new file mode 100644 index 0000000000..c507f83eab --- /dev/null +++ b/tests/prague/test_eof.py @@ -0,0 +1,78 @@ +import json +import os +from glob import glob +from typing import Dict, Generator, Tuple + +import pytest + +from ethereum.prague.vm.eof import validate_eof_container +from ethereum.prague.vm.exceptions import InvalidEOF +from ethereum.utils.hexadecimal import hex_to_bytes + +TEST_DIRS = ( + "tests/fixtures/ethereum_tests/EOFTests/EIP3540", + "tests/fixtures/ethereum_tests/EOFTests/EIP3670", + "tests/fixtures/ethereum_tests/EOFTests/EIP4200", +) + + +def fetch_eof_tests(test_dirs: Tuple[str, ...]) -> Generator: + for test_dir in test_dirs: + all_jsons = [ + y + for x in os.walk(test_dir) + for y in glob(os.path.join(x[0], "*.json")) + ] + + for full_path in all_jsons: + # Read the json file and yield the test cases + with open(full_path, "r") as file: + data = json.load(file) + for test in data.keys(): + for key in data[test]["vectors"].keys(): + yield { + "test_file": full_path, + "test_name": test, + "test_key": key, + } + + +# Test case Identifier +def idfn(test_case: Dict) -> str: + if isinstance(test_case, dict): + return ( + test_case["test_file"] + + " - " + + test_case["test_name"] + + " - " + + test_case["test_key"] + ) + + +# Run the tests +@pytest.mark.parametrize( + "test_case", + fetch_eof_tests(TEST_DIRS), + ids=idfn, +) +def test_eof(test_case: Dict) -> None: + test_file = test_case["test_file"] + test_name = test_case["test_name"] + test_key = test_case["test_key"] + + with open(test_file, "r") as file: + test_data = json.load(file) + test_vector = test_data[test_name]["vectors"][test_key] + + # Extract the test data + code = hex_to_bytes(test_vector["code"]) + prague_validation = test_vector["results"]["Prague"] + + if "exception" in prague_validation and prague_validation["result"]: + raise Exception("Test case has both exception and result") + + if "exception" in prague_validation: + with pytest.raises(InvalidEOF): + validate_eof_container(code) + else: + validate_eof_container(code) diff --git a/tests/prague/test_rlp.py b/tests/prague/test_rlp.py index bf8e9ba1b2..4a82cad4ef 100644 --- a/tests/prague/test_rlp.py +++ b/tests/prague/test_rlp.py @@ -2,6 +2,7 @@ import ethereum.rlp as rlp from ethereum.base_types import U64, U256, Bytes, Bytes0, Bytes8, Bytes32, Uint +from ethereum.crypto.hash import keccak256 from ethereum.prague.blocks import Block, Header, Log, Receipt, Withdrawal from ethereum.prague.transactions import ( AccessListTransaction, @@ -12,7 +13,6 @@ encode_transaction, ) from ethereum.prague.utils.hexadecimal import hex_to_address -from ethereum.crypto.hash import keccak256 from ethereum.utils.hexadecimal import hex_to_bytes256 hash1 = keccak256(b"foo") diff --git a/tests/prague/test_state_transition.py b/tests/prague/test_state_transition.py index a3d3713bcf..309ff567f2 100644 --- a/tests/prague/test_state_transition.py +++ b/tests/prague/test_state_transition.py @@ -1,5 +1,5 @@ from functools import partial -from typing import Dict +from typing import Dict, Generator, Tuple import pytest @@ -92,22 +92,25 @@ ) -@pytest.mark.parametrize( - "test_case", - fetch_state_tests(), - ids=idfn, +# Run temporary test fixtures for Prague +test_dirs = ( + "tests/fixtures/ethereum_tests/EIPTests/BlockchainTests/StateTests/stEOF/stEIP3540", + "tests/fixtures/ethereum_tests/EIPTests/BlockchainTests/StateTests/stEOF/stEIP4200", ) -def test_general_state_tests(test_case: Dict) -> None: - run_prague_blockchain_st_tests(test_case) -# Run execution-spec-generated-tests -test_dir = f"{ETHEREUM_SPEC_TESTS_PATH}/fixtures/withdrawals" +def fetch_temporary_tests(test_dirs: Tuple[str, ...]) -> Generator: + """ + Fetch the relevant tests for a particular EIP-Implementation + from among the temporary fixtures from ethereum-spec-tests. + """ + for test_dir in test_dirs: + yield from fetch_prague_tests(test_dir) @pytest.mark.parametrize( "test_case", - fetch_prague_tests(test_dir), + fetch_temporary_tests(test_dirs), ids=idfn, ) def test_execution_specs_generated_tests(test_case: Dict) -> None: