Skip to content

Commit

Permalink
refactor: move out core into separate crate (#247)
Browse files Browse the repository at this point in the history
* refactor: Move out core into separate crate

Make host only responsible for running server and docs creation

* fix: Update imports

* fix: Unignore core directory for docker

* chore(repo): merge updates from main

* refactor(lib): cleanup unwrap for chain specs struct

* chore(repo): rebase main into core-extract
  • Loading branch information
petarvujovic98 authored May 27, 2024
1 parent d660a17 commit 8bc4a0f
Show file tree
Hide file tree
Showing 23 changed files with 399 additions and 245 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
!/taiko.toml
!/tests
!/provers/sgx/setup
!/kzg_settings_raw.bin
!/kzg_settings_raw.bin
!/core
38 changes: 38 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"provers/sgx/guest",
"provers/sgx/setup",
"pipeline",
"core",
]

# Always optimize; building and running the guest takes much longer without optimization.
Expand All @@ -33,6 +34,7 @@ opt-level = 3
# raiko
raiko-lib = { path = "./lib", features = ["std"] }
raiko-primitives = { path = "./primitives" }
raiko-core = { path = "./core" }

# revm
revm-primitives = { git = "https://github.com/taikoxyz/revm.git", branch = "v35_taiko_v2", default-features = false }
Expand Down
65 changes: 65 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[package]
name = "raiko-core"
version = "0.1.0"
edition = "2021"

[dependencies]

# provers
sp1-driver = { path = "../provers/sp1/driver", optional = true }
risc0-driver = { path = "../provers/risc0/driver", optional = true }
sgx-prover = { path = "../provers/sgx/prover", optional = true }

# raiko
raiko-lib = { workspace = true }
raiko-primitives = { workspace = true, features = ["c-kzg"] }

# alloy
alloy-rlp = { workspace = true }
alloy-rlp-derive = { workspace = true }
alloy-sol-types = { workspace = true }
alloy-primitives = { workspace = true }
alloy-rpc-types = { workspace = true }
alloy-provider = { workspace = true }
alloy-transport-http = { workspace = true }
alloy-consensus = { workspace = true }
alloy-network = { workspace = true }
alloy-rpc-client = { workspace = true }
revm = { workspace = true }

# tracing and logging
tracing = { workspace = true }

# errors
anyhow = { workspace = true }
thiserror = { workspace = true }

# serde
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }

# c-kzg
c-kzg = { workspace = true }

# async
tokio = { workspace = true }
reqwest = { workspace = true }
reqwest_alloy = { workspace = true }

# docs
utoipa = { workspace = true }

# cli
clap = { workspace = true }

[dev-dependencies]
assert_cmd = { workspace = true }
rstest = { workspace = true }
ethers-core = { workspace = true }

[features]
# powdr = ["dep:powdr"]
sp1 = ["dep:sp1-driver", "sp1-driver/enable"]
risc0 = ["dep:risc0-driver", "risc0-driver/enable"]
sgx = ["dep:sgx-prover", "sgx-prover/enable"]
109 changes: 82 additions & 27 deletions host/src/interfaces/request.rs → core/src/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,78 @@
use core::fmt::Debug;
use std::collections::HashMap;
use std::{path::Path, str::FromStr};
use std::{collections::HashMap, path::Path, str::FromStr};

use alloy_primitives::{Address, B256};
use clap::{Args, ValueEnum};
use raiko_lib::{
input::{GuestInput, GuestOutput},
prover::{Proof, Prover},
prover::{Proof, Prover, ProverError},
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::{serde_as, DisplayFromStr};
use utoipa::ToSchema;

use crate::{
interfaces::error::{HostError, HostResult},
merge,
raiko::NativeProver,
};
use crate::{merge, prover::NativeProver};

#[derive(Debug, thiserror::Error, ToSchema)]
pub enum RaikoError {
/// For invalid proof type generation request.
#[error("Unknown proof type: {0}")]
InvalidProofType(String),

/// For invalid proof request configuration.
#[error("Invalid proof request: {0}")]
InvalidRequestConfig(String),

/// For requesting a proof of a type that is not supported.
#[error("Feature not supported: {0}")]
#[schema(value_type = Value)]
FeatureNotSupportedError(ProofType),

/// For invalid type conversion.
#[error("Invalid conversion: {0}")]
Conversion(String),

/// For RPC errors.
#[error("There was an error with the RPC provider: {0}")]
RPC(String),

/// For preflight errors.
#[error("There was an error running the preflight: {0}")]
Preflight(String),

/// For errors produced by the guest provers.
#[error("There was an error with a guest prover: {0}")]
#[schema(value_type = Value)]
Guest(#[from] ProverError),

/// For db errors.
#[error("There was an error with the db: {0}")]
#[schema(value_type = Value)]
Db(raiko_lib::mem_db::DbError),

/// For I/O errors.
#[error("There was a I/O error: {0}")]
#[schema(value_type = Value)]
Io(#[from] std::io::Error),

/// For Serde errors.
#[error("There was a deserialization error: {0}")]
#[schema(value_type = Value)]
Serde(#[from] serde_json::Error),

/// A catch-all error for any other error type.
#[error("There was an unexpected error: {0}")]
#[schema(value_type = Value)]
Anyhow(#[from] anyhow::Error),
}

impl From<raiko_lib::mem_db::DbError> for RaikoError {
fn from(e: raiko_lib::mem_db::DbError) -> Self {
RaikoError::Db(e)
}
}

pub type RaikoResult<T> = Result<T, RaikoError>;

#[derive(
PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Deserialize, Serialize, ToSchema, Hash, ValueEnum,
Expand Down Expand Up @@ -54,15 +109,15 @@ impl std::fmt::Display for ProofType {
}

impl FromStr for ProofType {
type Err = HostError;
type Err = RaikoError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim().to_lowercase().as_str() {
"native" => Ok(ProofType::Native),
"sp1" => Ok(ProofType::Sp1),
"sgx" => Ok(ProofType::Sgx),
"risc0" => Ok(ProofType::Risc0),
_ => Err(HostError::InvalidProofType(s.to_string())),
_ => Err(RaikoError::InvalidProofType(s.to_string())),
}
}
}
Expand All @@ -74,7 +129,7 @@ impl ProofType {
input: GuestInput,
output: &GuestOutput,
config: &Value,
) -> HostResult<Proof> {
) -> RaikoResult<Proof> {
match self {
ProofType::Native => NativeProver::run(input, output, config)
.await
Expand All @@ -85,23 +140,23 @@ impl ProofType {
.await
.map_err(|e| e.into());

Err(HostError::FeatureNotSupportedError(self.clone()))
Err(RaikoError::FeatureNotSupportedError(self.clone()))
}
ProofType::Risc0 => {
#[cfg(feature = "risc0")]
return risc0_driver::Risc0Prover::run(input, output, config)
.await
.map_err(|e| e.into());

Err(HostError::FeatureNotSupportedError(self.clone()))
Err(RaikoError::FeatureNotSupportedError(self.clone()))
}
ProofType::Sgx => {
#[cfg(feature = "sgx")]
return sgx_prover::SgxProver::run(input, output, config)
.await
.map_err(|e| e.into());

Err(HostError::FeatureNotSupportedError(self.clone()))
Err(RaikoError::FeatureNotSupportedError(self.clone()))
}
}
}
Expand Down Expand Up @@ -187,7 +242,7 @@ impl<S: ::std::hash::BuildHasher + ::std::default::Default> From<ProverSpecificO

impl ProofRequestOpt {
/// Read a partial proof request config from a file.
pub fn from_file<T>(path: T) -> HostResult<Self>
pub fn from_file<T>(path: T) -> RaikoResult<Self>
where
T: AsRef<Path>,
{
Expand All @@ -198,7 +253,7 @@ impl ProofRequestOpt {
}

/// Merge a partial proof request into current one.
pub fn merge(&mut self, other: &Value) -> HostResult<()> {
pub fn merge(&mut self, other: &Value) -> RaikoResult<()> {
let mut this = serde_json::to_value(&self)?;
merge(&mut this, other);
*self = serde_json::from_value(this)?;
Expand All @@ -207,40 +262,40 @@ impl ProofRequestOpt {
}

impl TryFrom<ProofRequestOpt> for ProofRequest {
type Error = HostError;
type Error = RaikoError;

fn try_from(value: ProofRequestOpt) -> Result<Self, Self::Error> {
Ok(Self {
block_number: value.block_number.ok_or(HostError::InvalidRequestConfig(
block_number: value.block_number.ok_or(RaikoError::InvalidRequestConfig(
"Missing block number".to_string(),
))?,
network: value.network.ok_or(HostError::InvalidRequestConfig(
network: value.network.ok_or(RaikoError::InvalidRequestConfig(
"Missing network".to_string(),
))?,
l1_network: value.l1_network.ok_or(HostError::InvalidRequestConfig(
l1_network: value.l1_network.ok_or(RaikoError::InvalidRequestConfig(
"Missing l1_network".to_string(),
))?,
graffiti: value
.graffiti
.ok_or(HostError::InvalidRequestConfig(
.ok_or(RaikoError::InvalidRequestConfig(
"Missing graffiti".to_string(),
))?
.parse()
.map_err(|_| HostError::InvalidRequestConfig("Invalid graffiti".to_string()))?,
.map_err(|_| RaikoError::InvalidRequestConfig("Invalid graffiti".to_string()))?,
prover: value
.prover
.ok_or(HostError::InvalidRequestConfig(
.ok_or(RaikoError::InvalidRequestConfig(
"Missing prover".to_string(),
))?
.parse()
.map_err(|_| HostError::InvalidRequestConfig("Invalid prover".to_string()))?,
.map_err(|_| RaikoError::InvalidRequestConfig("Invalid prover".to_string()))?,
proof_type: value
.proof_type
.ok_or(HostError::InvalidRequestConfig(
.ok_or(RaikoError::InvalidRequestConfig(
"Missing proof_type".to_string(),
))?
.parse()
.map_err(|_| HostError::InvalidRequestConfig("Invalid proof_type".to_string()))?,
.map_err(|_| RaikoError::InvalidRequestConfig("Invalid proof_type".to_string()))?,
prover_args: value.prover_args.into(),
})
}
Expand Down
Loading

0 comments on commit 8bc4a0f

Please sign in to comment.