Skip to content

Commit

Permalink
even though the type stub for get_hash() says bytes32, we currently r…
Browse files Browse the repository at this point in the history
…eturn a plain bytes object. This fixes it by actually constructing a bytes32 object to be returned. Also extend the built-in conversion to also support bytes48
  • Loading branch information
arvidn committed Aug 14, 2024
1 parent d409073 commit 25cfb6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ impl<const N: usize> ChiaToPython for BytesImpl<N> {
let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?;
let ty = bytes_module.getattr("bytes32")?;
ty.call1((self.0.into_py(py),))
} else if N == 48 {
let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?;
let ty = bytes_module.getattr("bytes48")?;
ty.call1((self.0.into_py(py),))
} else {
Ok(PyBytes::new_bound(py, &self.0).into_any())
}
Expand Down
9 changes: 7 additions & 2 deletions crates/chia_py_streamable_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,15 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
}
}

pub fn get_hash<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
pub fn get_hash<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::types::PyAny>> {
use pyo3::IntoPy;
use pyo3::types::PyModule;
use pyo3::prelude::PyAnyMethods;
let mut ctx = clvmr::sha2::Sha256::new();
#crate_name::Streamable::update_digest(self, &mut ctx);
Ok(pyo3::types::PyBytes::new_bound(py, &ctx.finalize()))
let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?;
let ty = bytes_module.getattr("bytes32")?;
ty.call1((&ctx.finalize().into_py(py),))
}
#[pyo3(name = "to_bytes")]
pub fn py_to_bytes<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
Expand Down

0 comments on commit 25cfb6d

Please sign in to comment.