pub struct TreeNode {
pub id: [u8; 32],
pub hash: [u8; 32],
pub children: Vec<[u8; 32]>,
pub leaf_data: Option<TreeLeafData>,
}Expand description
A node in the Merkle tree.
Can be either an internal node (has children) or a leaf node (has data).
Fields§
§id: [u8; 32]Node ID - stable identifier derived from the node’s position/key in the tree.
For internal nodes: typically hash of path or concatenation of child keys. For leaf nodes: typically hash of the entity key. This ID remains stable even when content changes.
hash: [u8; 32]Merkle hash - changes when subtree content changes.
For internal nodes: hash of all children’s hashes (propagates changes up). For leaf nodes: hash of the leaf data (key + value + metadata). Used for efficient comparison: if hashes match, subtrees are identical.
children: Vec<[u8; 32]>Child node IDs (empty for leaf nodes).
Typically limited to MAX_CHILDREN_PER_NODE. Use is_valid() to check
bounds after deserialization from untrusted sources.
leaf_data: Option<TreeLeafData>Leaf data (present only for leaf nodes).
Implementations§
Source§impl TreeNode
impl TreeNode
Sourcepub fn internal(id: [u8; 32], hash: [u8; 32], children: Vec<[u8; 32]>) -> Self
pub fn internal(id: [u8; 32], hash: [u8; 32], children: Vec<[u8; 32]>) -> Self
Create an internal node.
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Check if node is within valid bounds and structurally valid.
Call this after deserializing from untrusted sources. Validates:
- Children count within MAX_CHILDREN_PER_NODE
- Structural invariant: must have exactly one of children OR leaf_data
- Leaf data validity (value size within limits)
Sourcepub fn is_internal(&self) -> bool
pub fn is_internal(&self) -> bool
Check if this is an internal node.
Sourcepub fn child_count(&self) -> usize
pub fn child_count(&self) -> usize
Get number of children (0 for leaf nodes).
Trait Implementations§
Source§impl BorshDeserialize for TreeNode
impl BorshDeserialize for TreeNode
fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>
Source§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Source§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
Source§impl BorshSerialize for TreeNode
impl BorshSerialize for TreeNode
impl StructuralPartialEq for TreeNode
Auto Trait Implementations§
impl Freeze for TreeNode
impl RefUnwindSafe for TreeNode
impl Send for TreeNode
impl Sync for TreeNode
impl Unpin for TreeNode
impl UnsafeUnpin for TreeNode
impl UnwindSafe for TreeNode
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more