chore(chain-state): MemoryOverlayStateProvider as alias (#13285)

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
This commit is contained in:
Kunal Arora
2024-12-20 19:46:48 +05:30
committed by GitHub
parent 4e9f8c2747
commit f29dd4c5cc

View File

@ -30,19 +30,9 @@ pub struct MemoryOverlayStateProviderRef<'a, N: NodePrimitives = reth_primitives
/// A state provider that stores references to in-memory blocks along with their state as well as
/// the historical state provider for fallback lookups.
#[allow(missing_debug_implementations)]
pub struct MemoryOverlayStateProvider<N: NodePrimitives = reth_primitives::EthPrimitives> {
/// Historical state provider for state lookups that are not found in in-memory blocks.
pub(crate) historical: Box<dyn StateProvider>,
/// The collection of executed parent blocks. Expected order is newest to oldest.
pub(crate) in_memory: Vec<ExecutedBlock<N>>,
/// Lazy-loaded in-memory trie data.
pub(crate) trie_state: OnceLock<MemoryOverlayTrieState>,
}
pub type MemoryOverlayStateProvider<N> = MemoryOverlayStateProviderRef<'static, N>;
macro_rules! impl_state_provider {
([$($tokens:tt)*],$type:ty, $historical_type:ty) => {
impl $($tokens)* $type {
impl<'a, N: NodePrimitives> MemoryOverlayStateProviderRef<'a, N> {
/// Create new memory overlay state provider.
///
/// ## Arguments
@ -50,12 +40,12 @@ macro_rules! impl_state_provider {
/// - `in_memory` - the collection of executed ancestor blocks in reverse.
/// - `historical` - a historical state provider for the latest ancestor block stored in the
/// database.
pub fn new(historical: $historical_type, in_memory: Vec<ExecutedBlock<N>>) -> Self {
pub fn new(historical: Box<dyn StateProvider + 'a>, in_memory: Vec<ExecutedBlock<N>>) -> Self {
Self { historical, in_memory, trie_state: OnceLock::new() }
}
/// Turn this state provider into a state provider
pub fn boxed(self) -> $historical_type {
pub fn boxed(self) -> Box<dyn StateProvider + 'a> {
Box::new(self)
}
@ -70,13 +60,13 @@ macro_rules! impl_state_provider {
trie_state
})
}
}
}
impl $($tokens)* BlockHashReader for $type {
impl<N: NodePrimitives> BlockHashReader for MemoryOverlayStateProviderRef<'_, N> {
fn block_hash(&self, number: BlockNumber) -> ProviderResult<Option<B256>> {
for block in &self.in_memory {
if block.block.number() == number {
return Ok(Some(block.block.hash()))
return Ok(Some(block.block.hash()));
}
}
@ -103,21 +93,21 @@ macro_rules! impl_state_provider {
hashes.append(&mut in_memory_hashes);
Ok(hashes)
}
}
}
impl $($tokens)* AccountReader for $type {
impl<N: NodePrimitives> AccountReader for MemoryOverlayStateProviderRef<'_, N> {
fn basic_account(&self, address: Address) -> ProviderResult<Option<Account>> {
for block in &self.in_memory {
if let Some(account) = block.execution_output.account(&address) {
return Ok(account)
return Ok(account);
}
}
self.historical.basic_account(address)
}
}
}
impl $($tokens)* StateRootProvider for $type {
impl<N: NodePrimitives> StateRootProvider for MemoryOverlayStateProviderRef<'_, N> {
fn state_root(&self, state: HashedPostState) -> ProviderResult<B256> {
self.state_root_from_nodes(TrieInput::from_state(state))
}
@ -143,9 +133,9 @@ macro_rules! impl_state_provider {
input.prepend_cached(nodes, state);
self.historical.state_root_from_nodes_with_updates(input)
}
}
}
impl $($tokens)* StorageRootProvider for $type {
impl<N: NodePrimitives> StorageRootProvider for MemoryOverlayStateProviderRef<'_, N> {
// TODO: Currently this does not reuse available in-memory trie nodes.
fn storage_root(&self, address: Address, storage: HashedStorage) -> ProviderResult<B256> {
let state = &self.trie_state().state;
@ -182,9 +172,9 @@ macro_rules! impl_state_provider {
hashed_storage.extend(&storage);
self.historical.storage_multiproof(address, slots, hashed_storage)
}
}
}
impl $($tokens)* StateProofProvider for $type {
impl<N: NodePrimitives> StateProofProvider for MemoryOverlayStateProviderRef<'_, N> {
fn proof(
&self,
mut input: TrieInput,
@ -215,15 +205,15 @@ macro_rules! impl_state_provider {
input.prepend_cached(nodes, state);
self.historical.witness(input, target)
}
}
}
impl $($tokens)* HashedPostStateProvider for $type {
impl<N: NodePrimitives> HashedPostStateProvider for MemoryOverlayStateProviderRef<'_, N> {
fn hashed_post_state(&self, bundle_state: &BundleState) -> HashedPostState {
self.historical.hashed_post_state(bundle_state)
}
}
}
impl $($tokens)* StateProvider for $type {
impl<N: NodePrimitives> StateProvider for MemoryOverlayStateProviderRef<'_, N> {
fn storage(
&self,
address: Address,
@ -231,7 +221,7 @@ macro_rules! impl_state_provider {
) -> ProviderResult<Option<StorageValue>> {
for block in &self.in_memory {
if let Some(value) = block.execution_output.storage(&address, storage_key.into()) {
return Ok(Some(value))
return Ok(Some(value));
}
}
@ -241,19 +231,14 @@ macro_rules! impl_state_provider {
fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>> {
for block in &self.in_memory {
if let Some(contract) = block.execution_output.bytecode(&code_hash) {
return Ok(Some(contract))
return Ok(Some(contract));
}
}
self.historical.bytecode_by_hash(code_hash)
}
}
};
}
impl_state_provider!([<N: NodePrimitives>], MemoryOverlayStateProvider<N>, Box<dyn StateProvider>);
impl_state_provider!([<'a, N: NodePrimitives>], MemoryOverlayStateProviderRef<'a, N>, Box<dyn StateProvider + 'a>);
/// The collection of data necessary for trie-related operations for [`MemoryOverlayStateProvider`].
#[derive(Clone, Default, Debug)]
pub(crate) struct MemoryOverlayTrieState {