Skip to content

Commit

Permalink
Merge pull request #283 from OpShin/fix/deterministic_compilation
Browse files Browse the repository at this point in the history
Fix nondeterministic compilation
  • Loading branch information
nielstron authored Nov 1, 2023
2 parents b40b49a + eebc7ca commit 08bf254
Show file tree
Hide file tree
Showing 4 changed files with 395 additions and 327 deletions.
7 changes: 6 additions & 1 deletion opshin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def perform_command(args):
if command == Command.compile_pluto:
print(code.dumps())
return
code = pluthon.compile(code)
code = pluthon.compile(code, optimize_patterns=not args.no_optimize_patterns)

# apply parameters from the command line to the contract (instantiates parameterized contract!)
code = code.term
Expand Down Expand Up @@ -402,6 +402,11 @@ def parse_args():
action="store_true",
help="Enables the use of isinstance(x, D) in the contract where x is of type Anything. This is not recommended as it only checks the constructor id and not the actual type of the data.",
)
a.add_argument(
"--no-optimize-patterns",
action="store_true",
help="Disables the compression of re-occurring code patterns. Can reduce memory and CPU steps but increases the size of the compiled contract.",
)
a.add_argument(
"args",
nargs="*",
Expand Down
40 changes: 40 additions & 0 deletions opshin/tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import sys

import subprocess

import json
import tempfile
import xml.etree.ElementTree
Expand Down Expand Up @@ -2171,3 +2175,39 @@ def validator(
return d
"""
builder._compile(source_code)

def test_compilation_deterministic_local(self):
input_file = "examples/smart_contracts/assert_sum.py"
with open(input_file) as fp:
source_code = fp.read()
code = builder._compile(source_code)
for i in range(10):
code_2 = builder._compile(source_code)
self.assertEqual(code.dumps(), code_2.dumps())

def test_compilation_deterministic_external(self):
input_file = "examples/smart_contracts/assert_sum.py"
code = subprocess.run(
[
sys.executable,
"-m",
"opshin",
"compile",
"spending",
input_file,
],
capture_output=True,
)
for i in range(10):
code_2 = subprocess.run(
[
sys.executable,
"-m",
"opshin",
"compile",
"spending",
input_file,
],
capture_output=True,
)
self.assertEqual(code.stdout, code_2.stdout)
Loading

0 comments on commit 08bf254

Please sign in to comment.