Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/check status api #19

Merged
merged 20 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 248 additions & 36 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ members = [
"crates/worker",
]

[workspace.dependencies]
[workspace.dependencies]
andrew2003 marked this conversation as resolved.
Show resolved Hide resolved
irelia_adapter = { path = "crates/adapter" }
irelia_common = { path = "crates/common" }
irelia_core = { path = "crates/core" }
Expand Down Expand Up @@ -51,10 +51,10 @@ scopeguard = { version = "1.2.0" }
serde = { version = "1.0.210", features = ["derive"] }
sqlx = { version = "*" }
serde_json = { version = "1.0.128" }
stone-cli = { git = "https://github.com/sota-zk-labs/stone-cli" }
stone-cli = { git = "https://github.com/zksecurity/stone-cli.git" }
tempfile = { version = "3.13.0" }
test-log = { version = "0.2.16" }
testcontainers-modules = { version = "0.11.2" }
testcontainers-modules = { version = "0.11.3" }
thiserror = { version = "1.0.64" }
tokio = { version = "1.39.3", features = ["macros", "rt-multi-thread"] }
tonic = { version = "0.12.2" }
Expand Down
9 changes: 6 additions & 3 deletions crates/adapter/src/prover/stone_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use irelia_core::entities::sharp_proof::SharpProof;
use irelia_core::ports::prover::ProverPort;
use scopeguard::defer;
use stone_cli::args::Network::ethereum;
use stone_cli::args::{LayoutName, SerializeArgs, VerifyArgs};
use stone_cli::args::{LayoutName, SerializeArgs, StoneVersion, VerifyArgs};
use stone_cli::bootloader::run_bootloader;
use stone_cli::prover::run_stone_prover_bootloader;
use stone_cli::serialize::serialize_proof;
Expand Down Expand Up @@ -104,16 +104,20 @@ impl ProverPort for StoneProver {
proof: proof_tmp_dir.path().join(BOOTLOADER_PROOF_NAME),
annotation_file: Some(proof_tmp_dir.path().join(ANNOTATION_PATH)),
extra_output_file: Some(proof_tmp_dir.path().join(EXTRA_OUTPUT_PATH)),
stone_version: StoneVersion::V5,
};
run_stone_verifier(verify_args).map_err(|e| VerifierError(e.to_string()))?;

// serialize proof
let serialize_args = SerializeArgs {
proof: proof_tmp_dir.path().join(BOOTLOADER_PROOF_NAME),
network: ethereum,
output: proof_tmp_dir.path().join(SERIALIZED_PROOF_PATH),
output: Option::from(proof_tmp_dir.path().join(SERIALIZED_PROOF_PATH)),
output_dir: None,
layout: None,
annotation_file: Some(proof_tmp_dir.path().join(ANNOTATION_PATH)),
extra_output_file: Some(proof_tmp_dir.path().join(EXTRA_OUTPUT_PATH)),
serialization_type: None,
};
serialize_proof(serialize_args).map_err(|e| SerializationError(e.to_string()))?;

Expand Down Expand Up @@ -143,6 +147,5 @@ mod tests {
let layout = LayoutName::starknet;
let stone_prover = StoneProver { layout, cairo_pie };
assert!(stone_prover.generate_proof().await.is_ok());
// println!("vjp");
}
}
22 changes: 22 additions & 0 deletions crates/adapter/src/repositories/postgres/job_db.rs
andrew2003 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use async_trait::async_trait;
use deadpool_diesel::postgres::Pool;
use diesel::dsl::select;
use diesel::{
delete, insert_into, update, ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper,
};
use diesel_migrations::{embed_migrations, EmbeddedMigrations};
use irelia_core::common::core_error::CoreError;
use irelia_core::entities::job::{JobEntity, JobId};
use irelia_core::ports::job::JobPort;
use uuid::Uuid;

use crate::repositories::postgres::models::job::JobModel;
use crate::repositories::postgres::schema::jobs::dsl::jobs;
Expand Down Expand Up @@ -117,4 +119,24 @@ impl JobPort for JobDBRepository {
.await
.unwrap()
}

// async fn get_job_id(&self, params: &Vec<String>) -> Result<JobId, CoreError> {
// let customer_id = &params[0];
// let cairo_job_key = &params[1];
//
// self.db.get().await.unwrap().interact(move |conn| {
// use crate::repositories::postgres::schema::jobs::dsl::{jobs, customer_id as db_customer_id, cairo_job_key as db_cairo_job_key, id};
//
// let job_id: Uuid = jobs
// .filter(db_customer_id.eq(customer_id))
// .filter(db_cairo_job_key.eq(cairo_job_key))
// .select(id)
// .first(conn)
// .map_err(|err| match err {
// diesel::result::Error::NotFound => CoreError::NotFound,
// _ => CoreError::InternalError(err.into()),
// })?;
// Ok(JobId(job_id))
// }).await.unwrap()
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE status_job;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Your SQL goes here
CREATE TABLE job_status (
id UUID PRIMARY KEY,
customer_id VARCHAR NOT NULL,
cairo_job_key VARCHAR NOT NULL,
status VARCHAR NOT NULL,
validation_done BOOLEAN NOT NULL,
created_on TIMESTAMP NOT NULL DEFAULT NOW()
);
1 change: 1 addition & 0 deletions crates/adapter/src/repositories/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod config;
pub mod job_db;
pub mod models;
pub mod schema;
pub mod status_db;
1 change: 1 addition & 0 deletions crates/adapter/src/repositories/postgres/models/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl TryFrom<JobEntity> for JobModel {
offchain_proof: entity.offchain_proof,
proof_layout: entity.proof_layout,
cairo_pie: entity.cairo_pie,

created_on: SystemTime::now(),
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/adapter/src/repositories/postgres/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod job;
pub mod status;
55 changes: 55 additions & 0 deletions crates/adapter/src/repositories/postgres/models/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::io::{Error, ErrorKind};
use std::time::SystemTime;

use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable};
use irelia_core::entities::job::JobId;
use irelia_core::entities::status::{StatusEntity, StatusId};
use uuid::Uuid;

use crate::repositories::postgres::models::job::JobModel;
use crate::repositories::postgres::schema::jobs::{cairo_job_key, customer_id};

#[derive(Debug, Queryable, Insertable, Selectable, AsChangeset, Identifiable, Clone)]
#[diesel(table_name = super::super::schema::job_status)]
pub struct StatusModel {
pub id: Uuid,
pub customer_id: String,
pub cairo_job_key: String,
pub status: String,
pub validation_done: bool,

pub created_on: SystemTime,
}

impl TryFrom<StatusEntity> for StatusModel {
type Error = Error;

fn try_from(entity: StatusEntity) -> Result<Self, Self::Error> {
let id = entity
.id
.0
.try_into()
.map_err(|_| Error::new(ErrorKind::InvalidInput, "Invalid ID"))?;
Ok(StatusModel {
id,
customer_id: entity.customer_id,
cairo_job_key: entity.cairo_job_key,
status: entity.status,
validation_done: entity.validation_done,

created_on: SystemTime::now(),
})
}
}

impl From<StatusModel> for StatusEntity {
fn from(val: StatusModel) -> Self {
StatusEntity {
id: StatusId(val.id.try_into().unwrap()),
customer_id: val.customer_id,
cairo_job_key: val.cairo_job_key,
status: val.status,
validation_done: val.validation_done,
}
}
}
11 changes: 11 additions & 0 deletions crates/adapter/src/repositories/postgres/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ diesel::table! {
created_on -> Timestamp,
}
}

diesel::table! {
job_status (id) {
id -> Uuid,
customer_id -> Varchar,
cairo_job_key -> Varchar,
status -> Varchar,
validation_done -> Bool,
created_on -> Timestamp,
}
}
121 changes: 121 additions & 0 deletions crates/adapter/src/repositories/postgres/status_db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use async_trait::async_trait;
use deadpool_diesel::postgres::Pool;
use diesel::dsl::select;
use diesel::{
delete, insert_into, update, ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper,
};
use diesel_migrations::{embed_migrations, EmbeddedMigrations};
use irelia_core::common::core_error::CoreError;
use irelia_core::entities::status::{StatusEntity, StatusId};
use irelia_core::ports::status::StatusPort;
use uuid::Uuid;

use crate::repositories::postgres::models::status::StatusModel;
use crate::repositories::postgres::schema::job_status::dsl::job_status;
use crate::repositories::postgres::schema::job_status::id;

// NOTE: path relative to Cargo.toml
pub const MIGRATIONS: EmbeddedMigrations =
embed_migrations!("./src/repositories/postgres/migrations");

#[derive(Clone)]
pub struct StatusJobDBRepository {
pub db: Pool,
}

impl StatusJobDBRepository {
pub fn new(db: Pool) -> Self {
StatusJobDBRepository { db }
}
}

#[async_trait]
impl StatusPort for StatusJobDBRepository {
async fn add(&self, status_job: StatusEntity) -> Result<StatusEntity, CoreError> {
self.db
.get()
.await
.unwrap()
.interact(move |conn| {
let status_job = StatusModel::try_from(status_job)
.map_err(|err| CoreError::InternalError(err.into()))?;
let response = insert_into(job_status)
.values(&status_job)
.get_result::<StatusModel>(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError(err.into()),
})
.unwrap();
Ok(response.into())
})
.await
.unwrap()
}

async fn update(&self, status_job: StatusEntity) -> Result<StatusEntity, CoreError> {
self.db
.get()
.await
.unwrap()
.interact(move |conn| {
let status_job = StatusModel::try_from(status_job)
.map_err(|err| CoreError::InternalError(err.into()))?;
let response = update(job_status.filter(id.eq(status_job.id)))
.set(&status_job)
.get_result::<StatusModel>(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError(err.into()),
})?
.into();

Ok(response)
})
.await
.unwrap()
}

async fn delete(&self, status_job_id: &StatusId) -> Result<(), CoreError> {
let status_job_id = status_job_id.0;
self.db
.get()
.await
.unwrap()
.interact(move |conn| {
let _ = delete(job_status.filter(id.eq(status_job_id)))
.execute(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError(err.into()),
})?;

Ok(())
})
.await
.unwrap()
}

async fn get(&self, status_job_id: &StatusId) -> Result<StatusEntity, CoreError> {
let status_job_id = status_job_id.0;
self.db
.get()
.await
.unwrap()
.interact(move |conn| {
let response = job_status
.select(StatusModel::as_select())
.find(status_job_id)
.first(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError(err.into()),
})?
.into();

Ok(response)
})
.await
.unwrap()
}
}
2 changes: 1 addition & 1 deletion crates/common/src/workers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, Debug)]
pub struct Worker<T> {
pub data: T,
pub tracing: HashMap<String, String>,
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/entities/job.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use serde::{Deserialize, Serialize};
use uuid::Uuid;
/// Identifier for a question.
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod job;
pub mod memory_statement;
pub mod merkle_statement;
pub mod sharp_proof;
pub mod status;
41 changes: 41 additions & 0 deletions crates/core/src/entities/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::fmt;

use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::entities::job::JobId;

#[derive(Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Clone)]
pub struct StatusId(pub Uuid);

pub enum JobStatus {
Pending,
InProgress,
Completed,
Failed,
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct StatusEntity {
pub id: StatusId,
pub customer_id: String,
pub cairo_job_key: String,
pub status: String,
pub validation_done: bool,
}
impl fmt::Display for JobStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
JobStatus::Pending => write!(f, "PENDING"),
JobStatus::InProgress => write!(f, "IN_PROGRESS"),
JobStatus::Completed => write!(f, "COMPLETED"),
JobStatus::Failed => write!(f, "FAILED"),
}
}
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct StatusReponse {
pub status: Option<String>,
pub validation: Option<String>,
}
1 change: 1 addition & 0 deletions crates/core/src/ports/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cache;
pub mod job;
pub mod prover;
pub mod status;
pub mod worker;
Loading