Skip to content

Commit

Permalink
chore: move sn_api_to_cairo_vm_program into snapi
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 14, 2024
1 parent 6120a00 commit 0d41708
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 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.

2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use serde::de::Error as DeserializationError;
use serde::{Deserialize, Deserializer};
use starknet_api::core::EntryPointSelector;
use starknet_api::deprecated_contract_class::{
sn_api_to_cairo_vm_program,
ContractClass as DeprecatedContractClass,
EntryPoint,
EntryPointOffset,
Expand All @@ -35,7 +36,6 @@ use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants::{self, CONSTRUCTOR_ENTRY_POINT_NAME};
use crate::execution::entry_point::CallEntryPoint;
use crate::execution::errors::{ContractClassError, PreExecutionError};
use crate::execution::execution_utils::sn_api_to_cairo_vm_program;
use crate::fee::eth_gas_constants;
use crate::transaction::errors::TransactionExecutionError;

Expand Down
42 changes: 0 additions & 42 deletions crates/blockifier/src/execution/execution_utils.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
use std::collections::HashMap;

use cairo_lang_runner::casm_run::format_next_item;
use cairo_vm::serde::deserialize_program::{
deserialize_array_of_bigint_hex,
Attribute,
HintParams,
Identifier,
ReferenceManager,
};
use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::types::errors::program_errors::ProgramError;
use cairo_vm::types::program::Program;
use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable};
use cairo_vm::vm::errors::memory_errors::MemoryError;
use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResources};
use cairo_vm::vm::vm_core::VirtualMachine;
use num_bigint::BigUint;
use starknet_api::core::ClassHash;
use starknet_api::deprecated_contract_class::Program as DeprecatedProgram;
use starknet_api::transaction::Calldata;
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -116,38 +106,6 @@ pub fn felt_range_from_ptr(
Ok(values)
}

// TODO(Elin,01/05/2023): aim to use LC's implementation once it's in a separate crate.
pub fn sn_api_to_cairo_vm_program(program: DeprecatedProgram) -> Result<Program, ProgramError> {
let identifiers = serde_json::from_value::<HashMap<String, Identifier>>(program.identifiers)?;
let builtins = serde_json::from_value(program.builtins)?;
let data = deserialize_array_of_bigint_hex(program.data)?;
let hints = serde_json::from_value::<HashMap<usize, Vec<HintParams>>>(program.hints)?;
let main = None;
let error_message_attributes = match program.attributes {
serde_json::Value::Null => vec![],
attributes => serde_json::from_value::<Vec<Attribute>>(attributes)?
.into_iter()
.filter(|attr| attr.name == "error_message")
.collect(),
};

let instruction_locations = None;
let reference_manager = serde_json::from_value::<ReferenceManager>(program.reference_manager)?;

let program = Program::new(
builtins,
data,
main,
hints,
reference_manager,
identifiers,
error_message_attributes,
instruction_locations,
)?;

Ok(program)
}

#[derive(Debug)]
// Invariant: read-only.
pub struct ReadOnlySegment {
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ testing = []
[dependencies]
bitvec = "1.0.1"
cairo-lang-starknet-classes = "2.7.0-dev.0"
cairo-vm.workspace = true
derive_more = "0.99.17"
hex = "0.4.3"
indexmap = { version = "2.1.0", features = ["serde"] }
Expand Down
41 changes: 41 additions & 0 deletions crates/starknet_api/src/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ use std::collections::HashMap;
use std::num::ParseIntError;

use cairo_lang_starknet_classes::casm_contract_class::CasmContractEntryPoint;
use cairo_vm::serde::deserialize_program::{
deserialize_array_of_bigint_hex,
Attribute,
HintParams,
Identifier,
ReferenceManager,
};
use cairo_vm::types::errors::program_errors::ProgramError;
use cairo_vm::types::program::Program as CairoVmProgram;
use itertools::Itertools;
use serde::de::Error as DeserializationError;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -140,6 +149,38 @@ pub struct Program {
pub reference_manager: serde_json::Value,
}

// TODO(Elin,01/05/2023): aim to use LC's implementation once it's in a separate crate.
pub fn sn_api_to_cairo_vm_program(program: Program) -> Result<CairoVmProgram, ProgramError> {
let identifiers = serde_json::from_value::<HashMap<String, Identifier>>(program.identifiers)?;
let builtins = serde_json::from_value(program.builtins)?;
let data = deserialize_array_of_bigint_hex(program.data)?;
let hints = serde_json::from_value::<HashMap<usize, Vec<HintParams>>>(program.hints)?;
let main = None;
let error_message_attributes = match program.attributes {
serde_json::Value::Null => vec![],
attributes => serde_json::from_value::<Vec<Attribute>>(attributes)?
.into_iter()
.filter(|attr| attr.name == "error_message")
.collect(),
};

let instruction_locations = None;
let reference_manager = serde_json::from_value::<ReferenceManager>(program.reference_manager)?;

let program = CairoVmProgram::new(
builtins,
data,
main,
hints,
reference_manager,
identifiers,
error_message_attributes,
instruction_locations,
)?;

Ok(program)
}

// Serialize hints as a sorted mapping for correct hash computation.
fn serialize_hints_sorted<S>(hints: &serde_json::Value, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit 0d41708

Please sign in to comment.