From a96ba81a6479f2a7d9b703248678f847196520bc Mon Sep 17 00:00:00 2001 From: Richard Kiss Date: Fri, 12 Feb 2021 18:36:15 -0800 Subject: [PATCH] Add `deserialize_and_run_program`. --- src/py/api.rs | 10 ++++-- src/py/f_table.rs | 66 +++++++++++++++++++------------------- src/py/native_op_lookup.rs | 2 +- src/py/run_program.rs | 64 +++++++++++++++++++++++++++++++++++- 4 files changed, 104 insertions(+), 38 deletions(-) diff --git a/src/py/api.rs b/src/py/api.rs index e85ea235..f57e495e 100644 --- a/src/py/api.rs +++ b/src/py/api.rs @@ -9,7 +9,10 @@ use super::arc_allocator::ArcAllocator; use super::glue::{_py_run_program, _serialize_from_bytes, _serialize_to_bytes}; use super::native_op_lookup::GenericNativeOpLookup; use super::py_node::PyNode; -use super::run_program::{__pyo3_get_function_serialize_and_run_program, STRICT_MODE}; +use super::run_program::{ + __pyo3_get_function_deserialize_and_run_program, __pyo3_get_function_serialize_and_run_program, + STRICT_MODE, +}; type AllocatorT<'a> = ArcAllocator; type NodeClass = PyNode; @@ -22,7 +25,7 @@ pub struct NativeOpLookup { #[pymethods] impl NativeOpLookup { #[new] - fn new(opcode_lookup_by_name: HashMap, unknown_op_callback: PyObject) -> Self { + fn new(opcode_lookup_by_name: HashMap>, unknown_op_callback: PyObject) -> Self { NativeOpLookup::new_from_gnol(Box::new(GenericNativeOpLookup::new( opcode_lookup_by_name, unknown_op_callback, @@ -117,10 +120,11 @@ fn serialize_to_bytes(py: Python, sexp: &PyAny) -> PyResult { #[pymodule] fn clvm_rs(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(py_run_program, m)?)?; - m.add_function(wrap_pyfunction!(serialize_and_run_program, m)?)?; m.add_function(wrap_pyfunction!(serialize_from_bytes, m)?)?; m.add_function(wrap_pyfunction!(serialize_to_bytes, m)?)?; + m.add_function(wrap_pyfunction!(serialize_and_run_program, m)?)?; + m.add_function(wrap_pyfunction!(deserialize_and_run_program, m)?)?; m.add("STRICT_MODE", STRICT_MODE)?; m.add_class::()?; diff --git a/src/py/f_table.rs b/src/py/f_table.rs index 429d87e9..0ae11cc6 100644 --- a/src/py/f_table.rs +++ b/src/py/f_table.rs @@ -14,40 +14,40 @@ type OpFn = fn(&mut T, ::Ptr) -> Response<::P pub type FLookup = [Option>; 256]; pub fn opcode_by_name(name: &str) -> Option> { - let opcode_lookup: [(&str, OpFn); 30] = [ - ("i", op_if), - ("c", op_cons), - ("f", op_first), - ("r", op_rest), - ("l", op_listp), - ("x", op_raise), - ("=", op_eq), - ("sha256", op_sha256), - ("+", op_add), - ("-", op_subtract), - ("*", op_multiply), - ("divmod", op_divmod), - ("substr", op_substr), - ("strlen", op_strlen), - ("point_add", op_point_add), - ("pubkey_for_exp", op_pubkey_for_exp), - ("concat", op_concat), - (">", op_gr), - (">s", op_gr_bytes), - ("logand", op_logand), - ("logior", op_logior), - ("logxor", op_logxor), - ("lognot", op_lognot), - ("ash", op_ash), - ("lsh", op_lsh), - ("not", op_not), - ("any", op_any), - ("all", op_all), - ("softfork", op_softfork), - ("div", op_div), + let opcode_lookup: [(OpFn, &str); 30] = [ + (op_if, "op_if"), + (op_cons, "op_cons"), + (op_first, "op_first"), + (op_rest, "op_rest"), + (op_listp, "op_listp"), + (op_raise, "op_raise"), + (op_eq, "op_eq"), + (op_sha256, "op_sha256"), + (op_add, "op_add"), + (op_subtract, "op_subtract"), + (op_multiply, "op_multiply"), + (op_divmod, "op_divmod"), + (op_substr, "op_substr"), + (op_strlen, "op_strlen"), + (op_point_add, "op_point_add"), + (op_pubkey_for_exp, "op_pubkey_for_exp"), + (op_concat, "op_concat"), + (op_gr, "op_gr"), + (op_gr_bytes, "op_gr_bytes"), + (op_logand, "op_logand"), + (op_logior, "op_logior"), + (op_logxor, "op_logxor"), + (op_lognot, "op_lognot"), + (op_ash, "op_ash"), + (op_lsh, "op_lsh"), + (op_not, "op_not"), + (op_any, "op_any"), + (op_all, "op_all"), + (op_softfork, "op_softfork"), + (op_div, "op_div"), ]; let name: &[u8] = name.as_ref(); - for (op, f) in opcode_lookup.iter() { + for (f, op) in opcode_lookup.iter() { let pu8: &[u8] = op.as_ref(); if pu8 == name { return Some(*f); @@ -57,7 +57,7 @@ pub fn opcode_by_name(name: &str) -> Option> { } pub fn f_lookup_for_hashmap( - opcode_lookup_by_name: HashMap, + opcode_lookup_by_name: HashMap>, ) -> FLookup { let mut f_lookup = [None; 256]; for (name, idx) in opcode_lookup_by_name.iter() { diff --git a/src/py/native_op_lookup.rs b/src/py/native_op_lookup.rs index c4c26482..91ac2bd1 100644 --- a/src/py/native_op_lookup.rs +++ b/src/py/native_op_lookup.rs @@ -47,7 +47,7 @@ where ::Ptr: From, { pub fn new( - opcode_lookup_by_name: HashMap, + opcode_lookup_by_name: HashMap>, unknown_op_callback: PyObject, ) -> Self { let f_lookup = f_lookup_for_hashmap(opcode_lookup_by_name); diff --git a/src/py/run_program.rs b/src/py/run_program.rs index ae7812cb..a8edb5c6 100644 --- a/src/py/run_program.rs +++ b/src/py/run_program.rs @@ -50,7 +50,69 @@ pub fn serialize_and_run_program( args: &[u8], quote_kw: u8, apply_kw: u8, - opcode_lookup_by_name: HashMap, + max_cost: u32, + flags: u32, +) -> PyResult<(u32, Py)> { + let mut opcode_lookup_by_name = HashMap::>::new(); + for (v, s) in [ + (4, "op_if"), + (5, "op_cons"), + (6, "op_first"), + (7, "op_rest"), + (8, "op_listp"), + (9, "op_raise"), + (10, "op_eq"), + (11, "op_sha256"), + (12, "op_add"), + (13, "op_subtract"), + (14, "op_multiply"), + (15, "op_divmod"), + (16, "op_substr"), + (17, "op_strlen"), + (18, "op_point_add"), + (19, "op_pubkey_for_exp"), + (20, "op_concat"), + (22, "op_gr"), + (23, "op_gr_bytes"), + (24, "op_logand"), + (25, "op_logior"), + (26, "op_logxor"), + (27, "op_lognot"), + (28, "op_ash"), + (29, "op_lsh"), + (30, "op_not"), + (31, "op_any"), + (32, "op_all"), + (33, "op_softfork"), + (34, "op_div"), + ] + .iter() + { + let v: Vec = vec![*v as u8]; + opcode_lookup_by_name.insert(s.to_string(), v); + } + + deserialize_and_run_program( + py, + program, + args, + quote_kw, + apply_kw, + opcode_lookup_by_name, + max_cost, + flags, + ) +} + +#[allow(clippy::too_many_arguments)] +#[pyfunction] +pub fn deserialize_and_run_program( + py: Python, + program: &[u8], + args: &[u8], + quote_kw: u8, + apply_kw: u8, + opcode_lookup_by_name: HashMap>, max_cost: u32, flags: u32, ) -> PyResult<(u32, Py)> {