-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My take on dialects, which I think is now uniform in interface with the run_program from stage_0 #98
base: main
Are you sure you want to change the base?
My take on dialects, which I think is now uniform in interface with the run_program from stage_0 #98
Changes from 10 commits
46aed1e
6a1c172
29b2b2f
788b2d4
76a5562
5f87760
204c312
fe9a243
1665e20
46c708f
36a9624
30dd2da
d5bf131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional, Tuple | ||
|
||
from .types import CLVMObjectType, MultiOpFn, OperatorDict | ||
|
||
|
||
@dataclass | ||
class ChainableMultiOpFn: | ||
""" | ||
This structure handles clvm operators. Given an atom, it looks it up in a `dict`, then | ||
falls back to calling `unknown_op_handler`. | ||
""" | ||
op_lookup: OperatorDict | ||
unknown_op_handler: MultiOpFn | ||
|
||
def __call__( | ||
self, op: bytes, arguments: CLVMObjectType, max_cost: Optional[int] = None | ||
) -> Tuple[int, CLVMObjectType]: | ||
f = self.op_lookup.get(op) | ||
if f: | ||
try: | ||
return f(arguments) | ||
except TypeError: | ||
# some operators require `max_cost` | ||
return f(arguments, max_cost) | ||
return self.unknown_op_handler(op, arguments, max_cost) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from .SExp import SExp | ||
from .casts import int_to_bytes | ||
from .types import CLVMObjectType, ConversionFn, MultiOpFn, OperatorDict | ||
from .chainable_multi_op_fn import ChainableMultiOpFn | ||
from .handle_unknown_op import ( | ||
handle_unknown_op_softfork_ready, | ||
handle_unknown_op_strict, | ||
) | ||
from .dialect import ConversionFn, Dialect, new_dialect, opcode_table_for_backend, python_new_dialect, native_new_dialect | ||
from .chia_dialect_constants import KEYWORDS, KEYWORD_FROM_ATOM, KEYWORD_TO_ATOM # noqa | ||
from .operators import OPERATOR_LOOKUP | ||
|
||
def configure_chia_dialect(dialect: Dialect, backend=None) -> Dialect: | ||
quote_kw = KEYWORD_TO_ATOM["q"] | ||
apply_kw = KEYWORD_TO_ATOM["a"] | ||
table = opcode_table_for_backend(KEYWORD_TO_ATOM, backend=backend) | ||
dialect.update(table) | ||
return dialect | ||
|
||
|
||
def chia_dialect(strict: bool, to_python: ConversionFn, backend=None) -> Dialect: | ||
dialect = new_dialect(quote_kw, apply_kw, strict, to_python, backend=backend) | ||
return configure_chia_dialect(dialect, backend) | ||
|
||
class DebugDialect(Dialect): | ||
def __init__( | ||
self, | ||
quote_kw: bytes, | ||
apply_kw: bytes, | ||
multi_op_fn: MultiOpFn, | ||
to_python: ConversionFn, | ||
): | ||
super().__init__(quote_kw, apply_kw, multi_op_fn, to_python) | ||
self.tracer = lambda x,y: None | ||
|
||
def do_sha256_with_trace(self,prev): | ||
def _run(value,max_cost=None): | ||
try: | ||
cost, result = prev(value) | ||
except TypeError: | ||
cost, result = prev(value,max_cost) | ||
|
||
self.tracer(value,result) | ||
return cost, result | ||
|
||
return _run | ||
|
||
def configure(self,**kwargs): | ||
if 'sha256_tracer' in kwargs: | ||
self.tracer = kwargs['sha256_tracer'] | ||
|
||
|
||
def chia_python_new_dialect( | ||
quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn, | ||
backend="python" | ||
) -> Dialect: | ||
unknown_op_callback = ( | ||
handle_unknown_op_strict if strict else handle_unknown_op_softfork_ready | ||
) | ||
|
||
# Setup as a chia style clvm provider giving the chia operators. | ||
return configure_chia_dialect( | ||
DebugDialect(quote_kw,apply_kw,OPERATOR_LOOKUP,to_python), | ||
backend | ||
) | ||
|
||
|
||
# Dialect that can allow acausal tracing of sha256 hashes. | ||
def debug_new_dialect( | ||
quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn, | ||
backend="python" | ||
) -> Dialect: | ||
d = chia_python_new_dialect(quote_kw, apply_kw, strict, to_python, backend) | ||
|
||
# Override operators we want to track. | ||
std_op_table = opcode_table_for_backend(KEYWORD_TO_ATOM, backend="python") | ||
table = { b'\x0b': d.do_sha256_with_trace(std_op_table[b'\x0b']) } | ||
prozacchiwawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
d.update(table) | ||
|
||
return d | ||
|
||
dialect_factories = { | ||
'python': chia_python_new_dialect, | ||
'native': native_new_dialect, | ||
'debug': debug_new_dialect, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from .casts import int_to_bytes | ||
|
||
KEYWORDS = ( | ||
# core opcodes 0x01-x08 | ||
". q a i c f r l x " | ||
|
||
# opcodes on atoms as strings 0x09-0x0f | ||
"= >s sha256 substr strlen concat . " | ||
|
||
# opcodes on atoms as ints 0x10-0x17 | ||
"+ - * / divmod > ash lsh " | ||
|
||
# opcodes on atoms as vectors of bools 0x18-0x1c | ||
"logand logior logxor lognot . " | ||
|
||
# opcodes for bls 1381 0x1d-0x1f | ||
"point_add pubkey_for_exp . " | ||
|
||
# bool opcodes 0x20-0x23 | ||
"not any all . " | ||
|
||
# misc 0x24 | ||
"softfork " | ||
).split() | ||
|
||
KEYWORD_FROM_ATOM = {int_to_bytes(k): v for k, v in enumerate(KEYWORDS)} | ||
KEYWORD_TO_ATOM = {v: k for k, v in KEYWORD_FROM_ATOM.items()} | ||
|
||
KEYWORD_TO_LONG_KEYWORD = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you build this table algorithmically from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or vice-versa? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this constant is actually Chia dialect-specific. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I included it there mainly to avoid another layer when resolving dependencies, but it does make sense to put this on a layer in between. |
||
"i": "op_if", | ||
"c": "op_cons", | ||
"f": "op_first", | ||
"r": "op_rest", | ||
"l": "op_listp", | ||
"x": "op_raise", | ||
"=": "op_eq", | ||
"+": "op_add", | ||
"-": "op_subtract", | ||
"*": "op_multiply", | ||
"/": "op_divmod", | ||
">": "op_gr", | ||
">s": "op_gr_bytes", | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
from typing import Callable, Optional, Tuple | ||
from .SExp import SExp | ||
try: | ||
import clvm_rs | ||
except ImportError: | ||
clvm_rs = None | ||
|
||
import io | ||
from . import core_ops, more_ops | ||
from .chainable_multi_op_fn import ChainableMultiOpFn | ||
from .handle_unknown_op import ( | ||
handle_unknown_op_softfork_ready, | ||
handle_unknown_op_strict, | ||
) | ||
from .run_program import _run_program | ||
from .types import CLVMObjectType, ConversionFn, MultiOpFn, OperatorDict | ||
from clvm.serialize import sexp_from_stream, sexp_to_stream | ||
from .chia_dialect_constants import KEYWORD_FROM_ATOM, KEYWORD_TO_LONG_KEYWORD | ||
|
||
|
||
OP_REWRITE = { | ||
"+": "add", | ||
"-": "subtract", | ||
"*": "multiply", | ||
"/": "div", | ||
"i": "if", | ||
"c": "cons", | ||
"f": "first", | ||
"r": "rest", | ||
"l": "listp", | ||
"x": "raise", | ||
"=": "eq", | ||
">": "gr", | ||
">s": "gr_bytes", | ||
} | ||
|
||
|
||
def op_table_for_module(mod): | ||
return {k: v for k, v in mod.__dict__.items() if k.startswith("op_")} | ||
|
||
|
||
def op_imp_table_for_backend(backend): | ||
if backend is None and clvm_rs: | ||
backend = "native" | ||
|
||
if backend == "native": | ||
if clvm_rs is None: | ||
raise RuntimeError("native backend not installed") | ||
return clvm_rs.native_opcodes_dict() | ||
|
||
table = {} | ||
table.update(op_table_for_module(core_ops)) | ||
table.update(op_table_for_module(more_ops)) | ||
return table | ||
|
||
|
||
def op_atom_to_imp_table(op_imp_table, keyword_to_atom, op_rewrite=OP_REWRITE): | ||
op_atom_to_imp_table = {} | ||
for op, bytecode in keyword_to_atom.items(): | ||
op_name = "op_%s" % op_rewrite.get(op, op) | ||
op_f = op_imp_table.get(op_name) | ||
if op_f: | ||
op_atom_to_imp_table[bytecode] = op_f | ||
return op_atom_to_imp_table | ||
|
||
|
||
def opcode_table_for_backend(keyword_to_atom, backend): | ||
op_imp_table = op_imp_table_for_backend(backend) | ||
return op_atom_to_imp_table(op_imp_table, keyword_to_atom) | ||
|
||
|
||
class Dialect: | ||
def __init__( | ||
self, | ||
quote_kw: bytes, | ||
apply_kw: bytes, | ||
multi_op_fn: MultiOpFn, | ||
to_python: ConversionFn, | ||
): | ||
self.quote_kw = quote_kw | ||
self.apply_kw = apply_kw | ||
self.opcode_lookup = dict() | ||
self.multi_op_fn = ChainableMultiOpFn(self.opcode_lookup, multi_op_fn) | ||
self.to_python = to_python | ||
|
||
def configure(self, **kwargs): | ||
pass | ||
|
||
def update(self, d: OperatorDict) -> None: | ||
self.opcode_lookup.update(d) | ||
|
||
def clear(self) -> None: | ||
self.opcode_lookup.clear() | ||
|
||
def run_program( | ||
self, | ||
program: CLVMObjectType, | ||
env: CLVMObjectType, | ||
max_cost: int, | ||
pre_eval_f: Optional[ | ||
Callable[[CLVMObjectType, CLVMObjectType], Tuple[int, CLVMObjectType]] | ||
] = None, | ||
) -> Tuple[int, CLVMObjectType]: | ||
cost, r = _run_program( | ||
program, | ||
env, | ||
self.multi_op_fn, | ||
self.quote_kw, | ||
self.apply_kw, | ||
max_cost, | ||
pre_eval_f, | ||
) | ||
return cost, self.to_python(r) | ||
|
||
|
||
class NativeDialect: | ||
def __init__( | ||
self, | ||
quote_kw: bytes, | ||
apply_kw: bytes, | ||
multi_op_fn: MultiOpFn, | ||
to_python: ConversionFn, | ||
): | ||
native_dict = clvm_rs.native_opcodes_dict() | ||
def get_native_op_for_kw(op, k): | ||
kw = KEYWORD_TO_LONG_KEYWORD[k] if k in KEYWORD_TO_LONG_KEYWORD else "op_%s" % k | ||
return (op, native_dict[kw]) | ||
|
||
native_opcode_names_by_opcode = dict( | ||
get_native_op_for_kw(op, k) | ||
for op, k in KEYWORD_FROM_ATOM.items() | ||
if k not in "qa." | ||
) | ||
|
||
self.quote_kw = quote_kw | ||
self.apply_kw = apply_kw | ||
self.to_python = to_python | ||
self.callbacks = multi_op_fn | ||
self.held = clvm_rs.Dialect( | ||
quote_kw, | ||
apply_kw, | ||
multi_op_fn, | ||
to_python | ||
) | ||
|
||
self.held.update(native_opcode_names_by_opcode) | ||
|
||
|
||
def update(self,d): | ||
return self.held.update(d) | ||
|
||
|
||
def clear(self) -> None: | ||
return self.held.clear() | ||
|
||
|
||
def run_program( | ||
self, | ||
program: CLVMObjectType, | ||
env: CLVMObjectType, | ||
max_cost: int, | ||
pre_eval_f: Optional[ | ||
Callable[[CLVMObjectType, CLVMObjectType], Tuple[int, CLVMObjectType]] | ||
] = None, | ||
) -> Tuple[int, CLVMObjectType]: | ||
prog = io.BytesIO() | ||
e = io.BytesIO() | ||
sexp_to_stream(program, prog) | ||
sexp_to_stream(env, e) | ||
|
||
return self.held.deserialize_and_run_program( | ||
prog.getvalue(), | ||
e.getvalue(), | ||
max_cost, | ||
pre_eval_f | ||
) | ||
|
||
def configure(self,**kwargs): | ||
pass | ||
|
||
|
||
def native_new_dialect( | ||
quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn | ||
) -> Dialect: | ||
unknown_op_callback = ( | ||
clvm_rs.NATIVE_OP_UNKNOWN_STRICT | ||
if strict | ||
else clvm_rs.NATIVE_OP_UNKNOWN_NON_STRICT | ||
) | ||
|
||
dialect = NativeDialect( | ||
quote_kw, | ||
apply_kw, | ||
unknown_op_callback, | ||
to_python=to_python, | ||
) | ||
return dialect | ||
|
||
|
||
def python_new_dialect( | ||
quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn | ||
) -> Dialect: | ||
unknown_op_callback = ( | ||
handle_unknown_op_strict if strict else handle_unknown_op_softfork_ready | ||
) | ||
|
||
dialect = Dialect( | ||
quote_kw, | ||
apply_kw, | ||
unknown_op_callback, | ||
to_python=to_python, | ||
) | ||
return dialect | ||
|
||
|
||
def new_dialect(quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn, backend=None): | ||
if backend is None: | ||
backend = "python" if clvm_rs is None else "native" | ||
backend_f = native_new_dialect if backend == "native" else python_new_dialect | ||
return backend_f(quote_kw, apply_kw, strict, to_python) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you run
black
on all the.py
files to standardize formatting?