Skip to content

Commit

Permalink
rename node path fields to partial_path (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Mar 12, 2024
1 parent c9fa316 commit e762c0e
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 90 deletions.
79 changes: 42 additions & 37 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ where
NodeType::Leaf(n) => EncodedNode::new(EncodedNodeType::Leaf(n.clone())),

NodeType::Branch(n) => {
let path = n.path.clone();
let path = n.partial_path.clone();

// pair up DiskAddresses with encoded children and pick the right one
let encoded_children = n.chd().iter().zip(n.children_encoded.iter());
Expand Down Expand Up @@ -179,7 +179,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
self.store
.put_item(
Node::from_branch(BranchNode {
path: vec![].into(),
partial_path: vec![].into(),
children: [None; BranchNode::MAX_CHILDREN],
value: None,
children_encoded: Default::default(),
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
.chain(key_nibbles.clone())
.collect::<Vec<_>>();

let overlap = PrefixOverlap::from(&n.path, &key_remainder);
let overlap = PrefixOverlap::from(&n.partial_path, &key_remainder);

#[allow(clippy::indexing_slicing)]
match (overlap.unique_a.len(), overlap.unique_b.len()) {
Expand Down Expand Up @@ -341,7 +341,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
children[new_leaf_index as usize] = Some(new_leaf);

let new_branch = BranchNode {
path: PartialPath(overlap.shared.to_vec()),
partial_path: PartialPath(overlap.shared.to_vec()),
children,
value: n.data.clone().into(),
children_encoded: Default::default(),
Expand Down Expand Up @@ -374,7 +374,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
.as_ptr();

let mut new_branch = BranchNode {
path: PartialPath(new_branch_path),
partial_path: PartialPath(new_branch_path),
children: [None; BranchNode::MAX_CHILDREN],
value: Some(val.into()),
children_encoded: Default::default(),
Expand Down Expand Up @@ -418,7 +418,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
let new_leaf = self.put_node(new_leaf)?.as_ptr();

let mut new_branch = BranchNode {
path: PartialPath(new_branch_path),
partial_path: PartialPath(new_branch_path),
children: [None; BranchNode::MAX_CHILDREN],
value: None,
children_encoded: Default::default(),
Expand All @@ -437,7 +437,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
break None;
}

NodeType::Branch(n) if n.path.len() == 0 => {
NodeType::Branch(n) if n.partial_path.len() == 0 => {
#[allow(clippy::indexing_slicing)]
match n.children[next_nibble as usize] {
Some(c) => (node, c),
Expand Down Expand Up @@ -470,7 +470,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
.chain(key_nibbles.clone())
.collect::<Vec<_>>();

let overlap = PrefixOverlap::from(&n.path, &key_remainder);
let overlap = PrefixOverlap::from(&n.partial_path, &key_remainder);

#[allow(clippy::indexing_slicing)]
match (overlap.unique_a.len(), overlap.unique_b.len()) {
Expand Down Expand Up @@ -537,7 +537,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
.as_ptr();

let mut new_branch = BranchNode {
path: PartialPath(new_branch_path),
partial_path: PartialPath(new_branch_path),
children: [None; BranchNode::MAX_CHILDREN],
value: Some(val.into()),
children_encoded: Default::default(),
Expand Down Expand Up @@ -583,7 +583,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
let new_leaf = self.put_node(new_leaf)?.as_ptr();

let mut new_branch = BranchNode {
path: PartialPath(new_branch_path),
partial_path: PartialPath(new_branch_path),
children: [None; BranchNode::MAX_CHILDREN],
value: None,
children_encoded: Default::default(),
Expand Down Expand Up @@ -623,15 +623,15 @@ impl<S: CachedStore, T> Merkle<S, T> {
None
}
NodeType::Leaf(n) => {
if n.path.len() == 0 {
if n.partial_path.len() == 0 {
n.data = Data(val);

None
} else {
#[allow(clippy::indexing_slicing)]
let idx = n.path[0];
let idx = n.partial_path[0];
#[allow(clippy::indexing_slicing)]
(n.path = PartialPath(n.path[1..].to_vec()));
(n.partial_path = PartialPath(n.partial_path[1..].to_vec()));
u.rehash();

Some((idx, true, None, val))
Expand Down Expand Up @@ -664,7 +664,7 @@ impl<S: CachedStore, T> Merkle<S, T> {

let branch = self
.put_node(Node::from_branch(BranchNode {
path: vec![].into(),
partial_path: vec![].into(),
children: chd,
value: Some(Data(val)),
children_encoded: Default::default(),
Expand Down Expand Up @@ -714,7 +714,7 @@ impl<S: CachedStore, T> Merkle<S, T> {

// don't change the sentinal node
if children.len() == 1 && !parents.is_empty() {
let branch_path = &branch.path.0;
let branch_path = &branch.partial_path.0;

#[allow(clippy::indexing_slicing)]
let (child_index, child) = children[0];
Expand Down Expand Up @@ -774,7 +774,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
match (children.len(), &branch.value, !parents.is_empty()) {
// node is invalid, all single-child nodes should have data
(1, None, true) => {
let parent_path = &branch.path.0;
let parent_path = &branch.partial_path.0;

#[allow(clippy::indexing_slicing)]
let (child_index, child) = children[0];
Expand All @@ -790,10 +790,10 @@ impl<S: CachedStore, T> Merkle<S, T> {
.iter()
.copied()
.chain(once(child_index as u8))
.chain(child.path.0.iter().copied())
.chain(child.partial_path.0.iter().copied())
.collect();

child.path = PartialPath(path);
child.partial_path = PartialPath(path);

Node::from_branch(child)
}
Expand All @@ -802,10 +802,10 @@ impl<S: CachedStore, T> Merkle<S, T> {
.iter()
.copied()
.chain(once(child_index as u8))
.chain(child.path.0.iter().copied())
.chain(child.partial_path.0.iter().copied())
.collect();

child.path = PartialPath(path);
child.partial_path = PartialPath(path);

Node::from_leaf(child)
}
Expand All @@ -821,7 +821,7 @@ impl<S: CachedStore, T> Merkle<S, T> {
// branch nodes shouldn't have no children
(0, Some(data), true) => {
let leaf = Node::from_leaf(LeafNode::new(
PartialPath(branch.path.0.clone()),
PartialPath(branch.partial_path.0.clone()),
data.clone(),
));

Expand Down Expand Up @@ -958,12 +958,14 @@ impl<S: CachedStore, T> Merkle<S, T> {

let next_ptr = match &node_ref.inner {
#[allow(clippy::indexing_slicing)]
NodeType::Branch(n) if n.path.len() == 0 => match n.children[nib as usize] {
Some(c) => c,
None => return Ok(None),
},
NodeType::Branch(n) if n.partial_path.len() == 0 => {
match n.children[nib as usize] {
Some(c) => c,
None => return Ok(None),
}
}
NodeType::Branch(n) => {
let mut n_path_iter = n.path.iter().copied();
let mut n_path_iter = n.partial_path.iter().copied();

if n_path_iter.next() != Some(nib) {
return Ok(None);
Expand Down Expand Up @@ -994,7 +996,10 @@ impl<S: CachedStore, T> Merkle<S, T> {
}
}
NodeType::Leaf(n) => {
let node_ref = if once(nib).chain(key_nibbles).eq(n.path.iter().copied()) {
let node_ref = if once(nib)
.chain(key_nibbles)
.eq(n.partial_path.iter().copied())
{
Some(node_ref)
} else {
None
Expand All @@ -1011,10 +1016,10 @@ impl<S: CachedStore, T> Merkle<S, T> {

// when we're done iterating over nibbles, check if the node we're at has a value
let node_ref = match &node_ref.inner {
NodeType::Branch(n) if n.value.as_ref().is_some() && n.path.is_empty() => {
NodeType::Branch(n) if n.value.as_ref().is_some() && n.partial_path.is_empty() => {
Some(node_ref)
}
NodeType::Leaf(n) if n.path.len() == 0 => Some(node_ref),
NodeType::Leaf(n) if n.partial_path.len() == 0 => Some(node_ref),
_ => None,
};

Expand Down Expand Up @@ -1461,7 +1466,7 @@ mod tests {
}

Node::from_branch(BranchNode {
path,
partial_path: path,
children,
value,
children_encoded,
Expand All @@ -1483,7 +1488,7 @@ mod tests {
}

Node::from_branch(BranchNode {
path,
partial_path: path,
children,
value,
children_encoded,
Expand Down Expand Up @@ -2084,7 +2089,7 @@ mod tests {
.collect::<Vec<_>>();

let node = Node::from_leaf(LeafNode {
path: PartialPath::from(path),
partial_path: PartialPath::from(path),
data: Data(data.clone()),
});

Expand All @@ -2103,7 +2108,7 @@ mod tests {
.collect::<Vec<_>>();

let node = Node::from_leaf(LeafNode {
path: PartialPath::from(path.clone()),
partial_path: PartialPath::from(path.clone()),
data: Data(data),
});

Expand All @@ -2122,7 +2127,7 @@ mod tests {
.collect::<Vec<_>>();

let node = Node::from_branch(BranchNode {
path: PartialPath::from(path.clone()),
partial_path: PartialPath::from(path.clone()),
children: Default::default(),
value: Some(Data(data.clone())),
children_encoded: Default::default(),
Expand All @@ -2143,7 +2148,7 @@ mod tests {
.collect::<Vec<_>>();

let node = Node::from_branch(BranchNode {
path: PartialPath::from(path.clone()),
partial_path: PartialPath::from(path.clone()),
children: Default::default(),
value: Some(Data(data)),
children_encoded: Default::default(),
Expand Down Expand Up @@ -2187,8 +2192,8 @@ mod tests {
assert_eq!(&to_delete[0], &addr);

let (path, data) = match node.inner() {
NodeType::Leaf(leaf) => (&leaf.path, Some(&leaf.data)),
NodeType::Branch(branch) => (&branch.path, branch.value.as_ref()),
NodeType::Leaf(leaf) => (&leaf.partial_path, Some(&leaf.data)),
NodeType::Branch(branch) => (&branch.partial_path, branch.value.as_ref()),
};

assert_eq!(path, &PartialPath(new_path));
Expand Down
30 changes: 18 additions & 12 deletions firewood/src/merkle/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ impl NodeType {

pub fn path_mut(&mut self) -> &mut PartialPath {
match self {
NodeType::Branch(u) => &mut u.path,
NodeType::Leaf(node) => &mut node.path,
NodeType::Branch(u) => &mut u.partial_path,
NodeType::Leaf(node) => &mut node.partial_path,
}
}

pub fn set_path(&mut self, path: PartialPath) {
match self {
NodeType::Branch(u) => u.path = path,
NodeType::Leaf(node) => node.path = path,
NodeType::Branch(u) => u.partial_path = path,
NodeType::Leaf(node) => node.partial_path = path,
}
}

Expand Down Expand Up @@ -210,7 +210,7 @@ impl Node {
is_encoded_longer_than_hash_len: OnceLock::new(),
inner: NodeType::Branch(
BranchNode {
path: vec![].into(),
partial_path: vec![].into(),
children: [Some(DiskAddress::null()); BranchNode::MAX_CHILDREN],
value: Some(Data(Vec::new())),
children_encoded: Default::default(),
Expand Down Expand Up @@ -524,7 +524,7 @@ impl Serialize for EncodedNode<PlainCodec> {
EncodedNodeType::Leaf(n) => {
let data = Some(&*n.data);
let chd: Vec<(u64, Vec<u8>)> = Default::default();
let path: Vec<_> = from_nibbles(&n.path.encode()).collect();
let path: Vec<_> = from_nibbles(&n.partial_path.encode()).collect();
(chd, data, path)
}

Expand Down Expand Up @@ -580,7 +580,10 @@ impl<'de> Deserialize<'de> for EncodedNode<PlainCodec> {
Data(Vec::new())
};

let node = EncodedNodeType::Leaf(LeafNode { path, data });
let node = EncodedNodeType::Leaf(LeafNode {
partial_path: path,
data,
});

Ok(Self::new(node))
} else {
Expand Down Expand Up @@ -608,7 +611,10 @@ impl Serialize for EncodedNode<Bincode> {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match &self.node {
EncodedNodeType::Leaf(n) => {
let list = [from_nibbles(&n.path.encode()).collect(), n.data.to_vec()];
let list = [
from_nibbles(&n.partial_path.encode()).collect(),
n.data.to_vec(),
];
let mut seq = serializer.serialize_seq(Some(list.len()))?;
for e in list {
seq.serialize_element(&e)?;
Expand Down Expand Up @@ -681,7 +687,7 @@ impl<'de> Deserialize<'de> for EncodedNode<Bincode> {
};
let path = PartialPath::from_nibbles(Nibbles::<0>::new(&path).into_iter());
let node = EncodedNodeType::Leaf(LeafNode {
path,
partial_path: path,
data: Data(data),
});
Ok(Self::new(node))
Expand Down Expand Up @@ -828,7 +834,7 @@ mod tests {
) {
let leaf = NodeType::Leaf(LeafNode::new(PartialPath(vec![1, 2, 3]), Data(vec![4, 5])));
let branch = NodeType::Branch(Box::new(BranchNode {
path: vec![].into(),
partial_path: vec![].into(),
children: [Some(DiskAddress::from(1)); BranchNode::MAX_CHILDREN],
value: Some(Data(vec![1, 2, 3])),
children_encoded: std::array::from_fn(|_| Some(vec![1])),
Expand Down Expand Up @@ -912,7 +918,7 @@ mod tests {
value: impl Into<Option<u8>>,
children_encoded: [Option<Vec<u8>>; BranchNode::MAX_CHILDREN],
) {
let path = PartialPath(path.iter().copied().map(|x| x & 0xf).collect());
let partial_path = PartialPath(path.iter().copied().map(|x| x & 0xf).collect());

let mut children = children.into_iter().map(|x| {
if x == 0 {
Expand All @@ -929,7 +935,7 @@ mod tests {
.map(|x| Data(std::iter::repeat(x).take(x as usize).collect()));

let node = Node::from_branch(BranchNode {
path,
partial_path,
children,
value,
children_encoded,
Expand Down
Loading

0 comments on commit e762c0e

Please sign in to comment.