Repository: paradigmxyz/reth (Rust)

Issue:
remove PersistenceState from TreeCtx
### Describe the feature

After recent improvements this is unused:

https://github.com/paradigmxyz/reth/blob/5091482dec7ccd7df03b349030eb1f0382a8f1d5/crates/engine/tree/src/tree/payload_validator.rs#L54-L55

and can be removed together with these functions:

https://github.com/paradigmxyz/reth/blob/5091482dec7ccd7df03b349030eb1f0382a8f1d5/crates/engine/tree/src/tree/payload_validator.rs#L90-L93

https://github.com/paradigmxyz/reth/blob/5091482dec7ccd7df03b349030eb1f0382a8f1d5/crates/engine/tree/src/tree/payload_validator.rs#L106-L106

we can further remove this type:

https://github.com/paradigmxyz/reth/blob/5091482dec7ccd7df03b349030eb1f0382a8f1d5/crates/engine/tree/src/tree/mod.rs#L2806-L2815



### Additional context

_No response_


Discussion:
I'm ready
assigned, ty!



Patch:
diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs
index 324e3375d2c..5e2ed1c513c 100644
--- a/crates/engine/tree/src/tree/mod.rs
+++ b/crates/engine/tree/src/tree/mod.rs
@@ -2458,8 +2458,7 @@ where
             Ok(is_fork) => is_fork,
         };
 
-        let ctx =
-            TreeCtx::new(&mut self.state, &self.persistence_state, &self.canonical_in_memory_state);
+        let ctx = TreeCtx::new(&mut self.state, &self.canonical_in_memory_state);
 
         let start = Instant::now();
 
@@ -2802,30 +2801,3 @@ pub enum InsertPayloadOk {
     /// The payload was valid and inserted into the tree.
     Inserted(BlockStatus),
 }
-
-/// Whether or not the blocks are currently persisting and the input block is a descendant.
-#[derive(Debug, Clone, Copy)]
-pub enum PersistingKind {
-    /// The blocks are not currently persisting.
-    NotPersisting,
-    /// The blocks are currently persisting but the input block is not a descendant.
-    PersistingNotDescendant,
-    /// The blocks are currently persisting and the input block is a descendant.
-    PersistingDescendant,
-}
-
-impl PersistingKind {
-    /// Returns true if the parallel state root can be run.
-    ///
-    /// We only run the parallel state root if we are not currently persisting any blocks or
-    /// persisting blocks that are all ancestors of the one we are calculating the state root for.
-    pub const fn can_run_parallel_state_root(&self) -> bool {
-        matches!(self, Self::NotPersisting | Self::PersistingDescendant)
-    }
-
-    /// Returns true if the blocks are currently being persisted and the input block is a
-    /// descendant.
-    pub const fn is_descendant(&self) -> bool {
-        matches!(self, Self::PersistingDescendant)
-    }
-}
diff --git a/crates/engine/tree/src/tree/payload_validator.rs b/crates/engine/tree/src/tree/payload_validator.rs
index ecc475dd53a..7aac153b276 100644
--- a/crates/engine/tree/src/tree/payload_validator.rs
+++ b/crates/engine/tree/src/tree/payload_validator.rs
@@ -6,11 +6,10 @@ use crate::tree::{
     executor::WorkloadExecutor,
     instrumented_state::InstrumentedStateProvider,
     payload_processor::{multiproof::MultiProofConfig, PayloadProcessor},
-    persistence_state::CurrentPersistenceAction,
     precompile_cache::{CachedPrecompile, CachedPrecompileMetrics, PrecompileCacheMap},
     sparse_trie::StateRootComputeOutcome,
-    EngineApiMetrics, EngineApiTreeState, ExecutionEnv, PayloadHandle, PersistenceState,
-    PersistingKind, StateProviderBuilder, StateProviderDatabase, TreeConfig,
+    EngineApiMetrics, EngineApiTreeState, ExecutionEnv, PayloadHandle, StateProviderBuilder,
+    StateProviderDatabase, TreeConfig,
 };
 use alloy_consensus::transaction::Either;
 use alloy_eips::{eip1898::BlockWithParent, NumHash};
@@ -51,8 +50,6 @@ use tracing::{debug, debug_span, error, info, instrument, trace, warn};
 pub struct TreeCtx<'a, N: NodePrimitives> {
     /// The engine API tree state
     state: &'a mut EngineApiTreeState<N>,
-    /// Information about the current persistence state
-    persistence: &'a PersistenceState,
     /// Reference to the canonical in-memory state
     canonical_in_memory_state: &'a CanonicalInMemoryState<N>,
 }
@@ -61,7 +58,6 @@ impl<'a, N: NodePrimitives> std::fmt::Debug for TreeCtx<'a, N> {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         f.debug_struct("TreeCtx")
             .field("state", &"EngineApiTreeState")
-            .field("persistence_info", &self.persistence)
             .field("canonical_in_memory_state", &self.canonical_in_memory_state)
             .finish()
     }
@@ -71,10 +67,9 @@ impl<'a, N: NodePrimitives> TreeCtx<'a, N> {
     /// Creates a new tree context
     pub const fn new(
         state: &'a mut EngineApiTreeState<N>,
-        persistence: &'a PersistenceState,
         canonical_in_memory_state: &'a CanonicalInMemoryState<N>,
     ) -> Self {
-        Self { state, persistence, canonical_in_memory_state }
+        Self { state, canonical_in_memory_state }
     }
 
     /// Returns a reference to the engine tree state
@@ -87,43 +82,10 @@ impl<'a, N: NodePrimitives> TreeCtx<'a, N> {
         self.state
     }
 
-    /// Returns a reference to the persistence info
-    pub const fn persistence(&self) -> &PersistenceState {
-        self.persistence
-    }
-
     /// Returns a reference to the canonical in-memory state
     pub const fn canonical_in_memory_state(&self) -> &'a CanonicalInMemoryState<N> {
         self.canonical_in_memory_state
     }
-
-    /// Determines the persisting kind for the given block based on persistence info.
-    ///
-    /// Based on the given header it returns whether any conflicting persistence operation is
-    /// currently in progress.
-    ///
-    /// This is adapted from the `persisting_kind_for` method in `EngineApiTreeHandler`.
-    pub fn persisting_kind_for(&self, block: BlockWithParent) -> PersistingKind {
-        // Check that we're currently persisting.
-        let Some(action) = self.persistence().current_action() else {
-            return PersistingKind::NotPersisting
-        };
-        // Check that the persistince action is saving blocks, not removing them.
-        let CurrentPersistenceAction::SavingBlocks { highest } = action else {
-            return PersistingKind::PersistingNotDescendant
-        };
-
-        // The block being validated can only be a descendant if its number is higher than
-        // the highest block persisting. Otherwise, it's likely a fork of a lower block.
-        if block.block.number > highest.number &&
-            self.state().tree_state.is_descendant(*highest, block)
-        {
-            return PersistingKind::PersistingDescendant
-        }
-
-        // In all other cases, the block is not a descendant.
-        PersistingKind::PersistingNotDescendant
-    }
 }
 
 /// A helper type that provides reusable payload validation logic for network-specific validators.
diff --git a/crates/engine/tree/src/tree/persistence_state.rs b/crates/engine/tree/src/tree/persistence_state.rs
index bbb981a531a..82a8078447d 100644
--- a/crates/engine/tree/src/tree/persistence_state.rs
+++ b/crates/engine/tree/src/tree/persistence_state.rs
@@ -67,6 +67,7 @@ impl PersistenceState {
 
     /// Returns the current persistence action. If there is no persistence task in progress, then
     /// this returns `None`.
+    #[cfg(test)]
     pub(crate) fn current_action(&self) -> Option<&CurrentPersistenceAction> {
         self.rx.as_ref().map(|rx| &rx.2)
     }
diff --git a/crates/engine/tree/src/tree/state.rs b/crates/engine/tree/src/tree/state.rs
index a10d26e3f27..2f083a4d9e7 100644
--- a/crates/engine/tree/src/tree/state.rs
+++ b/crates/engine/tree/src/tree/state.rs
@@ -1,7 +1,7 @@
 //! Functionality related to tree state.
 
 use crate::engine::EngineApiKind;
-use alloy_eips::{eip1898::BlockWithParent, BlockNumHash};
+use alloy_eips::BlockNumHash;
 use alloy_primitives::{
     map::{HashMap, HashSet},
     BlockNumber, B256,
@@ -294,10 +294,37 @@ impl<N: NodePrimitives> TreeState<N> {
         }
     }
 
+    /// Updates the canonical head to the given block.
+    pub(crate) const fn set_canonical_head(&mut self, new_head: BlockNumHash) {
+        self.current_canonical_head = new_head;
+    }
+
+    /// Returns the tracked canonical head.
+    pub(crate) const fn canonical_head(&self) -> &BlockNumHash {
+        &self.current_canonical_head
+    }
+
+    /// Returns the block hash of the canonical head.
+    pub(crate) const fn canonical_block_hash(&self) -> B256 {
+        self.canonical_head().hash
+    }
+
+    /// Returns the block number of the canonical head.
+    pub(crate) const fn canonical_block_number(&self) -> BlockNumber {
+        self.canonical_head().number
+    }
+}
+
+#[cfg(test)]
+impl<N: NodePrimitives> TreeState<N> {
     /// Determines if the second block is a descendant of the first block.
     ///
     /// If the two blocks are the same, this returns `false`.
-    pub(crate) fn is_descendant(&self, first: BlockNumHash, second: BlockWithParent) -> bool {
+    pub(crate) fn is_descendant(
+        &self,
+        first: BlockNumHash,
+        second: alloy_eips::eip1898::BlockWithParent,
+    ) -> bool {
         // If the second block's parent is the first block's hash, then it is a direct child
         // and we can return early.
         if second.parent == first.hash {
@@ -330,26 +357,6 @@ impl<N: NodePrimitives> TreeState<N> {
         // Now the block numbers should be equal, so we compare hashes.
         current_block.recovered_block().parent_hash() == first.hash
     }
-
-    /// Updates the canonical head to the given block.
-    pub(crate) const fn set_canonical_head(&mut self, new_head: BlockNumHash) {
-        self.current_canonical_head = new_head;
-    }
-
-    /// Returns the tracked canonical head.
-    pub(crate) const fn canonical_head(&self) -> &BlockNumHash {
-        &self.current_canonical_head
-    }
-
-    /// Returns the block hash of the canonical head.
-    pub(crate) const fn canonical_block_hash(&self) -> B256 {
-        self.canonical_head().hash
-    }
-
-    /// Returns the block number of the canonical head.
-    pub(crate) const fn canonical_block_number(&self) -> BlockNumber {
-        self.canonical_head().number
-    }
 }
 
 #[cfg(test)]


Test patch:
diff --git a/crates/engine/tree/src/tree/tests.rs b/crates/engine/tree/src/tree/tests.rs
index 7c40680c809..7fbae4cac5c 100644
--- a/crates/engine/tree/src/tree/tests.rs
+++ b/crates/engine/tree/src/tree/tests.rs
@@ -403,7 +403,7 @@ impl ValidatorTestHarness {
         Self { harness, validator, metrics: TestMetrics::default() }
     }
 
-    /// Configure `PersistenceState` for specific `PersistingKind` scenarios
+    /// Configure `PersistenceState` for specific persistence scenarios
     fn start_persistence_operation(&mut self, action: CurrentPersistenceAction) {
         use tokio::sync::oneshot;
 
@@ -432,7 +432,6 @@ impl ValidatorTestHarness {
     ) -> ValidationOutcome<EthPrimitives> {
         let ctx = TreeCtx::new(
             &mut self.harness.tree.state,
-            &self.harness.tree.persistence_state,
             &self.harness.tree.canonical_in_memory_state,
         );
         let result = self.validator.validate_block(block, ctx);