Enum indexed_merkle_tree::Node
source · pub enum Node {
Inner(InnerNode),
Leaf(LeafNode),
}
Expand description
An enum representing the types of nodes in the indexed Merkle Tree.
This enum allows for the differentiation between inner and leaf nodes in the tree,
facilitating operations like traversal, insertion, and proof generation.
It encapsulates either an InnerNode
or a LeafNode
, depending on the node’s position
and role in the tree.
Variants:
Inner(InnerNode)
: An inner node of the tree, containing references to child nodes.Leaf(LeafNode)
: A leaf node, containing the actual data (hash of its metadata).
Variants§
Implementations§
source§impl Node
impl Node
sourcepub const EMPTY_HASH: &'static str = "0000000000000000000000000000000000000000000000000000000000000000"
pub const EMPTY_HASH: &'static str = "0000000000000000000000000000000000000000000000000000000000000000"
A placeholder for label/value values in inactive (empty) leaf nodes in the indexed Merkle Tree. It’s also the fixed label of the first element in the indexed Merkle tree, because it’s the lowest possible number in with 256 output bits from sha256.
sourcepub const TAIL: &'static str = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
pub const TAIL: &'static str = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
This constant is used to designate the last element (because it’s the highest possible number in with 256 output bits from sha256) in the indexed Merkle tree. The next pointer from the largest label in the current tree, as well as the next pointer from inactive leaf nodes “point” to it.
sourcepub fn get_hash(&self) -> String
pub fn get_hash(&self) -> String
Returns the hash of the node.
This function returns the hash of either an inner node or a leaf node, depending on the node type.
sourcepub fn is_left_sibling(&self) -> bool
pub fn is_left_sibling(&self) -> bool
Determines if the node is a left sibling.
This function checks whether the node (either inner or leaf) is a left sibling in its parent node’s context. This information is important in the tree’s traversal and structure maintenance, ensuring the correct positioning and integrity of the nodes.
sourcepub fn is_active(&self) -> bool
pub fn is_active(&self) -> bool
Determines if the node is active.
For inner nodes, this function always returns true. For leaf nodes, it checks the active
flag.
This method is important to understand the current state of a node within the tree,
especially for insert operations to recognize the need for capacity duplication of the tree if necessary.
sourcepub fn set_left_sibling_value(&mut self, is_left: bool)
pub fn set_left_sibling_value(&mut self, is_left: bool)
Sets the left sibling status of the node.
This function updates whether the node (inner or leaf) is considered a left sibling. This is crucial for maintaining the structural integrity of the tree, especially when nodes are inserted or reorganized.
sourcepub fn set_node_active(&mut self)
pub fn set_node_active(&mut self)
Activates a leaf node.
This function sets the active
flag of a leaf node to true. It has no effect on inner nodes, because they are always active.
Activating a leaf node can be an important operation when managing the data within the indexed Merkle Tree,
especially in scenarios involving data updates or dynamic tree modifications.
sourcepub fn initialize_leaf(
active: bool,
is_left: bool,
label: String,
value: String,
next: String
) -> Self
pub fn initialize_leaf( active: bool, is_left: bool, label: String, value: String, next: String ) -> Self
Initializes a new leaf node with the specified properties.
This function creates a leaf node with above defined attributes. The hash is generated based on
its active status, label, value, and next pointer. Additionally, the node is marked as a left sibling or not.
Arguments
active
- Boolean indicating if the leaf is active.is_left
- Boolean indicating if this is a left sibling.label
- Unique 256 bit identifier for the leaf.value
- 256 Bit data value of the leaf.next
- Reference to the next largest node (identified by the label value) in the sequence.
sourcepub fn add_left(&mut self, left: Arc<Self>)
pub fn add_left(&mut self, left: Arc<Self>)
Attaches a node as the left child of an inner node.
This function sets the provided node as the left child of the current inner node.
Arguments
left
- AnArc<Self>
pointing to the node to be set as the left child.
sourcepub fn add_right(&mut self, right: Arc<Self>)
pub fn add_right(&mut self, right: Arc<Self>)
Attaches a node as the right child of an inner node.
This function sets the provided node as the right child of the current inner node.
Arguments
right
- AnArc<Self>
pointing to the node to be set as the right child.
sourcepub fn update_next_pointer(existing_node: &mut Self, new_node: &Self)
pub fn update_next_pointer(existing_node: &mut Self, new_node: &Self)
Updates the ‘next’ pointer of a leaf node.
This function is used to update the reference to the next node in the indexed Merkle Tree.
Arguments
existing_node
- The leaf node to update.new_node
- The new node whose label will be used for the ‘next’ pointer.
sourcepub fn generate_hash(&mut self)
pub fn generate_hash(&mut self)
Generates and updates the hash for the node.
This function computes the hash of a node based on its type and properties. For an inner node, the hash is generated from the concatenated hashes of its left and right children in form of: SHA256(left_child_hash || right_child_hash) For a leaf node, the hash is based on its active status, label, value, and the reference to the next node in the tree: SHA256(active || label || value || next)