Skip to content

Commit b22f392

Browse files
starknet_os: fetch final HP state and use in test_revert
1 parent 3846361 commit b22f392

File tree

5 files changed

+87
-65
lines changed

5 files changed

+87
-65
lines changed

crates/starknet_os/src/hints/hint_implementation/cairo1_revert/test.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::rc::Rc;
44
use std::sync::LazyLock;
55

66
use apollo_starknet_os_program::OS_PROGRAM_BYTES;
7+
use blockifier::state::state_api::StateReader;
78
use cairo_vm::hint_processor::builtin_hint_processor::dict_manager::DictManager;
89
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::insert_value_into_ap;
910
use cairo_vm::types::layout_name::LayoutName;
@@ -150,7 +151,7 @@ fn test_revert(#[case] test_vector: Vec<Operation>) {
150151
)))];
151152
let state_reader = None;
152153
let expected_explicit_return_values = vec![];
153-
let (implicit_return_values, _explicit_return_values) = run_cairo_0_entrypoint(
154+
let (implicit_return_values, _explicit_return_values, hint_processor) = run_cairo_0_entrypoint(
154155
entrypoint,
155156
&explicit_args,
156157
&implicit_args,
@@ -182,6 +183,11 @@ fn test_revert(#[case] test_vector: Vec<Operation>) {
182183
HashSet::from_iter(actual_contract_changes.keys().map(|address| ***address)),
183184
contract_addresses
184185
);
186+
let final_hint_processor_state = &hint_processor
187+
.execution_helpers_manager
188+
.get_current_execution_helper()
189+
.unwrap()
190+
.cached_state;
185191
for (contract_address, contract_change) in actual_contract_changes.iter() {
186192
// Iterate over all storage changes for the contract address and verify that each change is
187193
// as expected.
@@ -192,9 +198,12 @@ fn test_revert(#[case] test_vector: Vec<Operation>) {
192198
let expected_value = expected_contract_storage.get(&full_contract_change.key).unwrap();
193199
assert_eq!(full_contract_change.prev_value, Felt::ZERO);
194200
assert_eq!(full_contract_change.new_value, *expected_value);
195-
// TODO(Dori): If and when we get access to the final state of the hint processor,
196-
// verify that the current state in the execution helper for this contract address
197-
// and storage key is as expected.
201+
assert_eq!(
202+
final_hint_processor_state
203+
.get_storage_at(*contract_address, full_contract_change.key)
204+
.unwrap(),
205+
*expected_value
206+
);
198207
}
199208

200209
// Verify class hashes.

crates/starknet_os/src/hints/hint_implementation/compiled_class/compiled_class_test.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,16 +324,17 @@ fn run_compiled_class_hash_entry_point(
324324
// Run the Cairo entrypoint function.
325325
// State reader is not used in this test.
326326
let state_reader = None;
327-
let (_implicit_return_values, explicit_return_values) = run_cairo_0_entrypoint(
328-
entrypoint,
329-
&explicit_args,
330-
&implicit_args,
331-
state_reader,
332-
&mut runner,
333-
&program,
334-
&runner_config,
335-
&expected_return_values,
336-
)?;
327+
let (_implicit_return_values, explicit_return_values, _hint_processor) =
328+
run_cairo_0_entrypoint(
329+
entrypoint,
330+
&explicit_args,
331+
&implicit_args,
332+
state_reader,
333+
&mut runner,
334+
&program,
335+
&runner_config,
336+
&expected_return_values,
337+
)?;
337338

338339
// Get the actual execution resources, and compare with expected values.
339340
let actual_execution_resources = runner.get_execution_resources().unwrap();

crates/starknet_os/src/hints/hint_implementation/state_diff_encryption/state_diff_encryption_test.rs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,18 @@ fn test_state_diff_encryption_function(
153153
];
154154
let state_reader = None;
155155
let expected_explicit_return_values: Vec<EndpointArg> = vec![];
156-
let (implicit_return_values, _explicit_return_values) = run_cairo_0_entrypoint(
157-
entrypoint,
158-
&explicit_args,
159-
&implicit_args,
160-
state_reader,
161-
&mut runner,
162-
&program,
163-
&runner_config,
164-
&expected_explicit_return_values,
165-
)
166-
.unwrap();
156+
let (implicit_return_values, _explicit_return_values, _hint_processor) =
157+
run_cairo_0_entrypoint(
158+
entrypoint,
159+
&explicit_args,
160+
&implicit_args,
161+
state_reader,
162+
&mut runner,
163+
&program,
164+
&runner_config,
165+
&expected_explicit_return_values,
166+
)
167+
.unwrap();
167168
// [range_check_ptr, encrypted_dst_end]
168169
assert_eq!(implicit_return_values.len(), 2);
169170
let EndpointArg::Value(ValueArg::Single(MaybeRelocatable::RelocatableValue(encrypted_dst_end))) =
@@ -257,17 +258,18 @@ fn test_compute_public_keys_function(#[case] seed: u64, #[case] num_committee_me
257258
];
258259
let state_reader = None;
259260
let expected_explicit_return_values: Vec<EndpointArg> = vec![];
260-
let (implicit_return_values, _explicit_return_values) = run_cairo_0_entrypoint(
261-
entrypoint,
262-
&explicit_args,
263-
&implicit_args,
264-
state_reader,
265-
&mut runner,
266-
&program,
267-
&runner_config,
268-
&expected_explicit_return_values,
269-
)
270-
.unwrap();
261+
let (implicit_return_values, _explicit_return_values, _hint_processor) =
262+
run_cairo_0_entrypoint(
263+
entrypoint,
264+
&explicit_args,
265+
&implicit_args,
266+
state_reader,
267+
&mut runner,
268+
&program,
269+
&runner_config,
270+
&expected_explicit_return_values,
271+
)
272+
.unwrap();
271273

272274
// [range_check_ptr, ec_op_ptr, encrypted_dst_end]
273275
assert_eq!(implicit_return_values.len(), 3);
@@ -357,17 +359,18 @@ fn test_symmetric_key_encryption_function(#[case] seed: u64, #[case] num_committ
357359

358360
let state_reader = None;
359361
let expected_explicit_return_values: Vec<EndpointArg> = vec![];
360-
let (implicit_return_values, _explicit_return_values) = run_cairo_0_entrypoint(
361-
entrypoint,
362-
&explicit_args,
363-
&implicit_args,
364-
state_reader,
365-
&mut runner,
366-
&program,
367-
&runner_config,
368-
&expected_explicit_return_values,
369-
)
370-
.unwrap();
362+
let (implicit_return_values, _explicit_return_values, _hint_processor) =
363+
run_cairo_0_entrypoint(
364+
entrypoint,
365+
&explicit_args,
366+
&implicit_args,
367+
state_reader,
368+
&mut runner,
369+
&program,
370+
&runner_config,
371+
&expected_explicit_return_values,
372+
)
373+
.unwrap();
371374

372375
// [range_check_ptr, ec_op_ptr, encrypted_dst_end]
373376
assert_eq!(implicit_return_values.len(), 3);

crates/starknet_os/src/hints/hint_implementation/stateful_compression/tests.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,17 +529,18 @@ fn test_allocate_addresses_for_state_diff_and_replace(
529529
];
530530

531531
// Run the entrypoint with validations on the explicit & implicit args.
532-
let (_, explicit_return_values) = run_cairo_0_entrypoint(
533-
entrypoint,
534-
&explicit_args,
535-
&implicit_args,
536-
Some(state_reader),
537-
&mut cairo_runner,
538-
&program,
539-
&runner_config,
540-
&expected_explicit_return_values,
541-
)
542-
.unwrap();
532+
let (_implicit_return_values, explicit_return_values, _hint_processor) =
533+
run_cairo_0_entrypoint(
534+
entrypoint,
535+
&explicit_args,
536+
&implicit_args,
537+
Some(state_reader),
538+
&mut cairo_runner,
539+
&program,
540+
&runner_config,
541+
&expected_explicit_return_values,
542+
)
543+
.unwrap();
543544

544545
let [
545546
EndpointArg::Pointer(PointerArg::Array(aliases_storage_updates)),

crates/starknet_os/src/test_utils/cairo_runner.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::any::Any;
22
use std::collections::{HashMap, HashSet};
3+
use std::sync::LazyLock;
34

45
use blockifier::blockifier_versioned_constants::VersionedConstants;
56
use blockifier::test_utils::dict_state_reader::DictStateReader;
@@ -33,6 +34,8 @@ pub type Cairo0EntryPointRunnerResult<T> = Result<T, Cairo0EntryPointRunnerError
3334
#[path = "cairo_runner_test.rs"]
3435
mod test;
3536

37+
pub static EMPTY_BLOCK_INPUT: LazyLock<OsBlockInput> = LazyLock::new(|| OsBlockInput::default());
38+
3639
/// An arg passed by value (i.e., a felt, tuple, named tuple or struct).
3740
#[derive(Clone, Debug, Eq, PartialEq)]
3841
pub enum ValueArg {
@@ -592,7 +595,7 @@ pub fn initialize_and_run_cairo_0_entry_point(
592595
implicit_args,
593596
hint_locals,
594597
)?;
595-
let (explicit_return_values, implicit_return_values) = run_cairo_0_entrypoint(
598+
let (explicit_return_values, implicit_return_values, _hint_processor) = run_cairo_0_entrypoint(
596599
entrypoint,
597600
explicit_args,
598601
implicit_args,
@@ -643,17 +646,23 @@ pub fn initialize_cairo_runner(
643646
Ok((cairo_runner, program, entrypoint))
644647
}
645648

649+
/// Runs a Cairo0 entry point.
650+
/// Returns the implicit and explicit return values, and the final state of the hint processor.
646651
#[allow(clippy::too_many_arguments)]
647-
pub fn run_cairo_0_entrypoint(
652+
pub fn run_cairo_0_entrypoint<'a>(
648653
entrypoint: String,
649654
explicit_args: &[EndpointArg],
650655
implicit_args: &[ImplicitArg],
651656
state_reader: Option<DictStateReader>,
652657
cairo_runner: &mut CairoRunner,
653-
program: &Program,
658+
program: &'a Program,
654659
runner_config: &EntryPointRunnerConfig,
655660
expected_explicit_return_values: &[EndpointArg],
656-
) -> Cairo0EntryPointRunnerResult<(Vec<EndpointArg>, Vec<EndpointArg>)> {
661+
) -> Cairo0EntryPointRunnerResult<(
662+
Vec<EndpointArg>,
663+
Vec<EndpointArg>,
664+
SnosHintProcessor<'a, DictStateReader>,
665+
)> {
657666
// TODO(Amos): Perform complete validations.
658667
perform_basic_validations_on_explicit_args(explicit_args, program, &entrypoint)?;
659668
perform_basic_validations_on_implicit_args(implicit_args, program, &entrypoint)?;
@@ -667,12 +676,11 @@ pub fn run_cairo_0_entrypoint(
667676
info!("Converted explicit & implicit args to Cairo args.");
668677

669678
let (os_hints_config, os_state_input) = (None, None);
670-
let os_block_input = OsBlockInput::default();
671679
let mut hint_processor = SnosHintProcessor::new_for_testing(
672680
state_reader,
673681
program,
674682
os_hints_config,
675-
&os_block_input,
683+
&*EMPTY_BLOCK_INPUT,
676684
os_state_input,
677685
)
678686
.unwrap_or_else(|err| panic!("Failed to create SnosHintProcessor: {err:?}"));
@@ -700,5 +708,5 @@ pub fn run_cairo_0_entrypoint(
700708
info!("Successfully finished running entrypoint {entrypoint}");
701709
let (implicit_return_values, explicit_return_values) =
702710
get_return_values(implicit_args, expected_explicit_return_values, &cairo_runner.vm)?;
703-
Ok((implicit_return_values, explicit_return_values))
711+
Ok((implicit_return_values, explicit_return_values, hint_processor))
704712
}

0 commit comments

Comments
 (0)