|
| 1 | +"""Verify refilled test vs original generated test.""" |
| 2 | + |
| 3 | +import re |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from pydantic import BaseModel, RootModel |
| 7 | + |
| 8 | + |
| 9 | +# Define only relevant data we need to read from the files |
| 10 | +class Indexes(BaseModel): |
| 11 | + """Post Section Indexes.""" |
| 12 | + |
| 13 | + data: int |
| 14 | + gas: int |
| 15 | + value: int |
| 16 | + |
| 17 | + |
| 18 | +class PostRecord(BaseModel): |
| 19 | + """Post results record.""" |
| 20 | + |
| 21 | + hash: str |
| 22 | + indexes: Indexes |
| 23 | + |
| 24 | + |
| 25 | +class StateTest(BaseModel): |
| 26 | + """StateTest in filled file.""" |
| 27 | + |
| 28 | + post: dict[str, list[PostRecord]] |
| 29 | + |
| 30 | + |
| 31 | +class FilledStateTest(RootModel[dict[str, StateTest]]): |
| 32 | + """State Test Wrapper.""" |
| 33 | + |
| 34 | + |
| 35 | +def verify_refilled(refilled: Path, original: Path) -> int: |
| 36 | + """ |
| 37 | + Verify post hash of the refilled test against original: |
| 38 | + Regex the original d,g,v from the refilled test name. |
| 39 | + Find the post record for this d,g,v and the fork of refilled test. |
| 40 | + Compare the post hash. |
| 41 | + """ |
| 42 | + verified_vectors = 0 |
| 43 | + json_str = refilled.read_text(encoding="utf-8") |
| 44 | + refilled_test_wrapper = FilledStateTest.model_validate_json(json_str) |
| 45 | + |
| 46 | + json_str = original.read_text(encoding="utf-8") |
| 47 | + original_test_wrapper = FilledStateTest.model_validate_json(json_str) |
| 48 | + |
| 49 | + # Each original test has only 1 test with many posts for each fork and many txs |
| 50 | + original_test_name, test_original = list(original_test_wrapper.root.items())[0] |
| 51 | + |
| 52 | + for refilled_test_name, refilled_test in refilled_test_wrapper.root.items(): |
| 53 | + # Each refilled test has only 1 post for 1 fork and 1 transaction |
| 54 | + refilled_fork, refilled_result = list(refilled_test.post.items())[0] |
| 55 | + pattern = r"v=(\d+)-g=(\d+)-d=(\d+)" |
| 56 | + match = re.search(pattern, refilled_test_name) |
| 57 | + if match: |
| 58 | + v, g, d = match.groups() |
| 59 | + v, g, d = int(v), int(g), int(d) |
| 60 | + |
| 61 | + found = False |
| 62 | + original_result = test_original.post[refilled_fork] |
| 63 | + for res in original_result: |
| 64 | + if res.indexes.data == d and res.indexes.gas == g and res.indexes.value == v: |
| 65 | + print(f"check: {refilled_fork}, d:{d}, g:{g}, v:{v}") |
| 66 | + if res.hash != refilled_result[0].hash: |
| 67 | + raise Exception( |
| 68 | + "\nRefilled test post hash mismatch: \n" |
| 69 | + f"test_name: {refilled_test_name}\n" |
| 70 | + f"original_name: {original}\n" |
| 71 | + f"refilled_hash: {refilled_result[0].hash}\n" |
| 72 | + f"original_hash: {res.hash} f: {refilled_fork}, d: {d}, g: {g}, v: {v}" |
| 73 | + ) |
| 74 | + found = True |
| 75 | + verified_vectors += 1 |
| 76 | + break |
| 77 | + |
| 78 | + if not found: |
| 79 | + raise Exception( |
| 80 | + "\nRefilled test not found in original: \n" |
| 81 | + f"test_name: {refilled_test_name}\n" |
| 82 | + f"original_name: {original}\n" |
| 83 | + ) |
| 84 | + else: |
| 85 | + raise Exception("Could not regex match d.g.v indexes from refilled test name!") |
| 86 | + |
| 87 | + return verified_vectors |
0 commit comments