Skip to content
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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
5 changes: 4 additions & 1 deletion clvm/CLVMObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def __new__(class_, v):
self = super(CLVMObject, class_).__new__(class_)
if isinstance(v, tuple):
if len(v) != 2:
raise ValueError("tuples must be of size 2, cannot create CLVMObject from: %s" % str(v))
raise ValueError(
"tuples must be of size 2, cannot create CLVMObject from: %s"
% str(v)
)
self.pair = v
self.atom = None
else:
Expand Down
1 change: 1 addition & 0 deletions clvm/SExp.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class SExp:
elements implementing the CLVM object protocol.
Exactly one of "atom" and "pair" must be None.
"""

true: "SExp"
false: "SExp"
__null__: "SExp"
Expand Down
4 changes: 3 additions & 1 deletion clvm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .SExp import SExp
from .dialect import Dialect
from .chia_dialect import dialect_factories # noqa
from .operators import ( # noqa
QUOTE_ATOM,
QUOTE_ATOM, # deprecated
KEYWORD_TO_ATOM,
KEYWORD_FROM_ATOM,
)
Expand Down
27 changes: 27 additions & 0 deletions clvm/chainable_multi_op_fn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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)
102 changes: 102 additions & 0 deletions clvm/chia_dialect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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")
sha256_op = KEYWORD_TO_ATOM["sha256"]
table = {sha256_op: d.do_sha256_with_trace(std_op_table[sha256_op])}
d.update(table)

return d


dialect_factories = {
"python": chia_python_new_dialect,
"native": native_new_dialect,
"debug": debug_new_dialect,
}
37 changes: 37 additions & 0 deletions clvm/chia_dialect_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you build this table algorithmically from OP_REWRITE?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or vice-versa?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this constant is actually Chia dialect-specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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",
}
Loading