From ae377e6af4e46a1b828d010d1abe959319ea5e87 Mon Sep 17 00:00:00 2001 From: Arun Jangra Date: Mon, 6 Jan 2025 20:08:13 +0530 Subject: [PATCH] l3 changes --- crates/orchestrator/src/constants.rs | 1 + .../orchestrator/src/jobs/snos_job/error.rs | 2 + .../src/jobs/snos_job/fact_info.rs | 3 +- crates/orchestrator/src/jobs/snos_job/mod.rs | 71 ++++++++++++++++++- madara-bootstrapper | 2 +- 5 files changed, 75 insertions(+), 4 deletions(-) diff --git a/crates/orchestrator/src/constants.rs b/crates/orchestrator/src/constants.rs index 049d3ee3..43f9dd1e 100644 --- a/crates/orchestrator/src/constants.rs +++ b/crates/orchestrator/src/constants.rs @@ -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"; diff --git a/crates/orchestrator/src/jobs/snos_job/error.rs b/crates/orchestrator/src/jobs/snos_job/error.rs index 5dcdb80e..2418f277 100644 --- a/crates/orchestrator/src/jobs/snos_job/error.rs +++ b/crates/orchestrator/src/jobs/snos_job/error.rs @@ -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, } diff --git a/crates/orchestrator/src/jobs/snos_job/fact_info.rs b/crates/orchestrator/src/jobs/snos_job/fact_info.rs index 60464c48..87f7466d 100644 --- a/crates/orchestrator/src/jobs/snos_job/fact_info.rs +++ b/crates/orchestrator/src/jobs/snos_job/fact_info.rs @@ -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; @@ -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, diff --git a/crates/orchestrator/src/jobs/snos_job/mod.rs b/crates/orchestrator/src/jobs/snos_job/mod.rs index 5a9892bf..a68d9dfd 100644 --- a/crates/orchestrator/src/jobs/snos_job/mod.rs +++ b/crates/orchestrator/src/jobs/snos_job/mod.rs @@ -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; @@ -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. @@ -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, @@ -185,6 +190,68 @@ impl SnosJob { cairo_pie: CairoPie, snos_output: StarknetOsOutput, program_output: Vec, + ) -> 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, + ) -> 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, ) -> 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| { diff --git a/madara-bootstrapper b/madara-bootstrapper index f717bf17..b0b64750 160000 --- a/madara-bootstrapper +++ b/madara-bootstrapper @@ -1 +1 @@ -Subproject commit f717bf179581da53d68fee03b50ef78e0628ee20 +Subproject commit b0b647500c2ae3e3b0d99e345fa652989bca4726