diff --git a/Cargo.lock b/Cargo.lock index 5dec8d9a29..2f3555f1ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8772,6 +8772,7 @@ dependencies = [ "assert_matches", "bitvec", "cairo-lang-starknet-classes", + "cairo-vm", "derive_more", "hex", "indexmap 2.2.6", diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index 8ea14307a6..488e6f8895 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -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, @@ -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; diff --git a/crates/blockifier/src/execution/execution_utils.rs b/crates/blockifier/src/execution/execution_utils.rs index 7824e13873..ad2f2ffeed 100644 --- a/crates/blockifier/src/execution/execution_utils.rs +++ b/crates/blockifier/src/execution/execution_utils.rs @@ -1,16 +1,7 @@ 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; @@ -18,7 +9,6 @@ use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResour 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; @@ -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 { - let identifiers = serde_json::from_value::>(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::>>(program.hints)?; - let main = None; - let error_message_attributes = match program.attributes { - serde_json::Value::Null => vec![], - attributes => serde_json::from_value::>(attributes)? - .into_iter() - .filter(|attr| attr.name == "error_message") - .collect(), - }; - - let instruction_locations = None; - let reference_manager = serde_json::from_value::(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 { diff --git a/crates/starknet_api/Cargo.toml b/crates/starknet_api/Cargo.toml index cc7a44a7f3..b8ee2dbcb3 100644 --- a/crates/starknet_api/Cargo.toml +++ b/crates/starknet_api/Cargo.toml @@ -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"] } diff --git a/crates/starknet_api/src/deprecated_contract_class.rs b/crates/starknet_api/src/deprecated_contract_class.rs index eb5de5db90..dcb359582a 100644 --- a/crates/starknet_api/src/deprecated_contract_class.rs +++ b/crates/starknet_api/src/deprecated_contract_class.rs @@ -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}; @@ -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 { + let identifiers = serde_json::from_value::>(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::>>(program.hints)?; + let main = None; + let error_message_attributes = match program.attributes { + serde_json::Value::Null => vec![], + attributes => serde_json::from_value::>(attributes)? + .into_iter() + .filter(|attr| attr.name == "error_message") + .collect(), + }; + + let instruction_locations = None; + let reference_manager = serde_json::from_value::(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(hints: &serde_json::Value, serializer: S) -> Result where