Skip to content

Commit

Permalink
feat(snos_job): Block number fetch + Dummy test
Browse files Browse the repository at this point in the history
  • Loading branch information
akhercha committed Jul 22, 2024
1 parent b50c5df commit 0a99bdb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
2 changes: 2 additions & 0 deletions crates/orchestrator/src/jobs/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ pub const JOB_METADATA_STATE_UPDATE_BLOCKS_TO_SETTLE_KEY: &str = "blocks_number_
pub const JOB_METADATA_STATE_UPDATE_FETCH_FROM_TESTS: &str = "fetch_from_test_data";
pub const JOB_METADATA_STATE_UPDATE_ATTEMPT_PREFIX: &str = "attempt_tx_hashes_";
pub const JOB_METADATA_STATE_UPDATE_LAST_FAILED_BLOCK_NO: &str = "last_failed_block_no";

pub const JOB_METADATA_SNOS_BLOCK: &str = "block_number_to_run";
38 changes: 24 additions & 14 deletions crates/orchestrator/src/jobs/snos_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use blockifier::context::{ChainInfo, FeeTokenAddresses};
use blockifier::versioned_constants::VersionedConstants;
use cairo_vm::types::layout_name::LayoutName;
use cairo_vm::Felt252;
use color_eyre::eyre::eyre;
use color_eyre::Result;
use num::FromPrimitive;
use snos::execution::helper::ExecutionHelperWrapper;
Expand All @@ -26,6 +27,8 @@ use crate::jobs::snos_job::dummy_state::DummyState;
use crate::jobs::types::{JobItem, JobStatus, JobType, JobVerificationStatus};
use crate::jobs::Job;

use super::constants::JOB_METADATA_SNOS_BLOCK;

pub struct SnosJob;

#[async_trait]
Expand Down Expand Up @@ -60,10 +63,9 @@ impl Job for SnosJob {

let block_number_and_hash = BlockNumberHashPair {
number: block_number,
// NOTE: 😹😹😹😹😹
hash: BlockHash(StarkFelt::from(
FieldElement::from_bytes_be(&snos_input.block_hash.clone().to_bytes_be())
.expect("Could not convert Felt to FieldElement 😹"),
.expect("Could not convert Felt to FieldElement"),
)),
};

Expand Down Expand Up @@ -91,17 +93,18 @@ impl Job for SnosJob {
},
};

let block_context = pre_process_block(
let block_context = match pre_process_block(
&mut state,
Some(block_number_and_hash),
block_info,
chain_info,
VersionedConstants::latest_constants().clone(),
)
// TODO: Handle result instead of unsafe unwrap
.unwrap();
) {
Ok(block_context) => block_context,
Err(e) => return Err(eyre!("pre_process_block failed for block #{}: {}", block_number, e)),
};

// TODO: contract_storage_map should be retrieved from the state?
// TODO: contract_storage_map should be retrieved from where?
let contract_storage_map = HashMap::default();
let execution_helper = ExecutionHelperWrapper::new(
contract_storage_map,
Expand All @@ -111,17 +114,18 @@ impl Job for SnosJob {
);

// 3. Import SNOS in Rust and execute it with the input data
let (_cairo_pie, _snos_output) = run_os(
let (_cairo_pie, _snos_output) = match run_os(
// TODO: what is this path?
String::from("PATH/TO/THE/OS"),
// TODO: which layout should we choose?
LayoutName::plain,
snos_input,
block_context,
execution_helper,
)
// TODO: Handle result instead of unsafe unwrap
.unwrap();
) {
Ok((cairo_pie, snos_output)) => (cairo_pie, snos_output),
Err(e) => return Err(eyre!("Could not run SNOS for block #{}: {}", block_number, e)),
};

// 3. Store the received PIE in DB
// TODO: do we want to store the SnosOutput also?
Expand Down Expand Up @@ -150,11 +154,17 @@ impl Job for SnosJob {
}

impl SnosJob {
// TODO: actually parse the metadata
fn get_block_number_from_metadata(&self, _job: &JobItem) -> Result<BlockNumber> {
Ok(BlockNumber(42_u64))
/// Get the block number that needs to be run with SNOS for the current
/// job.
fn get_block_number_from_metadata(&self, job: &JobItem) -> Result<BlockNumber> {
let block_number = job
.metadata
.get(JOB_METADATA_SNOS_BLOCK)
.ok_or_else(|| eyre!("Block number to run with SNOS must be specified (snos job #{})", job.internal_id))?;
Ok(BlockNumber(block_number.parse()?))
}

/// Retrieves the [StarknetOsInput] for the provided block number from Madara.
fn get_snos_input_from_madara(&self, _block_number: &BlockNumber) -> Result<StarknetOsInput> {
// TODO: JSON RPC call to `getSnosInput` for a specific block
let snos_input = StarknetOsInput::load(std::path::Path::new("i_do_not_exist_😹.txt")).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions crates/orchestrator/src/tests/jobs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use rstest::rstest;

#[cfg(test)]
pub mod snos_job;

#[cfg(test)]
pub mod da_job;

Expand Down
28 changes: 28 additions & 0 deletions crates/orchestrator/src/tests/jobs/snos_job/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use rstest::*;
use std::collections::HashMap;

use super::super::common::init_config;

use crate::jobs::{
snos_job::SnosJob,
types::{JobStatus, JobType},
Job,
};

#[rstest]
#[tokio::test]
async fn test_create_job() {
let config = init_config(None, None, None, None, None, None).await;

let job = SnosJob.create_job(&config, String::from("0"), HashMap::default()).await;
assert!(job.is_ok());

let job = job.unwrap();
let job_type = job.job_type;

assert_eq!(job_type, JobType::SnosRun, "job_type should be SnosRun");
assert!(!(job.id.is_nil()), "id should not be nil");
assert_eq!(job.status, JobStatus::Created, "status should be Created");
assert_eq!(job.version, 0_i32, "version should be 0");
assert_eq!(job.external_id.unwrap_string().unwrap(), String::new(), "external_id should be empty string");
}

0 comments on commit 0a99bdb

Please sign in to comment.