serve proofs through http interface

This commit is contained in:
Giacomo Pasini 2025-04-24 10:30:12 +02:00
parent fe80b62415
commit 97b128dc65
No known key found for this signature in database
GPG Key ID: FC08489D2D895D4B
2 changed files with 72 additions and 1 deletions

View File

@ -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<ProofRequest>) -> 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()
}
}
}

View File

@ -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<u64, String> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
}
}
}
#[tokio::main]
async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
// 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(())
}