From fe13e4c831c15455b4bb40de530cda31543c00ae Mon Sep 17 00:00:00 2001 From: Richard Pringle Date: Wed, 22 Nov 2023 16:40:24 -0500 Subject: [PATCH] Rename Node Constructors (#368) --- firewood/src/merkle.rs | 30 ++++++++++++++++++------------ firewood/src/merkle/node.rs | 10 +++++----- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/firewood/src/merkle.rs b/firewood/src/merkle.rs index 10fb47869..c0e28f319 100644 --- a/firewood/src/merkle.rs +++ b/firewood/src/merkle.rs @@ -84,7 +84,7 @@ impl + Send + Sync> Merkle { pub fn init_root(&self) -> Result { self.store .put_item( - Node::branch(BranchNode { + Node::from_branch(BranchNode { // path: vec![].into(), children: [None; MAX_CHILDREN], value: None, @@ -198,7 +198,10 @@ impl + Send + Sync> Merkle { node.rehash(); })?; - let new_node = Node::leaf(PartialPath(new_node_path.to_vec()), Data(val)); + let new_node = Node::from_leaf(LeafNode::new( + PartialPath(new_node_path.to_vec()), + Data(val), + )); let leaf_address = self.put_node(new_node)?.as_ptr(); let mut chd = [None; MAX_CHILDREN]; @@ -216,7 +219,7 @@ impl + Send + Sync> Merkle { chd[n_path[idx] as usize] = Some(address); - let new_branch = Node::branch(BranchNode { + let new_branch = Node::from_branch(BranchNode { // path: PartialPath(matching_path[..idx].to_vec()), children: chd, value: None, @@ -323,10 +326,10 @@ impl + Send + Sync> Merkle { } // insert path is greather than the path of the leaf (Ordering::Greater, Some(n_value)) => { - let leaf = Node::leaf( + let leaf = Node::from_leaf(LeafNode::new( PartialPath(insert_path[n_path.len() + 1..].to_vec()), Data(val), - ); + )); let leaf_address = self.put_node(leaf)?.as_ptr(); @@ -347,7 +350,7 @@ impl + Send + Sync> Merkle { children[idx] = leaf_address.into(); let branch_address = self - .put_node(Node::branch(BranchNode { + .put_node(Node::from_branch(BranchNode { children, value, children_encoded: Default::default(), @@ -456,10 +459,10 @@ impl + Send + Sync> Merkle { // insert the leaf to the empty slot // create a new leaf let leaf_ptr = self - .put_node(Node::leaf( + .put_node(Node::from_leaf(LeafNode::new( PartialPath(key_nibbles.collect()), Data(val), - ))? + )))? .as_ptr(); // set the current child to point to this leaf node.write(|u| { @@ -575,7 +578,7 @@ impl + Send + Sync> Merkle { chd[idx as usize] = Some(c_ptr); let branch = self - .put_node(Node::branch(BranchNode { + .put_node(Node::from_branch(BranchNode { // path: vec![].into(), children: chd, value: Some(Data(val)), @@ -620,7 +623,7 @@ impl + Send + Sync> Merkle { // from: [p: Branch] -> [b (v)]x -> [Leaf]x // to: [p: Branch] -> [Leaf (v)] let leaf = self - .put_node(Node::leaf(PartialPath(Vec::new()), val))? + .put_node(Node::from_leaf(LeafNode::new(PartialPath(Vec::new()), val)))? .as_ptr(); p_ref .write(|p| { @@ -633,7 +636,10 @@ impl + Send + Sync> Merkle { // from: P -> [p: Ext]x -> [b (v)]x -> [leaf]x // to: P -> [Leaf (v)] let leaf = self - .put_node(Node::leaf(PartialPath(n.path.clone().into_inner()), val))? + .put_node(Node::from_leaf(LeafNode::new( + PartialPath(n.path.clone().into_inner()), + val, + )))? .as_ptr(); deleted.push(p_ptr); set_parent(leaf, parents); @@ -1696,7 +1702,7 @@ mod tests { children_encoded[0] = Some(child); } - Node::branch(BranchNode { + Node::from_branch(BranchNode { // path: vec![].into(), children, value, diff --git a/firewood/src/merkle/node.rs b/firewood/src/merkle/node.rs index bcf165f4e..7821aa84d 100644 --- a/firewood/src/merkle/node.rs +++ b/firewood/src/merkle/node.rs @@ -245,12 +245,12 @@ impl Node { self.root_hash = OnceLock::new(); } - pub fn branch>>(node: B) -> Self { + pub fn from_branch>>(node: B) -> Self { Self::from(NodeType::Branch(node.into())) } - pub fn leaf(path: PartialPath, data: Data) -> Self { - Self::from(NodeType::Leaf(LeafNode { path, data })) + pub fn from_leaf(leaf: LeafNode) -> Self { + Self::from(NodeType::Leaf(leaf)) } pub fn inner(&self) -> &NodeType { @@ -643,7 +643,7 @@ pub(super) mod tests { use test_case::test_case; pub fn leaf(path: Vec, data: Vec) -> Node { - Node::leaf(PartialPath(path), Data(data)) + Node::from_leaf(LeafNode::new(PartialPath(path), Data(data))) } pub fn branch( @@ -671,7 +671,7 @@ pub(super) mod tests { }) .unwrap_or_default(); - Node::branch(BranchNode { + Node::from_branch(BranchNode { // path: vec![].into(), children, value: value.map(Data),