From ff365a2d0b78e408b90c703f1b4de1ca16af7377 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Tue, 7 Jan 2025 11:52:40 -0500 Subject: [PATCH 1/4] make datalayer a submodule for the namespacing benefits --- tests/test_datalayer.py | 2 +- wheel/src/api.rs | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/test_datalayer.py b/tests/test_datalayer.py index 73723f15b..78de456c1 100644 --- a/tests/test_datalayer.py +++ b/tests/test_datalayer.py @@ -1,4 +1,4 @@ -from chia_rs import LeafNode, MerkleBlob +from chia_rs.datalayer import LeafNode, MerkleBlob from chia_rs.sized_bytes import bytes32 from chia_rs.sized_ints import int64, uint8 diff --git a/wheel/src/api.rs b/wheel/src/api.rs index 6cc98f1f5..ac16aaf7a 100644 --- a/wheel/src/api.rs +++ b/wheel/src/api.rs @@ -55,7 +55,7 @@ use pyo3::pybacked::PyBackedBytes; use pyo3::types::PyBytes; use pyo3::types::PyList; use pyo3::types::PyTuple; -use pyo3::wrap_pyfunction; +use pyo3::{py_run, wrap_pyfunction, wrap_pymodule}; use std::collections::HashSet; use std::iter::zip; @@ -452,7 +452,7 @@ pub fn py_get_flags_for_height_and_constants(height: u32, constants: &ConsensusC } #[pymodule] -pub fn chia_rs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { +pub fn chia_rs(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { // generator functions m.add_function(wrap_pyfunction!(run_block_generator, m)?)?; m.add_function(wrap_pyfunction!(run_block_generator2, m)?)?; @@ -475,11 +475,6 @@ pub fn chia_rs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { // constants m.add_class::()?; - // datalayer - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - // merkle tree m.add_class::()?; m.add_function(wrap_pyfunction!(confirm_included_already_hashed, m)?)?; @@ -647,5 +642,24 @@ pub fn chia_rs(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; + add_datalayer_submodule(py, m)?; + + Ok(()) +} + +pub fn add_datalayer_submodule(py: Python<'_>, parent: &Bound<'_, PyModule>) -> PyResult<()> { + let datalayer = PyModule::new(py, "datalayer")?; + parent.add_submodule(&datalayer)?; + + datalayer.add_class::()?; + datalayer.add_class::()?; + datalayer.add_class::()?; + + // https://github.com/PyO3/pyo3/issues/1517#issuecomment-808664021 + // https://github.com/PyO3/pyo3/issues/759 + py.import("sys")? + .getattr("modules")? + .set_item("chia_rs.datalayer", datalayer)?; + Ok(()) } From c6bca3981ee8c5846df1b5b946e45daeacd8d2cb Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Tue, 7 Jan 2025 12:08:27 -0500 Subject: [PATCH 2/4] tidy --- wheel/src/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wheel/src/api.rs b/wheel/src/api.rs index ac16aaf7a..3168dd3e1 100644 --- a/wheel/src/api.rs +++ b/wheel/src/api.rs @@ -55,7 +55,7 @@ use pyo3::pybacked::PyBackedBytes; use pyo3::types::PyBytes; use pyo3::types::PyList; use pyo3::types::PyTuple; -use pyo3::{py_run, wrap_pyfunction, wrap_pymodule}; +use pyo3::wrap_pyfunction; use std::collections::HashSet; use std::iter::zip; From a1f86e245cbcc5875f5c5305086193605bc3f56e Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Tue, 7 Jan 2025 12:43:42 -0500 Subject: [PATCH 3/4] move the datalayer python type hints --- wheel/generate_type_stubs.py | 55 --------------------------- wheel/python/chia_rs/chia_rs.pyi | 55 --------------------------- wheel/python/chia_rs/datalayer.pyi | 61 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 110 deletions(-) create mode 100644 wheel/python/chia_rs/datalayer.pyi diff --git a/wheel/generate_type_stubs.py b/wheel/generate_type_stubs.py index 888c7f409..5f4a67d56 100644 --- a/wheel/generate_type_stubs.py +++ b/wheel/generate_type_stubs.py @@ -396,61 +396,6 @@ def derive_child_sk_unhardened(sk: PrivateKey, index: int) -> PrivateKey: ... def derive_child_pk_unhardened(pk: G1Element, index: int) -> G1Element: ... -@final -class InternalNode: - @property - def parent(self) -> Optional[uint32]: ... - @property - def hash(self) -> bytes: ... - - @property - def left(self) -> uint32: ... - @property - def right(self) -> uint32: ... - - -@final -class LeafNode: - @property - def parent(self) -> Optional[uint32]: ... - @property - def hash(self) -> bytes: ... - - @property - def key(self) -> int64: ... - @property - def value(self) -> int64: ... - - -@final -class MerkleBlob: - @property - def blob(self) -> bytearray: ... - @property - def free_indexes(self) -> set[uint32]: ... - @property - def key_to_index(self) -> Mapping[int64, uint32]: ... - @property - def check_integrity_on_drop(self) -> bool: ... - - def __init__( - self, - blob: bytes, - ) -> None: ... - - def insert(self, key: int64, value: int64, hash: bytes32, reference_kid: Optional[int64] = None, side: Optional[uint8] = None) -> None: ... - def delete(self, key: int64) -> None: ... - def get_raw_node(self, index: uint32) -> Union[InternalNode, LeafNode]: ... - def calculate_lazy_hashes(self) -> None: ... - def get_lineage_with_indexes(self, index: uint32) -> list[tuple[uint32, Union[InternalNode, LeafNode]]]:... - def get_nodes_with_indexes(self) -> list[tuple[uint32, Union[InternalNode, LeafNode]]]: ... - def empty(self) -> bool: ... - def get_root_hash(self) -> bytes32: ... - def batch_insert(self, keys_values: list[tuple[int64, int64]], hashes: list[bytes32]): ... - def get_hash_at_index(self, index: uint32): ... - - def __len__(self) -> int: ... - @final class MerkleSet: def get_root(self) -> bytes32: ... diff --git a/wheel/python/chia_rs/chia_rs.pyi b/wheel/python/chia_rs/chia_rs.pyi index b5f35b98b..4b6e1f4de 100644 --- a/wheel/python/chia_rs/chia_rs.pyi +++ b/wheel/python/chia_rs/chia_rs.pyi @@ -125,61 +125,6 @@ class AugSchemeMPL: def derive_child_pk_unhardened(pk: G1Element, index: int) -> G1Element: ... -@final -class InternalNode: - @property - def parent(self) -> Optional[uint32]: ... - @property - def hash(self) -> bytes: ... - - @property - def left(self) -> uint32: ... - @property - def right(self) -> uint32: ... - - -@final -class LeafNode: - @property - def parent(self) -> Optional[uint32]: ... - @property - def hash(self) -> bytes: ... - - @property - def key(self) -> int64: ... - @property - def value(self) -> int64: ... - - -@final -class MerkleBlob: - @property - def blob(self) -> bytearray: ... - @property - def free_indexes(self) -> set[uint32]: ... - @property - def key_to_index(self) -> Mapping[int64, uint32]: ... - @property - def check_integrity_on_drop(self) -> bool: ... - - def __init__( - self, - blob: bytes, - ) -> None: ... - - def insert(self, key: int64, value: int64, hash: bytes32, reference_kid: Optional[int64] = None, side: Optional[uint8] = None) -> None: ... - def delete(self, key: int64) -> None: ... - def get_raw_node(self, index: uint32) -> Union[InternalNode, LeafNode]: ... - def calculate_lazy_hashes(self) -> None: ... - def get_lineage_with_indexes(self, index: uint32) -> list[tuple[uint32, Union[InternalNode, LeafNode]]]:... - def get_nodes_with_indexes(self) -> list[tuple[uint32, Union[InternalNode, LeafNode]]]: ... - def empty(self) -> bool: ... - def get_root_hash(self) -> bytes32: ... - def batch_insert(self, keys_values: list[tuple[int64, int64]], hashes: list[bytes32]): ... - def get_hash_at_index(self, index: uint32): ... - - def __len__(self) -> int: ... - @final class MerkleSet: def get_root(self) -> bytes32: ... diff --git a/wheel/python/chia_rs/datalayer.pyi b/wheel/python/chia_rs/datalayer.pyi new file mode 100644 index 000000000..ebc7a5bea --- /dev/null +++ b/wheel/python/chia_rs/datalayer.pyi @@ -0,0 +1,61 @@ +from typing import Mapping, Optional, Sequence, Union, Any, ClassVar, final +from .sized_bytes import bytes32, bytes100 +from .sized_ints import uint8, uint16, uint32, uint64, uint128, int8, int16, int32, int64 +from typing_extensions import Self +from chia.types.blockchain_format.program import Program as ChiaProgram + +@final +class InternalNode: + @property + def parent(self) -> Optional[uint32]: ... + @property + def hash(self) -> bytes: ... + + @property + def left(self) -> uint32: ... + @property + def right(self) -> uint32: ... + + +@final +class LeafNode: + @property + def parent(self) -> Optional[uint32]: ... + @property + def hash(self) -> bytes: ... + + @property + def key(self) -> int64: ... + @property + def value(self) -> int64: ... + + +@final +class MerkleBlob: + @property + def blob(self) -> bytearray: ... + @property + def free_indexes(self) -> set[uint32]: ... + @property + def key_to_index(self) -> Mapping[int64, uint32]: ... + @property + def check_integrity_on_drop(self) -> bool: ... + + def __init__( + self, + blob: bytes, + ) -> None: ... + + def insert(self, key: int64, value: int64, hash: bytes32, reference_kid: Optional[int64] = None, side: Optional[uint8] = None) -> None: ... + def delete(self, key: int64) -> None: ... + def get_raw_node(self, index: uint32) -> Union[InternalNode, LeafNode]: ... + def calculate_lazy_hashes(self) -> None: ... + def get_lineage_with_indexes(self, index: uint32) -> list[tuple[uint32, Union[InternalNode, LeafNode]]]:... + def get_nodes_with_indexes(self) -> list[tuple[uint32, Union[InternalNode, LeafNode]]]: ... + def empty(self) -> bool: ... + def get_root_hash(self) -> bytes32: ... + def batch_insert(self, keys_values: list[tuple[int64, int64]], hashes: list[bytes32]): ... + def get_hash_at_index(self, index: uint32): ... + + def __len__(self) -> int: ... + From d6bec340ae71cbbe6659c920209b2c8a48136852 Mon Sep 17 00:00:00 2001 From: Kyle Altendorf Date: Tue, 7 Jan 2025 12:53:08 -0500 Subject: [PATCH 4/4] add `__all__` even though i don't want to --- wheel/python/chia_rs/datalayer.pyi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wheel/python/chia_rs/datalayer.pyi b/wheel/python/chia_rs/datalayer.pyi index ebc7a5bea..fd5edc134 100644 --- a/wheel/python/chia_rs/datalayer.pyi +++ b/wheel/python/chia_rs/datalayer.pyi @@ -59,3 +59,5 @@ class MerkleBlob: def __len__(self) -> int: ... +# TODO: i would rather not specify this at all +__all__: Sequence[str] = ["InternalNode", "LeafNode", "MerkleBlob"]