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): early return on empty state #11271

Merged
merged 1 commit into from
Sep 27, 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
55 changes: 30 additions & 25 deletions crates/trie/trie/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,36 @@ impl HashedPostState {
self
}

/// Returns `true` if the hashed state is empty.
pub fn is_empty(&self) -> bool {
self.accounts.is_empty() && self.storages.is_empty()
}

/// Construct [`TriePrefixSetsMut`] from hashed post state.
/// The prefix sets contain the hashed account and storage keys that have been changed in the
/// post state.
pub fn construct_prefix_sets(&self) -> TriePrefixSetsMut {
// Populate account prefix set.
let mut account_prefix_set = PrefixSetMut::with_capacity(self.accounts.len());
let mut destroyed_accounts = HashSet::default();
for (hashed_address, account) in &self.accounts {
account_prefix_set.insert(Nibbles::unpack(hashed_address));

if account.is_none() {
destroyed_accounts.insert(*hashed_address);
}
}

// Populate storage prefix sets.
let mut storage_prefix_sets = HashMap::with_capacity(self.storages.len());
for (hashed_address, hashed_storage) in &self.storages {
account_prefix_set.insert(Nibbles::unpack(hashed_address));
storage_prefix_sets.insert(*hashed_address, hashed_storage.construct_prefix_set());
}

TriePrefixSetsMut { account_prefix_set, storage_prefix_sets, destroyed_accounts }
}

/// Extend this hashed post state with contents of another.
/// Entries in the second hashed post state take precedence.
pub fn extend(&mut self, other: Self) {
Expand Down Expand Up @@ -166,31 +196,6 @@ impl HashedPostState {

HashedPostStateSorted { accounts, storages }
}

/// Construct [`TriePrefixSetsMut`] from hashed post state.
/// The prefix sets contain the hashed account and storage keys that have been changed in the
/// post state.
pub fn construct_prefix_sets(&self) -> TriePrefixSetsMut {
// Populate account prefix set.
let mut account_prefix_set = PrefixSetMut::with_capacity(self.accounts.len());
let mut destroyed_accounts = HashSet::default();
for (hashed_address, account) in &self.accounts {
account_prefix_set.insert(Nibbles::unpack(hashed_address));

if account.is_none() {
destroyed_accounts.insert(*hashed_address);
}
}

// Populate storage prefix sets.
let mut storage_prefix_sets = HashMap::with_capacity(self.storages.len());
for (hashed_address, hashed_storage) in &self.storages {
account_prefix_set.insert(Nibbles::unpack(hashed_address));
storage_prefix_sets.insert(*hashed_address, hashed_storage.construct_prefix_set());
}

TriePrefixSetsMut { account_prefix_set, storage_prefix_sets, destroyed_accounts }
}
}

/// Representation of in-memory hashed storage.
Expand Down
5 changes: 5 additions & 0 deletions crates/trie/trie/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ where
mut self,
state: HashedPostState,
) -> Result<HashMap<B256, Bytes>, TrieWitnessError> {
if state.is_empty() {
return Ok(self.witness)
}

let proof_targets = HashMap::from_iter(
state
.accounts
Expand All @@ -92,6 +96,7 @@ where
(*hashed_address, storage.storage.keys().copied().collect())
})),
);

let mut account_multiproof =
Proof::new(self.trie_cursor_factory.clone(), self.hashed_cursor_factory.clone())
.with_prefix_sets_mut(self.prefix_sets.clone())
Expand Down
Loading