Skip to content

Commit f5362f1

Browse files
committed
Nits
1 parent 29d7b10 commit f5362f1

3 files changed

Lines changed: 21 additions & 21 deletions

File tree

trie-db/src/iterator.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ impl<L: TrieLayout> TrieDBRawIterator<L> {
128128
/// Skip the descendants of the node most recently yielded by `next_raw_item`: iteration
129129
/// continues with the node's next sibling (or an ancestor's).
130130
///
131-
/// Must only be called directly after `next_raw_item(_, true)` yielded a node. At that point
132-
/// the node's own partial and child index have not been pushed to `key_nibbles` yet (that
133-
/// happens when its `Status::At` is processed), so the crumb can jump straight to
134-
/// `Status::AftExiting` — popping it and stepping the parent — with nothing to unwind.
131+
/// Must only be called directly after `next_raw_item(_, true)` yielded a node.
135132
pub(crate) fn skip_current_subtree(&mut self) {
136133
if let Some(crumb) = self.trail.last_mut() {
137134
crumb.status = Status::AftExiting;

trie-db/src/trie_codec.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,7 @@ where
299299
Ok((prefix, node_hash, node)) => {
300300
// Skip inline nodes, as they cannot contain hash references to other nodes by
301301
// assumption.
302-
let node_hash = match node_hash {
303-
Some(node_hash) => node_hash,
304-
None => continue,
305-
};
302+
let Some(node_hash) = node_hash else { continue };
306303

307304
if let Some(seen_hashes) = seen_hashes.as_deref_mut() {
308305
// A subtree whose root was already emitted is skipped entirely; the parent's
@@ -428,10 +425,10 @@ impl<'a, C: NodeCodec> DecoderStackEntry<'a, C> {
428425
match child {
429426
NodeHandle::Inline(data) if data.is_empty() => return Ok(false),
430427
_ => {
431-
let child_ref = child.try_into().map_err(|hash| {
428+
let child_ref: ChildReference<_> = child.try_into().map_err(|hash| {
432429
Box::new(TrieError::InvalidHash(C::HashOut::default(), hash))
433430
})?;
434-
if let ChildReference::Hash(_) = child_ref {
431+
if child_ref.is_hash() {
435432
self.hash_ref_children |= 1u16 << self.child_index;
436433
}
437434
self.children[self.child_index] = Some(child_ref);
@@ -444,10 +441,11 @@ impl<'a, C: NodeCodec> DecoderStackEntry<'a, C> {
444441
match children[self.child_index] {
445442
Some(NodeHandle::Inline(data)) if data.is_empty() => return Ok(false),
446443
Some(child) => {
447-
let child_ref = child.try_into().map_err(|hash| {
448-
Box::new(TrieError::InvalidHash(C::HashOut::default(), hash))
449-
})?;
450-
if let ChildReference::Hash(_) = child_ref {
444+
let child_ref: ChildReference<_> =
445+
child.try_into().map_err(|hash| {
446+
Box::new(TrieError::InvalidHash(C::HashOut::default(), hash))
447+
})?;
448+
if child_ref.is_hash() {
451449
self.hash_ref_children |= 1u16 << self.child_index;
452450
}
453451
self.children[self.child_index] = Some(child_ref);
@@ -697,6 +695,9 @@ where
697695
/// Decoding work is proportional to the *un-deduplicated* encoding: re-inserting subtrees at every
698696
/// occurrence can touch far more positions than there are items in `encoded`, so callers decoding
699697
/// untrusted input should bound the logical size, not the encoded size.
698+
///
699+
/// Returns the root hash of the reconstructed partial trie and the number of items consumed from
700+
/// `encoded`.
700701
pub fn decode_compact_from_iter_with_known_items<'a, L, DB, I>(
701702
db: &mut DB,
702703
encoded: I,
@@ -714,12 +715,8 @@ where
714715
// The prefix of the next item to be read from the slice of encoded items.
715716
let mut prefix = NibbleVec::new();
716717

717-
// The number of items consumed so far, including attached values.
718-
let mut used;
719-
720718
let mut iter = encoded.into_iter().enumerate();
721719
while let Some((i, encoded_node)) = iter.next() {
722-
used = i + 1;
723720
let mut attached_node = 0;
724721
if let Some(header) = L::Codec::ESCAPE_HEADER {
725722
if encoded_node.starts_with(&[header]) {
@@ -745,8 +742,7 @@ where
745742

746743
if attached_node > 0 {
747744
// Read value
748-
if let Some((i, fetched_value)) = iter.next() {
749-
used = i + 1;
745+
if let Some((_, fetched_value)) = iter.next() {
750746
last_entry.attached_value = Some(fetched_value);
751747
// Record immediately: a node deduplicated against this one can complete before
752748
// this node does, e.g. a leaf below a branch carrying the value.
@@ -818,7 +814,7 @@ where
818814
last_entry.children[last_entry.child_index] = Some(ChildReference::Hash(node_hash));
819815
last_entry.child_index += 1;
820816
} else {
821-
return Ok((node_hash, used))
817+
return Ok((node_hash, i + 1 + attached_node))
822818
}
823819
}
824820
}

trie-db/src/triedbmut.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,13 @@ pub enum ChildReference<HO> {
564564
Inline(HO, usize), // usize is the length of the node data we store in the `H::Out`
565565
}
566566

567+
impl<HO> ChildReference<HO> {
568+
/// Returns true if this is a hash reference
569+
pub fn is_hash(&self) -> bool {
570+
matches!(self, ChildReference::Hash(_))
571+
}
572+
}
573+
567574
impl<'a, HO> TryFrom<EncodedNodeHandle<'a>> for ChildReference<HO>
568575
where
569576
HO: AsRef<[u8]> + AsMut<[u8]> + Default + Clone + Copy,

0 commit comments

Comments
 (0)