Skip to content

Commit f1e2c5e

Browse files
feat(apollo_starknet_os_program): add fixer for program hash test
1 parent 97979f0 commit f1e2c5e

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

crates/apollo_starknet_os_program/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dump_source_files = []
1414
workspace = true
1515

1616
[dependencies]
17+
apollo_infra_utils.workspace = true
1718
cairo-vm.workspace = true
1819
serde = { workspace = true, features = ["derive"] }
1920
serde_json.workspace = true

crates/apollo_starknet_os_program/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub static OS_PROGRAM: LazyLock<Program> = LazyLock::new(|| {
2323
});
2424

2525
pub static PROGRAM_HASH: LazyLock<ProgramHash> = LazyLock::new(|| {
26+
// As the program hash file may not exist at runtime, it's contents must be included at compile
27+
// time.
2628
serde_json::from_str(include_str!("program_hash.json"))
2729
.expect("Failed to deserialize program_hash.json.")
2830
});

crates/apollo_starknet_os_program/src/program_hash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ pub enum ProgramHashError {
2020
UnexpectedRelocatable,
2121
}
2222

23-
#[derive(Deserialize, Serialize)]
23+
#[derive(Debug, Deserialize, Serialize, PartialEq)]
2424
pub struct ProgramHash {
25-
os: Felt,
25+
pub os: Felt,
2626
}
2727

2828
const BOOTLOADER_VERSION: u8 = 0;
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
use crate::program_hash::compute_os_program_hash;
1+
use std::path::PathBuf;
2+
use std::sync::LazyLock;
3+
4+
use apollo_infra_utils::compile_time_cargo_manifest_dir;
5+
6+
use crate::program_hash::{compute_os_program_hash, ProgramHash};
27
use crate::PROGRAM_HASH;
38

9+
static PROGRAM_HASH_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
10+
PathBuf::from(compile_time_cargo_manifest_dir!()).join("src/program_hash.json")
11+
});
12+
13+
/// Asserts the program hash of the compiled Starknet OS program matches the program hash in the
14+
/// JSON.
15+
/// To fix this test, run the following command:
16+
/// ```bash
17+
/// FIX_PROGRAM_HASH=1 cargo test -p apollo_starknet_os_program test_program_hash
18+
/// ```
419
#[test]
520
fn test_program_hash() {
6-
assert_eq!(compute_os_program_hash().unwrap(), PROGRAM_HASH.os)
21+
let computed_hash = ProgramHash { os: compute_os_program_hash().unwrap() };
22+
if std::env::var("FIX_PROGRAM_HASH").is_ok() {
23+
std::fs::write(
24+
PROGRAM_HASH_PATH.as_path(),
25+
serde_json::to_string_pretty(&computed_hash).unwrap(),
26+
)
27+
.unwrap_or_else(|error| panic!("Failed to write the program hash file: {error:?}."));
28+
} else {
29+
assert_eq!(computed_hash, *PROGRAM_HASH);
30+
}
731
}

0 commit comments

Comments
 (0)