Skip to content

Commit

Permalink
chore(trie): early return on empty state (paradigmxyz#11271)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored Sep 27, 2024
1 parent 6722124 commit 5706e03
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
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

0 comments on commit 5706e03

Please sign in to comment.