Skip to content

Commit

Permalink
feat(starknet_os): added PanickingStateReader and stateless OS runner…
Browse files Browse the repository at this point in the history
… function (#3947)
  • Loading branch information
dorimedini-starkware authored Feb 5, 2025
1 parent c9b798b commit 1819f6e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/starknet_os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ blockifier.workspace = true
cairo-vm.workspace = true
indoc.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
strum = { workspace = true, optional = true }
strum_macros = { workspace = true, optional = true }
thiserror.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_os/src/hint_processor.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod execution_helper;
pub mod panicking_state_reader;
35 changes: 35 additions & 0 deletions crates/starknet_os/src/hint_processor/panicking_state_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use blockifier::execution::contract_class::RunnableCompiledClass;
use blockifier::state::state_api::{StateReader, StateResult};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;

/// State reader that always panics.
/// Use this as the `OsExecutionHelper`'s state reader to ensure the OS execution is "stateless".
pub struct PanickingStateReader;

impl StateReader for PanickingStateReader {
fn get_storage_at(
&self,
contract_address: ContractAddress,
key: StorageKey,
) -> StateResult<Felt> {
panic!("Called get_storage_at with address {contract_address} and key {key:?}.");
}

fn get_nonce_at(&self, contract_address: ContractAddress) -> StateResult<Nonce> {
panic!("Called get_nonce_at with address {contract_address}.");
}

fn get_class_hash_at(&self, contract_address: ContractAddress) -> StateResult<ClassHash> {
panic!("Called get_class_hash_at with address {contract_address}.");
}

fn get_compiled_class(&self, class_hash: ClassHash) -> StateResult<RunnableCompiledClass> {
panic!("Called get_compiled_class with class hash {class_hash}.");
}

fn get_compiled_class_hash(&self, class_hash: ClassHash) -> StateResult<CompiledClassHash> {
panic!("Called get_compiled_class_hash with class hash {class_hash}.");
}
}
12 changes: 12 additions & 0 deletions crates/starknet_os/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cairo_vm::types::layout_name::LayoutName;

use crate::errors::StarknetOsError;
use crate::hint_processor::execution_helper::OsExecutionHelper;
use crate::hint_processor::panicking_state_reader::PanickingStateReader;
use crate::io::os_input::StarknetOsInput;
use crate::io::os_output::StarknetOsRunnerOutput;

Expand All @@ -16,3 +17,14 @@ pub fn run_os<S: StateReader>(
let _execution_helper = OsExecutionHelper::<S>::new(os_input);
todo!()
}

/// Run the OS with a "stateless" state reader - panics if the state is accessed for data that was
/// not pre-loaded as part of the input.
pub fn run_os_stateless(
compiled_os: &[u8],
layout: LayoutName,
block_context: BlockContext,
os_input: &StarknetOsInput,
) -> Result<StarknetOsRunnerOutput, StarknetOsError> {
run_os::<PanickingStateReader>(compiled_os, layout, block_context, os_input)
}

0 comments on commit 1819f6e

Please sign in to comment.