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

feat(trie): sorted iterators for updated hashed state entries #9516

Merged
merged 2 commits into from
Jul 15, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/trie/trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tracing.workspace = true
rayon.workspace = true
derive_more.workspace = true
auto_impl.workspace = true
itertools.workspace = true

# `metrics` feature
reth-metrics = { workspace = true, optional = true }
Expand Down
42 changes: 41 additions & 1 deletion crates/trie/trie/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
updates::TrieUpdates,
Nibbles, StateRoot,
};
use itertools::Itertools;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use reth_db::{tables, DatabaseError};
use reth_db_api::{
Expand Down Expand Up @@ -329,6 +330,18 @@ pub struct HashedPostStateSorted {
pub(crate) storages: HashMap<B256, HashedStorageSorted>,
}

impl HashedPostStateSorted {
/// Returns reference to hashed accounts.
pub const fn accounts(&self) -> &HashedAccountsSorted {
&self.accounts
}

/// Returns reference to hashed account storages.
pub const fn account_storages(&self) -> &HashMap<B256, HashedStorageSorted> {
&self.storages
}
}

/// Sorted account state optimized for iterating during state trie calculation.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct HashedAccountsSorted {
Expand All @@ -338,17 +351,44 @@ pub struct HashedAccountsSorted {
pub(crate) destroyed_accounts: HashSet<B256>,
}

impl HashedAccountsSorted {
/// Returns a sorted iterator over updated accounts.
pub fn accounts_sorted(&self) -> impl Iterator<Item = (B256, Option<Account>)> {
self.accounts
.iter()
.map(|(address, account)| (*address, Some(*account)))
.chain(self.destroyed_accounts.iter().map(|address| (*address, None)))
.sorted_by_key(|entry| *entry.0)
}
}

/// Sorted hashed storage optimized for iterating during state trie calculation.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct HashedStorageSorted {
/// Sorted hashed storage slots with non-zero value.
pub(crate) non_zero_valued_slots: Vec<(B256, U256)>,
/// Slots that have been zero valued.
pub(crate) zero_valued_slots: HashSet<B256>,
/// Flag indicating hether the storage was wiped or not.
/// Flag indicating whether the storage was wiped or not.
pub(crate) wiped: bool,
}

impl HashedStorageSorted {
/// Returns `true` if the account was wiped.
pub const fn is_wiped(&self) -> bool {
self.wiped
}

/// Returns a sorted iterator over updated storage slots.
pub fn storage_slots_sorted(&self) -> impl Iterator<Item = (B256, U256)> {
self.non_zero_valued_slots
.iter()
.map(|(hashed_slot, value)| (*hashed_slot, *value))
.chain(self.zero_valued_slots.iter().map(|hashed_slot| (*hashed_slot, U256::ZERO)))
.sorted_by_key(|entry| *entry.0)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading