Skip to content

Commit

Permalink
l3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ocdbytes committed Jan 6, 2025
1 parent 383c88b commit ae377e6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/orchestrator/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub const BLOB_DATA_FILE_NAME: &str = "blob_data.txt";
pub const SNOS_OUTPUT_FILE_NAME: &str = "snos_output.json";
pub const PROGRAM_OUTPUT_FILE_NAME: &str = "program_output.txt";
pub const CAIRO_PIE_FILE_NAME: &str = "cairo_pie.zip";
pub const ON_CHAIN_DATA_FILE_NAME: &str = "onchain_data.json";
2 changes: 2 additions & 0 deletions crates/orchestrator/src/jobs/snos_job/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ pub enum FactError {
TreeStructureRootOffsetInvalid(usize, usize),
#[error("Program output doesn't match the segment size.")]
InvalidSegment,
#[error("Failed to build OnChainData from Cairo PIE.")]
OnChainDataCompute,
}
3 changes: 2 additions & 1 deletion crates/orchestrator/src/jobs/snos_job/fact_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::runners::cairo_pie::CairoPie;
use cairo_vm::Felt252;
use serde::{Deserialize, Serialize};
use starknet::core::types::Felt;
use starknet_os::crypto::poseidon::poseidon_hash_many_bytes;

Expand All @@ -30,7 +31,7 @@ pub struct FactInfo {
pub fact: B256,
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct OnChainData {
pub on_chain_data_hash: B256,
pub on_chain_data_size: usize,
Expand Down
71 changes: 69 additions & 2 deletions crates/orchestrator/src/jobs/snos_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use uuid::Uuid;
use super::constants::{JOB_METADATA_SNOS_BLOCK, JOB_METADATA_SNOS_FACT};
use super::{JobError, OtherError};
use crate::config::Config;
use crate::constants::{CAIRO_PIE_FILE_NAME, PROGRAM_OUTPUT_FILE_NAME, SNOS_OUTPUT_FILE_NAME};
use crate::constants::{CAIRO_PIE_FILE_NAME, ON_CHAIN_DATA_FILE_NAME, PROGRAM_OUTPUT_FILE_NAME, SNOS_OUTPUT_FILE_NAME};
use crate::data_storage::DataStorage;
use crate::jobs::snos_job::error::FactError;
use crate::jobs::snos_job::fact_info::get_fact_info;
use crate::jobs::snos_job::fact_info::{build_on_chain_data, get_fact_info};
use crate::jobs::types::{JobItem, JobStatus, JobType, JobVerificationStatus};
use crate::jobs::Job;

Expand Down Expand Up @@ -54,6 +54,10 @@ pub enum SnosError {
SnosOutputUnstorable { internal_id: String, message: String },
#[error("Could not store the Program output (snos job #{internal_id:?}): {message}")]
ProgramOutputUnstorable { internal_id: String, message: String },
#[error("Could not serialize the On Chain Data (Snos job #{internal_id:?}): {message}")]
OnChainDataUnserializable { internal_id: String, message: String },
#[error("Could not store the On Chain Data (snos job #{internal_id:?}): {message}")]
OnChainDataUnstorable { internal_id: String, message: String },

// ProveBlockError from Snos is not usable with #[from] since it does not implement PartialEq.
// Instead, we convert it to string & pass it into the [SnosExecutionError] error.
Expand Down Expand Up @@ -177,6 +181,7 @@ impl SnosJob {
/// The paths will be:
/// - [block_number]/cairo_pie.zip
/// - [block_number]/snos_output.json
/// - [block_number]/program_output.json
async fn store(
&self,
data_storage: &dyn DataStorage,
Expand All @@ -185,6 +190,68 @@ impl SnosJob {
cairo_pie: CairoPie,
snos_output: StarknetOsOutput,
program_output: Vec<Felt252>,
) -> Result<(), SnosError> {
self.store_cairo_pie_and_snos_output(
data_storage,
internal_id,
block_number,
cairo_pie,
snos_output,
program_output,
)
.await?;
Ok(())
}

/// Stores the [CairoPie] and the [StarknetOsOutput] and [OnChainData] in the Data Storage.
/// The paths will be:
/// - [block_number]/cairo_pie.zip
/// - [block_number]/snos_output.json
/// - [block_number]/onchain_data.json
/// - [block_number]/program_output.json
async fn store_l2(
&self,
data_storage: &dyn DataStorage,
internal_id: &str,
block_number: u64,
cairo_pie: CairoPie,
snos_output: StarknetOsOutput,
program_output: Vec<Felt252>,
) -> Result<(), SnosError> {
self.store_cairo_pie_and_snos_output(
data_storage,
internal_id,
block_number,
cairo_pie,
snos_output,
program_output,
)
.await?;
let on_chain_data = build_on_chain_data(&cairo_pie)
.map_err(|e| SnosError::FactCalculationError(FactError::OnChainDataCompute))?;
let on_chain_data_key = format!("{block_number}/{ON_CHAIN_DATA_FILE_NAME}");
let on_chain_data_vec = serde_json::to_vec(&on_chain_data).map_err(|e| {
SnosError::OnChainDataUnserializable { internal_id: internal_id.to_string(), message: e.to_string() }
})?;
data_storage.put_data(on_chain_data_vec.into(), &on_chain_data_key).await.map_err(|e| {
SnosError::OnChainDataUnstorable { internal_id: internal_id.to_string(), message: e.to_string() }
})?;
Ok(())
}

/// Stores the [CairoPie] and the [StarknetOsOutput] in the Data Storage.
/// The paths will be:
/// - [block_number]/cairo_pie.zip
/// - [block_number]/snos_output.json
/// - [block_number]/program_output.json
async fn store_cairo_pie_and_snos_output(
&self,
data_storage: &dyn DataStorage,
internal_id: &str,
block_number: u64,
cairo_pie: CairoPie,
snos_output: StarknetOsOutput,
program_output: Vec<Felt252>,
) -> Result<(), SnosError> {
let cairo_pie_key = format!("{block_number}/{CAIRO_PIE_FILE_NAME}");
let cairo_pie_zip_bytes = self.cairo_pie_to_zip_bytes(cairo_pie).await.map_err(|e| {
Expand Down

0 comments on commit ae377e6

Please sign in to comment.