Skip to content

Commit

Permalink
[CHIA-902] default flags for Program.run() and `Program.run_with_co…
Browse files Browse the repository at this point in the history
…st()` (#18287)

make plain Program.run() and Program.run_with_cost() default to enabling all the most recent features, and to disallow unknown opcodes (i.e. strict mode). To have full control of which features are enabled, introduce a run2() function
  • Loading branch information
arvidn authored Jul 12, 2024
1 parent 204b518 commit 2ff9c87
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
18 changes: 18 additions & 0 deletions chia/_tests/clvm/test_program.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import pytest
from chia_rs import ENABLE_FIXED_DIV
from clvm.EvalError import EvalError
from clvm.operators import KEYWORD_TO_ATOM
from clvm_tools.binutils import assemble, disassemble
Expand Down Expand Up @@ -108,3 +109,20 @@ def test_uncurry_args_garbage():
# there's garbage at the end of the args list
plus = Program.to(assemble("(2 (q . 1) (c (q . 1) (q . 1) (q . 0x1337)))"))
assert plus.uncurry() == (plus, Program.to(0))


def test_run() -> None:
div = Program.to(assemble("(/ 2 5)"))
ret = div.run([10, 5])
assert ret.atom == bytes([2])

ret = div.run([10, -5])
assert ret.atom == bytes([0xFE])

with pytest.raises(ValueError, match="div operator with negative operands is deprecated"):
cost, ret = div.run_with_flags(100000, 0, [10, -5])

cost, ret = div.run_with_flags(100000, ENABLE_FIXED_DIV, [10, -5])
assert cost == 1107
print(ret)
assert ret.atom == bytes([0xFE])
29 changes: 27 additions & 2 deletions chia/types/blockchain_format/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
import io
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Set, Tuple, Type, TypeVar

from chia_rs import ALLOW_BACKREFS, run_chia_program, tree_hash
from chia_rs import (
AGG_SIG_ARGS,
ALLOW_BACKREFS,
DISALLOW_INFINITY_G1,
ENABLE_BLS_OPS_OUTSIDE_GUARD,
ENABLE_FIXED_DIV,
ENABLE_MESSAGE_CONDITIONS,
ENABLE_SOFTFORK_CONDITION,
MEMPOOL_MODE,
run_chia_program,
tree_hash,
)
from clvm.casts import int_from_bytes
from clvm.CLVMObject import CLVMStorage
from clvm.EvalError import EvalError
Expand Down Expand Up @@ -129,12 +140,26 @@ def _run(self, max_cost: int, flags: int, args: Any) -> Tuple[int, Program]:
return cost, Program.to(r)

def run_with_cost(self, max_cost: int, args: Any) -> Tuple[int, Program]:
return self._run(max_cost, 0, args)
# when running puzzles in the wallet, default to enabling all soft-forks
# as well as enabling mempool-mode (i.e. strict mode)
default_flags = (
ENABLE_SOFTFORK_CONDITION
| ENABLE_BLS_OPS_OUTSIDE_GUARD
| ENABLE_FIXED_DIV
| AGG_SIG_ARGS
| ENABLE_MESSAGE_CONDITIONS
| DISALLOW_INFINITY_G1
| MEMPOOL_MODE
)
return self._run(max_cost, default_flags, args)

def run(self, args: Any) -> Program:
cost, r = self.run_with_cost(INFINITE_COST, args)
return r

def run_with_flags(self, max_cost: int, flags: int, args: Any) -> Tuple[int, Program]:
return self._run(max_cost, flags, args)

# Replicates the curry function from clvm_tools, taking advantage of *args
# being a list. We iterate through args in reverse building the code to
# create a clvm list.
Expand Down

0 comments on commit 2ff9c87

Please sign in to comment.