Skip to content

Commit

Permalink
add pyfunction for validate_clvm_and_signature
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-o-how committed Jun 11, 2024
1 parent b029577 commit ae55a97
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 38 deletions.
44 changes: 7 additions & 37 deletions crates/chia-consensus/src/multiprocess_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn pre_validate_spendbundle(
// currently in mempool_manager.py
// called in threads from pre_validate_spend_bundle()
// returns (error, cached_results, new_cache_entries, duration)
fn validate_clvm_and_signature(
pub fn validate_clvm_and_signature(
spend_bundle: &SpendBundle,
max_cost: u64,
constants: &ConsensusConstants,
Expand Down Expand Up @@ -98,40 +98,6 @@ fn validate_clvm_and_signature(
Ok((npcresult, added, start_time.elapsed()))
}

// #[cfg(feature = "py-bindings")]
// mod py_funcs {
// use super::*;
// use pyo3::{
// exceptions::PyValueError,
// pybacked::PyBackedBytes,
// pyfunction,
// types::{PyAnyMethods, PyList},
// Bound, PyObject, PyResult,
// };
// use crate::gen::owned_conditions;

// #[pyfunction]
// #[pyo3(name = "pre_validate_spendbundle")]
// pub fn py_pre_validate_spendbundle(
// new_spend: SpendBundle,
// max_cost: u64,
// constants: ConsensusConstants,
// peak_height: u32,
// syncing: bool,
// cache: BlsCache
// ) -> Result<(SpendBundle, OwnedSpendBundleConditions), ErrorCode> {
// let sbc = validate_clvm_and_signature(&new_spend, max_cost, constants, peak_height, syncing, Arc::new(Mutex::new(cache))); // TODO: use cache properly
// match sbc {
// Ok(owned_conditions) => {
// Ok((new_spend, owned_conditions.0))
// },
// Err(e) => {
// Err(e)
// }
// }
// }
// }

pub fn get_flags_for_height_and_constants(height: u32, constants: &ConsensusConstants) -> u32 {
let mut flags: u32 = 0;
if height >= constants.soft_fork2_height {
Expand Down Expand Up @@ -436,7 +402,9 @@ ff01\
[
test_coin.parent_coin_info.as_slice(),
u64_to_bytes(test_coin.amount).as_slice(),
TEST_CONSTANTS.agg_sig_parent_amount_additional_data.as_slice(),
TEST_CONSTANTS
.agg_sig_parent_amount_additional_data
.as_slice(),
]
.concat(),
);
Expand Down Expand Up @@ -504,7 +472,9 @@ ff01\
[
test_coin.puzzle_hash.as_slice(),
u64_to_bytes(test_coin.amount).as_slice(),
TEST_CONSTANTS.agg_sig_puzzle_amount_additional_data.as_slice(),
TEST_CONSTANTS
.agg_sig_puzzle_amount_additional_data
.as_slice(),
]
.concat(),
);
Expand Down
52 changes: 51 additions & 1 deletion wheel/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use chia_consensus::gen::solution_generator::solution_generator as native_soluti
use chia_consensus::gen::solution_generator::solution_generator_backrefs as native_solution_generator_backrefs;
use chia_consensus::merkle_set::compute_merkle_set_root as compute_merkle_root_impl;
use chia_consensus::merkle_tree::{validate_merkle_proof, MerkleSet};
use chia_consensus::multiprocess_validation::validate_clvm_and_signature;

use chia_consensus::gen::validation_error::ErrorCode;
use chia_protocol::{
BlockRecord, Bytes32, ChallengeBlockInfo, ChallengeChainSubSlot, ClassgroupElement, Coin,
CoinSpend, CoinState, CoinStateFilters, CoinStateUpdate, EndOfSubSlotBundle, Foliage,
Expand Down Expand Up @@ -42,14 +45,17 @@ use chia_protocol::{
use clvm_utils::tree_hash_from_bytes;
use clvmr::{ENABLE_BLS_OPS_OUTSIDE_GUARD, ENABLE_FIXED_DIV, LIMIT_HEAP, NO_UNKNOWN_OPS};
use pyo3::buffer::PyBuffer;
use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::exceptions::{PyRuntimeError, PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedBytes;
use pyo3::types::PyBytes;
use pyo3::types::PyList;
use pyo3::types::PyTuple;
use pyo3::wrap_pyfunction;
use std::collections::HashMap;
use std::iter::zip;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::run_program::{run_chia_program, serialized_length};

Expand Down Expand Up @@ -365,6 +371,47 @@ fn fast_forward_singleton<'p>(
))
}

#[pyfunction]
#[pyo3(name = "validate_clvm_and_signature")]
pub fn py_validate_clvm_and_signature(
new_spend: SpendBundle,
max_cost: u64,
constants: ConsensusConstants,
peak_height: u32,
cache: BlsCache,
) -> PyResult<(
SpendBundle,
OwnedSpendBundleConditions,
HashMap<[u8; 32], GTElement>,
f32,
)> {
let sbc: Result<
(
OwnedSpendBundleConditions,
HashMap<[u8; 32], GTElement>,
Duration,
),
ErrorCode,
> = validate_clvm_and_signature(
&new_spend,
max_cost,
&constants,
peak_height,
Arc::new(Mutex::new(cache)),
); // TODO: use cache properly
match sbc {
Ok((owned_conditions, additions, duration)) => Ok((
new_spend,
owned_conditions,
additions,
duration.as_secs_f32(),
)),
Err(e) => {
Err(PyErr::new::<PyTypeError, _>(e as u32))
}
}
}

#[pymodule]
pub fn chia_rs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
// generator functions
Expand Down Expand Up @@ -394,6 +441,9 @@ pub fn chia_rs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(confirm_included_already_hashed, m)?)?;
m.add_function(wrap_pyfunction!(confirm_not_included_already_hashed, m)?)?;

// multithread validattion
m.add_function(wrap_pyfunction!(py_validate_clvm_and_signature, m)?)?;

// clvm functions
m.add("COND_ARGS_NIL", COND_ARGS_NIL)?;
m.add("NO_UNKNOWN_CONDS", NO_UNKNOWN_CONDS)?;
Expand Down

0 comments on commit ae55a97

Please sign in to comment.