Skip to content

Commit 3683a70

Browse files
feat(starknet_committer_and_os_cli): add command to dump the OS compiled program to file (#5876)
* feat(apollo_starknet_os_program): add fixer for program hash test * feat(starknet_committer_and_os_cli): add command to dump the OS compiled program to file * fix(ci): allow service CLI access to cairo-compile Signed-off-by: Dori Medini <dori@starkware.co> --------- Signed-off-by: Dori Medini <dori@starkware.co>
1 parent aa8d9df commit 3683a70

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

.github/workflows/committer_and_os_cli_push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
- run: pip install -r scripts/requirements.txt
8080

8181
- name: Build CLI binary
82-
run: ./build_native_in_docker.sh rustup toolchain install && cargo build -p starknet_committer_and_os_cli -r --bin starknet_committer_and_os_cli --target-dir CLI_TARGET
82+
run: ./build_native_in_docker.sh cargo build -p starknet_committer_and_os_cli -r --bin starknet_committer_and_os_cli --target-dir CLI_TARGET
8383

8484
- id: auth
8585
uses: "google-github-actions/auth@v2"

crates/apollo_starknet_os_program/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ pub static CAIRO_FILES_MAP: LazyLock<HashMap<String, String>> = LazyLock::new(||
1414
.unwrap_or_else(|error| panic!("Failed to deserialize cairo_files_map.json: {error:?}."))
1515
});
1616

17+
pub const OS_PROGRAM_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/starknet_os_bytes"));
18+
1719
pub static OS_PROGRAM: LazyLock<Program> = LazyLock::new(|| {
18-
Program::from_bytes(
19-
include_bytes!(concat!(env!("OUT_DIR"), "/starknet_os_bytes")),
20-
Some("main"),
21-
)
22-
.expect("Failed to load the OS bytes.")
20+
Program::from_bytes(OS_PROGRAM_BYTES, Some("main")).expect("Failed to load the OS bytes.")
2321
});
2422

2523
pub static PROGRAM_HASH: LazyLock<ProgramHash> = LazyLock::new(|| {

crates/starknet_committer_and_os_cli/src/os_cli/commands.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fs;
22
use std::path::Path;
33

4-
use apollo_starknet_os_program::CAIRO_FILES_MAP;
4+
use apollo_starknet_os_program::{CAIRO_FILES_MAP, OS_PROGRAM_BYTES, PROGRAM_HASH};
55
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
66
use cairo_vm::types::layout_name::LayoutName;
77
use cairo_vm::vm::runners::cairo_pie::CairoPie;
@@ -14,7 +14,7 @@ use starknet_os::io::os_output::StarknetOsRunnerOutput;
1414
use starknet_os::runner::run_os_stateless;
1515
use tracing::info;
1616

17-
use super::run_os_cli::OsCliOutput;
17+
use crate::os_cli::run_os_cli::{OsCliOutput, ProgramToDump};
1818
use crate::shared_utils::read::{load_input, write_to_file};
1919

2020
#[derive(Deserialize, Debug)]
@@ -106,3 +106,18 @@ pub(crate) fn serialize_os_runner_output(
106106
pub(crate) fn dump_source_files(output_path: String) {
107107
write_to_file(&output_path, &*CAIRO_FILES_MAP);
108108
}
109+
110+
pub(crate) fn dump_program(output_path: String, program: ProgramToDump) {
111+
let bytes = match program {
112+
ProgramToDump::Os => OS_PROGRAM_BYTES,
113+
};
114+
// Dumping the `Program` struct won't work - it is not deserializable via cairo-lang's Program
115+
// class. JSONify the raw bytes instead.
116+
let program_json = serde_json::from_slice::<serde_json::Value>(bytes)
117+
.expect("Program bytes are JSON-serializable.");
118+
write_to_file(&output_path, &program_json);
119+
}
120+
121+
pub(crate) fn dump_program_hashes(output_path: String) {
122+
write_to_file(&output_path, &*PROGRAM_HASH);
123+
}

crates/starknet_committer_and_os_cli/src/os_cli/run_os_cli.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ use tracing::level_filters::LevelFilter;
99
use tracing_subscriber::reload::Handle;
1010
use tracing_subscriber::Registry;
1111

12-
use crate::os_cli::commands::{dump_source_files, parse_and_run_os};
12+
use crate::os_cli::commands::{
13+
dump_program,
14+
dump_program_hashes,
15+
dump_source_files,
16+
parse_and_run_os,
17+
};
1318
use crate::os_cli::tests::python_tests::OsPythonTestRunner;
1419
use crate::shared_utils::types::{run_python_test, IoArgs, PythonTestArg};
1520

@@ -19,8 +24,28 @@ pub struct OsCliCommand {
1924
command: Command,
2025
}
2126

27+
#[derive(clap::ValueEnum, Clone, Debug, Serialize)]
28+
#[serde(rename_all = "kebab-case")]
29+
pub enum ProgramToDump {
30+
Os,
31+
}
32+
2233
#[derive(Debug, Subcommand)]
2334
enum Command {
35+
DumpProgram {
36+
/// File path to output.
37+
#[clap(long, short = 'o', default_value = "stdout")]
38+
output_path: String,
39+
40+
/// Program to dump.
41+
#[clap(long, value_enum)]
42+
program: ProgramToDump,
43+
},
44+
DumpProgramHashes {
45+
/// File path to output.
46+
#[clap(long, short = 'o', default_value = "stdout")]
47+
output_path: String,
48+
},
2449
DumpSourceFiles {
2550
/// File path to output.
2651
#[clap(long, short = 'o')]
@@ -39,6 +64,8 @@ pub async fn run_os_cli(
3964
) {
4065
info!("Starting starknet-os-cli with command: \n{:?}", os_command);
4166
match os_command.command {
67+
Command::DumpProgram { output_path, program } => dump_program(output_path, program),
68+
Command::DumpProgramHashes { output_path } => dump_program_hashes(output_path),
4269
Command::DumpSourceFiles { output_path } => dump_source_files(output_path),
4370
Command::PythonTest(python_test_arg) => {
4471
run_python_test::<OsPythonTestRunner>(python_test_arg).await;

0 commit comments

Comments
 (0)