Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make datalayer py streamable #895

Draft
wants to merge 1 commit into
base: long_lived/initial_datalayer
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ dependencies = [
"chia-protocol",
"chia-sha2 0.17.0",
"chia-traits 0.18.0",
"chia_py_streamable_macro",
"chia_streamable_macro 0.17.0",
"clvm-utils",
"expect-test",
Expand Down
3 changes: 2 additions & 1 deletion crates/chia-datalayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/Chia-Network/chia_rs"
workspace = true

[features]
py-bindings = ["dep:pyo3", "chia-protocol/py-bindings"]
py-bindings = ["dep:pyo3", "dep:chia_py_streamable_macro", "chia-protocol/py-bindings"]

[lib]
crate-type = ["rlib"]
Expand All @@ -21,6 +21,7 @@ crate-type = ["rlib"]
num-traits = { workspace = true }
pyo3 = { workspace = true, optional = true }
thiserror = { workspace = true }
chia_py_streamable_macro = { workspace = true, optional = true }
chia_streamable_macro = { workspace = true }
chia-traits = { workspace = true }
chia-sha2 = { workspace = true }
Expand Down
69 changes: 33 additions & 36 deletions crates/chia-datalayer/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use pyo3::{
};

use chia_protocol::Bytes32;
use chia_py_streamable_macro::{PyJsonDict, PyStreamable};
use chia_sha2::Sha256;
use chia_streamable_macro::Streamable;
use chia_traits::Streamable;
use chia_traits::{Streamable, ToJsonDict};
use num_traits::ToBytes;
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet, VecDeque};
Expand All @@ -34,13 +35,42 @@ impl<'py> IntoPyObject<'py> for TreeIndex {
}
}

#[cfg(feature = "py-bindings")]
impl ToJsonDict for TreeIndex {
fn to_json_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
Ok(self.into_pyobject(py)?.unbind().into_any())
}
}

impl std::fmt::Display for TreeIndex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

type Parent = Option<TreeIndex>;
#[cfg_attr(feature = "py-bindings", derive(FromPyObject), pyo3(transparent))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Streamable)]
struct Parent(Option<TreeIndex>);

#[cfg(feature = "py-bindings")]
impl<'py> IntoPyObject<'py> for Parent {
// TODO: can we describe this optional int better?
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
self.0.into_pyobject(py)
}
}

#[cfg(feature = "py-bindings")]
impl ToJsonDict for Parent {
fn to_json_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
Ok(self.into_pyobject(py)?.unbind().into_any())
}
}

type Hash = Bytes32;
/// Key and value ids are provided from outside of this code and are implemented as
/// the row id from sqlite which is a signed 8 byte integer. The actual key and
Expand Down Expand Up @@ -339,7 +369,7 @@ pub struct NodeMetadata {
pub dirty: bool,
}

#[cfg_attr(feature = "py-bindings", pyclass(get_all))]
#[cfg_attr(feature = "py-bindings", pyclass, derive(PyJsonDict, PyStreamable))]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Streamable)]
pub struct InternalNode {
pub parent: Parent,
Expand All @@ -360,25 +390,6 @@ impl InternalNode {
}
}

#[cfg(feature = "py-bindings")]
#[pymethods]
impl InternalNode {
#[new]
pub fn py_init(
parent: Parent,
hash: Hash,
left: TreeIndex,
right: TreeIndex,
) -> PyResult<Self> {
Ok(Self {
parent,
hash,
left,
right,
})
}
}

#[cfg_attr(feature = "py-bindings", pyclass(get_all))]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Streamable)]
pub struct LeafNode {
Expand All @@ -388,20 +399,6 @@ pub struct LeafNode {
pub value: ValueId,
}

#[cfg(feature = "py-bindings")]
#[pymethods]
impl LeafNode {
#[new]
pub fn py_init(parent: Parent, hash: Hash, key: KeyId, value: ValueId) -> PyResult<Self> {
Ok(Self {
parent,
hash,
key,
value,
})
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Node {
Internal(InternalNode),
Expand Down
4 changes: 4 additions & 0 deletions wheel/python/chia_rs/datalayer.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BlockIndexOutOfBoundsError(Exception): ...
@final
class InternalNode:
def __init__(self, parent: Optional[uint32], hash: bytes32, left: uint32, right: uint32) -> None: ...
# TODO: also the rest of streamable
def __bytes__(self) -> bytes: ...

@property
def parent(self) -> Optional[uint32]: ...
Expand All @@ -49,6 +51,8 @@ class InternalNode:
@final
class LeafNode:
def __init__(self, parent: Optional[uint32], hash: bytes32, key: int64, value: int64) -> None: ...
# TODO: also the rest of streamable
def __bytes__(self) -> bytes: ...

@property
def parent(self) -> Optional[uint32]: ...
Expand Down
Loading