|
| 1 | +""" |
| 2 | +Implementation of the EELS T8N for execution-spec-tests. |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +import tempfile |
| 7 | +from io import StringIO |
| 8 | +from typing import Any, Dict, Optional |
| 9 | + |
| 10 | +from ethereum_clis.clis.execution_specs import ExecutionSpecsExceptionMapper |
| 11 | +from ethereum_clis.file_utils import dump_files_to_directory |
| 12 | +from ethereum_clis.transition_tool import TransitionTool, model_dump_config |
| 13 | +from ethereum_clis.types import TransitionToolOutput |
| 14 | +from ethereum_test_forks import Fork |
| 15 | + |
| 16 | +import ethereum |
| 17 | + |
| 18 | +from .. import create_parser |
| 19 | +from ..utils import get_supported_forks |
| 20 | +from . import T8N |
| 21 | + |
| 22 | + |
| 23 | +class EELST8N(TransitionTool): |
| 24 | + """Implementation of the EELS T8N for execution-spec-tests.""" |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + *, |
| 29 | + trace: bool = False, |
| 30 | + ): |
| 31 | + """Initialize the EELS Transition Tool interface.""" |
| 32 | + self.exception_mapper = ExecutionSpecsExceptionMapper() |
| 33 | + self.trace = trace |
| 34 | + self._info_metadata: Optional[Dict[str, Any]] = {} |
| 35 | + |
| 36 | + def version(self) -> str: |
| 37 | + """Version of the t8n tool.""" |
| 38 | + return ethereum.__version__ |
| 39 | + |
| 40 | + def is_fork_supported(self, fork: Fork) -> bool: |
| 41 | + """Return True if the fork is supported by the tool.""" |
| 42 | + return fork.transition_tool_name() in get_supported_forks() |
| 43 | + |
| 44 | + def evaluate( |
| 45 | + self, |
| 46 | + *, |
| 47 | + transition_tool_data: TransitionTool.TransitionToolData, |
| 48 | + debug_output_path: str = "", |
| 49 | + slow_request: bool = False, # noqa: U100, F841 |
| 50 | + ) -> TransitionToolOutput: |
| 51 | + """ |
| 52 | + Evaluate using the EELS T8N entry point. |
| 53 | + """ |
| 54 | + request_data = transition_tool_data.get_request_data() |
| 55 | + request_data_json = request_data.model_dump( |
| 56 | + mode="json", **model_dump_config |
| 57 | + ) |
| 58 | + |
| 59 | + t8n_args = [ |
| 60 | + "t8n", |
| 61 | + "--input.alloc=stdin", |
| 62 | + "--input.env=stdin", |
| 63 | + "--input.txs=stdin", |
| 64 | + "--output.result=stdout", |
| 65 | + "--output.body=stdout", |
| 66 | + "--output.alloc=stdout", |
| 67 | + f"--state.fork={request_data_json['state']['fork']}", |
| 68 | + f"--state.chainid={request_data_json['state']['chainid']}", |
| 69 | + f"--state.reward={request_data_json['state']['reward']}", |
| 70 | + ] |
| 71 | + |
| 72 | + if transition_tool_data.state_test: |
| 73 | + t8n_args.append("--state-test") |
| 74 | + |
| 75 | + temp_dir = tempfile.TemporaryDirectory() |
| 76 | + if self.trace: |
| 77 | + t8n_args.extend( |
| 78 | + [ |
| 79 | + "--trace", |
| 80 | + "--trace.memory", |
| 81 | + "--trace.returndata", |
| 82 | + f"--output.basedir={temp_dir.name}", |
| 83 | + ] |
| 84 | + ) |
| 85 | + |
| 86 | + parser = create_parser() |
| 87 | + t8n_options = parser.parse_args(t8n_args) |
| 88 | + |
| 89 | + out_stream = StringIO() |
| 90 | + |
| 91 | + in_stream = StringIO(json.dumps(request_data_json["input"])) |
| 92 | + |
| 93 | + t8n = T8N(t8n_options, out_stream, in_stream) |
| 94 | + t8n.run() |
| 95 | + |
| 96 | + output_dict = json.loads(out_stream.getvalue()) |
| 97 | + output: TransitionToolOutput = TransitionToolOutput.model_validate( |
| 98 | + output_dict, context={"exception_mapper": self.exception_mapper} |
| 99 | + ) |
| 100 | + |
| 101 | + if debug_output_path: |
| 102 | + dump_files_to_directory( |
| 103 | + debug_output_path, |
| 104 | + { |
| 105 | + "input/alloc.json": request_data.input.alloc, |
| 106 | + "input/env.json": request_data.input.env, |
| 107 | + "input/txs.json": [ |
| 108 | + tx.model_dump(mode="json", **model_dump_config) |
| 109 | + for tx in request_data.input.txs |
| 110 | + ], |
| 111 | + }, |
| 112 | + ) |
| 113 | + |
| 114 | + dump_files_to_directory( |
| 115 | + debug_output_path, |
| 116 | + { |
| 117 | + "output/alloc.json": output.alloc, |
| 118 | + "output/result.json": output.result, |
| 119 | + }, |
| 120 | + ) |
| 121 | + |
| 122 | + if self.trace: |
| 123 | + self.collect_traces( |
| 124 | + output.result.receipts, temp_dir, debug_output_path |
| 125 | + ) |
| 126 | + temp_dir.cleanup() |
| 127 | + |
| 128 | + return output |
0 commit comments