Skip to content

Commit

Permalink
run available prague tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gurukamath committed Jun 27, 2024
1 parent 94fd4d0 commit ab31dd7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 11 deletions.
78 changes: 78 additions & 0 deletions tests/prague/test_eof.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion tests/prague/test_rlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down
23 changes: 13 additions & 10 deletions tests/prague/test_state_transition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import partial
from typing import Dict
from typing import Dict, Generator, Tuple

import pytest

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ab31dd7

Please sign in to comment.