-
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
Open
prozacchiwawa
wants to merge
13
commits into
Chia-Network:main
Choose a base branch
from
prozacchiwawa:20210630-dialect-with-debug-freeze
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
My take on dialects, which I think is now uniform in interface with the run_program from stage_0 #98
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
46aed1e
First crack at `Dialect`.#
richardkiss 6a1c172
Improvements
richardkiss 29b2b2f
Tweak dialect api.
richardkiss 788b2d4
Tweak `Dialect` so it works with rust version.
richardkiss 76a5562
All tests passing with python backend.
richardkiss 5f87760
Add `to_python` and remove `DialectInfo`.
richardkiss 204c312
Improve `Dialect` stuff.
richardkiss fe9a243
My take on dialects, which I think is now uniform in interface with t…
prozacchiwawa 1665e20
Merge branch 'main' into 20210630-dialect-with-debug-freeze
prozacchiwawa 46c708f
Allow 'native' dialect to work
prozacchiwawa 36a9624
Merge
prozacchiwawa 30dd2da
format with black
prozacchiwawa d5bf131
use KEYWORD_TO_ATOM instead of a constant for sha256
prozacchiwawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
"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", | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 build this table algorithmically from
OP_REWRITE
?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.
Or vice-versa?
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.
I'm not sure this constant is actually Chia dialect-specific.
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.
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.