Skip to content

Commit

Permalink
Rename Node Constructors (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle authored Nov 22, 2023
1 parent c08dfcb commit fe13e4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
30 changes: 18 additions & 12 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
pub fn init_root(&self) -> Result<DiskAddress, MerkleError> {
self.store
.put_item(
Node::branch(BranchNode {
Node::from_branch(BranchNode {
// path: vec![].into(),
children: [None; MAX_CHILDREN],
value: None,
Expand Down Expand Up @@ -198,7 +198,10 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
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];
Expand All @@ -216,7 +219,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {

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,
Expand Down Expand Up @@ -323,10 +326,10 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
}
// 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();

Expand All @@ -347,7 +350,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
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(),
Expand Down Expand Up @@ -456,10 +459,10 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
// 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| {
Expand Down Expand Up @@ -575,7 +578,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
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)),
Expand Down Expand Up @@ -620,7 +623,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
// 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| {
Expand All @@ -633,7 +636,10 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
// 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);
Expand Down Expand Up @@ -1696,7 +1702,7 @@ mod tests {
children_encoded[0] = Some(child);
}

Node::branch(BranchNode {
Node::from_branch(BranchNode {
// path: vec![].into(),
children,
value,
Expand Down
10 changes: 5 additions & 5 deletions firewood/src/merkle/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ impl Node {
self.root_hash = OnceLock::new();
}

pub fn branch<B: Into<Box<BranchNode>>>(node: B) -> Self {
pub fn from_branch<B: Into<Box<BranchNode>>>(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 {
Expand Down Expand Up @@ -643,7 +643,7 @@ pub(super) mod tests {
use test_case::test_case;

pub fn leaf(path: Vec<u8>, data: Vec<u8>) -> Node {
Node::leaf(PartialPath(path), Data(data))
Node::from_leaf(LeafNode::new(PartialPath(path), Data(data)))
}

pub fn branch(
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit fe13e4c

Please sign in to comment.