From 017217f3eb244affa3fe8ecef34bdc958da9ee0a Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Thu, 9 Jan 2025 12:40:45 +0100 Subject: [PATCH] feat(trie): add leaf value retrieval methods to `SparseStateTrie` (#13750) --- crates/trie/sparse/src/state.rs | 10 ++++++++++ crates/trie/sparse/src/trie.rs | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/crates/trie/sparse/src/state.rs b/crates/trie/sparse/src/state.rs index fecb3c5fb40e..953198a36794 100644 --- a/crates/trie/sparse/src/state.rs +++ b/crates/trie/sparse/src/state.rs @@ -97,6 +97,16 @@ impl SparseStateTrie { 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> { + 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> { + 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, diff --git a/crates/trie/sparse/src/trie.rs b/crates/trie/sparse/src/trie.rs index f472578e3d9d..c41fe5ee420d 100644 --- a/crates/trie/sparse/src/trie.rs +++ b/crates/trie/sparse/src/trie.rs @@ -72,6 +72,15 @@ impl

SparseTrie

{ matches!(self, Self::Blind) } + /// Returns reference to revealed sparse trie if the trie is not blind. + pub fn as_revealed_ref(&self) -> Option<&RevealedSparseTrie

> { + 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

> { if let Self::Revealed(revealed) = self {