Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions crates/apollo_starknet_os_program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions crates/apollo_starknet_os_program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub static OS_PROGRAM: LazyLock<Program> = LazyLock::new(|| {
});

pub static PROGRAM_HASH: LazyLock<ProgramHash> = 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.")
});
4 changes: 2 additions & 2 deletions crates/apollo_starknet_os_program/src/program_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 26 additions & 2 deletions crates/apollo_starknet_os_program/src/program_hash_test.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> = 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);
}
}
Loading