diff --git a/bin/client/src/lib.rs b/bin/client/src/lib.rs index 1b993288b..9364c458e 100644 --- a/bin/client/src/lib.rs +++ b/bin/client/src/lib.rs @@ -14,7 +14,7 @@ use kona_executor::ExecutorError; use kona_preimage::{HintWriterClient, PreimageOracleClient}; use kona_proof::{ errors::OracleProviderError, - executor::KonaExecutorConstructor, + executor::KonaExecutor, l1::{OracleBlobProvider, OracleL1ChainProvider, OraclePipeline}, l2::OracleL2ChainProvider, sync::new_pipeline_cursor, @@ -116,7 +116,7 @@ where l1_provider.clone(), l2_provider.clone(), ); - let executor = KonaExecutorConstructor::new(&cfg, l2_provider.clone(), l2_provider, None); + let executor = KonaExecutor::new(&cfg, l2_provider.clone(), l2_provider, None, None); let mut driver = Driver::new(cursor, executor, pipeline); // Run the derivation pipeline until we are able to produce the output root of the claimed diff --git a/crates/driver/src/core.rs b/crates/driver/src/core.rs index 835a3fb96..aeba51304 100644 --- a/crates/driver/src/core.rs +++ b/crates/driver/src/core.rs @@ -15,17 +15,13 @@ use op_alloy_genesis::RollupConfig; use op_alloy_protocol::L2BlockInfo; use op_alloy_rpc_types_engine::OpAttributesWithParent; -use crate::{ - DriverError, DriverPipeline, DriverResult, Executor, ExecutorConstructor, PipelineCursor, - TipCursor, -}; +use crate::{DriverError, DriverPipeline, DriverResult, Executor, PipelineCursor, TipCursor}; /// The Rollup Driver entrypoint. #[derive(Debug)] -pub struct Driver +pub struct Driver where E: Executor + Send + Sync + Debug, - EC: ExecutorConstructor + Send + Sync + Debug, DP: DriverPipeline

+ Send + Sync + Debug, P: Pipeline + SignalReceiver + Send + Sync + Debug, { @@ -37,19 +33,18 @@ where pub pipeline: DP, /// Cursor to keep track of the L2 tip pub cursor: PipelineCursor, - /// Executor constructor. - pub executor: EC, + /// The Executor. + pub executor: E, } -impl Driver +impl Driver where E: Executor + Send + Sync + Debug, - EC: ExecutorConstructor + Send + Sync + Debug, DP: DriverPipeline

+ Send + Sync + Debug, P: Pipeline + SignalReceiver + Send + Sync + Debug, { /// Creates a new [Driver]. - pub const fn new(cursor: PipelineCursor, executor: EC, pipeline: DP) -> Self { + pub const fn new(cursor: PipelineCursor, executor: E, pipeline: DP) -> Self { Self { _marker: core::marker::PhantomData, _marker2: core::marker::PhantomData, @@ -108,9 +103,8 @@ where } }; - let mut executor = - self.executor.new_executor(self.cursor.l2_safe_head_header().clone()); - let header = match executor.execute_payload(attributes.clone()) { + self.executor.update_safe_head(self.cursor.l2_safe_head_header().clone()); + let header = match self.executor.execute_payload(attributes.clone()) { Ok(header) => header, Err(e) => { error!(target: "client", "Failed to execute L2 block: {}", e); @@ -133,9 +127,8 @@ where }); // Retry the execution. - executor = - self.executor.new_executor(self.cursor.l2_safe_head_header().clone()); - match executor.execute_payload(attributes.clone()) { + self.executor.update_safe_head(self.cursor.l2_safe_head_header().clone()); + match self.executor.execute_payload(attributes.clone()) { Ok(header) => header, Err(e) => { error!( @@ -176,7 +169,7 @@ where let cursor = TipCursor::new( l2_info, header.clone().seal_slow(), - executor.compute_output_root().map_err(DriverError::Executor)?, + self.executor.compute_output_root().map_err(DriverError::Executor)?, ); self.cursor.advance(origin, cursor); } diff --git a/crates/driver/src/executor.rs b/crates/driver/src/executor.rs index 11887958b..d7edc76e5 100644 --- a/crates/driver/src/executor.rs +++ b/crates/driver/src/executor.rs @@ -19,6 +19,9 @@ pub trait Executor { /// The error type for the Executor. type Error: Error + Debug + Display + ToString; + /// Updates the safe header. + fn update_safe_head(&mut self, header: Sealed

); + /// Execute the gicen [OpPayloadAttributes]. fn execute_payload(&mut self, attributes: OpPayloadAttributes) -> Result<&Header, Self::Error>; @@ -26,14 +29,3 @@ pub trait Executor { /// Expected to be called after the payload has been executed. fn compute_output_root(&mut self) -> Result; } - -/// Constructs the Executor. -/// -/// This trait abstracts the construction of the Executor. -pub trait ExecutorConstructor -where - E: Executor, -{ - /// Construct the Executor. - fn new_executor(&self, header: Sealed
) -> E; -} diff --git a/crates/driver/src/lib.rs b/crates/driver/src/lib.rs index 6b9c1e24b..bd5bb6ce8 100644 --- a/crates/driver/src/lib.rs +++ b/crates/driver/src/lib.rs @@ -19,7 +19,7 @@ mod pipeline; pub use pipeline::DriverPipeline; mod executor; -pub use executor::{Executor, ExecutorConstructor}; +pub use executor::Executor; mod core; pub use core::Driver; diff --git a/crates/executor/src/errors.rs b/crates/executor/src/errors.rs index 48917fe6b..853badc5c 100644 --- a/crates/executor/src/errors.rs +++ b/crates/executor/src/errors.rs @@ -43,6 +43,9 @@ pub enum ExecutorError { /// RLP error. #[error("RLP error: {0}")] RLPError(alloy_eips::eip2718::Eip2718Error), + /// Missing the executor. + #[error("Missing the executor")] + MissingExecutor, } /// A [Result] type for the [ExecutorError] enum. diff --git a/crates/proof-sdk/proof/src/executor.rs b/crates/proof-sdk/proof/src/executor.rs index 95376881f..ddc38d21a 100644 --- a/crates/proof-sdk/proof/src/executor.rs +++ b/crates/proof-sdk/proof/src/executor.rs @@ -3,7 +3,7 @@ use alloc::sync::Arc; use alloy_consensus::{Header, Sealed}; use alloy_primitives::B256; -use kona_driver::{Executor, ExecutorConstructor}; +use kona_driver::Executor; use kona_executor::{KonaHandleRegister, StatelessL2BlockExecutor, TrieDBProvider}; use kona_mpt::TrieHinter; use op_alloy_genesis::RollupConfig; @@ -11,43 +11,7 @@ use op_alloy_rpc_types_engine::OpPayloadAttributes; /// An executor wrapper type. #[derive(Debug)] -pub struct KonaExecutor<'a, P, H>(StatelessL2BlockExecutor<'a, P, H>) -where - P: TrieDBProvider + Send + Sync + Clone, - H: TrieHinter + Send + Sync + Clone; - -impl<'a, P, H> KonaExecutor<'a, P, H> -where - P: TrieDBProvider + Send + Sync + Clone, - H: TrieHinter + Send + Sync + Clone, -{ - /// Creates a new executor. - pub const fn new(executor: StatelessL2BlockExecutor<'a, P, H>) -> Self { - Self(executor) - } -} - -impl Executor for KonaExecutor<'_, P, H> -where - P: TrieDBProvider + Send + Sync + Clone, - H: TrieHinter + Send + Sync + Clone, -{ - type Error = kona_executor::ExecutorError; - - /// Execute the given payload attributes. - fn execute_payload(&mut self, attributes: OpPayloadAttributes) -> Result<&Header, Self::Error> { - self.0.execute_payload(attributes) - } - - /// Computes the output root. - fn compute_output_root(&mut self) -> Result { - self.0.compute_output_root() - } -} - -/// An executor constructor. -#[derive(Debug)] -pub struct KonaExecutorConstructor<'a, P, H> +pub struct KonaExecutor<'a, P, H> where P: TrieDBProvider + Send + Sync + Clone, H: TrieHinter + Send + Sync + Clone, @@ -60,31 +24,39 @@ where trie_hinter: H, /// The handle register for the executor. handle_register: Option>, + /// The executor. + inner: Option>, } -impl<'a, P, H> KonaExecutorConstructor<'a, P, H> +impl<'a, P, H> KonaExecutor<'a, P, H> where P: TrieDBProvider + Send + Sync + Clone, H: TrieHinter + Send + Sync + Clone, { - /// Creates a new executor constructor. - pub fn new( + /// Creates a new executor. + pub const fn new( rollup_config: &'a Arc, trie_provider: P, trie_hinter: H, handle_register: Option>, + inner: Option>, ) -> Self { - Self { rollup_config, trie_provider, trie_hinter, handle_register } + Self { rollup_config, trie_provider, trie_hinter, handle_register, inner } } } -impl<'a, P, H> ExecutorConstructor> for KonaExecutorConstructor<'a, P, H> +impl Executor for KonaExecutor<'_, P, H> where P: TrieDBProvider + Send + Sync + Clone, H: TrieHinter + Send + Sync + Clone, { - /// Constructs the executor. - fn new_executor(&self, header: Sealed
) -> KonaExecutor<'a, P, H> { + type Error = kona_executor::ExecutorError; + + /// Updates the safe header. + /// + /// Since the L2 block executor is stateless, on an update to the safe head, + /// a new executor is created with the updated header. + fn update_safe_head(&mut self, header: Sealed
) { let mut builder = StatelessL2BlockExecutor::builder( self.rollup_config, self.trie_provider.clone(), @@ -95,7 +67,22 @@ where if let Some(register) = self.handle_register { builder = builder.with_handle_register(register); } + self.inner = Some(builder.build()); + } + + /// Execute the given payload attributes. + fn execute_payload(&mut self, attributes: OpPayloadAttributes) -> Result<&Header, Self::Error> { + self.inner.as_mut().map_or_else( + || Err(kona_executor::ExecutorError::MissingExecutor), + |e| e.execute_payload(attributes), + ) + } - KonaExecutor::new(builder.build()) + /// Computes the output root. + fn compute_output_root(&mut self) -> Result { + self.inner.as_mut().map_or_else( + || Err(kona_executor::ExecutorError::MissingExecutor), + |e| e.compute_output_root(), + ) } }