Skip to content

Commit

Permalink
feat(driver): Introduce driver crate (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell authored Nov 8, 2024
1 parent d50844c commit b79054a
Show file tree
Hide file tree
Showing 19 changed files with 845 additions and 517 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ kona-mpt = { path = "crates/mpt", version = "0.0.6", default-features = false }
kona-client = { path = "bin/client", version = "0.1.0", default-features = false }
kona-common = { path = "crates/common", version = "0.0.4", default-features = false }
kona-derive = { path = "crates/derive", version = "0.0.6", default-features = false }
kona-driver = { path = "crates/driver", version = "0.0.0", default-features = false }
kona-preimage = { path = "crates/preimage", version = "0.0.4", default-features = false }
kona-executor = { path = "crates/executor", version = "0.0.5", default-features = false }
kona-common-proc = { path = "crates/common-proc", version = "0.0.5", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions bin/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repository.workspace = true
kona-mpt.workspace = true
kona-common.workspace = true
kona-derive.workspace = true
kona-driver.workspace = true
kona-preimage.workspace = true
kona-executor.workspace = true
kona-common-proc.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion bin/client/src/boot.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! This module contains the prologue phase of the client program, pulling in the boot information
//! through the `PreimageOracle` ABI as local keys.
use crate::errors::OracleProviderError;
use alloy_primitives::{B256, U256};
use kona_preimage::{PreimageKey, PreimageOracleClient};
use op_alloy_genesis::RollupConfig;
use serde::{Deserialize, Serialize};
use tracing::warn;

use crate::errors::OracleProviderError;

/// The local key ident for the L1 head hash.
pub const L1_HEAD_KEY: U256 = U256::from_be_slice(&[1]);

Expand Down
56 changes: 0 additions & 56 deletions bin/client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use alloc::string::{String, ToString};
use derive_more::derive::Display;
use kona_derive::errors::{PipelineError, PipelineErrorKind};
use kona_executor::ExecutorError;
use kona_mpt::OrderedListWalkerError;
use kona_preimage::errors::PreimageOracleError;
use op_alloy_protocol::{FromBlockError, OpBlockConversionError};
Expand Down Expand Up @@ -48,61 +47,6 @@ impl From<OracleProviderError> for PipelineErrorKind {
}
}

/// Driver error.
#[derive(Display, Debug)]
pub enum DriverError {
/// Pipeline error.
#[display("Pipeline error: {_0}")]
Pipeline(PipelineErrorKind),
/// Execution error.
#[display("Execution error: {_0}")]
Execution(ExecutorError),
/// Error from the oracle provider.
#[display("Oracle provider error: {_0}")]
Oracle(OracleProviderError),
/// Error parsing a hint.
#[display("Hint parsing error: {_0}")]
HintParsing(HintParsingError),
/// Error decoding or encoding RLP.
#[display("RLP error: {_0}")]
Rlp(alloy_rlp::Error),
}

impl core::error::Error for DriverError {}

impl From<OracleProviderError> for DriverError {
fn from(val: OracleProviderError) -> Self {
DriverError::Oracle(val)
}
}

impl From<PipelineErrorKind> for DriverError {
fn from(val: PipelineErrorKind) -> Self {
DriverError::Pipeline(val)
}
}

impl From<ExecutorError> for DriverError {
fn from(val: ExecutorError) -> Self {
DriverError::Execution(val)
}
}

impl From<HintParsingError> for DriverError {
fn from(val: HintParsingError) -> Self {
DriverError::HintParsing(val)
}
}

impl From<alloy_rlp::Error> for DriverError {
fn from(val: alloy_rlp::Error) -> Self {
DriverError::Rlp(val)
}
}

/// A [Result] type for the [DriverError].
pub type DriverResult<T> = Result<T, DriverError>;

/// Error parsing a hint.
#[derive(Display, Debug)]
#[display("Hint parsing error: {_0}")]
Expand Down
99 changes: 99 additions & 0 deletions bin/client/src/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! An executor constructor.
use alloc::sync::Arc;
use alloy_consensus::{Header, Sealed};
use alloy_primitives::B256;
use kona_driver::{Executor, ExecutorConstructor};
use kona_executor::{KonaHandleRegister, StatelessL2BlockExecutor};
use kona_mpt::{TrieHinter, TrieProvider};
use op_alloy_genesis::RollupConfig;
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: TrieProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone;

impl<'a, P, H> KonaExecutor<'a, P, H>
where
P: TrieProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
/// Creates a new executor.
pub fn new(executor: StatelessL2BlockExecutor<'a, P, H>) -> Self {
Self(executor)
}
}

impl<P, H> Executor for KonaExecutor<'_, P, H>
where
P: TrieProvider + 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<B256, Self::Error> {
self.0.compute_output_root()
}
}

/// An executor constructor.
#[derive(Debug)]
pub struct KonaExecutorConstructor<'a, P, H>
where
P: TrieProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
/// The rollup config for the executor.
rollup_config: &'a Arc<RollupConfig>,
/// The trie provider for the executor.
trie_provider: P,
/// The trie hinter for the executor.
trie_hinter: H,
/// The handle register for the executor.
handle_register: KonaHandleRegister<P, H>,
}

impl<'a, P, H> KonaExecutorConstructor<'a, P, H>
where
P: TrieProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
/// Creates a new executor constructor.
pub fn new(
rollup_config: &'a Arc<RollupConfig>,
trie_provider: P,
trie_hinter: H,
handle_register: KonaHandleRegister<P, H>,
) -> Self {
Self { rollup_config, trie_provider, trie_hinter, handle_register }
}
}

impl<'a, P, H> ExecutorConstructor<KonaExecutor<'a, P, H>> for KonaExecutorConstructor<'a, P, H>
where
P: TrieProvider + Send + Sync + Clone,
H: TrieHinter + Send + Sync + Clone,
{
/// Constructs the executor.
fn new_executor(&self, header: Sealed<Header>) -> KonaExecutor<'a, P, H> {
KonaExecutor::new(
StatelessL2BlockExecutor::builder(
self.rollup_config,
self.trie_provider.clone(),
self.trie_hinter.clone(),
)
.with_parent_header(header)
.with_handle_register(self.handle_register)
.build(),
)
}
}
50 changes: 35 additions & 15 deletions bin/client/src/kona.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ extern crate alloc;

use alloc::{string::String, sync::Arc};
use kona_client::{
errors::DriverError,
l1::{DerivationDriver, OracleBlobProvider, OracleL1ChainProvider},
executor::KonaExecutorConstructor,
l1::{OracleBlobProvider, OracleL1ChainProvider, OraclePipeline},
l2::OracleL2ChainProvider,
BootInfo, CachingOracle,
};
use kona_common::io;
use kona_common_proc::client_entry;
use kona_driver::{Driver, DriverError};

pub(crate) mod fault;
use fault::{fpvm_handle_register, HINT_WRITER, ORACLE_READER};
Expand All @@ -41,7 +42,14 @@ fn main() -> Result<(), String> {
////////////////////////////////////////////////////////////////

let oracle = Arc::new(CachingOracle::new(ORACLE_LRU_SIZE, ORACLE_READER, HINT_WRITER));
let boot = Arc::new(BootInfo::load(oracle.as_ref()).await?);
let boot = match BootInfo::load(oracle.as_ref()).await {
Ok(boot) => Arc::new(boot),
Err(e) => {
error!(target: "client", "Failed to load boot info: {:?}", e);
io::print(&alloc::format!("Failed to load boot info: {:?}\n", e));
io::exit(1);
}
};
let l1_provider = OracleL1ChainProvider::new(boot.clone(), oracle.clone());
let l2_provider = OracleL2ChainProvider::new(boot.clone(), oracle.clone());
let beacon = OracleBlobProvider::new(oracle.clone());
Expand All @@ -62,20 +70,32 @@ fn main() -> Result<(), String> {
////////////////////////////////////////////////////////////////

// Create a new derivation driver with the given boot information and oracle.
let mut driver =
DerivationDriver::new(boot.as_ref(), &oracle, beacon, l1_provider, l2_provider.clone())
.await?;
let Ok((pipeline, cursor)) = OraclePipeline::new(
&boot,
oracle.clone(),
beacon,
l1_provider.clone(),
l2_provider.clone(),
)
.await
else {
error!(target: "client", "Failed to create derivation pipeline");
io::print("Failed to create derivation pipeline\n");
io::exit(1);
};
let cfg = Arc::new(boot.rollup_config.clone());
let executor = KonaExecutorConstructor::new(
&cfg,
l2_provider.clone(),
l2_provider,
fpvm_handle_register,
);
let mut driver = Driver::new(cursor, executor, pipeline);

// Run the derivation pipeline until we are able to produce the output root of the claimed
// L2 block.
let (number, output_root) = driver
.advance_to_target(
&boot.rollup_config,
&l2_provider,
&l2_provider,
fpvm_handle_register,
)
.await?;
let (number, output_root) =
driver.advance_to_target(&boot.rollup_config, boot.claimed_l2_block_number).await?;

////////////////////////////////////////////////////////////////
// EPILOGUE //
Expand Down Expand Up @@ -108,6 +128,6 @@ fn main() -> Result<(), String> {
output_root
));

Ok::<_, DriverError>(())
Ok::<_, DriverError<kona_executor::ExecutorError>>(())
})
}
Loading

0 comments on commit b79054a

Please sign in to comment.