refactor in trie crate (#5897)

Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com>
This commit is contained in:
Thomas Coratger
2024-01-17 21:57:52 +01:00
committed by GitHub
parent aeee4486eb
commit d919f3da10
2 changed files with 20 additions and 36 deletions

View File

@ -42,10 +42,7 @@ impl From<StoredSubNode> for CursorSubNode {
/// Extracts necessary values from the `StoredSubNode` and constructs
/// a corresponding `CursorSubNode`.
fn from(value: StoredSubNode) -> Self {
let nibble = match value.nibble {
Some(n) => n as i8,
None => -1,
};
let nibble = value.nibble.map_or(-1, |n| n as i8);
let key = Nibbles::from_nibbles_unchecked(value.key);
let full_key = full_key(key.clone(), nibble);
Self { key, nibble, node: value.node, full_key }
@ -63,12 +60,9 @@ impl CursorSubNode {
/// Creates a new `CursorSubNode` from a key and an optional node.
pub fn new(key: Nibbles, node: Option<BranchNodeCompact>) -> Self {
// Find the first nibble that is set in the state mask of the node.
let nibble = match &node {
Some(n) if n.root_hash.is_none() => {
CHILD_INDEX_RANGE.clone().find(|i| n.state_mask.is_bit_set(*i)).unwrap() as i8
}
_ => -1,
};
let nibble = node.as_ref().filter(|n| n.root_hash.is_none()).map_or(-1, |n| {
CHILD_INDEX_RANGE.clone().find(|i| n.state_mask.is_bit_set(*i)).unwrap() as i8
});
let full_key = full_key(key.clone(), nibble);
CursorSubNode { key, node, nibble, full_key }
}
@ -80,35 +74,26 @@ impl CursorSubNode {
/// Returns `true` if the state flag is set for the current nibble.
pub fn state_flag(&self) -> bool {
if let Some(node) = &self.node {
if self.nibble >= 0 {
return node.state_mask.is_bit_set(self.nibble as u8)
}
}
true
self.node
.as_ref()
.map_or(true, |node| self.nibble < 0 || node.state_mask.is_bit_set(self.nibble as u8))
}
/// Returns `true` if the tree flag is set for the current nibble.
pub fn tree_flag(&self) -> bool {
if let Some(node) = &self.node {
if self.nibble >= 0 {
return node.tree_mask.is_bit_set(self.nibble as u8)
}
}
true
self.node
.as_ref()
.map_or(true, |node| self.nibble < 0 || node.tree_mask.is_bit_set(self.nibble as u8))
}
/// Returns `true` if the current nibble has a root hash.
pub fn hash_flag(&self) -> bool {
match &self.node {
Some(node) => match self.nibble {
// This guy has it
-1 => node.root_hash.is_some(),
// Or get it from the children
_ => node.hash_mask.is_bit_set(self.nibble as u8),
},
None => false,
}
self.node.as_ref().map_or(false, |node| match self.nibble {
// This guy has it
-1 => node.root_hash.is_some(),
// Or get it from the children
_ => node.hash_mask.is_bit_set(self.nibble as u8),
})
}
/// Returns the root hash of the current node, if it has one.

View File

@ -235,11 +235,10 @@ impl<C: TrieCursor> TrieWalker<C> {
/// Updates the skip node flag based on the walker's current state.
fn update_skip_node(&mut self) {
self.can_skip_current_node = if let Some(node) = self.stack.last() {
!self.changes.contains(node.full_key()) && node.hash_flag()
} else {
false
};
self.can_skip_current_node = self
.stack
.last()
.map_or(false, |node| !self.changes.contains(node.full_key()) && node.hash_flag());
}
}