From 45e80141aaf40d89e401d3f30b5c6b413cabab0c Mon Sep 17 00:00:00 2001 From: Dan Laine Date: Fri, 15 Mar 2024 09:35:17 -0400 Subject: [PATCH] always use 'value' instead of 'data' to describe the thing inside nodes (#591) --- firewood/examples/insert.rs | 6 +- firewood/src/merkle.rs | 117 ++++++++++++++--------------- firewood/src/merkle/node.rs | 20 ++--- firewood/src/merkle/node/branch.rs | 39 +++++----- firewood/src/merkle/node/leaf.rs | 54 +++++++------ firewood/src/merkle/proof.rs | 30 ++++---- firewood/src/merkle/stream.rs | 26 +++---- firewood/src/shale/mod.rs | 2 +- firewood/tests/merkle.rs | 22 +++--- 9 files changed, 162 insertions(+), 154 deletions(-) diff --git a/firewood/examples/insert.rs b/firewood/examples/insert.rs index 18d987fa4..471360e79 100644 --- a/firewood/examples/insert.rs +++ b/firewood/examples/insert.rs @@ -21,7 +21,7 @@ struct Args { #[arg(short, long, default_value = "1-64", value_parser = string_to_range)] keylen: RangeInclusive, #[arg(short, long, default_value = "32", value_parser = string_to_range)] - datalen: RangeInclusive, + valuelen: RangeInclusive, #[arg(short, long, default_value_t = 1)] batch_size: usize, #[arg(short, long, default_value_t = 100)] @@ -65,7 +65,7 @@ async fn main() -> Result<(), Box> { for _ in 0..args.number_of_batches { let keylen = rng.gen_range(args.keylen.clone()); - let datalen = rng.gen_range(args.datalen.clone()); + let valuelen = rng.gen_range(args.valuelen.clone()); let batch: Batch, Vec> = (0..keys) .map(|_| { ( @@ -75,7 +75,7 @@ async fn main() -> Result<(), Box> { .collect::>(), rng.borrow_mut() .sample_iter(&Alphanumeric) - .take(datalen) + .take(valuelen) .collect::>(), ) }) diff --git a/firewood/src/merkle.rs b/firewood/src/merkle.rs index 216969776..d5a15ee31 100644 --- a/firewood/src/merkle.rs +++ b/firewood/src/merkle.rs @@ -117,7 +117,7 @@ where EncodedNode { partial_path: n.partial_path.clone(), children: Box::new(children), - value: n.data.clone().into(), + value: n.value.clone().into(), phantom: PhantomData, } } @@ -318,9 +318,9 @@ impl Merkle { #[allow(clippy::indexing_slicing)] match (overlap.unique_a.len(), overlap.unique_b.len()) { - // same node, overwrite the data + // same node, overwrite the value (0, 0) => { - self.update_data_and_move_node_if_larger( + self.update_value_and_move_node_if_larger( (&mut parents, &mut deleted), node, val, @@ -344,7 +344,7 @@ impl Merkle { let new_branch = BranchNode { partial_path: Path(overlap.shared.to_vec()), children, - value: n.data.clone().into(), + value: n.value.clone().into(), children_encoded: Default::default(), }; @@ -472,9 +472,9 @@ impl Merkle { #[allow(clippy::indexing_slicing)] match (overlap.unique_a.len(), overlap.unique_b.len()) { - // same node, overwrite the data + // same node, overwrite the value (0, 0) => { - self.update_data_and_move_node_if_larger( + self.update_value_and_move_node_if_larger( (&mut parents, &mut deleted), node, val, @@ -619,7 +619,7 @@ impl Merkle { } NodeType::Leaf(n) => { if n.partial_path.len() == 0 { - n.data = val; + n.value = val; None } else { @@ -684,7 +684,7 @@ impl Merkle { let mut deleted = Vec::new(); - let data = { + let value = { let (node, mut parents) = self.get_node_and_parents_by_key(self.get_node(root)?, key)?; @@ -692,22 +692,21 @@ impl Merkle { return Ok(None); }; - let data = match &node.inner { + let value = match &node.inner { NodeType::Branch(branch) => { - let data = branch.value.clone(); - let children = branch.children; - - if data.is_none() { + let value = branch.value.clone(); + if value.is_none() { return Ok(None); } - let children: Vec<_> = children + let children: Vec<_> = branch + .children .iter() .enumerate() .filter_map(|(i, child)| child.map(|child| (i, child))) .collect(); - // don't change the sentinal node + // don't change the sentinel node if children.len() == 1 && !parents.is_empty() { let branch_path = &branch.partial_path.0; @@ -738,11 +737,11 @@ impl Merkle { })? } - data + value } NodeType::Leaf(n) => { - let data = Some(n.data.clone()); + let value = Some(n.value.clone()); // TODO: handle unwrap better deleted.push(node.as_ptr()); @@ -767,7 +766,7 @@ impl Merkle { .collect(); match (children.len(), &branch.value, !parents.is_empty()) { - // node is invalid, all single-child nodes should have data + // node is invalid, all single-child nodes should have a value (1, None, true) => { let parent_path = &branch.partial_path.0; @@ -814,10 +813,10 @@ impl Merkle { } // branch nodes shouldn't have no children - (0, Some(data), true) => { + (0, Some(value), true) => { let leaf = Node::from_leaf(LeafNode::new( Path(branch.partial_path.0.clone()), - data.clone(), + value.clone(), )); let leaf = self.put_node(leaf)?.as_ptr(); @@ -829,7 +828,7 @@ impl Merkle { _ => parent.write(|parent| parent.rehash())?, } - data + value } }; @@ -837,14 +836,14 @@ impl Merkle { parent.write(|u| u.rehash())?; } - data + value }; for ptr in deleted.into_iter() { self.free_node(ptr)?; } - Ok(data) + Ok(value) } fn remove_tree_( @@ -1146,7 +1145,7 @@ impl Merkle { // transpose the Option> to Result, E> // If this is an error, the ? operator will return it - let Some((first_key, first_data)) = first_result.transpose()? else { + let Some((first_key, first_value)) = first_result.transpose()? else { // nothing returned, either the trie is empty or the key wasn't found return Ok(None); }; @@ -1156,7 +1155,7 @@ impl Merkle { .map_err(|e| api::Error::InternalError(Box::new(e)))?; let limit = limit.map(|old_limit| old_limit - 1); - let mut middle = vec![(first_key.into_vec(), first_data)]; + let mut middle = vec![(first_key.into_vec(), first_value)]; // we stop streaming if either we hit the limit or the key returned was larger // than the largest key requested @@ -1220,16 +1219,16 @@ impl Merkle { self.move_node_if_write_failed((parents, to_delete), node, write_result) } - /// Try to update the [NodeObjRef]'s data/value in-place. If the update fails because the node can no longer fit at its old address, + /// Try to update the [NodeObjRef]'s value in-place. If the update fails because the node can no longer fit at its old address, /// then the old address is marked for deletion and the [Node] (with its update) is inserted at a new address. - fn update_data_and_move_node_if_larger<'a>( + fn update_value_and_move_node_if_larger<'a>( &'a self, (parents, to_delete): (&mut [(NodeObjRef, u8)], &mut Vec), mut node: NodeObjRef<'a>, - data: Vec, + value: Vec, ) -> Result { let write_result = node.write(|node| { - node.inner_mut().set_data(data); + node.inner_mut().set_value(value); node.rehash(); }); @@ -1285,7 +1284,7 @@ impl<'a> std::ops::Deref for Ref<'a> { fn deref(&self) -> &[u8] { match &self.0.inner { NodeType::Branch(n) => n.value.as_ref().unwrap(), - NodeType::Leaf(n) => &n.data, + NodeType::Leaf(n) => &n.value, } } } @@ -1324,7 +1323,7 @@ impl<'a, S: CachedStore, T> RefMut<'a, S, T> { #[allow(clippy::unwrap_used)] modify(match &mut u.inner { NodeType::Branch(n) => n.value.as_mut().unwrap(), - NodeType::Leaf(n) => &mut n.data, + NodeType::Leaf(n) => &mut n.value, }); u.rehash() }, @@ -1396,8 +1395,8 @@ mod tests { use shale::{cached::InMemLinearStore, CachedStore}; use test_case::test_case; - fn leaf(path: Vec, data: Vec) -> Node { - Node::from_leaf(LeafNode::new(Path(path), data)) + fn leaf(path: Vec, value: Vec) -> Node { + Node::from_leaf(LeafNode::new(Path(path), value)) } #[test_case(vec![0x12, 0x34, 0x56], &[0x1, 0x2, 0x3, 0x4, 0x5, 0x6])] @@ -1467,13 +1466,13 @@ mod tests { }) } - fn branch_without_data(path: &[u8], encoded_child: Option>) -> Node { + fn branch_without_value(path: &[u8], encoded_child: Option>) -> Node { let path = path.to_vec(); let path = Nibbles::<0>::new(&path); let path = Path(path.into_iter().collect()); let children = Default::default(); - // TODO: Properly test empty data as a value + // TODO: Properly test empty value let value = None; let mut children_encoded = <[Option>; BranchNode::MAX_CHILDREN]>::default(); @@ -1493,7 +1492,7 @@ mod tests { #[test_case(leaf(vec![1, 2, 3], vec![4, 5]) ; "leaf encoding")] #[test_case(branch(b"", b"value", vec![1, 2, 3].into()) ; "branch with chd")] #[test_case(branch(b"", b"value", None); "branch without chd")] - #[test_case(branch_without_data(b"", None); "branch without value and chd")] + #[test_case(branch_without_value(b"", None); "branch without value and chd")] #[test_case(branch(b"", b"", None); "branch without path value or children")] #[test_case(branch(b"", b"value", None) ; "branch with value")] #[test_case(branch(&[2], b"", None); "branch with path")] @@ -2074,7 +2073,7 @@ mod tests { #[test] fn update_leaf_with_larger_path() -> Result<(), MerkleError> { let path = vec![0x00]; - let data = vec![0x00]; + let value = vec![0x00]; let double_path = path .clone() @@ -2084,35 +2083,35 @@ mod tests { let node = Node::from_leaf(LeafNode { partial_path: Path::from(path), - data: data.clone(), + value: value.clone(), }); - check_node_update(node, double_path, data) + check_node_update(node, double_path, value) } #[test] - fn update_leaf_with_larger_data() -> Result<(), MerkleError> { + fn update_leaf_with_larger_value() -> Result<(), MerkleError> { let path = vec![0x00]; - let data = vec![0x00]; + let value = vec![0x00]; - let double_data = data + let double_value = value .clone() .into_iter() - .chain(data.clone()) + .chain(value.clone()) .collect::>(); let node = Node::from_leaf(LeafNode { partial_path: Path::from(path.clone()), - data, + value, }); - check_node_update(node, path, double_data) + check_node_update(node, path, double_value) } #[test] fn update_branch_with_larger_path() -> Result<(), MerkleError> { let path = vec![0x00]; - let data = vec![0x00]; + let value = vec![0x00]; let double_path = path .clone() @@ -2123,38 +2122,38 @@ mod tests { let node = Node::from_branch(BranchNode { partial_path: Path::from(path.clone()), children: Default::default(), - value: Some(data.clone()), + value: Some(value.clone()), children_encoded: Default::default(), }); - check_node_update(node, double_path, data) + check_node_update(node, double_path, value) } #[test] - fn update_branch_with_larger_data() -> Result<(), MerkleError> { + fn update_branch_with_larger_value() -> Result<(), MerkleError> { let path = vec![0x00]; - let data = vec![0x00]; + let value = vec![0x00]; - let double_data = data + let double_value = value .clone() .into_iter() - .chain(data.clone()) + .chain(value.clone()) .collect::>(); let node = Node::from_branch(BranchNode { partial_path: Path::from(path.clone()), children: Default::default(), - value: Some(data), + value: Some(value), children_encoded: Default::default(), }); - check_node_update(node, path, double_data) + check_node_update(node, path, double_value) } fn check_node_update( node: Node, new_path: Vec, - new_data: Vec, + new_value: Vec, ) -> Result<(), MerkleError> { let merkle = create_test_merkle(); let root = merkle.init_root()?; @@ -2166,7 +2165,7 @@ mod tests { // make sure that doubling the path length will fail on a normal write let write_result = node_ref.write(|node| { node.inner_mut().set_path(Path(new_path.clone())); - node.inner_mut().set_data(new_data.clone()); + node.inner_mut().set_value(new_value.clone()); node.rehash(); }); @@ -2185,13 +2184,13 @@ mod tests { assert_ne!(node.as_ptr(), addr); assert_eq!(&to_delete[0], &addr); - let (path, data) = match node.inner() { - NodeType::Leaf(leaf) => (&leaf.partial_path, Some(&leaf.data)), + let (path, value) = match node.inner() { + NodeType::Leaf(leaf) => (&leaf.partial_path, Some(&leaf.value)), NodeType::Branch(branch) => (&branch.partial_path, branch.value.as_ref()), }; assert_eq!(path, &Path(new_path)); - assert_eq!(data, Some(&new_data)); + assert_eq!(value, Some(&new_value)); Ok(()) } diff --git a/firewood/src/merkle/node.rs b/firewood/src/merkle/node.rs index a32d94830..df5871005 100644 --- a/firewood/src/merkle/node.rs +++ b/firewood/src/merkle/node.rs @@ -69,9 +69,9 @@ impl NodeType { let cur_key = cur_key_path.into_inner(); #[allow(clippy::unwrap_used)] - let data: Vec = items.next().unwrap(); + let value: Vec = items.next().unwrap(); - Ok(NodeType::Leaf(LeafNode::new(cur_key, data))) + Ok(NodeType::Leaf(LeafNode::new(cur_key, value))) } // TODO: add path BranchNode::MSIZE => Ok(NodeType::Branch(BranchNode::decode(buf)?.into())), @@ -102,10 +102,10 @@ impl NodeType { } } - pub fn set_data(&mut self, data: Vec) { + pub fn set_value(&mut self, value: Vec) { match self { - NodeType::Branch(u) => u.value = Some(data), - NodeType::Leaf(node) => node.data = data, + NodeType::Branch(u) => u.value = Some(value), + NodeType::Leaf(node) => node.value = value, } } } @@ -598,9 +598,9 @@ impl<'de> Deserialize<'de> for EncodedNode { "incorrect encoded type for leaf node path", )); }; - let Some(data) = items.next() else { + let Some(value) = items.next() else { return Err(D::Error::custom( - "incorrect encoded type for leaf node data", + "incorrect encoded type for leaf node value", )); }; let path = Path::from_nibbles(Nibbles::<0>::new(&path).into_iter()); @@ -608,7 +608,7 @@ impl<'de> Deserialize<'de> for EncodedNode { Ok(Self { partial_path: path, children: children.into(), - value: Some(data), + value: Some(value), phantom: PhantomData, }) } @@ -779,10 +779,10 @@ mod tests { (0..0, 0..15, 0..16, 0..31, 0..32), [0..0, 0..16, 0..32] )] - fn leaf_node>(path: Iter, data: Iter) { + fn leaf_node>(path: Iter, value: Iter) { let node = Node::from_leaf(LeafNode::new( Path(path.map(|x| x & 0xf).collect()), - data.collect::>(), + value.collect::>(), )); check_node_encoding(node); diff --git a/firewood/src/merkle/node/branch.rs b/firewood/src/merkle/node/branch.rs index 0159f0ea8..f511e87e8 100644 --- a/firewood/src/merkle/node/branch.rs +++ b/firewood/src/merkle/node/branch.rs @@ -16,7 +16,7 @@ use std::{ }; type PathLen = u8; -pub type DataLen = u32; +pub type ValueLen = u32; pub type EncodedChildLen = u8; const MAX_CHILDREN: usize = 16; @@ -90,9 +90,9 @@ impl BranchNode { // we've already validated the size, that's why we can safely unwrap #[allow(clippy::unwrap_used)] - let data = items.pop().unwrap(); + let value = items.pop().unwrap(); // Extract the value of the branch node and set to None if it's an empty Vec - let value = Some(data).filter(|data| !data.is_empty()); + let value = Some(value).filter(|value| !value.is_empty()); // encode all children. let mut chd_encoded: [Option>; Self::MAX_CHILDREN] = Default::default(); @@ -100,7 +100,7 @@ impl BranchNode { // we popped the last element, so their should only be NBRANCH items left for (i, chd) in items.into_iter().enumerate() { #[allow(clippy::indexing_slicing)] - (chd_encoded[i] = Some(chd).filter(|data| !data.is_empty())); + (chd_encoded[i] = Some(chd).filter(|value| !value.is_empty())); } Ok(BranchNode { @@ -175,14 +175,14 @@ impl BranchNode { impl Storable for BranchNode { fn serialized_len(&self) -> u64 { let children_len = Self::MAX_CHILDREN as u64 * DiskAddress::MSIZE; - let data_len = optional_data_len::(self.value.as_deref()); + let value_len = optional_value_len::(self.value.as_deref()); let children_encoded_len = self.children_encoded.iter().fold(0, |len, child| { - len + optional_data_len::(child.as_ref()) + len + optional_value_len::(child.as_ref()) }); let path_len_size = size_of::() as u64; let path_len = self.partial_path.serialized_len(); - children_len + data_len + children_encoded_len + path_len_size + path_len + children_len + value_len + children_encoded_len + path_len_size + path_len } fn serialize(&self, to: &mut [u8]) -> Result<(), crate::shale::ShaleError> { @@ -200,8 +200,8 @@ impl Storable for BranchNode { let (value_len, value) = self .value .as_ref() - .map(|val| (val.len() as DataLen, &**val)) - .unwrap_or((DataLen::MAX, &[])); + .map(|val| (val.len() as ValueLen, &**val)) + .unwrap_or((ValueLen::MAX, &[])); cursor.write_all(&value_len.to_le_bytes())?; cursor.write_all(value)?; @@ -224,9 +224,9 @@ impl Storable for BranchNode { mem: &T, ) -> Result { const PATH_LEN_SIZE: u64 = size_of::() as u64; - const DATA_LEN_SIZE: usize = size_of::(); + const VALUE_LEN_SIZE: usize = size_of::(); const BRANCH_HEADER_SIZE: u64 = - BranchNode::MAX_CHILDREN as u64 * DiskAddress::MSIZE + DATA_LEN_SIZE as u64; + BranchNode::MAX_CHILDREN as u64 * DiskAddress::MSIZE + VALUE_LEN_SIZE as u64; let path_len = mem .get_view(addr, PATH_LEN_SIZE) @@ -280,16 +280,16 @@ impl Storable for BranchNode { } let raw_len = { - let mut buf = [0; DATA_LEN_SIZE]; + let mut buf = [0; VALUE_LEN_SIZE]; cursor.read_exact(&mut buf)?; - Some(DataLen::from_le_bytes(buf)) - .filter(|len| *len != DataLen::MAX) + Some(ValueLen::from_le_bytes(buf)) + .filter(|len| *len != ValueLen::MAX) .map(|len| len as u64) }; let value = match raw_len { Some(len) => { - let data = mem + let value = mem .get_view(addr, len) .ok_or(ShaleError::InvalidCacheView { offset: addr, @@ -298,7 +298,7 @@ impl Storable for BranchNode { addr += len as usize; - Some(data.as_deref()) + Some(value.as_deref()) } None => None, }; @@ -354,6 +354,9 @@ impl Storable for BranchNode { } } -fn optional_data_len>(data: Option) -> u64 { - size_of::() as u64 + data.as_ref().map_or(0, |data| data.as_ref().len() as u64) +fn optional_value_len>(value: Option) -> u64 { + size_of::() as u64 + + value + .as_ref() + .map_or(0, |value| value.as_ref().len() as u64) } diff --git a/firewood/src/merkle/node/leaf.rs b/firewood/src/merkle/node/leaf.rs index a22fa850b..3cc438940 100644 --- a/firewood/src/merkle/node/leaf.rs +++ b/firewood/src/merkle/node/leaf.rs @@ -17,12 +17,12 @@ use std::{ pub const SIZE: usize = 2; type PathLen = u8; -type DataLen = u32; +type ValueLen = u32; #[derive(PartialEq, Eq, Clone)] pub struct LeafNode { pub(crate) partial_path: Path, - pub(crate) data: Vec, + pub(crate) value: Vec, } impl Debug for LeafNode { @@ -31,16 +31,16 @@ impl Debug for LeafNode { f, "[Leaf {:?} {}]", self.partial_path, - hex::encode(&*self.data) + hex::encode(&*self.value) ) } } impl LeafNode { - pub fn new, D: Into>>(partial_path: P, data: D) -> Self { + pub fn new, V: Into>>(partial_path: P, value: V) -> Self { Self { partial_path: partial_path.into(), - data: data.into(), + value: value.into(), } } @@ -48,8 +48,8 @@ impl LeafNode { &self.partial_path } - pub const fn data(&self) -> &Vec { - &self.data + pub const fn value(&self) -> &Vec { + &self.value } pub(super) fn encode(&self) -> Vec { @@ -58,7 +58,7 @@ impl LeafNode { .serialize( [ nibbles_to_bytes_iter(&self.partial_path.encode()).collect(), - self.data.to_vec(), + self.value.to_vec(), ] .as_slice(), ) @@ -70,7 +70,7 @@ impl LeafNode { #[repr(C, packed)] struct Meta { path_len: PathLen, - data_len: DataLen, + value_len: ValueLen, } impl Meta { @@ -81,9 +81,9 @@ impl Storable for LeafNode { fn serialized_len(&self) -> u64 { let meta_len = size_of::() as u64; let path_len = self.partial_path.serialized_len(); - let data_len = self.data.len() as u64; + let value_len = self.value.len() as u64; - meta_len + path_len + data_len + meta_len + path_len + value_len } fn serialize(&self, to: &mut [u8]) -> Result<(), crate::shale::ShaleError> { @@ -91,12 +91,15 @@ impl Storable for LeafNode { let path = &self.partial_path.encode(); let path = nibbles_to_bytes_iter(path); - let data = &self.data; + let value = &self.value; let path_len = self.partial_path.serialized_len() as PathLen; - let data_len = data.len() as DataLen; + let value_len = value.len() as ValueLen; - let meta = Meta { path_len, data_len }; + let meta = Meta { + path_len, + value_len, + }; cursor.write_all(bytemuck::bytes_of(&meta))?; @@ -104,7 +107,7 @@ impl Storable for LeafNode { cursor.write_all(&[nibble])?; } - cursor.write_all(data)?; + cursor.write_all(value)?; Ok(()) } @@ -125,24 +128,27 @@ impl Storable for LeafNode { .as_deref(); let offset = offset + Meta::SIZE; - let Meta { path_len, data_len } = *bytemuck::from_bytes(&node_header_raw); - let size = path_len as u64 + data_len as u64; + let Meta { + path_len, + value_len, + } = *bytemuck::from_bytes(&node_header_raw); + let size = path_len as u64 + value_len as u64; let remainder = mem .get_view(offset, size) .ok_or(InvalidCacheView { offset, size })? .as_deref(); - let (path, data) = remainder.split_at(path_len as usize); + let (path, value) = remainder.split_at(path_len as usize); let path = { let nibbles = Nibbles::<0>::new(path).into_iter(); Path::from_nibbles(nibbles).0 }; - let data = data.to_vec(); + let value = value.to_vec(); - Ok(Self::new(path, data)) + Ok(Self::new(path, value)) } } @@ -160,15 +166,15 @@ mod tests { // This is combined with the first nibble of the path (0b0000_0010) to become 0b0001_0010 #[test_case(0b0001_0010, vec![0x34], vec![2, 3, 4]; "odd length")] fn encode_regression_test(prefix: u8, path: Vec, nibbles: Vec) { - let data = vec![5, 6, 7, 8]; + let value = vec![5, 6, 7, 8]; let serialized_path = [vec![prefix], path.clone()].concat(); let serialized_path = [vec![serialized_path.len() as u8], serialized_path].concat(); - let serialized_data = [vec![data.len() as u8], data.clone()].concat(); + let serialized_value = [vec![value.len() as u8], value.clone()].concat(); - let serialized = [vec![2], serialized_path, serialized_data].concat(); + let serialized = [vec![2], serialized_path, serialized_value].concat(); - let node = LeafNode::new(nibbles, data.clone()); + let node = LeafNode::new(nibbles, value.clone()); assert_eq!(node.encode(), serialized); } diff --git a/firewood/src/merkle/proof.rs b/firewood/src/merkle/proof.rs index fc49f7c23..e07ae6a06 100644 --- a/firewood/src/merkle/proof.rs +++ b/firewood/src/merkle/proof.rs @@ -96,7 +96,7 @@ impl From for ProofError { /// A proof that a single key is present /// -/// The generic N represents the storage for the node data +/// The generic N represents the storage for the node #[derive(Clone, Debug)] pub struct Proof(pub HashMap); @@ -106,7 +106,7 @@ pub struct Proof(pub HashMap); #[derive(Debug)] enum SubProof { - Data(Vec), + Value(Vec), Hash(HashKey), } @@ -115,7 +115,7 @@ impl + Send> Proof { /// key in a trie with the given root hash. VerifyProof returns an error if the /// proof contains invalid trie nodes or the wrong value. /// - /// The generic N represents the storage for the node data + /// The generic N represents the storage for the node pub fn verify>( &self, key: K, @@ -138,7 +138,7 @@ impl + Send> Proof { cur_hash = match sub_proof { // Return when reaching the end of the key. - Some(SubProof::Data(value)) if key_nibbles.is_empty() => return Ok(Some(value)), + Some(SubProof::Value(value)) if key_nibbles.is_empty() => return Ok(Some(value)), // The trie doesn't contain the key. Some(SubProof::Hash(hash)) => hash, _ => return Ok(None), @@ -208,7 +208,7 @@ impl + Send> Proof { // Special case, there is only one element and two edge keys are same. // In this case, we can't construct two edge paths. So handle it here. if keys.len() == 1 && first_key.as_ref() == last_key.as_ref() { - let data = + let value = self.proof_to_path(first_key.as_ref(), root_hash, &mut in_mem_merkle, false)?; #[allow(clippy::indexing_slicing)] @@ -216,7 +216,7 @@ impl + Send> Proof { // correct proof but invalid key Err(ProofError::InvalidEdgeKeys) } else { - match data { + match value { #[allow(clippy::indexing_slicing)] Some(val) if val == vals[0].as_ref() => Ok(true), None => Ok(false), @@ -337,7 +337,7 @@ impl + Send> Proof { .iter() .copied() .eq(key_nibbles) // all nibbles have to match - .then(|| n.data().to_vec()); + .then(|| n.value().to_vec()); } NodeType::Branch(n) => { @@ -356,11 +356,11 @@ impl + Send> Proof { .chd_encode() .get(*index as usize) .and_then(|inner| inner.as_ref()) - .map(|data| &**data); + .map(|value| &**value); subproof } else { - break n.value.as_ref().map(|data| data.to_vec()); + break n.value.as_ref().map(|value| value.to_vec()); } } }; @@ -377,7 +377,7 @@ impl + Send> Proof { }; match sub_proof { - Some(data) => Ok(Some(data)), + Some(value) => Ok(Some(value)), None if allow_non_existent_node => Ok(None), None => Err(ProofError::NodeNotInTrie), } @@ -413,9 +413,9 @@ fn locate_subproof( return Ok((None, Nibbles::<0>::new(&[]).into_iter())); } - let encoded: Vec = n.data().to_vec(); + let encoded: Vec = n.value().to_vec(); - let sub_proof = SubProof::Data(encoded); + let sub_proof = SubProof::Value(encoded); Ok((sub_proof.into(), key_nibbles)) } @@ -434,17 +434,17 @@ fn locate_subproof( let Some(index) = key_nibbles.next().map(|nib| nib as usize) else { let encoded = n.value; - let sub_proof = encoded.map(SubProof::Data); + let sub_proof = encoded.map(SubProof::Value); return Ok((sub_proof, key_nibbles)); }; // consume items returning the item at index #[allow(clippy::indexing_slicing)] - let data = n.chd_encode()[index] + let value = n.chd_encode()[index] .as_ref() .ok_or(ProofError::InvalidData)?; - generate_subproof(data).map(|subproof| (Some(subproof), key_nibbles)) + generate_subproof(value).map(|subproof| (Some(subproof), key_nibbles)) } } } diff --git a/firewood/src/merkle/stream.rs b/firewood/src/merkle/stream.rs index d0752c46e..e8e3e16be 100644 --- a/firewood/src/merkle/stream.rs +++ b/firewood/src/merkle/stream.rs @@ -381,7 +381,7 @@ impl<'a, S: CachedStore, T> Stream for MerkleKeyValueStream<'a, S, T> { Poll::Ready(Some(Ok((key, value)))) } NodeType::Leaf(leaf) => { - let value = leaf.data.to_vec(); + let value = leaf.value.to_vec(); Poll::Ready(Some(Ok((key, value)))) } }, @@ -635,7 +635,7 @@ mod tests { }; assert_eq!(key, vec![0x01, 0x03, 0x03, 0x07].into_boxed_slice()); - assert_eq!(node.inner().as_leaf().unwrap().data, vec![0x42]); + assert_eq!(node.inner().as_leaf().unwrap().value, vec![0x42]); assert!(stream.next().is_none()); } @@ -683,7 +683,7 @@ mod tests { vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F].into_boxed_slice() ); assert_eq!( - node.inner().as_leaf().unwrap().data, + node.inner().as_leaf().unwrap().value, vec![0x00, 0x00, 0x00, 0x0FF], ); @@ -753,7 +753,7 @@ mod tests { let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00].into_boxed_slice()); - assert_eq!(node.inner().as_leaf().unwrap().data.to_vec(), vec![0x00]); + assert_eq!(node.inner().as_leaf().unwrap().value.to_vec(), vec![0x00]); check_stream_is_done(stream).await; } @@ -823,26 +823,26 @@ mod tests { let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00, 0x00, 0x00, 0x01].into_boxed_slice()); let node = node.inner().as_leaf().unwrap(); - assert_eq!(node.clone().data.to_vec(), vec![0x00, 0x00, 0x00, 0x01]); + assert_eq!(node.clone().value.to_vec(), vec![0x00, 0x00, 0x00, 0x01]); assert_eq!(node.partial_path.to_vec(), vec![0x01]); let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00, 0x00, 0x00, 0xFF].into_boxed_slice()); let node = node.inner().as_leaf().unwrap(); - assert_eq!(node.clone().data.to_vec(), vec![0x00, 0x00, 0x00, 0xFF]); + assert_eq!(node.clone().value.to_vec(), vec![0x00, 0x00, 0x00, 0xFF]); assert_eq!(node.partial_path.to_vec(), vec![0x0F]); let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00, 0xD0, 0xD0].into_boxed_slice()); let node = node.inner().as_leaf().unwrap(); - assert_eq!(node.clone().data.to_vec(), vec![0x00, 0xD0, 0xD0]); + assert_eq!(node.clone().value.to_vec(), vec![0x00, 0xD0, 0xD0]); assert_eq!(node.partial_path.to_vec(), vec![0x00, 0x0D, 0x00]); // 0x0D00 becomes 0xDO // Covers case of leaf with no partial path let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00, 0xFF].into_boxed_slice()); let node = node.inner().as_leaf().unwrap(); - assert_eq!(node.clone().data.to_vec(), vec![0x00, 0xFF]); + assert_eq!(node.clone().value.to_vec(), vec![0x00, 0xFF]); assert_eq!(node.partial_path.to_vec(), vec![0x0F]); check_stream_is_done(stream).await; @@ -857,7 +857,7 @@ mod tests { let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00, 0xD0, 0xD0].into_boxed_slice()); assert_eq!( - node.inner().as_leaf().unwrap().clone().data.to_vec(), + node.inner().as_leaf().unwrap().clone().value.to_vec(), vec![0x00, 0xD0, 0xD0] ); @@ -865,7 +865,7 @@ mod tests { let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00, 0xFF].into_boxed_slice()); assert_eq!( - node.inner().as_leaf().unwrap().clone().data.to_vec(), + node.inner().as_leaf().unwrap().clone().value.to_vec(), vec![0x00, 0xFF] ); @@ -881,7 +881,7 @@ mod tests { let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00, 0xD0, 0xD0].into_boxed_slice()); assert_eq!( - node.inner().as_leaf().unwrap().clone().data.to_vec(), + node.inner().as_leaf().unwrap().clone().value.to_vec(), vec![0x00, 0xD0, 0xD0] ); @@ -889,7 +889,7 @@ mod tests { let (key, node) = stream.next().await.unwrap().unwrap(); assert_eq!(key, vec![0x00, 0xFF].into_boxed_slice()); assert_eq!( - node.inner().as_leaf().unwrap().clone().data.to_vec(), + node.inner().as_leaf().unwrap().clone().value.to_vec(), vec![0x00, 0xFF] ); @@ -1039,7 +1039,7 @@ mod tests { } #[tokio::test] - async fn key_value_root_with_empty_data() { + async fn key_value_root_with_empty_value() { let mut merkle = create_test_merkle(); let root = merkle.init_root().unwrap(); diff --git a/firewood/src/shale/mod.rs b/firewood/src/shale/mod.rs index 1b0548fc6..77b06f4db 100644 --- a/firewood/src/shale/mod.rs +++ b/firewood/src/shale/mod.rs @@ -145,7 +145,7 @@ impl Obj { pub fn into_inner(mut self) -> Node { let empty_node = LeafNode { partial_path: Path(Vec::new()), - data: Vec::new(), + value: Vec::new(), }; std::mem::replace(&mut self.value.item, Node::from_leaf(empty_node)) diff --git a/firewood/tests/merkle.rs b/firewood/tests/merkle.rs index 14843e03b..078879744 100644 --- a/firewood/tests/merkle.rs +++ b/firewood/tests/merkle.rs @@ -946,8 +946,8 @@ fn test_empty_value_range_proof() -> Result<(), ProofError> { // Create a new entry with a slightly modified key let mid_index = items.len() / 2; let key = increase_key(items[mid_index - 1].0); - let empty_data: [u8; 20] = [0; 20]; - items.splice(mid_index..mid_index, [(&key, &empty_data)].iter().cloned()); + let empty_value: [u8; 20] = [0; 20]; + items.splice(mid_index..mid_index, [(&key, &empty_value)].iter().cloned()); let start = 1; let end = items.len() - 1; @@ -982,8 +982,8 @@ fn test_all_elements_empty_value_range_proof() -> Result<(), ProofError> { // Create a new entry with a slightly modified key let mid_index = items.len() / 2; let key = increase_key(items[mid_index - 1].0); - let empty_data: [u8; 20] = [0; 20]; - items.splice(mid_index..mid_index, [(&key, &empty_data)].iter().cloned()); + let empty_value: [u8; 20] = [0; 20]; + items.splice(mid_index..mid_index, [(&key, &empty_value)].iter().cloned()); let start = 0; let end = items.len() - 1; @@ -1049,12 +1049,12 @@ fn test_bloadted_range_proof() -> Result<(), ProofError> { let mut items = Vec::new(); for i in 0..100_u32 { let mut key: [u8; 32] = [0; 32]; - let mut data: [u8; 20] = [0; 20]; + let mut value: [u8; 20] = [0; 20]; for (index, d) in i.to_be_bytes().iter().enumerate() { key[index] = *d; - data[index] = *d; + value[index] = *d; } - items.push((key, data)); + items.push((key, value)); } let merkle = merkle_build_test(items.clone(), 0x10000, 0x10000)?; @@ -1085,18 +1085,18 @@ fn fixed_and_pseudorandom_data(random_count: u32) -> HashMap<[u8; 32], [u8; 20]> let mut items: HashMap<[u8; 32], [u8; 20]> = HashMap::new(); for i in 0..100_u32 { let mut key: [u8; 32] = [0; 32]; - let mut data: [u8; 20] = [0; 20]; + let mut value: [u8; 20] = [0; 20]; for (index, d) in i.to_be_bytes().iter().enumerate() { key[index] = *d; - data[index] = *d; + value[index] = *d; } - items.insert(key, data); + items.insert(key, value); let mut more_key: [u8; 32] = [0; 32]; for (index, d) in (i + 10).to_be_bytes().iter().enumerate() { more_key[index] = *d; } - items.insert(more_key, data); + items.insert(more_key, value); } // read FIREWOOD_TEST_SEED from the environment. If it's there, parse it into a u64.