Skip to content

Commit

Permalink
remove tree structure panics
Browse files Browse the repository at this point in the history
  • Loading branch information
xqft committed Nov 27, 2024
1 parent 73232ef commit 853ac3c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions crates/storage/trie/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ pub enum TrieError {
RLPDecode(#[from] RLPDecodeError),
#[error("Verification Error: {0}")]
Verify(String),
#[error("Inconsistent internal tree structure")]
InconsistentTree,
}
10 changes: 5 additions & 5 deletions crates/storage/trie/node/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl BranchNode {
if child_hash.is_valid() {
let child_node = state
.get_node(child_hash.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
child_node.get(state, path)
} else {
Ok(None)
Expand Down Expand Up @@ -94,7 +94,7 @@ impl BranchNode {
choice_hash => {
let child_node = state
.get_node(choice_hash.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;

let child_node = child_node.insert(state, path, value)?;
*choice_hash = child_node.insert_self(state)?;
Expand Down Expand Up @@ -139,7 +139,7 @@ impl BranchNode {
if self.choices[choice_index].is_valid() {
let child_node = state
.get_node(self.choices[choice_index].clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
// Remove value from child node
let (child_node, old_value) = child_node.remove(state, path.clone())?;
if let Some(child_node) = child_node {
Expand Down Expand Up @@ -180,7 +180,7 @@ impl BranchNode {
let (choice_index, child_hash) = children[0];
let child = state
.get_node(child_hash.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
match child {
// Replace self with an extension node leading to the child
Node::Branch(_) => ExtensionNode::new(
Expand Down Expand Up @@ -254,7 +254,7 @@ impl BranchNode {
if child_hash.is_valid() {
let child_node = state
.get_node(child_hash.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
child_node.get_path(state, path, node_path)?;
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/storage/trie/node/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl ExtensionNode {
if path.skip_prefix(&self.prefix) {
let child_node = state
.get_node(self.child.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;

child_node.get(state, path)
} else {
Expand Down Expand Up @@ -59,7 +59,7 @@ impl ExtensionNode {
// Insert into child node
let child_node = state
.get_node(self.child)?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
let new_child_node =
child_node.insert(state, path.offset(match_index), value.clone())?;
self.child = new_child_node.insert_self(state)?;
Expand All @@ -76,7 +76,7 @@ impl ExtensionNode {
Some(Node::Leaf(leaf)) => {
BranchNode::new_with_value(Box::new(choices), leaf.value)
}
_ => panic!("inconsistent internal tree structure"),
_ => return Err(TrieError::InconsistentTree),
}
} else {
choices[self.prefix.at(0)] = new_node;
Expand Down Expand Up @@ -109,7 +109,7 @@ impl ExtensionNode {
if path.skip_prefix(&self.prefix) {
let child_node = state
.get_node(self.child)?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
// Remove value from child subtrie
let (child_node, old_value) = child_node.remove(state, path)?;
// Restructure node based on removal
Expand Down Expand Up @@ -189,7 +189,7 @@ impl ExtensionNode {
if path.skip_prefix(&self.prefix) {
let child_node = state
.get_node(self.child.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
child_node.get_path(state, path, node_path)?;
}
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions crates/storage/trie/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Trie {
let root_node = self
.state
.get_node(root.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
root_node.get(&self.state, Nibbles::from_bytes(path))
} else {
Ok(None)
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Trie {
let root_node = self
.state
.get_node(root)?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
let (root_node, old_value) =
root_node.remove(&mut self.state, Nibbles::from_bytes(&path))?;
self.root = root_node
Expand Down Expand Up @@ -294,7 +294,7 @@ impl Trie {
let child_node = self
.state
.get_node(child_hash.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
self.get_node_inner(child_node, partial_path)
} else {
Ok(vec![])
Expand All @@ -309,7 +309,7 @@ impl Trie {
let child_node = self
.state
.get_node(extension_node.child.clone())?
.expect("inconsistent internal tree structure");
.ok_or(TrieError::InconsistentTree)?;
self.get_node_inner(child_node, partial_path)
} else {
Ok(vec![])
Expand Down

0 comments on commit 853ac3c

Please sign in to comment.