Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Package compiled OS as gzip #369

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
3 changes: 2 additions & 1 deletion crates/bin/prove_block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ pub async fn prove_block(
(old_block_number, old_block_hash),
);

Ok(run_os(config::DEFAULT_COMPILED_OS, layout, os_input, block_context, execution_helper)?)
let uncompressed_os = config::gunzip_default_os().expect("Could not uncompress default OS");
Ok(run_os(uncompressed_os.as_bytes(), layout, os_input, block_context, execution_helper)?)
}

pub fn debug_prove_error(err: ProveBlockError) -> ProveBlockError {
Expand Down
1 change: 1 addition & 0 deletions crates/starknet-os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cairo-lang-casm = { workspace = true }
cairo-type-derive = { path = "../cairo-type-derive" }
cairo-vm = { workspace = true }
c-kzg = { workspace = true }
flate2 = { workspace = true }
futures = { workspace = true }
futures-util = { workspace = true }
heck = { workspace = true }
Expand Down
22 changes: 21 additions & 1 deletion crates/starknet-os/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs::File;
use std::io::Read as _;
use std::path::PathBuf;

use blockifier::blockifier::block::{BlockInfo, GasPrices};
Expand All @@ -7,6 +8,7 @@ use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use blockifier::transaction::objects::FeeType;
use blockifier::versioned_constants::VersionedConstants;
use cairo_vm::types::layout_name::LayoutName;
use flate2::read::GzDecoder;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use starknet_api::block::{BlockNumber, BlockTimestamp};
Expand All @@ -23,7 +25,7 @@ pub const fn default_layout() -> LayoutName {
// https://github.com/starkware-libs/blockifier/blob/8da582b285bfbc7d4c21178609bbd43f80a69240/crates/native_blockifier/src/py_block_executor.rs#L44
const MAX_STEPS_PER_TX: u32 = 4_000_000;

pub const DEFAULT_COMPILED_OS: &[u8] = include_bytes!("../../../build/os_latest.json");
pub const DEFAULT_COMPILED_OS_GZIPPED: &[u8] = include_bytes!("../../../build/os_latest.json.gz");

const DEFAULT_CONFIG_PATH: &str = "../../cairo-lang/src/starkware/starknet/definitions/general_config.yml";
pub const STORED_BLOCK_HASH_BUFFER: u64 = 10;
Expand Down Expand Up @@ -67,6 +69,24 @@ const fn default_use_kzg_da() -> bool {
true
}

/// un-gzips the default OS that is baked into the binary.
///
/// Uncompressed, this file is roughly 40M, and compresses effectively down to less than 1M.
/// So it is included in the binary in gzipped form with this fn provided to reinflate it.
///
/// NOTE: the results are not cached, so this will always be an expensive call.
pub fn gunzip_default_os() -> std::io::Result<String> {
notlesh marked this conversation as resolved.
Show resolved Hide resolved
let mut decoder = GzDecoder::new(DEFAULT_COMPILED_OS_GZIPPED);
let mut os_gunzipped = String::new();
let uncompressed_size = decoder.read_to_string(&mut os_gunzipped)?;
log::debug!(
"Os inflated from {}b (gzipped) to {}b (raw json)",
DEFAULT_COMPILED_OS_GZIPPED.len(),
uncompressed_size
);
Ok(os_gunzipped)
}

#[derive(Debug, Serialize, Clone, Deserialize, PartialEq)]
pub struct StarknetGeneralConfig {
pub starknet_os_config: StarknetOsConfig,
Expand Down
1 change: 1 addition & 0 deletions scripts/setup-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ cairo-compile tests/integration/programs/fact.cairo --output build/programs/fact
# compile os with debug info
cairo-compile --debug_info_with_source cairo-lang/src/starkware/starknet/core/os/os.cairo --output build/os_debug.json --cairo_path cairo-lang/src
cairo-compile cairo-lang/src/starkware/starknet/core/os/os.cairo --output build/os_latest.json --cairo_path cairo-lang/src
gzip -f build/os_latest.json

# compile starknet contract
echo -e "compiling starknet contracts...\n"
Expand Down
1 change: 1 addition & 0 deletions scripts/teardown-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ git submodule update --init
echo -e "\nremoving compiled contracts/programs...\n"
rm starkware
rm -rf build/!(os_latest.json)
notlesh marked this conversation as resolved.
Show resolved Hide resolved
rm -rf build/!(os_latest.json.gz)
3 changes: 2 additions & 1 deletion tests/integration/common/transaction_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,8 @@ where
execute_txs(state, &block_context, txs, deprecated_contract_classes, contract_classes).await;

let layout = config::default_layout();
let result = run_os(config::DEFAULT_COMPILED_OS, layout, os_input, block_context, execution_helper);
let uncompressed_os = config::gunzip_default_os().expect("Could not uncompress default OS");
let result = run_os(uncompressed_os.as_bytes(), layout, os_input, block_context, execution_helper);

match &result {
Err(Runner(VmException(vme))) => {
Expand Down
Loading