Skip to content

Commit cbc8b39

Browse files
authored
Merge a0d62b6 into 329e229
2 parents 329e229 + a0d62b6 commit cbc8b39

File tree

5 files changed

+116
-43
lines changed

5 files changed

+116
-43
lines changed

crates/common/trie/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub enum TrieError {
77
RLPDecode(#[from] RLPDecodeError),
88
#[error("Verification Error: {0}")]
99
Verify(String),
10-
#[error("Inconsistent internal tree structure")]
11-
InconsistentTree,
10+
#[error("Inconsistent internal tree structure {0}")]
11+
InconsistentTree(String),
1212
#[error("Lock Error: Panicked when trying to acquire a lock")]
1313
LockError,
1414
#[error("Database error: {0}")]

crates/common/trie/node/branch.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ impl BranchNode {
4444
// Delegate to children if present
4545
let child_ref = &self.choices[choice];
4646
if child_ref.is_valid() {
47-
let child_node = child_ref.get_node(db)?.ok_or(TrieError::InconsistentTree)?;
47+
let child_node =
48+
child_ref
49+
.get_node(db)?
50+
.ok_or(TrieError::InconsistentTree(format!(
51+
"Node with hash {:?} not found in db",
52+
child_ref.compute_hash()
53+
)))?;
4854
child_node.get(db, path)
4955
} else {
5056
Ok(None)
@@ -73,9 +79,13 @@ impl BranchNode {
7379
}
7480
// Insert into existing child and then update it
7581
(choice_ref, ValueOrHash::Value(value)) => {
76-
let child_node = choice_ref
77-
.get_node(db)?
78-
.ok_or(TrieError::InconsistentTree)?;
82+
let child_node =
83+
choice_ref
84+
.get_node(db)?
85+
.ok_or(TrieError::InconsistentTree(format!(
86+
"Node with hash {:?} not found in db",
87+
choice_ref.compute_hash()
88+
)))?;
7989

8090
*choice_ref = child_node.insert(db, path, value)?.into();
8191
}
@@ -90,7 +100,10 @@ impl BranchNode {
90100
} else {
91101
*choice_ref = choice_ref
92102
.get_node(db)?
93-
.ok_or(TrieError::InconsistentTree)?
103+
.ok_or(TrieError::InconsistentTree(format!(
104+
"Node hash {:?} not found in db",
105+
choice_ref.compute_hash()
106+
)))?
94107
.insert(db, path, value)?
95108
.into();
96109
}
@@ -135,9 +148,13 @@ impl BranchNode {
135148
// Check if the value is located in a child subtrie
136149
let value = if let Some(choice_index) = path.next_choice() {
137150
if self.choices[choice_index].is_valid() {
138-
let child_node = self.choices[choice_index]
139-
.get_node(db)?
140-
.ok_or(TrieError::InconsistentTree)?;
151+
let child_node =
152+
self.choices[choice_index]
153+
.get_node(db)?
154+
.ok_or(TrieError::InconsistentTree(format!(
155+
"Node with hash {:?} not found in db",
156+
self.choices[choice_index].compute_hash()
157+
)))?;
141158
// Remove value from child node
142159
let (child_node, old_value) = child_node.remove(db, path.clone())?;
143160
if let Some(child_node) = child_node {
@@ -176,7 +193,12 @@ impl BranchNode {
176193
// If this node doesn't have a value and has only one child, replace it with its child node
177194
(1, false) => {
178195
let (choice_index, child_ref) = children[0];
179-
let child = child_ref.get_node(db)?.ok_or(TrieError::InconsistentTree)?;
196+
let child = child_ref
197+
.get_node(db)?
198+
.ok_or(TrieError::InconsistentTree(format!(
199+
"Node with hash {:?} not found in db",
200+
child_ref.compute_hash()
201+
)))?;
180202
match child {
181203
// Replace self with an extension node leading to the child
182204
Node::Branch(_) => ExtensionNode::new(
@@ -243,7 +265,13 @@ impl BranchNode {
243265
// Continue to child
244266
let child_ref = &self.choices[choice];
245267
if child_ref.is_valid() {
246-
let child_node = child_ref.get_node(db)?.ok_or(TrieError::InconsistentTree)?;
268+
let child_node =
269+
child_ref
270+
.get_node(db)?
271+
.ok_or(TrieError::InconsistentTree(format!(
272+
"Node with hash {:?} not found in db",
273+
child_ref.compute_hash()
274+
)))?;
247275
child_node.get_path(db, path, node_path)?;
248276
}
249277
}

crates/common/trie/node/extension.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ impl ExtensionNode {
2626
// If the path is prefixed by this node's prefix, delegate to its child.
2727
// Otherwise, no value is present.
2828
if path.skip_prefix(&self.prefix) {
29-
let child_node = self
30-
.child
31-
.get_node(db)?
32-
.ok_or(TrieError::InconsistentTree)?;
29+
let child_node =
30+
self.child
31+
.get_node(db)?
32+
.ok_or(TrieError::InconsistentTree(format!(
33+
"Node with hash {:?} not found in db",
34+
self.child.compute_hash()
35+
)))?;
3336

3437
child_node.get(db, path)
3538
} else {
@@ -57,10 +60,13 @@ impl ExtensionNode {
5760
let match_index = path.count_prefix(&self.prefix);
5861
if match_index == self.prefix.len() {
5962
// Insert into child node
60-
let child_node = self
61-
.child
62-
.get_node(db)?
63-
.ok_or(TrieError::InconsistentTree)?;
63+
let child_node =
64+
self.child
65+
.get_node(db)?
66+
.ok_or(TrieError::InconsistentTree(format!(
67+
"Node with hash {:?} not found in db",
68+
self.child.compute_hash()
69+
)))?;
6470
let new_child_node = child_node.insert(db, path.offset(match_index), value)?;
6571
self.child = new_child_node.into();
6672
Ok(self.into())
@@ -74,7 +80,12 @@ impl ExtensionNode {
7480
let branch_node = if self.prefix.at(0) == 16 {
7581
match new_node.get_node(db)? {
7682
Some(Node::Leaf(leaf)) => BranchNode::new_with_value(choices, leaf.value),
77-
_ => return Err(TrieError::InconsistentTree),
83+
_ => {
84+
return Err(TrieError::InconsistentTree(format!(
85+
"Node with hash {:?} not found in db",
86+
new_node.compute_hash()
87+
)));
88+
}
7889
}
7990
} else {
8091
choices[self.prefix.at(0)] = new_node;
@@ -105,10 +116,13 @@ impl ExtensionNode {
105116

106117
// Check if the value is part of the child subtrie according to the prefix
107118
if path.skip_prefix(&self.prefix) {
108-
let child_node = self
109-
.child
110-
.get_node(db)?
111-
.ok_or(TrieError::InconsistentTree)?;
119+
let child_node =
120+
self.child
121+
.get_node(db)?
122+
.ok_or(TrieError::InconsistentTree(format!(
123+
"Node with hash {:?} not found in db",
124+
self.child.compute_hash()
125+
)))?;
112126
// Remove value from child subtrie
113127
let (child_node, old_value) = child_node.remove(db, path)?;
114128
// Restructure node based on removal
@@ -172,10 +186,13 @@ impl ExtensionNode {
172186
};
173187
// Continue to child
174188
if path.skip_prefix(&self.prefix) {
175-
let child_node = self
176-
.child
177-
.get_node(db)?
178-
.ok_or(TrieError::InconsistentTree)?;
189+
let child_node =
190+
self.child
191+
.get_node(db)?
192+
.ok_or(TrieError::InconsistentTree(format!(
193+
"Node with hash {:?} not found in db",
194+
self.child.compute_hash()
195+
)))?;
179196
child_node.get_path(db, path, node_path)?;
180197
}
181198
Ok(())

crates/common/trie/trie.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ impl Trie {
9696
Ok(match self.root {
9797
NodeRef::Node(ref node, _) => node.get(self.db.as_ref(), Nibbles::from_bytes(path))?,
9898
NodeRef::Hash(hash) if hash.is_valid() => {
99-
let rlp = self.db.get(hash)?.ok_or(TrieError::InconsistentTree)?;
99+
let rlp = self
100+
.db
101+
.get(hash)?
102+
.ok_or(TrieError::InconsistentTree(format!(
103+
"Node hash {hash:?} not found in db"
104+
)))?;
100105
let node = Node::decode(&rlp).map_err(TrieError::RLPDecode)?;
101106
node.get(self.db.as_ref(), Nibbles::from_bytes(path))?
102107
}
@@ -112,7 +117,10 @@ impl Trie {
112117
// If the trie is not empty, call the root node's insertion logic.
113118
self.root
114119
.get_node(self.db.as_ref())?
115-
.ok_or(TrieError::InconsistentTree)?
120+
.ok_or(TrieError::InconsistentTree(format!(
121+
"Root node with hash {:?} not found in db",
122+
self.root.compute_hash()
123+
)))?
116124
.insert(self.db.as_ref(), path, value)?
117125
.into()
118126
} else {
@@ -134,7 +142,10 @@ impl Trie {
134142
let (node, value) = self
135143
.root
136144
.get_node(self.db.as_ref())?
137-
.ok_or(TrieError::InconsistentTree)?
145+
.ok_or(TrieError::InconsistentTree(format!(
146+
"Root node with hash {:?} not found in db",
147+
self.root.compute_hash()
148+
)))?
138149
.remove(self.db.as_ref(), Nibbles::from_bytes(path))?;
139150
self.root = node.map(Into::into).unwrap_or_default();
140151

@@ -234,7 +245,10 @@ impl Trie {
234245
let encoded_root = self
235246
.root
236247
.get_node(self.db.as_ref())?
237-
.ok_or(TrieError::InconsistentTree)?
248+
.ok_or(TrieError::InconsistentTree(format!(
249+
"Root node with hash {:?} not found in db",
250+
self.root.compute_hash()
251+
)))?
238252
.encode_raw();
239253

240254
let mut node_path = HashSet::new();
@@ -263,7 +277,9 @@ impl Trie {
263277
) -> Result<NodeRef, TrieError> {
264278
let root_rlp = all_nodes
265279
.get(&root_hash)
266-
.ok_or(TrieError::InconsistentTree)?;
280+
.ok_or(TrieError::InconsistentTree(format!(
281+
"Root node {root_hash:?} not found in db"
282+
)))?;
267283

268284
fn get_embedded_node(
269285
all_nodes: &BTreeMap<H256, Vec<u8>>,
@@ -385,8 +401,12 @@ impl Trie {
385401
Some(idx) => {
386402
let child_ref = &branch_node.choices[idx];
387403
if child_ref.is_valid() {
388-
let child_node =
389-
child_ref.get_node(db)?.ok_or(TrieError::InconsistentTree)?;
404+
let child_node = child_ref.get_node(db)?.ok_or(
405+
TrieError::InconsistentTree(format!(
406+
"Node with hash {:?} not found in db",
407+
child_ref.compute_hash()
408+
)),
409+
)?;
390410
get_node_inner(db, child_node, partial_path)
391411
} else {
392412
Ok(vec![])
@@ -398,10 +418,12 @@ impl Trie {
398418
if partial_path.skip_prefix(&extension_node.prefix)
399419
&& extension_node.child.is_valid()
400420
{
401-
let child_node = extension_node
402-
.child
403-
.get_node(db)?
404-
.ok_or(TrieError::InconsistentTree)?;
421+
let child_node = extension_node.child.get_node(db)?.ok_or(
422+
TrieError::InconsistentTree(format!(
423+
"Extension Node child with hash {:?} with not found in db",
424+
extension_node.child.compute_hash()
425+
)),
426+
)?;
405427
get_node_inner(db, child_node, partial_path)
406428
} else {
407429
Ok(vec![])
@@ -417,7 +439,10 @@ impl Trie {
417439
self.db.as_ref(),
418440
self.root
419441
.get_node(self.db.as_ref())?
420-
.ok_or(TrieError::InconsistentTree)?,
442+
.ok_or(TrieError::InconsistentTree(format!(
443+
"Root node with hash {:?} not found in db",
444+
self.root.compute_hash()
445+
)))?,
421446
partial_path,
422447
)
423448
} else {
@@ -468,7 +493,10 @@ impl ProofTrie {
468493
self.0
469494
.root
470495
.get_node(self.0.db.as_ref())?
471-
.ok_or(TrieError::InconsistentTree)?
496+
.ok_or(TrieError::InconsistentTree(format!(
497+
"Root node with hash {:?} not found in db",
498+
self.0.root.compute_hash()
499+
)))?
472500
.insert(self.0.db.as_ref(), partial_path, external_ref)?
473501
.into()
474502
} else {

crates/networking/p2p/rlpx/connection/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl GenServer for RLPxConnection {
331331
);
332332
return CastResponse::Stop;
333333
}
334-
RLPxError::StoreError(StoreError::Trie(TrieError::InconsistentTree)) => {
334+
RLPxError::StoreError(StoreError::Trie(TrieError::InconsistentTree(_))) => {
335335
if established_state.blockchain.is_synced() {
336336
log_peer_error(
337337
&established_state.node,

0 commit comments

Comments
 (0)