From 8d77b74eb1f56bffcf61af13a413a60c8b4aad8c Mon Sep 17 00:00:00 2001 From: arty Date: Wed, 30 Jun 2021 11:29:53 -0700 Subject: [PATCH 1/3] Run a dialect in non-rust mode rather than assuming a lot via the older (it seems) stage_0 interface. these now work compatibly --- clvm_tools/cmds.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/clvm_tools/cmds.py b/clvm_tools/cmds.py index dc8cd65..2b3d038 100644 --- a/clvm_tools/cmds.py +++ b/clvm_tools/cmds.py @@ -6,7 +6,7 @@ import sys import time -from clvm import to_sexp_f, KEYWORD_FROM_ATOM, KEYWORD_TO_ATOM, SExp +from clvm import to_sexp_f, KEYWORD_FROM_ATOM, KEYWORD_TO_ATOM, SExp, dialect_factories from clvm.EvalError import EvalError from clvm.serialize import sexp_from_stream, sexp_to_stream from clvm.operators import OP_REWRITE @@ -234,10 +234,8 @@ def launch_tool(args, tool_name, default_stage=0): use_rust = ( (tool_name != "run") and not pre_eval_f - and ( - args.backend == "rust" - or (deserialize_and_run_program and args.backend != "python") - ) + and (args.backend != "python" and args.backend != "debug") + and deserialize_and_run_program ) max_cost = max(0, args.max_cost - cost_offset if args.max_cost != 0 else 0) if use_rust: @@ -271,8 +269,22 @@ def launch_tool(args, tool_name, default_stage=0): input_sexp = sexp_from_stream(io.BytesIO(input_serialized), to_sexp_f) time_parse_input = time.perf_counter() - cost, result = run_program( - run_script, input_sexp, max_cost=max_cost, pre_eval_f=pre_eval_f, strict=args.strict) + backend = args.backend if args.backend is not None else "python" + dialect = dialect_factories[backend]( + KEYWORD_TO_ATOM["q"], + KEYWORD_TO_ATOM["a"], + args.strict, + to_sexp_f + ) + + def tracer(value,result): + print(f'sha256 with {result} came from {value}') + + dialect.configure(sha256_tracer=tracer) + + cost, result = dialect.run_program( + run_script, input_sexp, max_cost=max_cost, pre_eval_f=pre_eval_f) + time_done = time.perf_counter() if args.cost: cost += cost_offset if cost > 0 else 0 From c174b34f0df158e9b59eb2100eb40f7a1843a399 Mon Sep 17 00:00:00 2001 From: arty Date: Wed, 30 Jun 2021 22:38:10 -0700 Subject: [PATCH 2/3] Make a nice trace format --- clvm_tools/cmds.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/clvm_tools/cmds.py b/clvm_tools/cmds.py index 2b3d038..6a10cec 100644 --- a/clvm_tools/cmds.py +++ b/clvm_tools/cmds.py @@ -278,7 +278,18 @@ def launch_tool(args, tool_name, default_stage=0): ) def tracer(value,result): - print(f'sha256 with {result} came from {value}') + import json + try: + with open('sha256.trace','a') as f: + row_inputs = [x.hex() for x in value.as_python()] + row = { + 'hash': str(result)[2:], + 'inputs': row_inputs + } + f.write(json.dumps(row)) + f.write('\n') + except: + sys.stderr.write('warning: error writing sha256 trace\n') dialect.configure(sha256_tracer=tracer) From 91e9105e3f7cb30b3ff603c3fb5e331d17f46be1 Mon Sep 17 00:00:00 2001 From: arty Date: Thu, 22 Jul 2021 11:45:37 -0700 Subject: [PATCH 3/3] Remove special case for rust --- clvm_tools/cmds.py | 67 ++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 47 deletions(-) diff --git a/clvm_tools/cmds.py b/clvm_tools/cmds.py index de13583..1c0d87d 100644 --- a/clvm_tools/cmds.py +++ b/clvm_tools/cmds.py @@ -166,6 +166,10 @@ def launch_tool(args, tool_name, default_stage=0): action="append", default=[], ) + parser.add_argument( + "--sha256-trace", action="store_true", + help="Leave a sha256 trace behind, showing the sources of sha256 hashes" + ) parser.add_argument( "path_or_code", type=path_or_code, help="filepath to clvm script, or a literal script") @@ -231,52 +235,20 @@ def launch_tool(args, tool_name, default_stage=0): try: output = "(didn't finish)" - use_rust = ( - (tool_name != "run") - and not pre_eval_f - and (args.backend != "python" and args.backend != "debug") - and deserialize_and_run_program - ) max_cost = max(0, args.max_cost - cost_offset if args.max_cost != 0 else 0) - if use_rust: - if input_serialized is None: - input_serialized = input_sexp.as_bin() - - run_script = run_script.as_bin() - time_parse_input = time.perf_counter() - - # build the opcode look-up table - # this should eventually be subsumed by "Dialect" api - - native_opcode_names_by_opcode = dict( - ("op_%s" % OP_REWRITE.get(k, k), op) - for op, k in KEYWORD_FROM_ATOM.items() - if k not in "qa." - ) - cost, result = deserialize_and_run_program2( - run_script, - input_serialized, - KEYWORD_TO_ATOM["q"][0], - KEYWORD_TO_ATOM["a"][0], - native_opcode_names_by_opcode, - max_cost, - STRICT_MODE if args.strict else 0, - ) - time_done = time.perf_counter() - result = SExp.to(result) - else: - if input_sexp is None: - input_sexp = sexp_from_stream(io.BytesIO(input_serialized), to_sexp_f) - - time_parse_input = time.perf_counter() - backend = args.backend if args.backend is not None else "python" - dialect = dialect_factories[backend]( - KEYWORD_TO_ATOM["q"], - KEYWORD_TO_ATOM["a"], - args.strict, - to_sexp_f - ) + if input_sexp is None: + input_sexp = sexp_from_stream(io.BytesIO(input_serialized), to_sexp_f) + + time_parse_input = time.perf_counter() + backend = args.backend if args.backend is not None else "python" + dialect = dialect_factories[backend]( + KEYWORD_TO_ATOM["q"], + KEYWORD_TO_ATOM["a"], + args.strict, + to_sexp_f + ) + if args.sha256_trace: def tracer(value,result): import json try: @@ -293,10 +265,11 @@ def tracer(value,result): dialect.configure(sha256_tracer=tracer) - cost, result = dialect.run_program( - run_script, input_sexp, max_cost=max_cost, pre_eval_f=pre_eval_f) + cost, result = dialect.run_program( + run_script, input_sexp, max_cost=max_cost, pre_eval_f=pre_eval_f) + + time_done = time.perf_counter() - time_done = time.perf_counter() if args.cost: cost += cost_offset if cost > 0 else 0 print("cost = %d" % cost)