Walks down the trie to a leaf value with the given key, if it exists. Preimages for blinded nodes along the path are fetched using the `fetcher` function, and persisted in the inner [`TrieNode`] elements. ## Takes - `self` - The root trie node - `path` - The nibbles representation of the path to the leaf node - `fetcher` - The preimage fetcher for intermediate blinded nodes ## Returns - `Err(_)`
(
&'a mut self,
path: &Nibbles,
fetcher: &F,
)
| 151 | /// - `Ok(None)` - The node with the given key does not exist in the trie. |
| 152 | /// - `Ok(Some(_))` - The value of the node |
| 153 | pub fn open<'a, F: TrieProvider>( |
| 154 | &'a mut self, |
| 155 | path: &Nibbles, |
| 156 | fetcher: &F, |
| 157 | ) -> TrieNodeResult<Option<&'a mut Bytes>> { |
| 158 | match self { |
| 159 | Self::Branch { stack } => { |
| 160 | let branch_nibble = path.get(0).ok_or(TrieNodeError::PathTooShort)? as usize; |
| 161 | stack |
| 162 | .get_mut(branch_nibble) |
| 163 | .map(|node| node.open(&path.slice(BRANCH_NODE_NIBBLES..), fetcher)) |
| 164 | .unwrap_or(Ok(None)) |
| 165 | } |
| 166 | Self::Leaf { prefix, value } => Ok((path == prefix).then_some(value)), |
| 167 | Self::Extension { prefix, node } => { |
| 168 | if path.slice(..prefix.len()) == *prefix { |
| 169 | // Follow extension branch |
| 170 | node.unblind(fetcher)?; |
| 171 | node.open(&path.slice(prefix.len()..), fetcher) |
| 172 | } else { |
| 173 | Ok(None) |
| 174 | } |
| 175 | } |
| 176 | Self::Blinded { .. } => { |
| 177 | self.unblind(fetcher)?; |
| 178 | self.open(path, fetcher) |
| 179 | } |
| 180 | Self::Empty => Ok(None), |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// Inserts a [`TrieNode`] at the given path into the trie rooted at Self. |
| 185 | /// |