From 97b128dc65a67520ab4645882b67ce4568d4238c Mon Sep 17 00:00:00 2001 From: Giacomo Pasini Date: Thu, 24 Apr 2025 10:30:12 +0200 Subject: [PATCH] serve proofs through http interface --- sz-poc-offsite-2025/evm/prover/src/http.rs | 39 ++++++++++++++++++++++ sz-poc-offsite-2025/evm/prover/src/main.rs | 34 ++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 sz-poc-offsite-2025/evm/prover/src/http.rs diff --git a/sz-poc-offsite-2025/evm/prover/src/http.rs b/sz-poc-offsite-2025/evm/prover/src/http.rs new file mode 100644 index 0000000..0eca425 --- /dev/null +++ b/sz-poc-offsite-2025/evm/prover/src/http.rs @@ -0,0 +1,39 @@ + +use axum::{ + extract::Query, + http::StatusCode, + response::{IntoResponse, Response}, +}; +use serde::Deserialize; +use std::path::PathBuf; +use tokio::fs; + +#[derive(Deserialize)] +pub struct ProofRequest { + block_start: u64, + block_count: u64 +} + + +/// Handler for GET / +pub async fn serve_proof(Query(query): Query) -> Response { + let file_name = format!("{}-{}.zkp", query.block_start, query.block_count + query.block_start); + + let path = PathBuf::from(&file_name); + + // Read file contents + match fs::read(&path).await { + Ok(bytes) => ( + StatusCode::OK, + bytes, + ).into_response(), + Err(err) => { + let status = if err.kind() == std::io::ErrorKind::NotFound { + StatusCode::NOT_FOUND + } else { + StatusCode::INTERNAL_SERVER_ERROR + }; + (status, format!("Error reading file: {}", err)).into_response() + } + } +} \ No newline at end of file diff --git a/sz-poc-offsite-2025/evm/prover/src/main.rs b/sz-poc-offsite-2025/evm/prover/src/main.rs index 8694f4e..f84a060 100644 --- a/sz-poc-offsite-2025/evm/prover/src/main.rs +++ b/sz-poc-offsite-2025/evm/prover/src/main.rs @@ -1,9 +1,15 @@ use clap::Parser; use reqwest::blocking::Client; use serde_json::{Value, json}; -use std::{path::PathBuf, process::Command, thread, time::Duration}; +use std::{path::PathBuf, process::Command, thread, time::Duration, net::SocketAddr}; use tracing::{debug, error, info}; use tracing_subscriber::{EnvFilter, fmt}; +use axum::{ + routing::get, + Router, +}; + +mod http; #[derive(Parser, Debug)] #[clap(author, version, about = "Ethereum Proof Generation Tool")] @@ -117,6 +123,12 @@ fn get_latest_block(client: &Client, rpc: &str) -> Result { fn main() -> Result<(), Box> { let args = Args::parse(); + std::thread::spawn(move || { + if let Err(e) = run_server() { + error!("Error running server: {}", e); + } + }); + let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(&args.log_level)); @@ -166,3 +178,23 @@ fn main() -> Result<(), Box> { } } } + + + +#[tokio::main] +async fn run_server() -> Result<(), Box> { + // Initialize tracing + tracing_subscriber::fmt::init(); + + // Build our application with a route + let app = Router::new() + .route("/", get(http::serve_proof)); + + let addr = SocketAddr::from(([127, 0, 0, 1], 8070)); + // Run it on localhost:8070 + tracing::info!("Serving files on http://{}", addr); + let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); + axum::serve(listener, app).await.unwrap(); + + Ok(()) +} \ No newline at end of file