Skip to content

Commit

Permalink
perf(trie): use std for sparse trie updates & track wiped
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk committed Nov 27, 2024
1 parent 8d70e89 commit c772fe2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
22 changes: 11 additions & 11 deletions crates/trie/sparse/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ use std::iter::Peekable;
/// Sparse state trie representing lazy-loaded Ethereum state trie.
#[derive(Default, Debug)]
pub struct SparseStateTrie {
retain_updates: bool,
/// Sparse account trie.
state: SparseTrie,
/// Sparse storage tries.
storages: HashMap<B256, SparseTrie>,
/// Collection of revealed account and storage keys.
revealed: HashMap<B256, HashSet<B256>>,
/// Collection of addresses that had their storage tries wiped.
wiped_storages: HashSet<B256>,
/// Flag indicating whether update should be retained.
retain_updates: bool,
}

impl SparseStateTrie {
Expand Down Expand Up @@ -225,9 +224,10 @@ impl SparseStateTrie {

/// Wipe the storage trie at the provided address.
pub fn wipe_storage(&mut self, address: B256) -> SparseStateTrieResult<()> {
let Some(trie) = self.storages.get_mut(&address) else { return Ok(()) };
self.wiped_storages.insert(address);
trie.wipe().map_err(Into::into)
if let Some(trie) = self.storages.get_mut(&address) {
trie.wipe()?;
}
Ok(())
}

/// Calculates the hashes of the nodes below the provided level.
Expand All @@ -252,18 +252,18 @@ impl SparseStateTrie {
self.state.as_revealed_mut().map(|state| {
let updates = state.take_updates();
TrieUpdates {
account_nodes: HashMap::from_iter(updates.updated_nodes),
removed_nodes: HashSet::from_iter(updates.removed_nodes),
account_nodes: updates.updated_nodes,
removed_nodes: updates.removed_nodes,
storage_tries: self
.storages
.iter_mut()
.map(|(address, trie)| {
let trie = trie.as_revealed_mut().unwrap();
let updates = trie.take_updates();
let updates = StorageTrieUpdates {
is_deleted: self.wiped_storages.contains(address),
storage_nodes: HashMap::from_iter(updates.updated_nodes),
removed_nodes: HashSet::from_iter(updates.removed_nodes),
is_deleted: updates.wiped,
storage_nodes: updates.updated_nodes,
removed_nodes: updates.removed_nodes,
};
(*address, updates)
})
Expand Down
24 changes: 15 additions & 9 deletions crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::{SparseTrieError, SparseTrieResult};
use alloy_primitives::{
hex, keccak256,
map::{HashMap, HashSet},
B256,
};
use alloy_primitives::{hex, keccak256, map::HashMap, B256};
use alloy_rlp::Decodable;
use reth_tracing::tracing::debug;
use reth_trie_common::{
Expand Down Expand Up @@ -111,6 +107,7 @@ pub struct RevealedSparseTrie {
prefix_set: PrefixSetMut,
/// Reusable buffer for RLP encoding of nodes.
rlp_buf: Vec<u8>,
/// Retained trie updates.
updates: Option<SparseTrieUpdates>,
}

Expand Down Expand Up @@ -602,8 +599,10 @@ impl RevealedSparseTrie {

/// Wipe the trie, removing all values and nodes, and replacing the root with an empty node.
pub fn wipe(&mut self) {
let updates_retained = self.updates.is_some();
*self = Self::default();
self.prefix_set = PrefixSetMut::all();
self.updates = updates_retained.then(SparseTrieUpdates::wiped);
}

/// Return the root of the sparse trie.
Expand Down Expand Up @@ -1023,14 +1022,20 @@ impl RlpNodeBuffers {
/// The aggregation of sparse trie updates.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SparseTrieUpdates {
pub(crate) updated_nodes: HashMap<Nibbles, BranchNodeCompact>,
pub(crate) removed_nodes: HashSet<Nibbles>,
pub(crate) updated_nodes: std::collections::HashMap<Nibbles, BranchNodeCompact>,
pub(crate) removed_nodes: std::collections::HashSet<Nibbles>,
pub(crate) wiped: bool,
}

impl SparseTrieUpdates {
/// Create new wiped sparse trie updates.
pub fn wiped() -> Self {
Self { wiped: true, ..Default::default() }
}
}

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;

use super::*;
use alloy_primitives::{map::HashSet, U256};
use alloy_rlp::Encodable;
Expand All @@ -1052,6 +1057,7 @@ mod tests {
proof::{ProofNodes, ProofRetainer},
HashBuilder,
};
use std::collections::BTreeMap;

/// Pad nibbles to the length of a B256 hash with zeros on the left.
fn pad_nibbles_left(nibbles: Nibbles) -> Nibbles {
Expand Down

0 comments on commit c772fe2

Please sign in to comment.