Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(trie): clean up trie update operation matching #9202

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions crates/trie/trie/src/trie_cursor/in_memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{TrieCursor, TrieCursorFactory};
use crate::updates::{TrieKey, TrieOp, TrieUpdatesSorted};
use crate::updates::{TrieKey, TrieUpdatesSorted};
use reth_db::DatabaseError;
use reth_primitives::B256;
use reth_trie_common::{BranchNodeCompact, Nibbles};
Expand Down Expand Up @@ -58,10 +58,7 @@ impl<'a, C: TrieCursor> TrieCursor for InMemoryAccountTrieCursor<'a, C> {
) -> Result<Option<(Nibbles, BranchNodeCompact)>, DatabaseError> {
if let Some((trie_key, trie_op)) = self.trie_updates.find_account_node(&key) {
self.last_key = Some(trie_key);
match trie_op {
TrieOp::Update(node) => Ok(Some((key, node))),
TrieOp::Delete => Ok(None),
}
Ok(trie_op.into_update().map(|node| (key, node)))
} else {
let result = self.cursor.seek_exact(key)?;
self.last_key = result.as_ref().map(|(k, _)| TrieKey::AccountNode(k.clone()));
Expand All @@ -86,10 +83,7 @@ impl<'a, C: TrieCursor> TrieCursor for InMemoryAccountTrieCursor<'a, C> {
_ => panic!("Invalid trie key"),
};
self.last_key = Some(trie_key);
match trie_op {
TrieOp::Update(node) => return Ok(Some((nibbles, node))),
TrieOp::Delete => return Ok(None),
}
return Ok(trie_op.into_update().map(|node| (nibbles, node)))
}

let result = self.cursor.seek(key)?;
Expand Down Expand Up @@ -132,10 +126,7 @@ impl<'a, C: TrieCursor> TrieCursor for InMemoryStorageTrieCursor<'a, C> {
self.trie_updates.find_storage_node(&self.hashed_address, &key)
{
self.last_key = Some(trie_key);
match trie_op {
TrieOp::Update(node) => Ok(Some((key, node))),
TrieOp::Delete => Ok(None),
}
Ok(trie_op.into_update().map(|node| (key, node)))
} else {
let result = self.cursor.seek_exact(key)?;
self.last_key =
Expand Down Expand Up @@ -164,10 +155,7 @@ impl<'a, C: TrieCursor> TrieCursor for InMemoryStorageTrieCursor<'a, C> {
_ => panic!("this should not happen!"),
};
self.last_key = Some(trie_key.clone());
match trie_op {
TrieOp::Update(node) => return Ok(Some((nibbles, node.clone()))),
TrieOp::Delete => return Ok(None),
}
return Ok(trie_op.as_update().map(|node| (nibbles, node.clone())))
}

let result = self.cursor.seek(key)?;
Expand Down
9 changes: 9 additions & 0 deletions crates/trie/trie/src/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ impl TrieOp {
None
}
}

/// Returns owned updated branch node if operation is [`Self::Update`].
pub fn into_update(self) -> Option<BranchNodeCompact> {
if let Self::Update(node) = self {
Some(node)
} else {
None
}
}
}

/// The aggregation of trie updates.
Expand Down
Loading