Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
Fixup update_child_address
  • Loading branch information
rkuris committed May 22, 2024
1 parent 00ea8db commit a9c98c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
6 changes: 1 addition & 5 deletions firewood/src/hashednode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ impl<T: WriteLinearStore> HashedNodeStore<T> {

let mut updated_parent = parent.clone();

*updated_parent.child_mut(child_index as u8) = Some(new_addr);
*updated_parent
.child_hashes
.get_mut(child_index)
.expect("old_addr must be a child") = Default::default();
updated_parent.update_child_address(child_index, new_addr);

let updated_parent = Node::Branch(updated_parent);
self.update_node(ancestors, old_parent_address, updated_parent)?;
Expand Down
33 changes: 7 additions & 26 deletions storage/src/node/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,13 @@ impl BranchNode {
.as_ref()
}

/// consume a branch node, finding the old child address and setting it
/// to a new one. Also invalidates the hash for it.
pub fn update_child_address(
&self,
old_child_addr: LinearAddress,
new_child_addr: Option<LinearAddress>,
) -> BranchNode {
let mut new_children = self.children;
let (index, child_ref) = new_children
.iter_mut()
.enumerate()
.find(|(_, &mut child_addr)| child_addr == Some(old_child_addr))
.expect("child was not in the parent");
*child_ref = new_child_addr;

let mut new_child_hashes = self.child_hashes.clone();
*new_child_hashes
.get_mut(index)
.expect("arrays are same size, so offset into one must match the other") =
Default::default();
BranchNode {
partial_path: self.partial_path.clone(),
value: self.value.clone(),
children: new_children,
child_hashes: new_child_hashes,
}
/// Updates the child at the given index to the new address.
pub fn update_child_address(&mut self, child_index: usize, new_addr: LinearAddress) {
*self.child_mut(child_index as u8) = Some(new_addr);
*self
.child_hashes
.get_mut(child_index)
.expect("child_index must exist") = Default::default();
}

/// Update the child address of a branch node and invalidate the hash
Expand Down

0 comments on commit a9c98c8

Please sign in to comment.