diff --git a/firewood/src/merkle.rs b/firewood/src/merkle.rs index 224596cb2..ce1d18b2e 100644 --- a/firewood/src/merkle.rs +++ b/firewood/src/merkle.rs @@ -192,7 +192,7 @@ impl + Send + Sync> Merkle { .write(|u| { (*match &mut u.inner { NodeType::Leaf(u) => &mut u.0, - NodeType::Extension(u) => u.path_mut(), + NodeType::Extension(u) => &mut u.path, _ => unreachable!(), }) = PartialPath(n_path[idx + 1..].to_vec()); u.rehash(); @@ -208,7 +208,7 @@ impl + Send + Sync> Merkle { chd[rem_path[idx] as usize] = Some(leaf_ptr); chd[n_path[idx] as usize] = Some(match &u_ref.inner { NodeType::Extension(u) => { - if u.path().len() == 0 { + if u.path.len() == 0 { deleted.push(u_ptr); u.chd() } else { @@ -225,11 +225,11 @@ impl + Send + Sync> Merkle { }); let branch_ptr = self.new_node(Node::new(t))?.as_ptr(); if idx > 0 { - self.new_node(Node::new(NodeType::Extension(ExtNode::new( - rem_path[..idx].to_vec(), - branch_ptr, - None, - ))))? + self.new_node(Node::new(NodeType::Extension(ExtNode { + path: PartialPath(rem_path[..idx].to_vec()), + child: branch_ptr, + child_encoded: None, + })))? .as_ptr() } else { branch_ptr @@ -284,7 +284,7 @@ impl + Send + Sync> Merkle { .write(|u| { (*match &mut u.inner { NodeType::Leaf(u) => &mut u.0, - NodeType::Extension(u) => u.path_mut(), + NodeType::Extension(u) => &mut u.path, _ => unreachable!(), }) = PartialPath(n_path[rem_path.len() + 1..].to_vec()); u.rehash(); @@ -293,7 +293,7 @@ impl + Send + Sync> Merkle { ( match &u_ref.inner { NodeType::Extension(u) => { - if u.path().len() == 0 { + if u.path.len() == 0 { deleted.push(u_ptr); u.chd() } else { @@ -331,11 +331,11 @@ impl + Send + Sync> Merkle { })))? .as_ptr(); if !prefix.is_empty() { - self.new_node(Node::new(NodeType::Extension(ExtNode::new( - prefix.to_vec(), - branch_ptr, - None, - ))))? + self.new_node(Node::new(NodeType::Extension(ExtNode { + path: PartialPath(prefix.to_vec()), + child: branch_ptr, + child_encoded: None, + })))? .as_ptr() } else { branch_ptr @@ -430,7 +430,7 @@ impl + Send + Sync> Merkle { } NodeType::Extension(n) => { - let n_path = n.path().to_vec(); + let n_path = n.path.to_vec(); let n_ptr = n.chd(); let rem_path = once(key_nib).chain(key_nibbles.clone()).collect::>(); let n_path_len = n_path.len(); @@ -493,9 +493,9 @@ impl + Send + Sync> Merkle { } } NodeType::Extension(n) => { - let idx = n.path()[0]; - let more = if n.path().len() > 1 { - *n.path_mut() = PartialPath(n.path()[1..].to_vec()); + let idx = n.path[0]; + let more = if n.path.len() > 1 { + n.path = PartialPath(n.path[1..].to_vec()); true } else { false @@ -591,7 +591,7 @@ impl + Send + Sync> Merkle { // to: P -> [Leaf (v)] let leaf = self .new_node(Node::new(NodeType::Leaf(LeafNode( - PartialPath(n.path().clone().into_inner()), + PartialPath(n.path.clone().into_inner()), val, ))))? .as_ptr(); @@ -614,11 +614,11 @@ impl + Send + Sync> Merkle { // \____[Leaf]x // to: [p: Branch] -> [Ext] -> [Branch] let ext = self - .new_node(Node::new(NodeType::Extension(ExtNode::new( - vec![idx], - c_ptr, - None, - ))))? + .new_node(Node::new(NodeType::Extension(ExtNode { + path: PartialPath(vec![idx]), + child: c_ptr, + child_encoded: None, + })))? .as_ptr(); self.set_parent(ext, &mut [(p_ref, p_idx)]); } @@ -633,7 +633,7 @@ impl + Send + Sync> Merkle { p_ref, |p| { let pp = p.inner.as_extension_mut().unwrap(); - pp.path_mut().0.push(idx); + pp.path.0.push(idx); *pp.chd_mut() = c_ptr; p.rehash(); }, @@ -655,7 +655,7 @@ impl + Send + Sync> Merkle { let write_result = c_ref.write(|c| { let partial_path = match &mut c.inner { NodeType::Leaf(n) => &mut n.0, - NodeType::Extension(n) => n.path_mut(), + NodeType::Extension(n) => &mut n.path, _ => unreachable!(), }; @@ -692,11 +692,11 @@ impl + Send + Sync> Merkle { self, c_ref, |c| { - let mut path = n.path().clone().into_inner(); + let mut path = n.path.clone().into_inner(); path.push(idx); let path0 = match &mut c.inner { NodeType::Leaf(n) => &mut n.0, - NodeType::Extension(n) => n.path_mut(), + NodeType::Extension(n) => &mut n.path, _ => unreachable!(), }; path.extend(&**path0); @@ -743,16 +743,18 @@ impl + Send + Sync> Merkle { // from: [Branch] -> [Branch]x -> [Branch] // to: [Branch] -> [Ext] -> [Branch] n.chd[b_idx as usize] = Some( - self.new_node(Node::new(NodeType::Extension( - ExtNode::new(vec![idx], c_ptr, None), - )))? + self.new_node(Node::new(NodeType::Extension(ExtNode { + path: PartialPath(vec![idx]), + child: c_ptr, + child_encoded: None, + })))? .as_ptr(), ); } NodeType::Extension(n) => { // from: [Ext] -> [Branch]x -> [Branch] // to: [Ext] -> [Branch] - n.path_mut().0.push(idx); + n.path.0.push(idx); *n.chd_mut() = c_ptr } _ => unreachable!(), @@ -777,7 +779,7 @@ impl + Send + Sync> Merkle { let write_result = c_ref.write(|c| { match &mut c.inner { NodeType::Leaf(n) => &mut n.0, - NodeType::Extension(n) => n.path_mut(), + NodeType::Extension(n) => &mut n.path, _ => unreachable!(), } .0 @@ -802,11 +804,11 @@ impl + Send + Sync> Merkle { // from: P -> [Ext] -> [Branch]x -> [Leaf/Ext] // to: P -> [Leaf/Ext] let write_result = c_ref.write(|c| { - let mut path = n.path().clone().into_inner(); + let mut path = n.path.clone().into_inner(); path.push(idx); let path0 = match &mut c.inner { NodeType::Leaf(n) => &mut n.0, - NodeType::Extension(n) => n.path_mut(), + NodeType::Extension(n) => &mut n.path, _ => unreachable!(), }; path.extend(&**path0); @@ -869,7 +871,7 @@ impl + Send + Sync> Merkle { break; } NodeType::Extension(n) => { - let n_path = &*n.path().0; + let n_path = &*n.path.0; let rem_path = &chunks[i..]; if rem_path < n_path || &rem_path[..n_path.len()] != n_path { return Ok(None); @@ -990,7 +992,7 @@ impl + Send + Sync> Merkle { return Ok(Some(RefMut::new(u_ptr, parents, self))); } NodeType::Extension(n) => { - let n_path = &*n.path().0; + let n_path = &*n.path.0; let rem_path = &chunks[i..]; if rem_path.len() < n_path.len() || &rem_path[..n_path.len()] != n_path { return Ok(None); @@ -1070,7 +1072,7 @@ impl + Send + Sync> Merkle { NodeType::Extension(n) => { // the key passed in must match the entire remainder of this // extension node, otherwise we break out - let n_path = n.path(); + let n_path = &n.path; let remaining_path = key_nibbles.into_iter().skip(i); if remaining_path.size_hint().0 < n_path.len() { // all bytes aren't there @@ -1143,7 +1145,7 @@ impl + Send + Sync> Merkle { return Ok(Some(Ref(u_ref))); } NodeType::Extension(n) => { - let n_path = n.path(); + let n_path = &n.path; let rem_path = key_nibbles.into_iter().skip(i); if rem_path.size_hint().0 < n_path.len() { return Ok(None); @@ -1348,20 +1350,20 @@ mod test { Node::new_from_hash( None, None, - NodeType::Extension(ExtNode::new( - vec![0x1, 0x2, 0x3], - DiskAddress::from(0x42), - None, - )), + NodeType::Extension(ExtNode { + path: PartialPath(vec![0x1, 0x2, 0x3]), + child: DiskAddress::from(0x42), + child_encoded: None, + }), ), Node::new_from_hash( None, None, - NodeType::Extension(ExtNode::new( - vec![0x1, 0x2, 0x3], - DiskAddress::null(), - Some(vec![0x1, 0x2, 0x3]), - )), + NodeType::Extension(ExtNode { + path: PartialPath(vec![0x1, 0x2, 0x3]), + child: DiskAddress::null(), + child_encoded: Some(vec![0x1, 0x2, 0x3]), + }), ), Node::new_from_hash( None, @@ -1455,11 +1457,11 @@ mod test { let new_chd_encoded = new_chd.get_encoded(merkle.store.as_ref()); assert_eq!(chd_encoded, new_chd_encoded); - let node = Node::new(NodeType::Extension(ExtNode::new( - vec![0x1, 0x2, 0x3], - DiskAddress::null(), - Some(chd_encoded.to_vec()), - ))); + let node = Node::new(NodeType::Extension(ExtNode { + path: PartialPath(vec![0x1, 0x2, 0x3]), + child: DiskAddress::null(), + child_encoded: Some(chd_encoded.to_vec()), + })); let node_ref = merkle.new_node(node.clone()).unwrap(); let r = node_ref.get_encoded(merkle.store.as_ref()); diff --git a/firewood/src/merkle/node.rs b/firewood/src/merkle/node.rs index 472fd48b0..81967b572 100644 --- a/firewood/src/merkle/node.rs +++ b/firewood/src/merkle/node.rs @@ -244,18 +244,19 @@ impl LeafNode { #[derive(PartialEq, Eq, Clone)] pub struct ExtNode { - path: PartialPath, - chd: DiskAddress, - chd_encoded: Option>, + pub(crate) path: PartialPath, + pub(crate) child: DiskAddress, + pub(crate) child_encoded: Option>, } impl Debug for ExtNode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - write!( - f, - "[Extension {:?} {:?} {:?}]", - self.path, self.chd, self.chd_encoded - ) + let Self { + path, + child, + child_encoded, + } = self; + write!(f, "[Extension {path:?} {child:?} {child_encoded:?}]",) } } @@ -268,8 +269,8 @@ impl ExtNode { .unwrap(), ); - if !self.chd.is_null() { - let mut r = store.get_item(self.chd).unwrap(); + if !self.child.is_null() { + let mut r = store.get_item(self.child).unwrap(); if r.is_encoded_longer_than_hash_len(store) { list[1] = Encoded::Data( @@ -288,7 +289,7 @@ impl ExtNode { } else { // Check if there is already a caclucated encoded value for the child, which // can happen when manually constructing a trie from proof. - if let Some(v) = &self.chd_encoded { + if let Some(v) = &self.child_encoded { if v.len() == TRIE_HASH_LEN { list[1] = Encoded::Data(bincode::DefaultOptions::new().serialize(v).unwrap()); } else { @@ -302,36 +303,20 @@ impl ExtNode { .unwrap() } - pub fn new(path: Vec, chd: DiskAddress, chd_encoded: Option>) -> Self { - ExtNode { - path: PartialPath(path), - chd, - chd_encoded, - } - } - - pub fn path(&self) -> &PartialPath { - &self.path - } - - pub fn path_mut(&mut self) -> &mut PartialPath { - &mut self.path - } - pub fn chd(&self) -> DiskAddress { - self.chd + self.child } pub fn chd_encoded(&self) -> Option<&[u8]> { - self.chd_encoded.as_deref() + self.child_encoded.as_deref() } pub fn chd_mut(&mut self) -> &mut DiskAddress { - &mut self.chd + &mut self.child } pub fn chd_encoded_mut(&mut self) -> &mut Option> { - &mut self.chd_encoded + &mut self.child_encoded } } @@ -411,11 +396,11 @@ impl NodeType { if term { Ok(NodeType::Leaf(LeafNode::new(cur_key, data))) } else { - Ok(NodeType::Extension(ExtNode::new( - cur_key, - DiskAddress::null(), - Some(data), - ))) + Ok(NodeType::Extension(ExtNode { + path: PartialPath(cur_key), + child: DiskAddress::null(), + child_encoded: Some(data), + })) } } BRANCH_NODE_SIZE => Ok(NodeType::Branch(BranchNode::decode(buf)?)), @@ -685,8 +670,8 @@ impl Storable for Node { is_encoded_longer_than_hash_len, NodeType::Extension(ExtNode { path, - chd: DiskAddress::from(ptr as usize), - chd_encoded: encoded, + child: DiskAddress::from(ptr as usize), + child_encoded: encoded, }), )) } @@ -820,7 +805,7 @@ impl Storable for Node { cur.write_all(&[Self::EXT_NODE])?; let path: Vec = from_nibbles(&n.path.encode(false)).collect(); cur.write_all(&[path.len() as u8])?; - cur.write_all(&n.chd.to_le_bytes())?; + cur.write_all(&n.child.to_le_bytes())?; cur.write_all(&path)?; if let Some(encoded) = n.chd_encoded() { cur.write_all(&[encoded.len() as u8])?; diff --git a/firewood/src/proof.rs b/firewood/src/proof.rs index 5473ed728..50b1a3015 100644 --- a/firewood/src/proof.rs +++ b/firewood/src/proof.rs @@ -467,7 +467,7 @@ impl + Send> Proof { Ok((addr, subproof, cur_key.len())) } NodeType::Extension(n) => { - let cur_key = &n.path().0; + let cur_key = &n.path.0; // Check if the key of current node match with the given key. if key.len() < cur_key.len() || &key[..cur_key.len()] != cur_key { @@ -521,7 +521,7 @@ fn locate_subproof( Ok((sub_proof.into(), key_nibbles)) } NodeType::Extension(n) => { - let cur_key = &n.path().0; + let cur_key = &n.path.0; // Check if the key of current node match with the given key // and consume the current-key portion of the nibbles-iterator let does_not_match = key_nibbles.size_hint().0 < cur_key.len() @@ -629,7 +629,7 @@ fn unset_internal, S: ShaleStore + Send + Sync>( NodeType::Extension(n) => { // If either the key of left proof or right proof doesn't match with // shortnode, stop here and the forkpoint is the shortnode. - let cur_key = n.path().clone().into_inner(); + let cur_key = n.path.clone().into_inner(); fork_left = if left_chunks.len() - index < cur_key.len() { left_chunks[index..].cmp(&cur_key) @@ -703,7 +703,7 @@ fn unset_internal, S: ShaleStore + Send + Sync>( // - left proof points to the shortnode, but right proof is greater // - right proof points to the shortnode, but left proof is less let node = n.chd(); - let cur_key = n.path().clone().into_inner(); + let cur_key = n.path.clone().into_inner(); if fork_left.is_lt() && fork_right.is_lt() { return Err(ProofError::EmptyRange); @@ -877,13 +877,13 @@ fn unset_node_ref, S: ShaleStore + Send + Sync>( unset_node_ref(merkle, p, node, key, index + 1, remove_left) } - NodeType::Extension(n) if chunks[index..].starts_with(n.path()) => { + NodeType::Extension(n) if chunks[index..].starts_with(&n.path) => { let node = Some(n.chd()); - unset_node_ref(merkle, p, node, key, index + n.path().len(), remove_left) + unset_node_ref(merkle, p, node, key, index + n.path.len(), remove_left) } NodeType::Extension(n) => { - let cur_key = n.path(); + let cur_key = &n.path; let mut p_ref = merkle .get_node(parent) .map_err(|_| ProofError::NoSuchNode)?;