diff --git a/crates/apollo_starknet_os_program/Cargo.toml b/crates/apollo_starknet_os_program/Cargo.toml index ff53d25c7e2..1cdbefdf2a9 100644 --- a/crates/apollo_starknet_os_program/Cargo.toml +++ b/crates/apollo_starknet_os_program/Cargo.toml @@ -14,6 +14,7 @@ dump_source_files = [] workspace = true [dependencies] +apollo_infra_utils.workspace = true cairo-vm.workspace = true serde = { workspace = true, features = ["derive"] } serde_json.workspace = true diff --git a/crates/apollo_starknet_os_program/src/lib.rs b/crates/apollo_starknet_os_program/src/lib.rs index 0c3a9f28a83..267661d8df0 100644 --- a/crates/apollo_starknet_os_program/src/lib.rs +++ b/crates/apollo_starknet_os_program/src/lib.rs @@ -23,6 +23,8 @@ pub static OS_PROGRAM: LazyLock = LazyLock::new(|| { }); pub static PROGRAM_HASH: LazyLock = LazyLock::new(|| { + // As the program hash file may not exist at runtime, it's contents must be included at compile + // time. serde_json::from_str(include_str!("program_hash.json")) .expect("Failed to deserialize program_hash.json.") }); diff --git a/crates/apollo_starknet_os_program/src/program_hash.rs b/crates/apollo_starknet_os_program/src/program_hash.rs index 773b2db834f..d0183047a13 100644 --- a/crates/apollo_starknet_os_program/src/program_hash.rs +++ b/crates/apollo_starknet_os_program/src/program_hash.rs @@ -20,9 +20,9 @@ pub enum ProgramHashError { UnexpectedRelocatable, } -#[derive(Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize, PartialEq)] pub struct ProgramHash { - os: Felt, + pub os: Felt, } const BOOTLOADER_VERSION: u8 = 0; diff --git a/crates/apollo_starknet_os_program/src/program_hash_test.rs b/crates/apollo_starknet_os_program/src/program_hash_test.rs index 2327492bd71..e29c7bdb13d 100644 --- a/crates/apollo_starknet_os_program/src/program_hash_test.rs +++ b/crates/apollo_starknet_os_program/src/program_hash_test.rs @@ -1,7 +1,31 @@ -use crate::program_hash::compute_os_program_hash; +use std::path::PathBuf; +use std::sync::LazyLock; + +use apollo_infra_utils::compile_time_cargo_manifest_dir; + +use crate::program_hash::{compute_os_program_hash, ProgramHash}; use crate::PROGRAM_HASH; +static PROGRAM_HASH_PATH: LazyLock = LazyLock::new(|| { + PathBuf::from(compile_time_cargo_manifest_dir!()).join("src/program_hash.json") +}); + +/// Asserts the program hash of the compiled Starknet OS program matches the program hash in the +/// JSON. +/// To fix this test, run the following command: +/// ```bash +/// FIX_PROGRAM_HASH=1 cargo test -p apollo_starknet_os_program test_program_hash +/// ``` #[test] fn test_program_hash() { - assert_eq!(compute_os_program_hash().unwrap(), PROGRAM_HASH.os) + let computed_hash = ProgramHash { os: compute_os_program_hash().unwrap() }; + if std::env::var("FIX_PROGRAM_HASH").is_ok() { + std::fs::write( + PROGRAM_HASH_PATH.as_path(), + serde_json::to_string_pretty(&computed_hash).unwrap(), + ) + .unwrap_or_else(|error| panic!("Failed to write the program hash file: {error:?}.")); + } else { + assert_eq!(computed_hash, *PROGRAM_HASH); + } }