Skip to content

Commit

Permalink
test(os hint): load compiled classes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 10, 2023
1 parent 0cda4b2 commit 4d6d45e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 58 deletions.
1 change: 1 addition & 0 deletions scripts/setup-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ cairo-compile cairo-lang/src/starkware/starknet/core/os/os.cairo --output build/
cairo-compile tests/programs/different_output.cairo --output build/different_output.json
cairo-compile tests/programs/fact.cairo --output build/fact.json
cairo-compile tests/programs/hint.cairo --output build/hint.json
cairo-compile tests/programs/load_compiled_classes.cairo --output build/load_compiled_classes.json
8 changes: 3 additions & 5 deletions src/hints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
};
use cairo_vm::hint_processor::hint_processor_definition::HintReference;

use crate::io::StarknetOsInput;
use cairo_vm::serde::deserialize_program::ApTracking;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
Expand All @@ -18,9 +19,6 @@ use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_def
BuiltinHintProcessor, HintFunc,
};

use crate::os_input::StarknetOsInput;
use crate::utils::hasher::pedersen::PedersenHasher;

pub mod hints_raw;

pub fn sn_hint_processor() -> BuiltinHintProcessor {
Expand Down Expand Up @@ -98,8 +96,8 @@ pub fn load_compiled_class_facts(
// %{ os_input = ... %}
// Can't directly get os_input.compiled_classes so we need to get the whole os_input
let compiled_class_facts = exec_scopes
.get_ref::<StarknetOsInput<(), PedersenHasher>>("os_input")?
.compiled_classes
.get_ref::<StarknetOsInput>("os_input")?
.compiled_classes()
.clone();
// ids.n_compiled_class_facts = len(os_input.compiled_classes)
insert_value_from_var_name(
Expand Down
7 changes: 6 additions & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};

#[allow(unused)]
struct StarknetOsInput {
pub struct StarknetOsInput {
contract_state_commitment_info: CommitmentInfo,
contract_class_commitment_info: CommitmentInfo,
deprecated_compiled_classes: HashMap<Felt252, Felt252>, // TODO: Add contract_class module
Expand All @@ -21,6 +21,11 @@ struct StarknetOsInput {
transactions: Vec<InternalTransaction>,
block_hash: Felt252,
}
impl StarknetOsInput {
pub fn compiled_classes(&self) -> &HashMap<Felt252, Felt252> {
&self.compiled_classes
}
}

pub struct StarknetOsOutput {
/// The state commitment before this block.
Expand Down
9 changes: 0 additions & 9 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ pub trait Storage {
/// Returns the value of the leaf at the given path.
fn leaf(&self, path: &BitSlice<u8, Msb0>) -> anyhow::Result<Option<StarkFelt>>;
}
impl Storage for () {
fn set_value(&self, _key: Vec<u8>, _value: Vec<u8>) {}

fn get_value(&self, _key: Vec<u8>) -> Option<Vec<u8>> {
None
}

fn del_value(&self, _key: Vec<u8>) {}
}

#[derive(Clone, Debug)]
pub enum Node {
Expand Down
1 change: 0 additions & 1 deletion tests/contracts/load_compiled_classes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

func main(output_ptr: felt*) -> (output_ptr: felt*) {
alloc_locals;
%{ mock_os_input %}
local compiled_class_facts;
local n_compiled_class_facts;
%{
Expand Down
17 changes: 17 additions & 0 deletions tests/programs/load_compiled_classes.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%builtins output

func main(output_ptr: felt*) -> (output_ptr: felt*) {
alloc_locals;
local compiled_class_facts;
local n_compiled_class_facts;
%{
ids.compiled_class_facts = segments.add()
ids.n_compiled_class_facts = len(os_input.compiled_classes)
vm_enter_scope({
'compiled_class_facts': iter(os_input.compiled_classes.items()),
})
%}
// When entering a scope we need to exit it afterwards otherwise the vm panics.
%{ vm_exit_scope() %}
return(output_ptr = output_ptr);
}
44 changes: 2 additions & 42 deletions tests/snos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,9 @@ use snos::{state::SharedState, SnOsRunner};
use starknet_api::block::BlockNumber;
use starknet_api::transaction::Calldata;

use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::get_integer_from_var_name;
use cairo_vm::hint_processor::hint_processor_definition::HintReference;
use cairo_vm::serde::deserialize_program::ApTracking;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use common::{check_output_vs_python, compile_contracts};
use snos::hints::hints_raw::*;
use snos::hints::load_compiled_class_facts;
use snos::os_input::StarknetOsInput;
use snos::utils::hasher::pedersen::PedersenHasher;
use snos::SnOsRunner;
use std::collections::HashMap;

use std::fs;
use std::rc::Rc;

Expand All @@ -45,32 +36,6 @@ fn prepared_os_test(prepare_os_test: (SharedState<DictStateReader>, Vec<Calldata
assert_eq!(BlockNumber(2), shared_state.get_block_num())
}

#[rstest]
fn custom_hint_ok() {
let program_content = fs::read("build/hint.json").unwrap();

// Wrap the Rust hint implementation in a Box smart pointer inside a HintFunc
let hint = HintFunc(Box::new(print_a_hint));

//Instantiate the hint processor
let mut hint_processor = BuiltinHintProcessor::new_empty();

//Add the custom hint, together with the Python code
hint_processor.add_hint(String::from("print(ids.a)"), Rc::new(hint));

//Run the cairo program
let (_cairo_runner, virtual_machine) = cairo_run(
&program_content,
&CairoRunConfig {
layout: "all_cairo",
..Default::default()
},
&mut hint_processor,
)
.expect("Couldn't run program");
check_output_vs_python("build/hint.json", virtual_machine);
}

#[rstest]
#[should_panic(expected = "Output #0 is different")]
fn test_different_outputs() {
Expand All @@ -93,17 +58,12 @@ fn test_different_outputs() {
}

#[rstest]
fn load_compiled_classes_facts_test(_compile_contracts: ()) {
fn load_compiled_classes_facts_test() {
let program_path = "build/load_compiled_classes.json";

// Instantiate the hint processor
let mut hint_processor = BuiltinHintProcessor::new_empty();

//Add the custom hint, together with the Python code
hint_processor.add_hint(
String::from("mock_os_input"),
Rc::new(HintFunc(Box::new(mock_os_input))),
);
hint_processor.add_hint(
LOAD_COMPILED_CLASS_FACTS.to_owned(),
Rc::new(HintFunc(Box::new(load_compiled_class_facts))),
Expand Down

0 comments on commit 4d6d45e

Please sign in to comment.