Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Deserialize program
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen committed Oct 5, 2023
1 parent 5980361 commit b5d138a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 71 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

79 changes: 8 additions & 71 deletions src/services/api/contract_classes/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
use crate::core::contract_address::compute_hinted_class_hash;
use crate::services::api::contract_class_errors::ContractClassError;
use cairo_vm::felt::{Felt252, PRIME_STR};
use cairo_vm::serde::deserialize_program::{
deserialize_array_of_bigint_hex, deserialize_felt_hex, Attribute, BuiltinName, HintParams,
Identifier, ReferenceManager,
};
use cairo_vm::felt::Felt252;
use cairo_vm::serde::deserialize_program::deserialize_felt_hex;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::types::{errors::program_errors::ProgramError, program::Program};
use core::str::FromStr;
use getset::{CopyGetters, Getters};
use serde;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use starknet_api::deprecated_contract_class::{
number_or_string, ContractClassAbiEntry, EntryPoint,
};
use starknet_api::deprecated_contract_class::{number_or_string, ContractClassAbiEntry};
use std::collections::HashMap;
use std::path::Path;

Expand Down Expand Up @@ -104,12 +99,6 @@ pub struct ContractClass {
pub(crate) abi: Option<AbiType>,
}

fn deserialize_program<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Program, D::Error> {
use cairo_vm::serde::deserialize_program::{parse_program_json, ProgramJson};
let json = ProgramJson::deserialize(d)?;
Ok(parse_program_json(json, None).unwrap())
}

impl ContractClass {
pub fn new(
program_json: Value,
Expand Down Expand Up @@ -207,72 +196,20 @@ impl FromStr for ContractClass {

/// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON.
fn from_str(program_json: &str) -> Result<Self, ProgramError> {
let contract_class: starknet_api::deprecated_contract_class::ContractClass =
serde_json::from_str(program_json)?;
let program = to_cairo_runner_program(contract_class.program)?;
let entry_points_by_type = convert_entry_points(contract_class.entry_points_by_type);
let hinted_class_hash =
compute_hinted_class_hash(&serde_json::from_str(program_json)?).unwrap();
Ok(ContractClass {
hinted_class_hash,
program,
entry_points_by_type,
abi: contract_class.abi,
})
Self::from_program_json_and_class_hash(program_json, hinted_class_hash)
}
}

// -------------------
// Helper Functions
// -------------------

pub(crate) fn convert_entry_points(
entry_points: HashMap<starknet_api::deprecated_contract_class::EntryPointType, Vec<EntryPoint>>,
) -> HashMap<EntryPointType, Vec<ContractEntryPoint>> {
let mut converted_entries: HashMap<EntryPointType, Vec<ContractEntryPoint>> = HashMap::new();
for (entry_type, entry_points) in entry_points {
let en_type = entry_type.into();

let contracts_entry_points = entry_points
.into_iter()
.map(|e| {
let selector = Felt252::from_bytes_be(e.selector.0.bytes());
let offset = e.offset.0;
ContractEntryPoint::new(selector, offset)
})
.collect::<Vec<ContractEntryPoint>>();

converted_entries.insert(en_type, contracts_entry_points);
}

converted_entries
}

pub(crate) fn to_cairo_runner_program(
program: starknet_api::deprecated_contract_class::Program,
) -> Result<Program, ProgramError> {
let identifiers = serde_json::from_value::<HashMap<String, Identifier>>(program.identifiers)?;

if program.prime != *PRIME_STR {
return Err(ProgramError::PrimeDiffers(program.prime.to_string()));
};

let mut error_message_attributes =
serde_json::from_value::<Vec<Attribute>>(program.attributes).unwrap_or_default();
error_message_attributes.retain(|attr| attr.name == "error_message");

let program = Program::new(
serde_json::from_value::<Vec<BuiltinName>>(program.builtins)?,
deserialize_array_of_bigint_hex(program.data)?,
None,
serde_json::from_value::<HashMap<usize, Vec<HintParams>>>(program.hints)?,
serde_json::from_value::<ReferenceManager>(program.reference_manager)?,
identifiers,
error_message_attributes,
None,
)?;

Ok(program)
fn deserialize_program<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Program, D::Error> {
use cairo_vm::serde::deserialize_program::{parse_program_json, ProgramJson};
let json = ProgramJson::deserialize(d)?;
Ok(parse_program_json(json, None).unwrap())
}

#[cfg(test)]
Expand Down

0 comments on commit b5d138a

Please sign in to comment.