Skip to content

Commit

Permalink
Merge pull request #401 from Chia-Network/bytes32
Browse files Browse the repository at this point in the history
expose bytes32 as the right type, from chia-blockchain
  • Loading branch information
arvidn authored Feb 14, 2024
2 parents e68fb44 + ac7686c commit aae2708
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,13 @@ impl<const N: usize> IntoPy<PyObject> for BytesImpl<N> {
#[cfg(feature = "py-bindings")]
impl<const N: usize> ChiaToPython for BytesImpl<N> {
fn to_python<'a>(&self, py: Python<'a>) -> PyResult<&'a PyAny> {
Ok(PyBytes::new(py, &self.0).into())
if N == 32 {
let bytes_module = PyModule::import(py, "chia.types.blockchain_format.sized_bytes")?;
let ty = bytes_module.getattr("bytes32")?;
ty.call1((self.0.into_py(py),))
} else {
Ok(PyBytes::new(py, &self.0).into())
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/test_block_record_fidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ def get_block_record(rng: Random) -> BlockRecord:
)


def test_bytes32():
rng = Random()
rng.seed(1337)
br = get_block_record(rng)
assert isinstance(br.header_hash, bytes32)
assert f"{br.header_hash}" == "e433713dd932b2314eab219aa5504f71b9fe9f2d8e2f5cadfa892d8dc6a7ba53"
assert br.header_hash.__str__() == "e433713dd932b2314eab219aa5504f71b9fe9f2d8e2f5cadfa892d8dc6a7ba53"

def wrap_call(expr: str, br: Any) -> str:
try:
ret = eval(expr, None, {"br": br})
Expand Down

0 comments on commit aae2708

Please sign in to comment.