Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion tests/test_program.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from chia_rs import run_chia_program, Program
from chia_rs import (
run_chia_program,
Program,
serialized_length,
serialized_length_trusted,
)
from chia_rs.sized_bytes import bytes32


Expand Down Expand Up @@ -44,3 +49,15 @@ def test_print() -> None:
f"{temp.get_tree_hash()}"
== "a200d6417c8fdc7c7937382c1b61e219854e1efd8f2e15d6c88e6571bc29ed1a"
)


def test_serialized_length() -> None:
temp = Program.to([8, (1, "foo")])
buf = bytes(temp)
expect = len(buf)
assert serialized_length(buf) == expect
assert serialized_length_trusted(buf) == expect

buf = buf + b"garbage"
assert serialized_length(buf) == expect
assert serialized_length_trusted(buf) == expect
1 change: 1 addition & 0 deletions wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ class LazyNode:
atom: Optional[bytes]

def serialized_length(program: ReadableBuffer) -> int: ...
def serialized_length_trusted(program: ReadableBuffer) -> int: ...
def tree_hash(blob: ReadableBuffer) -> bytes32: ...
def get_puzzle_and_solution_for_coin(program: ReadableBuffer, args: ReadableBuffer, max_cost: int, find_parent: bytes32, find_amount: int, find_ph: bytes32, flags: int) -> tuple[bytes, bytes]: ...
def get_puzzle_and_solution_for_coin2(generator: Program, block_refs: list[ReadableBuffer], max_cost: int, find_coin: Coin, flags: int) -> tuple[Program, Program]: ...
Expand Down
1 change: 1 addition & 0 deletions wheel/python/chia_rs/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class LazyNode:
atom: Optional[bytes]

def serialized_length(program: ReadableBuffer) -> int: ...
def serialized_length_trusted(program: ReadableBuffer) -> int: ...
def tree_hash(blob: ReadableBuffer) -> bytes32: ...
def get_puzzle_and_solution_for_coin(program: ReadableBuffer, args: ReadableBuffer, max_cost: int, find_parent: bytes32, find_amount: int, find_ph: bytes32, flags: int) -> tuple[bytes, bytes]: ...
def get_puzzle_and_solution_for_coin2(generator: Program, block_refs: list[ReadableBuffer], max_cost: int, find_coin: Coin, flags: int) -> tuple[Program, Program]: ...
Expand Down
3 changes: 2 additions & 1 deletion wheel/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use pyo3::wrap_pyfunction;

use std::iter::zip;

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

use chia_consensus::fast_forward::fast_forward_singleton as native_ff;
use chia_consensus::get_puzzle_and_solution::get_puzzle_and_solution_for_coin as parse_puzzle_solution;
Expand Down Expand Up @@ -811,6 +811,7 @@ pub fn chia_rs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
)?;

m.add_function(wrap_pyfunction!(serialized_length, m)?)?;
m.add_function(wrap_pyfunction!(serialized_length_trusted, m)?)?;
m.add_function(wrap_pyfunction!(compute_merkle_set_root, m)?)?;
m.add_function(wrap_pyfunction!(tree_hash, m)?)?;
m.add_function(wrap_pyfunction!(get_puzzle_and_solution_for_coin, m)?)?;
Expand Down
13 changes: 12 additions & 1 deletion wheel/src/run_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ 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_backrefs, serialized_length_from_bytes};
use clvmr::serde::{
node_from_bytes_backrefs, serialized_length_from_bytes, serialized_length_from_bytes_trusted,
};
use pyo3::buffer::PyBuffer;
use pyo3::prelude::*;
use std::rc::Rc;
Expand All @@ -19,6 +21,15 @@ pub fn serialized_length(program: PyBuffer<u8>) -> PyResult<u64> {
serialized_length_from_bytes(program).map_err(map_pyerr)
}

#[allow(clippy::borrow_deref_ref)]
#[pyfunction]
pub fn serialized_length_trusted(program: PyBuffer<u8>) -> PyResult<u64> {
assert!(program.is_c_contiguous(), "program must be contiguous");
let program =
unsafe { std::slice::from_raw_parts(program.buf_ptr() as *const u8, program.len_bytes()) };
serialized_length_from_bytes_trusted(program).map_err(map_pyerr)
}

#[allow(clippy::borrow_deref_ref)]
#[pyfunction]
pub fn run_chia_program(
Expand Down
Loading