Skip to content

Commit

Permalink
server dev
Browse files Browse the repository at this point in the history
Okm165 committed Jan 27, 2025
1 parent 622abc9 commit 7c077d7
Showing 10 changed files with 157 additions and 14 deletions.
5 changes: 5 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
@@ -18,6 +18,7 @@ members = [
[workspace.dependencies]
alloy = { version = "0.7.2", features = ["full"] }
alloy-rlp = { version = "0.3.5", features = ["derive"] }
anyhow = "1.0.95"
axum = { version = "0.8", features = ["tracing"] }
bincode = { version = "2.0.0-rc.3", default-features = false, features = ["serde"]}
cairo-lang-casm = { version = "2.10.0-rc.1", default-features = false }
3 changes: 1 addition & 2 deletions crates/fetcher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -373,8 +373,7 @@ where
.collect()
}

pub fn parse_syscall_handler(input_file: &[u8]) -> Result<ProofKeys, FetcherError> {
let syscall_handler = serde_json::from_slice::<SyscallHandler>(input_file)?;
pub fn parse_syscall_handler(syscall_handler: SyscallHandler) -> Result<ProofKeys, FetcherError> {
let mut proof_keys = ProofKeys::default();

// Process EVM keys
5 changes: 4 additions & 1 deletion crates/fetcher/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fs, path::PathBuf};

use clap::{Parser, ValueHint};
use dry_hint_processor::syscall_handler::SyscallHandler;
use fetcher::{parse_syscall_handler, Fetcher};
use types::ChainProofs;

@@ -17,7 +18,9 @@ struct Args {
async fn main() -> Result<(), fetcher::FetcherError> {
let args = Args::try_parse_from(std::env::args()).map_err(fetcher::FetcherError::Args)?;
let input_file = fs::read(&args.filename)?;
let proof_keys = parse_syscall_handler(&input_file)?;

let syscall_handler: SyscallHandler = serde_json::from_slice(&input_file)?;
let proof_keys = parse_syscall_handler(syscall_handler)?;

let fetcher = Fetcher::new(&proof_keys);
let (evm_proofs, starknet_proofs) = tokio::try_join!(fetcher.collect_evm_proofs(), fetcher.collect_starknet_proofs())?;
7 changes: 6 additions & 1 deletion crates/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -4,10 +4,15 @@ version = "0.1.0"
edition = "2024"

[dependencies]
anyhow.workspace = true
axum.workspace = true
fetcher.workspace = true
cairo-vm.workspace = true
dry_hint_processor.workspace = true
sound_hint_processor.workspace = true
serde.workspace = true
tokio.workspace = true
tower-http.workspace = true
tracing-subscriber.workspace = true
types.workspace = true
types.workspace = true
hints.workspace = true
59 changes: 55 additions & 4 deletions crates/server/src/dry_run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
use std::{env, path::PathBuf};

use axum::Json;
use dry_hint_processor::syscall_handler::SyscallHandler;
use types::HDPDryRunInput;
use cairo_vm::{
cairo_run::{self, cairo_run_program},
types::{layout::CairoLayoutParams, layout_name::LayoutName, program::Program},
};
use dry_hint_processor::{
CustomHintProcessor,
syscall_handler::{SyscallHandler, SyscallHandlerWrapper},
};
use hints::vars;
use serde::Deserialize;
use types::{HDPDryRunInput, error::Error};

use crate::error::AppError;

#[derive(Debug, Deserialize)]
pub struct DryRunRequest {
params: Option<CairoLayoutParams>,
layout: LayoutName,
input: HDPDryRunInput,
}

pub async fn root(Json(value): Json<DryRunRequest>) -> Result<Json<SyscallHandler>, AppError> {
// Init CairoRunConfig
let cairo_run_config = cairo_run::CairoRunConfig {
trace_enabled: false,
relocate_mem: false,
layout: value.layout,
proof_mode: false,
secure_run: Some(true),
allow_missing_builtins: Some(true),
dynamic_layout_params: value.params,
..Default::default()
};

// Locate the compiled program file in the `OUT_DIR` folder.
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is not set"));
let program_file_path = out_dir.join("cairo").join("compiled.json");

let program_file = std::fs::read(program_file_path).map_err(Error::IO)?;

// Load the Program
let program = Program::from_bytes(&program_file, Some(cairo_run_config.entrypoint))?;

let mut hint_processor = CustomHintProcessor::new(value.input);
let cairo_runner = cairo_run_program(&program, &cairo_run_config, &mut hint_processor).unwrap();

pub async fn root(Json(value): Json<HDPDryRunInput>) -> Json<SyscallHandler> {
todo!()
Ok(Json(
cairo_runner
.exec_scopes
.get::<SyscallHandlerWrapper>(vars::scopes::SYSCALL_HANDLER)?
.syscall_handler
.try_read()?
.clone(),
))
}
25 changes: 25 additions & 0 deletions crates/server/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};

// Make our own error that wraps `anyhow::Error`.
pub struct AppError(anyhow::Error);

// Tell axum how to convert `AppError` into a response.
impl IntoResponse for AppError {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, format!("Something went wrong: {}", self.0)).into_response()
}
}

// This enables using `?` on functions that return `Result<_, anyhow::Error>` to turn them into
// `Result<_, AppError>`. That way you don't need to do that manually.
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}
16 changes: 14 additions & 2 deletions crates/server/src/fetch_proofs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
use axum::Json;
use dry_hint_processor::syscall_handler::SyscallHandler;
use fetcher::{Fetcher, parse_syscall_handler};
use types::ChainProofs;

pub async fn root(Json(value): Json<SyscallHandler>) -> Json<Vec<ChainProofs>> {
todo!()
use crate::error::AppError;

pub async fn root(Json(value): Json<SyscallHandler>) -> Result<Json<Vec<ChainProofs>>, AppError> {
let proof_keys = parse_syscall_handler(value)?;

let fetcher = Fetcher::new(&proof_keys);
let (evm_proofs, starknet_proofs) = tokio::try_join!(fetcher.collect_evm_proofs(), fetcher.collect_starknet_proofs())?;
let chain_proofs = vec![
ChainProofs::EthereumSepolia(evm_proofs),
ChainProofs::StarknetSepolia(starknet_proofs),
];

Ok(Json(chain_proofs))
}
1 change: 1 addition & 0 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ use tower_http::{timeout::TimeoutLayer, trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

pub mod dry_run;
pub mod error;
pub mod fetch_proofs;
pub mod sound_run;

49 changes: 45 additions & 4 deletions crates/server/src/sound_run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
use std::{env, path::PathBuf};

use axum::Json;
use cairo_vm::vm::runners::cairo_pie::CairoPie;
use types::HDPInput;
use cairo_vm::{
cairo_run::{self, cairo_run_program},
types::{layout::CairoLayoutParams, layout_name::LayoutName, program::Program},
vm::runners::cairo_pie::CairoPie,
};
use serde::Deserialize;
use sound_hint_processor::CustomHintProcessor;
use types::{HDPInput, error::Error};

use crate::error::AppError;

#[derive(Debug, Deserialize)]
pub struct SoundRunRequest {
params: Option<CairoLayoutParams>,
layout: LayoutName,
input: HDPInput,
}

pub async fn root(Json(value): Json<SoundRunRequest>) -> Result<Json<CairoPie>, AppError> {
// Init CairoRunConfig
let cairo_run_config = cairo_run::CairoRunConfig {
trace_enabled: false,
relocate_mem: false,
layout: value.layout,
proof_mode: false,
secure_run: Some(true),
allow_missing_builtins: Some(true),
dynamic_layout_params: value.params,
..Default::default()
};

// Locate the compiled program file in the `OUT_DIR` folder.
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is not set"));
let program_file_path = out_dir.join("cairo").join("compiled.json");

let program_file = std::fs::read(program_file_path).map_err(Error::IO)?;

// Load the Program
let program = Program::from_bytes(&program_file, Some(cairo_run_config.entrypoint))?;

let mut hint_processor = CustomHintProcessor::new(value.input);
let cairo_runner = cairo_run_program(&program, &cairo_run_config, &mut hint_processor).unwrap();

pub async fn root(Json(value): Json<HDPInput>) -> Json<CairoPie> {
todo!()
Ok(Json(cairo_runner.get_cairo_pie()?))
}

0 comments on commit 7c077d7

Please sign in to comment.