chore(trie): add set_* methods alongside with_* builders (#21639)

Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
DaniPopes
2026-01-30 23:42:57 +01:00
committed by GitHub
parent bfe778ab51
commit 8d28c4c8f2
4 changed files with 57 additions and 28 deletions

View File

@@ -152,27 +152,25 @@ impl Default for ParallelSparseTrie {
}
impl SparseTrie for ParallelSparseTrie {
fn with_root(
mut self,
fn set_root(
&mut self,
root: TrieNode,
masks: Option<BranchNodeMasks>,
retain_updates: bool,
) -> SparseTrieResult<Self> {
) -> SparseTrieResult<()> {
// A fresh/cleared `ParallelSparseTrie` has a `SparseNode::Empty` at its root in the upper
// subtrie. Delete that so we can reveal the new root node.
let path = Nibbles::default();
let _removed_root = self.upper_subtrie.nodes.remove(&path).expect("root node should exist");
debug_assert_eq!(_removed_root, SparseNode::Empty);
self = self.with_updates(retain_updates);
self.set_updates(retain_updates);
self.reveal_upper_node(Nibbles::default(), &root, masks)?;
Ok(self)
self.reveal_upper_node(Nibbles::default(), &root, masks)
}
fn with_updates(mut self, retain_updates: bool) -> Self {
fn set_updates(&mut self, retain_updates: bool) {
self.updates = retain_updates.then(Default::default);
self
}
fn reveal_nodes(&mut self, mut nodes: Vec<ProofTrieNode>) -> SparseTrieResult<()> {

View File

@@ -114,21 +114,37 @@ impl SparseStateTrie {
impl<A, S> SparseStateTrie<A, S> {
/// Set the retention of branch node updates and deletions.
pub const fn with_updates(mut self, retain_updates: bool) -> Self {
pub const fn set_updates(&mut self, retain_updates: bool) {
self.retain_updates = retain_updates;
}
/// Set the retention of branch node updates and deletions.
pub const fn with_updates(mut self, retain_updates: bool) -> Self {
self.set_updates(retain_updates);
self
}
/// Set the accounts trie to the given `RevealableSparseTrie`.
pub fn with_accounts_trie(mut self, trie: RevealableSparseTrie<A>) -> Self {
pub fn set_accounts_trie(&mut self, trie: RevealableSparseTrie<A>) {
self.state = trie;
}
/// Set the accounts trie to the given `RevealableSparseTrie`.
pub fn with_accounts_trie(mut self, trie: RevealableSparseTrie<A>) -> Self {
self.set_accounts_trie(trie);
self
}
/// Set the default trie which will be cloned when creating new storage
/// [`RevealableSparseTrie`]s.
pub fn with_default_storage_trie(mut self, trie: RevealableSparseTrie<S>) -> Self {
pub fn set_default_storage_trie(&mut self, trie: RevealableSparseTrie<S>) {
self.storage.default_trie = trie;
}
/// Set the default trie which will be cloned when creating new storage
/// [`RevealableSparseTrie`]s.
pub fn with_default_storage_trie(mut self, trie: RevealableSparseTrie<S>) -> Self {
self.set_default_storage_trie(trie);
self
}
}

View File

@@ -40,17 +40,30 @@ pub trait SparseTrie: Sized + Debug + Send + Sync {
///
/// # Returns
///
/// Self if successful, or an error if revealing fails.
/// `Ok(())` if successful, or an error if revealing fails.
///
/// # Panics
///
/// May panic if the trie is not new/cleared, and has already revealed nodes.
fn with_root(
self,
fn set_root(
&mut self,
root: TrieNode,
masks: Option<BranchNodeMasks>,
retain_updates: bool,
) -> SparseTrieResult<Self>;
) -> SparseTrieResult<()>;
/// Configures the trie to have the given root node revealed.
///
/// See [`Self::set_root`] for more details.
fn with_root(
mut self,
root: TrieNode,
masks: Option<BranchNodeMasks>,
retain_updates: bool,
) -> SparseTrieResult<Self> {
self.set_root(root, masks, retain_updates)?;
Ok(self)
}
/// Configures the trie to retain information about updates.
///
@@ -61,11 +74,15 @@ pub trait SparseTrie: Sized + Debug + Send + Sync {
/// # Arguments
///
/// * `retain_updates` - Whether to track updates
fn set_updates(&mut self, retain_updates: bool);
/// Configures the trie to retain information about updates.
///
/// # Returns
///
/// Self for method chaining.
fn with_updates(self, retain_updates: bool) -> Self;
/// See [`Self::set_updates`] for more details.
fn with_updates(mut self, retain_updates: bool) -> Self {
self.set_updates(retain_updates);
self
}
/// Reserves capacity for additional trie nodes.
///

View File

@@ -110,7 +110,7 @@ impl<T: SparseTrieTrait + Default> RevealableSparseTrie<T> {
Box::default()
};
*revealed_trie = revealed_trie.with_root(root, masks, retain_updates)?;
revealed_trie.set_root(root, masks, retain_updates)?;
*self = Self::Revealed(revealed_trie);
}
@@ -453,13 +453,13 @@ impl Default for SerialSparseTrie {
}
impl SparseTrieTrait for SerialSparseTrie {
fn with_root(
mut self,
fn set_root(
&mut self,
root: TrieNode,
masks: Option<BranchNodeMasks>,
retain_updates: bool,
) -> SparseTrieResult<Self> {
self = self.with_updates(retain_updates);
) -> SparseTrieResult<()> {
self.set_updates(retain_updates);
// A fresh/cleared `SerialSparseTrie` has a `SparseNode::Empty` at its root. Delete that
// so we can reveal the new root node.
@@ -467,15 +467,13 @@ impl SparseTrieTrait for SerialSparseTrie {
let _removed_root = self.nodes.remove(&path).expect("root node should exist");
debug_assert_eq!(_removed_root, SparseNode::Empty);
self.reveal_node(path, root, masks)?;
Ok(self)
self.reveal_node(path, root, masks)
}
fn with_updates(mut self, retain_updates: bool) -> Self {
fn set_updates(&mut self, retain_updates: bool) {
if retain_updates {
self.updates = Some(SparseTrieUpdates::default());
}
self
}
fn reserve_nodes(&mut self, additional: usize) {