Skip to content

Commit

Permalink
Add --traces support to besu
Browse files Browse the repository at this point in the history
Add support for adding traces to output.
  • Loading branch information
shemnon committed Apr 16, 2024
1 parent 3a6520e commit 13c155d
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/evm_transition_tool/besu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"""

import json
import os
import re
import subprocess
import tempfile
import textwrap
from pathlib import Path
from re import compile
Expand All @@ -30,6 +32,7 @@ class BesuTransitionTool(TransitionTool):
trace: bool
process: Optional[subprocess.Popen] = None
server_url: str
besu_trace_dir: Optional[tempfile.TemporaryDirectory]

def __init__(
self,
Expand All @@ -46,17 +49,24 @@ def __init__(
except Exception as e:
raise Exception(f"Unexpected exception calling evm tool: {e}.")
self.help_string = result.stdout
self.besu_trace_dir = tempfile.TemporaryDirectory() if self.trace else None

def start_server(self):
"""
Starts the t8n-server process, extracts the port, and leaves it running for future re-use.
"""
args = [
str(self.binary),
"t8n-server",
"--port=0", # OS assigned server port
]

if self.trace:
args.append("--trace")
args.append(f"--output.basedir={self.besu_trace_dir.name}")

self.process = subprocess.Popen(
args=[
str(self.binary),
"t8n-server",
"--port=0", # OS assigned server port
],
args=args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
Expand All @@ -67,7 +77,7 @@ def start_server(self):
if not line or "Failed to start transition server" in line:
raise Exception("Failed starting Besu subprocess\n" + line)
if "Transition server listening on" in line:
port = re.search("Transition server listening on ([0-9]+)", line).group(1)
port = re.search("Transition server listening on (\\d+)", line).group(1)
self.server_url = f"http://localhost:{port}/"
break

Expand All @@ -77,6 +87,8 @@ def shutdown(self):
"""
if self.process:
self.process.kill()
if self.besu_trace_dir:
self.besu_trace_dir.cleanup()

def evaluate(
self,
Expand All @@ -98,9 +110,6 @@ def evaluate(
if eips is not None:
fork_name = "+".join([fork_name] + [str(eip) for eip in eips])

if self.trace:
raise Exception("Besu `t8n-server` does not support tracing.")

input_json = {
"alloc": alloc,
"txs": txs,
Expand Down Expand Up @@ -175,6 +184,14 @@ def evaluate(
},
)

if self.trace and self.besu_trace_dir:
self.collect_traces(
output["result"]["receipts"], self.besu_trace_dir, debug_output_path
)
for i, r in enumerate(output["result"]["receipts"]):
trace_file_name = f"trace-{i}-{r['transactionHash']}.jsonl"
os.remove(os.path.join(self.besu_trace_dir.name, trace_file_name))

return output

def is_fork_supported(self, fork: Fork) -> bool:
Expand Down

0 comments on commit 13c155d

Please sign in to comment.