From c63823605d16927ee52052e5f8a6ba53a6b15e85 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 26 Apr 2023 14:33:40 +0200 Subject: [PATCH 1/2] tracer: make host:port configurable We make --host and --port new CLI arguments, such that they can be specified at startup. To ensure backwards compatibility, we default to the previous values localhost:8100 if the flags are not provided. --- src/starkware/cairo/lang/tracer/tracer.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/starkware/cairo/lang/tracer/tracer.py b/src/starkware/cairo/lang/tracer/tracer.py index 76b4aa20..ef091d4f 100755 --- a/src/starkware/cairo/lang/tracer/tracer.py +++ b/src/starkware/cairo/lang/tracer/tracer.py @@ -26,6 +26,7 @@ def trace_runner(runner): trace = runner.relocated_trace run_tracer( + "localhost", 8100, TracerData( program=runner.program, memory=memory, @@ -41,7 +42,7 @@ def server_bind(self): self.socket.bind(self.server_address) -def run_tracer(tracer_data: TracerData): +def run_tracer(host: str, port: int, tracer_data: TracerData): # Change directory for the SimpleHTTPRequestHandler. os.chdir(os.path.abspath(os.path.dirname(__file__))) @@ -92,18 +93,17 @@ def write_json(self, json_obj): # Request was canceled. pass - def start_server(): - port = 8100 + def start_server(host: str, port: int): while True: try: - return SimpleTCPServer(("localhost", port), Handler) + return SimpleTCPServer((host, port), Handler) except OSError: pass # port was not available. Try the next one. port += 1 - httpd = start_server() - print("Running tracer on http://localhost:%d/" % httpd.server_address[1]) + httpd = start_server(host, port) + print(f"Running tracer on http://{httpd.server_address[0]}:{httpd.server_address[1]}/") print() httpd.serve_forever() @@ -117,6 +117,8 @@ def main(): ) parser.add_argument("--memory", type=str, required=True, help="A path to the memory file.") parser.add_argument("--trace", type=str, required=True, help="A path to the trace file.") + parser.add_argument("--host", default="localhost", type=str, help="Host to serve on.") + parser.add_argument("--port", default="8100", type=int, help="Port to serve on.") parser.add_argument("--air_public_input", type=str, help="A path to the AIR public input file.") parser.add_argument("--debug_info", type=str, help="A path to the run time debug info file.") @@ -130,7 +132,7 @@ def main(): debug_info_path=args.debug_info, ) - run_tracer(tracer_data) + run_tracer(args.host, args.port, tracer_data) return 0 From d0b9cc6ed403f30900d6e45f819e02dff6d06820 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 26 Apr 2023 14:36:12 +0200 Subject: [PATCH 2/2] cairo-run: make tracer host:port configurable We make the --tracer flag take an optional argument, letting us configure the host:port to serve on at startup. We default to localhost:8100 if the optional argument is not set, and as previously don't start the tracer in case the --tracer flag is not set at all. --- src/starkware/cairo/lang/vm/cairo_run.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/starkware/cairo/lang/vm/cairo_run.py b/src/starkware/cairo/lang/vm/cairo_run.py index 7327802e..b453dd0f 100644 --- a/src/starkware/cairo/lang/vm/cairo_run.py +++ b/src/starkware/cairo/lang/vm/cairo_run.py @@ -148,7 +148,11 @@ def main(): default="plain", help="The layout of the Cairo AIR.", ) - parser.add_argument("--tracer", action="store_true", help="Run the tracer.") + parser.add_argument( + "--tracer", + nargs='?', + const="localhost:8100", + help="Run the tracer at the given host and port (default: localhost:8100.") parser.add_argument( "--profile_output", type=str, @@ -432,7 +436,12 @@ def cairo_run(args): debug_info_file=debug_info_file, debug_info=runner.get_relocated_debug_info() ) - if args.tracer: + if args.tracer is not None: + # Tracer set, split host and port. + splits = args.tracer.split(':') + host = splits[0] + port = int(splits[1]) + CAIRO_TRACER = "starkware.cairo.lang.tracer.tracer" subprocess.call( list( @@ -444,6 +453,8 @@ def cairo_run(args): CAIRO_TRACER, f"--program={args.program.name}", f"--trace={trace_file.name}", + f"--host={host}", + f"--port={port}", f"--memory={memory_file.name}", f"--air_public_input={args.air_public_input.name}" if args.air_public_input