From a34992e771801e111be1b85b02bcf24bac1ea767 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Tue, 6 Sep 2022 13:39:59 +0200 Subject: [PATCH] store: return raw bytes from Trie::retrieve_node (#7535) This will be needed in future commit where we will need access to the raw bytes of the node. See #7509. --- core/store/src/trie/iterator.rs | 2 +- core/store/src/trie/mod.rs | 17 +++++++++++++---- core/store/src/trie/state_parts.rs | 16 ++++++++-------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/core/store/src/trie/iterator.rs b/core/store/src/trie/iterator.rs index 488704d9963..3a332f56b3e 100644 --- a/core/store/src/trie/iterator.rs +++ b/core/store/src/trie/iterator.rs @@ -126,7 +126,7 @@ impl<'a> TrieIterator<'a> { /// /// The node is stored as the last [`Crumb`] in the trail. fn descend_into_node(&mut self, hash: &CryptoHash) -> Result<(), StorageError> { - let node = self.trie.retrieve_node(hash)?; + let node = self.trie.retrieve_node(hash)?.1; self.trail.push(Crumb { status: CrumbStatus::Entering, node }); Ok(()) } diff --git a/core/store/src/trie/mod.rs b/core/store/src/trie/mod.rs index dfdfec7593d..f21db2390c9 100644 --- a/core/store/src/trie/mod.rs +++ b/core/store/src/trie/mod.rs @@ -585,7 +585,7 @@ impl Trie { } let TrieNodeWithSize { node, memory_usage } = match handle { NodeHandle::InMemory(h) => memory.node_ref(h).clone(), - NodeHandle::Hash(h) => self.retrieve_node(&h).expect("storage failure"), + NodeHandle::Hash(h) => self.retrieve_node(&h).expect("storage failure").1, }; let mut memory_usage_naive = node.memory_usage_direct(memory); @@ -667,10 +667,19 @@ impl Trie { } } - fn retrieve_node(&self, hash: &CryptoHash) -> Result { + /// Retrieves decoded node alongside with its raw bytes representation. + /// + /// Note that because Empty nodes (those which are referenced by + /// [`Self::EMPTY_ROOT`] hash) aren’t stored in the database, they don’t + /// have a bytes representation. For those nodes the first return value + /// will be `None`. + fn retrieve_node( + &self, + hash: &CryptoHash, + ) -> Result<(Option>, TrieNodeWithSize), StorageError> { match self.retrieve_raw_node(hash)? { - None => Ok(TrieNodeWithSize::empty()), - Some((_bytes, node)) => Ok(TrieNodeWithSize::from_raw(node)), + None => Ok((None, TrieNodeWithSize::empty())), + Some((bytes, node)) => Ok((Some(bytes), TrieNodeWithSize::from_raw(node))), } } diff --git a/core/store/src/trie/state_parts.rs b/core/store/src/trie/state_parts.rs index 2517e0c0aca..1ee9b036dbc 100644 --- a/core/store/src/trie/state_parts.rs +++ b/core/store/src/trie/state_parts.rs @@ -71,7 +71,7 @@ impl Trie { if part_id == num_parts { return Ok(vec![16]); } - let root_node = self.retrieve_node(&self.root)?; + let root_node = self.retrieve_node(&self.root)?.1; let total_size = root_node.memory_usage; let size_start = (total_size + num_parts - 1) / num_parts * part_id; self.find_path(&root_node, size_start) @@ -99,7 +99,7 @@ impl Trie { } TrieNode::Branch(children, _) => { for child_index in 0..children.len() { - let child = match &children[child_index] { + let (_, child) = match &children[child_index] { None => { continue; } @@ -134,7 +134,7 @@ impl Trie { Ok(false) } TrieNode::Extension(key, child_handle) => { - let child = match child_handle { + let (_, child) = match child_handle { NodeHandle::InMemory(_) => unreachable!("only possible while mutating"), NodeHandle::Hash(h) => self.retrieve_node(h)?, }; @@ -312,7 +312,7 @@ mod tests { return Ok(()); } let mut stack: Vec<(CryptoHash, TrieNodeWithSize, CrumbStatus)> = Vec::new(); - let root_node = self.retrieve_node(&self.root)?; + let root_node = self.retrieve_node(&self.root)?.1; stack.push((self.root.clone(), root_node, CrumbStatus::Entering)); while let Some((hash, node, position)) = stack.pop() { if let CrumbStatus::Entering = position { @@ -353,7 +353,7 @@ mod tests { } if i < 16 { if let Some(NodeHandle::Hash(h)) = children[i].clone() { - let child = self.retrieve_node(&h)?; + let (_, child) = self.retrieve_node(&h)?; stack.push((hash, node, CrumbStatus::AtChild(i + 1))); stack.push((h, child, CrumbStatus::Entering)); } else { @@ -377,7 +377,7 @@ mod tests { unreachable!("only possible while mutating") } NodeHandle::Hash(h) => { - let child = self.retrieve_node(&h)?; + let (_, child) = self.retrieve_node(&h)?; stack.push((hash, node, CrumbStatus::Exiting)); stack.push((h, child, CrumbStatus::Entering)); } @@ -394,7 +394,7 @@ mod tests { size_start: u64, size_end: u64, ) -> Result<(), StorageError> { - let root_node = self.retrieve_node(&self.root)?; + let root_node = self.retrieve_node(&self.root)?.1; let path_begin = self.find_path(&root_node, size_start)?; let path_end = self.find_path(&root_node, size_end)?; @@ -424,7 +424,7 @@ mod tests { part_id: PartId, ) -> Result { assert!(self.storage.as_caching_storage().is_some()); - let root_node = self.retrieve_node(&self.root)?; + let root_node = self.retrieve_node(&self.root)?.1; let total_size = root_node.memory_usage; let size_start = (total_size + part_id.total - 1) / part_id.total * part_id.idx; let size_end = std::cmp::min(