Skip to content

Commit

Permalink
feat(trie): add leaf value retrieval methods to SparseStateTrie (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored Jan 9, 2025
1 parent 383eb23 commit 017217f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/trie/sparse/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
self.revealed.get(account).is_some_and(|slots| slots.contains(slot))
}

/// Returns reference to bytes representing leaf value for the target account.
pub fn get_account_value(&self, account: &B256) -> Option<&Vec<u8>> {
self.state.as_revealed_ref()?.get_leaf_value(&Nibbles::unpack(account))
}

/// Returns reference to bytes representing leaf value for the target account and storage slot.
pub fn get_storage_slot_value(&self, account: &B256, slot: &B256) -> Option<&Vec<u8>> {
self.storages.get(account)?.as_revealed_ref()?.get_leaf_value(&Nibbles::unpack(slot))
}

/// Returns mutable reference to storage sparse trie if it was revealed.
pub fn storage_trie_mut(
&mut self,
Expand Down
9 changes: 9 additions & 0 deletions crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ impl<P> SparseTrie<P> {
matches!(self, Self::Blind)
}

/// Returns reference to revealed sparse trie if the trie is not blind.
pub fn as_revealed_ref(&self) -> Option<&RevealedSparseTrie<P>> {
if let Self::Revealed(revealed) = self {
Some(revealed)
} else {
None
}
}

/// Returns mutable reference to revealed sparse trie if the trie is not blind.
pub fn as_revealed_mut(&mut self) -> Option<&mut RevealedSparseTrie<P>> {
if let Self::Revealed(revealed) = self {
Expand Down

0 comments on commit 017217f

Please sign in to comment.