Skip to content

Commit e25f141

Browse files
authored
New run function that doesn't import python code. (#246)
Only one `run` function, and it uses `ValueError` instead of `EvalError`.
1 parent fe1187e commit e25f141

File tree

7 files changed

+63
-146
lines changed

7 files changed

+63
-146
lines changed

benchmark/run-benchmark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import random
99
from clvm import KEYWORD_FROM_ATOM, KEYWORD_TO_ATOM
1010
from clvm.operators import OP_REWRITE
11-
from clvm_rs import run_chia_program
11+
from clvm_rs import run_serialized_chia_program
1212
from colorama import init, Fore, Style
1313

1414
init()
@@ -213,7 +213,7 @@ def need_update(file_path, mtime):
213213
env_data = bytes.fromhex(open(env_fn, 'r').read())
214214

215215
time_start = time.perf_counter()
216-
cost, result = run_chia_program(
216+
cost, result = run_serialized_chia_program(
217217
program_data,
218218
env_data,
219219
max_cost,

tests/run.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

3-
from clvm_rs import run_chia_program
3+
from clvm_rs import run_serialized_chia_program
4+
45

56
def run_clvm(fn, env=None):
67

@@ -15,13 +16,14 @@ def run_clvm(fn, env=None):
1516
cost_per_byte = 12000
1617

1718
max_cost -= (len(program_data) + len(env_data)) * cost_per_byte
18-
return run_chia_program(
19+
return run_serialized_chia_program(
1920
program_data,
2021
env_data,
2122
max_cost,
2223
0,
2324
)
2425

26+
2527
def count_tree_size(tree) -> int:
2628
stack = [tree]
2729
ret = 0
@@ -48,7 +50,7 @@ def count_tree_size(tree) -> int:
4850
print(f"cost: {cost}")
4951
print(f"execution time: {duration:.2f}s")
5052
except Exception as e:
51-
print("FAIL:", e)
53+
print("FAIL:", e.args[0])
5254
sys.exit(1)
5355
start = time()
5456
ret_size = count_tree_size(result)

wheel/src/adapt_response.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
1+
use std::rc::Rc;
2+
13
use crate::lazy_node::LazyNode;
24
use clvmr::allocator::Allocator;
3-
use clvmr::reduction::{EvalErr, Response};
4-
5-
use std::rc::Rc;
5+
use clvmr::reduction::Response;
66

7+
use pyo3::exceptions::PyValueError;
78
use pyo3::prelude::*;
8-
use pyo3::types::PyDict;
9+
use pyo3::types::PyTuple;
910

10-
pub fn eval_err_to_pyresult<T>(py: Python, eval_err: EvalErr, allocator: Allocator) -> PyResult<T> {
11-
let node = LazyNode::new(Rc::new(allocator), eval_err.0);
12-
let msg = eval_err.1;
13-
let ctx: &PyDict = PyDict::new(py);
14-
ctx.set_item("msg", msg)?;
15-
ctx.set_item("node", node)?;
16-
Err(py
17-
.run(
18-
"
19-
from clvm.EvalError import EvalError
20-
raise EvalError(msg, node)",
21-
None,
22-
Some(ctx),
23-
)
24-
.unwrap_err())
25-
}
26-
27-
pub fn adapt_response_to_py(
11+
pub fn adapt_response(
2812
py: Python,
2913
allocator: Allocator,
30-
r: Response,
14+
response: Response,
3115
) -> PyResult<(u64, LazyNode)> {
32-
match r {
16+
match response {
3317
Ok(reduction) => {
3418
let val = LazyNode::new(Rc::new(allocator), reduction.1);
3519
Ok((reduction.0, val))
3620
}
37-
Err(eval_err) => eval_err_to_pyresult(py, eval_err, allocator),
21+
Err(eval_err) => {
22+
let sexp = LazyNode::new(Rc::new(allocator), eval_err.0).to_object(py);
23+
let msg = eval_err.1.to_object(py);
24+
let tuple = PyTuple::new(py, [msg, sexp]);
25+
let value_error: PyErr = PyValueError::new_err(tuple.to_object(py));
26+
Err(value_error)
27+
}
3828
}
3929
}

wheel/src/api.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
1+
use super::lazy_node::LazyNode;
2+
use crate::adapt_response::adapt_response;
3+
use clvmr::allocator::Allocator;
4+
use clvmr::chia_dialect::ChiaDialect;
5+
use clvmr::cost::Cost;
6+
use clvmr::reduction::Response;
7+
use clvmr::run_program::run_program;
8+
use clvmr::serde::{node_from_bytes, serialized_length_from_bytes};
9+
use clvmr::{LIMIT_HEAP, LIMIT_STACK, MEMPOOL_MODE, NO_NEG_DIV, NO_UNKNOWN_OPS};
110
use pyo3::prelude::*;
211
use pyo3::wrap_pyfunction;
312

4-
use super::lazy_node::LazyNode;
5-
use super::run_program::{
6-
__pyo3_get_function_deserialize_and_run_program2, __pyo3_get_function_run_chia_program,
7-
__pyo3_get_function_serialized_length,
8-
};
9-
use clvmr::{LIMIT_HEAP, NO_NEG_DIV, NO_UNKNOWN_OPS, LIMIT_STACK, MEMPOOL_MODE};
13+
#[pyfunction]
14+
pub fn serialized_length(program: &[u8]) -> PyResult<u64> {
15+
Ok(serialized_length_from_bytes(program)?)
16+
}
17+
18+
#[pyfunction]
19+
pub fn run_serialized_chia_program(
20+
py: Python,
21+
program: &[u8],
22+
args: &[u8],
23+
max_cost: Cost,
24+
flags: u32,
25+
) -> PyResult<(u64, LazyNode)> {
26+
let mut allocator = if flags & LIMIT_HEAP != 0 {
27+
Allocator::new_limited(500000000, 62500000, 62500000)
28+
} else {
29+
Allocator::new()
30+
};
31+
32+
let r: Response = (|| -> PyResult<Response> {
33+
let program = node_from_bytes(&mut allocator, program)?;
34+
let args = node_from_bytes(&mut allocator, args)?;
35+
let dialect = ChiaDialect::new(flags);
36+
37+
Ok(py.allow_threads(|| run_program(&mut allocator, &dialect, program, args, max_cost)))
38+
})()?;
39+
adapt_response(py, allocator, r)
40+
}
1041

1142
#[pymodule]
1243
fn clvm_rs(_py: Python, m: &PyModule) -> PyResult<()> {
13-
m.add_function(wrap_pyfunction!(deserialize_and_run_program2, m)?)?;
14-
m.add_function(wrap_pyfunction!(run_chia_program, m)?)?;
44+
m.add_function(wrap_pyfunction!(run_serialized_chia_program, m)?)?;
45+
m.add_function(wrap_pyfunction!(serialized_length, m)?)?;
46+
1547
m.add("NO_NEG_DIV", NO_NEG_DIV)?;
1648
m.add("NO_UNKNOWN_OPS", NO_UNKNOWN_OPS)?;
1749
m.add("LIMIT_HEAP", LIMIT_HEAP)?;
1850
m.add("LIMIT_STACK", LIMIT_STACK)?;
1951
m.add("MEMPOOL_MODE", MEMPOOL_MODE)?;
2052
m.add_class::<LazyNode>()?;
2153

22-
m.add_function(wrap_pyfunction!(serialized_length, m)?)?;
23-
2454
Ok(())
2555
}

wheel/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod adapt_response;
1+
mod adapt_response;
22
pub mod api;
33
pub mod lazy_node;
4-
mod run_program;

wheel/src/mod.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

wheel/src/run_program.rs

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)