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

Commit b5d138a

Browse files
committed
Deserialize program
1 parent 5980361 commit b5d138a

File tree

2 files changed

+10
-71
lines changed

2 files changed

+10
-71
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/services/api/contract_classes/deprecated_contract_class.rs

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
use crate::core::contract_address::compute_hinted_class_hash;
22
use crate::services::api::contract_class_errors::ContractClassError;
3-
use cairo_vm::felt::{Felt252, PRIME_STR};
4-
use cairo_vm::serde::deserialize_program::{
5-
deserialize_array_of_bigint_hex, deserialize_felt_hex, Attribute, BuiltinName, HintParams,
6-
Identifier, ReferenceManager,
7-
};
3+
use cairo_vm::felt::Felt252;
4+
use cairo_vm::serde::deserialize_program::deserialize_felt_hex;
85
use cairo_vm::types::relocatable::MaybeRelocatable;
96
use cairo_vm::types::{errors::program_errors::ProgramError, program::Program};
107
use core::str::FromStr;
118
use getset::{CopyGetters, Getters};
129
use serde;
1310
use serde::{Deserialize, Serialize};
1411
use serde_json::Value;
15-
use starknet_api::deprecated_contract_class::{
16-
number_or_string, ContractClassAbiEntry, EntryPoint,
17-
};
12+
use starknet_api::deprecated_contract_class::{number_or_string, ContractClassAbiEntry};
1813
use std::collections::HashMap;
1914
use std::path::Path;
2015

@@ -104,12 +99,6 @@ pub struct ContractClass {
10499
pub(crate) abi: Option<AbiType>,
105100
}
106101

107-
fn deserialize_program<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Program, D::Error> {
108-
use cairo_vm::serde::deserialize_program::{parse_program_json, ProgramJson};
109-
let json = ProgramJson::deserialize(d)?;
110-
Ok(parse_program_json(json, None).unwrap())
111-
}
112-
113102
impl ContractClass {
114103
pub fn new(
115104
program_json: Value,
@@ -207,72 +196,20 @@ impl FromStr for ContractClass {
207196

208197
/// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON.
209198
fn from_str(program_json: &str) -> Result<Self, ProgramError> {
210-
let contract_class: starknet_api::deprecated_contract_class::ContractClass =
211-
serde_json::from_str(program_json)?;
212-
let program = to_cairo_runner_program(contract_class.program)?;
213-
let entry_points_by_type = convert_entry_points(contract_class.entry_points_by_type);
214199
let hinted_class_hash =
215200
compute_hinted_class_hash(&serde_json::from_str(program_json)?).unwrap();
216-
Ok(ContractClass {
217-
hinted_class_hash,
218-
program,
219-
entry_points_by_type,
220-
abi: contract_class.abi,
221-
})
201+
Self::from_program_json_and_class_hash(program_json, hinted_class_hash)
222202
}
223203
}
224204

225205
// -------------------
226206
// Helper Functions
227207
// -------------------
228208

229-
pub(crate) fn convert_entry_points(
230-
entry_points: HashMap<starknet_api::deprecated_contract_class::EntryPointType, Vec<EntryPoint>>,
231-
) -> HashMap<EntryPointType, Vec<ContractEntryPoint>> {
232-
let mut converted_entries: HashMap<EntryPointType, Vec<ContractEntryPoint>> = HashMap::new();
233-
for (entry_type, entry_points) in entry_points {
234-
let en_type = entry_type.into();
235-
236-
let contracts_entry_points = entry_points
237-
.into_iter()
238-
.map(|e| {
239-
let selector = Felt252::from_bytes_be(e.selector.0.bytes());
240-
let offset = e.offset.0;
241-
ContractEntryPoint::new(selector, offset)
242-
})
243-
.collect::<Vec<ContractEntryPoint>>();
244-
245-
converted_entries.insert(en_type, contracts_entry_points);
246-
}
247-
248-
converted_entries
249-
}
250-
251-
pub(crate) fn to_cairo_runner_program(
252-
program: starknet_api::deprecated_contract_class::Program,
253-
) -> Result<Program, ProgramError> {
254-
let identifiers = serde_json::from_value::<HashMap<String, Identifier>>(program.identifiers)?;
255-
256-
if program.prime != *PRIME_STR {
257-
return Err(ProgramError::PrimeDiffers(program.prime.to_string()));
258-
};
259-
260-
let mut error_message_attributes =
261-
serde_json::from_value::<Vec<Attribute>>(program.attributes).unwrap_or_default();
262-
error_message_attributes.retain(|attr| attr.name == "error_message");
263-
264-
let program = Program::new(
265-
serde_json::from_value::<Vec<BuiltinName>>(program.builtins)?,
266-
deserialize_array_of_bigint_hex(program.data)?,
267-
None,
268-
serde_json::from_value::<HashMap<usize, Vec<HintParams>>>(program.hints)?,
269-
serde_json::from_value::<ReferenceManager>(program.reference_manager)?,
270-
identifiers,
271-
error_message_attributes,
272-
None,
273-
)?;
274-
275-
Ok(program)
209+
fn deserialize_program<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Program, D::Error> {
210+
use cairo_vm::serde::deserialize_program::{parse_program_json, ProgramJson};
211+
let json = ProgramJson::deserialize(d)?;
212+
Ok(parse_program_json(json, None).unwrap())
276213
}
277214

278215
#[cfg(test)]

0 commit comments

Comments
 (0)