Skip to content

Commit

Permalink
feat: run prover_client based on the argument
Browse files Browse the repository at this point in the history
  • Loading branch information
fborello-lambda committed Dec 4, 2024
1 parent d9c6873 commit d560fef
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 38 deletions.
7 changes: 5 additions & 2 deletions crates/l2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ L1_AUTH_PORT=8551
# Used in the .env file. Ensure the same port is used for `ENGINE_API_RPC_URL`.
L2_AUTH_PORT=8552

# It can be `risc0` or `sp1`
L2_PROVER_TYPE=risc0

# Local L1

init-local-l1: ## 🚀 Initializes an L1 Lambda ethrex Client with Docker (Used with make init)
Expand Down Expand Up @@ -87,10 +90,10 @@ down-l2: ## 🛑 Shuts down the L2 Lambda ethrex Client
restart-l2: down-l2 init-l2 ## 🔄 Restarts the L2 Lambda ethrex Client

init-l2-prover: ## 🚀 Initializes the Prover
cargo run --release --features build_zkvm --manifest-path ../../Cargo.toml --bin ethrex_prover
cargo run --release --features build_zkvm --manifest-path ../../Cargo.toml --bin ethrex_prover -- ${L2_PROVER_TYPE}

init-l2-prover-gpu: ## 🚀 Initializes the Prover with GPU support
cargo run --release --features "build_zkvm,gpu" --manifest-path ../../Cargo.toml --bin ethrex_prover
cargo run --release --features "build_zkvm,gpu" --manifest-path ../../Cargo.toml --bin ethrex_prover -- ${L2_PROVER_TYPE}

rm_db_l2: ## 🛑 Removes the DB used by the L2
cargo run --release --manifest-path ../../Cargo.toml --bin ethrex -- removedb --datadir ${ethrex_L2_DEV_LIBMDBX}
Expand Down
11 changes: 3 additions & 8 deletions crates/l2/proposer/prover_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ struct ProverServer {
verifier_private_key: SecretKey,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub enum ProverType {
RISC0,
SP1,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Risc0Proof {
pub receipt: Box<risc0_zkvm::Receipt>,
Expand Down Expand Up @@ -274,13 +268,14 @@ impl ProverServer {
block_number,
zk_proof,
}) => {
self.handle_submit(&mut stream, block_number)?;

if block_number != (last_verified_block + 1) {
return Err(ProverServerError::Custom(format!("Prover Client submitted an invalid block_number: {block_number}. The last_proved_block is: {}", last_verified_block)));
}

self.handle_proof_submission(block_number, zk_proof).await?;

self.handle_submit(&mut stream, block_number)?;
}
Err(e) => {
warn!("Failed to parse request: {e}");
Expand Down
9 changes: 3 additions & 6 deletions crates/l2/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@ name = "ethrex_prover"
path = "src/main.rs"

[features]
default = ["risc0"]
risc0 = []
sp1 = []
default = []
## risc0
build_zkvm = ["zkvm_interface/build_zkvm"]
gpu = ["risc0-zkvm/cuda"]
build_zkvm = ["zkvm_interface/build_zkvm", "build_sp1"]
gpu = ["risc0-zkvm/cuda", "sp1-sdk/cuda"]
## sp1
build_sp1 = []
sp1_gpu = ["sp1-sdk/cuda"]


[lints.clippy]
Expand Down
Binary file modified crates/l2/prover/sp1/zkvm/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
5 changes: 3 additions & 2 deletions crates/l2/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pub mod prover;
pub mod prover_client;

use ethrex_l2::utils::config::prover_client::ProverClientConfig;
use prover::ProverType;
use tracing::warn;

pub async fn init_client(config: ProverClientConfig) {
prover_client::start_proof_data_client(config).await;
pub async fn init_client(config: ProverClientConfig, prover_type: ProverType) {
prover_client::start_proof_data_client(config, prover_type).await;
warn!("Prover finished!");
}
26 changes: 23 additions & 3 deletions crates/l2/prover/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ethrex_l2::utils::config::{prover_client::ProverClientConfig, read_env_file};
use ethrex_prover_lib::init_client;

use ethrex_prover_lib::{init_client, prover::ProverType};
use std::env;
use tracing::{self, debug, error, warn, Level};

#[tokio::main]
Expand All @@ -23,6 +23,26 @@ async fn main() {
return;
};

let args: Vec<String> = env::args().collect();

let prover_type = match args.len() {
2 => {
let prover_type_str = args.get(1).map_or("none", |v| v);
match prover_type_str {
"sp1" => ProverType::SP1,
"risc0" => ProverType::RISC0,
_ => {
error!("Wrong argument, try with 'risc0' or 'sp1'");
return;
}
}
}
_ => {
error!("Try passing risc0 or sp1 as argument.");
return;
}
};

debug!("Prover Client has started");
init_client(config).await;
init_client(config, prover_type).await;
}
15 changes: 15 additions & 0 deletions crates/l2/prover/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,25 @@ pub enum ProvingOutput {
Sp1Prover(Sp1Proof),
}

pub enum ProverType {
RISC0,
SP1,
}

pub fn create_prover(prover_type: ProverType) -> Box<dyn Prover> {
match prover_type {
ProverType::RISC0 => Box::new(Risc0Prover::new()),
ProverType::SP1 => Box::new(Sp1Prover::new()),
}
}

// Implement the Prover trait for the enum
pub trait Prover {
fn prove(&mut self, input: ProgramInput) -> Result<ProvingOutput, Box<dyn std::error::Error>>;
fn verify(&self, proving_output: &ProvingOutput) -> Result<(), Box<dyn std::error::Error>>;
fn get_gas(&self) -> Result<u64, Box<dyn std::error::Error>>;
fn get_commitment(
&self,
proving_output: &ProvingOutput,
) -> Result<ProgramOutput, Box<dyn std::error::Error>>;
}
Expand Down Expand Up @@ -109,6 +122,7 @@ impl<'a> Prover for Risc0Prover<'a> {
}

fn get_commitment(
&self,
proving_output: &ProvingOutput,
) -> Result<ProgramOutput, Box<dyn std::error::Error>> {
let commitment = match proving_output {
Expand Down Expand Up @@ -163,6 +177,7 @@ impl<'a> Prover for Sp1Prover<'a> {
}

fn get_commitment(
&self,
proving_output: &ProvingOutput,
) -> Result<ProgramOutput, Box<dyn std::error::Error>> {
// TODO
Expand Down
25 changes: 8 additions & 17 deletions crates/l2/prover/src/prover_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use ethrex_l2::{
utils::config::prover_client::ProverClientConfig,
};

use crate::prover::{Prover, ProvingOutput};
use crate::prover::{ create_prover, ProverType, ProvingOutput};

pub async fn start_proof_data_client(config: ProverClientConfig) {
pub async fn start_proof_data_client(config: ProverClientConfig, prover_type: ProverType) {
let proof_data_client = ProverClient::new(config);
proof_data_client.start().await;
proof_data_client.start(prover_type).await;
}

struct ProverData {
Expand All @@ -39,24 +39,15 @@ impl ProverClient {
}
}

pub async fn start(&self) {
pub async fn start(&self, prover_type: ProverType) {
// Build the prover depending on the prover_type passed as argument.
let mut prover = create_prover(prover_type);

loop {
match self.request_new_input() {
// If we get the input
Ok(prover_data) => {
// Build the prover based on the feature.
#[cfg(feature= "risc0")]
let mut prover = crate::prover::Risc0Prover::new();
#[cfg(feature= "sp1")]
let mut prover = crate::prover::Sp1Prover::new();

// Handle case where neither feature is enabled
#[cfg(not(any(feature = "risc0", feature = "sp1")))]
{
sleep(Duration::from_millis(self.interval_ms)).await;
error!("Either 'risc0' or 'sp1' feature must be enabled.");
}

// Generate the Proof
match prover.prove(prover_data.input) {
Ok(proving_output) => {
if let Err(e) =
Expand Down

0 comments on commit d560fef

Please sign in to comment.