From 9bb5558616b36771e6cb2c8e46174cc904377e84 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Wed, 18 Jun 2025 10:28:44 -0400 Subject: [PATCH] feat: add from_root for ParallelSparseTrie (#16865) --- crates/trie/sparse/src/parallel_trie.rs | 55 ++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/crates/trie/sparse/src/parallel_trie.rs b/crates/trie/sparse/src/parallel_trie.rs index 591b6aa262..ee5706abb2 100644 --- a/crates/trie/sparse/src/parallel_trie.rs +++ b/crates/trie/sparse/src/parallel_trie.rs @@ -1,7 +1,8 @@ -use crate::{SparseNode, SparseTrieUpdates}; +use crate::{SparseNode, SparseTrieUpdates, TrieMasks}; use alloc::vec::Vec; use alloy_primitives::map::HashMap; -use reth_trie_common::Nibbles; +use reth_execution_errors::SparseTrieResult; +use reth_trie_common::{Nibbles, TrieNode}; /// A revealed sparse trie with subtries that can be updated in parallel. /// @@ -38,6 +39,56 @@ impl Default for ParallelSparseTrie { } } +impl ParallelSparseTrie { + /// Creates a new revealed sparse trie from the given root node. + /// + /// # Returns + /// + /// A [`ParallelSparseTrie`] if successful, or an error if revealing fails. + pub fn from_root( + root_node: TrieNode, + masks: TrieMasks, + retain_updates: bool, + ) -> SparseTrieResult { + let mut trie = Self::default().with_updates(retain_updates); + trie.reveal_node(Nibbles::default(), root_node, masks)?; + Ok(trie) + } + + /// Reveals a trie node if it has not been revealed before. + /// + /// This internal function decodes a trie node and inserts it into the nodes map. + /// It handles different node types (leaf, extension, branch) by appropriately + /// adding them to the trie structure and recursively revealing their children. + /// + /// + /// # Returns + /// + /// `Ok(())` if successful, or an error if node was not revealed. + pub fn reveal_node( + &mut self, + path: Nibbles, + node: TrieNode, + masks: TrieMasks, + ) -> SparseTrieResult<()> { + let _path = path; + let _node = node; + let _masks = masks; + todo!() + } + + /// Configures the trie to retain information about updates. + /// + /// If `retain_updates` is true, the trie will record branch node updates and deletions. + /// This information can then be used to efficiently update an external database. + pub fn with_updates(mut self, retain_updates: bool) -> Self { + if retain_updates { + self.updates = Some(SparseTrieUpdates::default()); + } + self + } +} + /// This is a subtrie of the `ParallelSparseTrie` that contains a map from path to sparse trie /// nodes. #[derive(Clone, PartialEq, Eq, Debug)]