Skip to content

Commit

Permalink
simplify error handling in run_chia_program() and get_puzzle_and_solu…
Browse files Browse the repository at this point in the history
…tion_for_coin() and remove eval_err_to_pyresult()
  • Loading branch information
arvidn committed Jul 30, 2024
1 parent 28404dc commit f6c1982
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
11 changes: 0 additions & 11 deletions wheel/src/adapt_response.rs

This file was deleted.

33 changes: 17 additions & 16 deletions wheel/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ use std::iter::zip;

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

use crate::adapt_response::eval_err_to_pyresult;
use chia_consensus::fast_forward::fast_forward_singleton as native_ff;
use chia_consensus::gen::get_puzzle_and_solution::get_puzzle_and_solution_for_coin as parse_puzzle_solution;
use chia_consensus::gen::validation_error::ValidationErr;
Expand Down Expand Up @@ -138,14 +137,19 @@ pub fn get_puzzle_and_solution_for_coin<'a>(
let args = deserialize(&mut allocator, args)?;
let dialect = &ChiaDialect::new(flags);

let r = py.allow_threads(|| -> Result<(NodePtr, NodePtr), EvalErr> {
let Reduction(_cost, result) =
run_program(&mut allocator, dialect, program, args, max_cost)?;
match parse_puzzle_solution(&allocator, result, find_parent, find_amount, find_ph) {
Err(ValidationErr(n, _)) => Err(EvalErr(n, "coin not found".to_string())),
Ok(pair) => Ok(pair),
}
});
let (puzzle, solution) = py
.allow_threads(|| -> Result<(NodePtr, NodePtr), EvalErr> {
let Reduction(_cost, result) =
run_program(&mut allocator, dialect, program, args, max_cost)?;
match parse_puzzle_solution(&allocator, result, find_parent, find_amount, find_ph) {
Err(ValidationErr(n, _)) => Err(EvalErr(n, "coin not found".to_string())),
Ok(pair) => Ok(pair),
}
})
.map_err(|e| {
let blob = node_to_bytes(&allocator, e.0).ok().map(hex::encode);
PyValueError::new_err((e.1, blob))
})?;

// keep serializing normally, until wallets support backrefs
let serialize = node_to_bytes;
Expand All @@ -156,13 +160,10 @@ pub fn get_puzzle_and_solution_for_coin<'a>(
node_to_bytes
};
*/
match r {
Err(eval_err) => eval_err_to_pyresult(eval_err, &allocator),
Ok((puzzle, solution)) => Ok((
PyBytes::new_bound(py, &serialize(&allocator, puzzle)?),
PyBytes::new_bound(py, &serialize(&allocator, solution)?),
)),
}
Ok((
PyBytes::new_bound(py, &serialize(&allocator, puzzle)?),
PyBytes::new_bound(py, &serialize(&allocator, solution)?),
))
}

#[pyfunction]
Expand Down
1 change: 0 additions & 1 deletion wheel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(unsafe_code, clippy::needless_pass_by_value)]

mod adapt_response;
mod api;
mod run_generator;
mod run_program;
23 changes: 12 additions & 11 deletions wheel/src/run_program.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use super::adapt_response::eval_err_to_pyresult;
use chia_consensus::allocator::make_allocator;
use chia_consensus::gen::flags::ALLOW_BACKREFS;
use chia_protocol::LazyNode;
use clvmr::chia_dialect::ChiaDialect;
use clvmr::cost::Cost;
use clvmr::reduction::Response;
use clvmr::run_program::run_program;
use clvmr::serde::{node_from_bytes, node_from_bytes_backrefs, serialized_length_from_bytes};
use clvmr::serde::{
node_from_bytes, node_from_bytes_backrefs, node_to_bytes, serialized_length_from_bytes,
};
use pyo3::buffer::PyBuffer;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use std::rc::Rc;

Expand All @@ -31,7 +33,7 @@ pub fn run_chia_program(
) -> PyResult<(Cost, LazyNode)> {
let mut allocator = make_allocator(flags);

let r: Response = (|| -> PyResult<Response> {
let reduction = (|| -> PyResult<Response> {
let deserialize = if (flags & ALLOW_BACKREFS) != 0 {
node_from_bytes_backrefs
} else {
Expand All @@ -42,12 +44,11 @@ pub fn run_chia_program(
let dialect = ChiaDialect::new(flags);

Ok(py.allow_threads(|| run_program(&mut allocator, &dialect, program, args, max_cost)))
})()?;
match r {
Ok(reduction) => {
let val = LazyNode::new(Rc::new(allocator), reduction.1);
Ok((reduction.0, val))
}
Err(eval_err) => eval_err_to_pyresult(eval_err, &allocator),
}
})()?
.map_err(|e| {
let blob = node_to_bytes(&allocator, e.0).ok().map(hex::encode);
PyValueError::new_err((e.1, blob))
})?;
let val = LazyNode::new(Rc::new(allocator), reduction.1);
Ok((reduction.0, val))
}

0 comments on commit f6c1982

Please sign in to comment.