|
| 1 | +from ethereum_clis import TransitionTool |
| 2 | +from ethereum_clis.clis.execution_specs import ExecutionSpecsExceptionMapper |
| 3 | +from ethereum_clis.transition_tool import model_dump_config |
| 4 | +import ethereum |
| 5 | +import tempfile |
| 6 | + |
| 7 | +from . import T8N |
| 8 | +from .. import create_parser |
| 9 | +from io import StringIO, TextIOWrapper |
| 10 | +import json |
| 11 | +from ethereum_clis.types import TransitionToolOutput |
| 12 | +from ethereum_clis.file_utils import dump_files_to_directory |
| 13 | + |
| 14 | +class ExecutionSpecsWrapper(TransitionTool): |
| 15 | + t8n_use_eels: bool = True |
| 16 | + detect_binary_pattern = "" |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + *, |
| 21 | + trace: bool = False, |
| 22 | + ): |
| 23 | + """Initialize the EELS Transition Tool interface.""" |
| 24 | + super().__init__( |
| 25 | + exception_mapper=ExecutionSpecsExceptionMapper(), trace=trace |
| 26 | + ) |
| 27 | + |
| 28 | + def version(self): |
| 29 | + return ethereum.__version__ |
| 30 | + |
| 31 | + def _evaluate_custom_t8n(self, t8n_data, debug_output_path): |
| 32 | + request_data = t8n_data.get_request_data() |
| 33 | + request_data_json = request_data.model_dump(mode="json", **model_dump_config) |
| 34 | + |
| 35 | + t8n_args = [ |
| 36 | + "t8n", |
| 37 | + "--input.alloc=stdin", |
| 38 | + "--input.env=stdin", |
| 39 | + "--input.txs=stdin", |
| 40 | + "--output.result=stdout", |
| 41 | + "--output.body=stdout", |
| 42 | + "--output.alloc=stdout", |
| 43 | + f"--state.fork={request_data_json['state']['fork']}", |
| 44 | + f"--state.chainid={request_data_json['state']['chainid']}", |
| 45 | + f"--state.reward={request_data_json['state']['reward']}", |
| 46 | + ] |
| 47 | + |
| 48 | + if t8n_data.state_test: |
| 49 | + t8n_args.append("--state-test") |
| 50 | + |
| 51 | + temp_dir = tempfile.TemporaryDirectory() |
| 52 | + if self.trace: |
| 53 | + t8n_args.extend( |
| 54 | + [ |
| 55 | + "--trace", |
| 56 | + "--trace.memory", |
| 57 | + "--trace.returndata", |
| 58 | + f"--output.basedir={temp_dir.name}", |
| 59 | + ] |
| 60 | + ) |
| 61 | + |
| 62 | + parser = create_parser() |
| 63 | + t8n_options = parser.parse_args(t8n_args) |
| 64 | + |
| 65 | + out_stream = StringIO() |
| 66 | + |
| 67 | + in_stream = StringIO(json.dumps(request_data_json["input"])) |
| 68 | + |
| 69 | + t8n = T8N(t8n_options, out_stream, in_stream) |
| 70 | + t8n.run() |
| 71 | + |
| 72 | + output_dict = json.loads(out_stream.getvalue()) |
| 73 | + output: TransitionToolOutput = TransitionToolOutput.model_validate( |
| 74 | + output_dict, context={"exception_mapper": self.exception_mapper} |
| 75 | + ) |
| 76 | + |
| 77 | + if debug_output_path: |
| 78 | + dump_files_to_directory( |
| 79 | + debug_output_path, |
| 80 | + { |
| 81 | + "input/alloc.json": request_data.input.alloc, |
| 82 | + "input/env.json": request_data.input.env, |
| 83 | + "input/txs.json": [ |
| 84 | + tx.model_dump(mode="json", **model_dump_config) |
| 85 | + for tx in request_data.input.txs |
| 86 | + ], |
| 87 | + }, |
| 88 | + ) |
| 89 | + |
| 90 | + dump_files_to_directory( |
| 91 | + debug_output_path, |
| 92 | + { |
| 93 | + "output/alloc.json": output.alloc, |
| 94 | + "output/result.json": output.result, |
| 95 | + }, |
| 96 | + ) |
| 97 | + |
| 98 | + if self.trace: |
| 99 | + self.collect_traces(output.result.receipts, temp_dir, debug_output_path) |
| 100 | + temp_dir.cleanup() |
| 101 | + |
| 102 | + return output |
| 103 | + |
0 commit comments