From 0be2755acacf28e9b99acc6e96dedb49a25ebc9d Mon Sep 17 00:00:00 2001 From: Amine Khaldi Date: Fri, 6 Sep 2024 14:50:45 +0100 Subject: [PATCH] Release the GIL when calling validate_clvm_and_signature in the python binding. --- crates/chia-consensus/src/spendbundle_conditions.rs | 4 ++-- wheel/src/api.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/chia-consensus/src/spendbundle_conditions.rs b/crates/chia-consensus/src/spendbundle_conditions.rs index 0baae8642..55f51b049 100644 --- a/crates/chia-consensus/src/spendbundle_conditions.rs +++ b/crates/chia-consensus/src/spendbundle_conditions.rs @@ -15,6 +15,8 @@ use clvmr::reduction::Reduction; use clvmr::run_program::run_program; use clvmr::serde::node_from_bytes; +const QUOTE_BYTES: usize = 2; + pub fn get_conditions_from_spendbundle( a: &mut Allocator, spend_bundle: &SpendBundle, @@ -40,7 +42,6 @@ pub fn get_conditions_from_spendbundle( }); // We don't pay the size cost (nor execution cost) of being wrapped by a // quote (in solution_generator). - const QUOTE_BYTES: usize = 2; let generator_length_without_quote = solution_generator(spends_info)?.len() - QUOTE_BYTES; let byte_cost = generator_length_without_quote as u64 * constants.cost_per_byte; subtract_cost(a, &mut cost_left, byte_cost)?; @@ -351,7 +352,6 @@ mod tests { &coin_spend.solution, ) }); - const QUOTE_BYTES: usize = 2; let generator_length_without_quote = solution_generator(program_spends) .expect("solution_generator failed") .len() diff --git a/wheel/src/api.rs b/wheel/src/api.rs index ebf03094b..2b603d123 100644 --- a/wheel/src/api.rs +++ b/wheel/src/api.rs @@ -422,16 +422,19 @@ fn fast_forward_singleton<'p>( #[pyo3(name = "validate_clvm_and_signature")] #[allow(clippy::type_complexity)] pub fn py_validate_clvm_and_signature( + py: Python<'_>, new_spend: &SpendBundle, max_cost: u64, constants: &ConsensusConstants, peak_height: u32, ) -> PyResult<(OwnedSpendBundleConditions, Vec<([u8; 32], GTElement)>, f32)> { - let (owned_conditions, additions, duration) = - validate_clvm_and_signature(new_spend, max_cost, constants, peak_height).map_err(|e| { + let (owned_conditions, additions, duration) = py + .allow_threads(|| validate_clvm_and_signature(new_spend, max_cost, constants, peak_height)) + .map_err(|e| { + // cast validation error to int let error_code: u32 = e.into(); PyErr::new::(error_code) - })?; // cast validation error to int + })?; Ok((owned_conditions, additions, duration.as_secs_f32())) }