Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/core/contract_address/deprecated_contract_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ pub struct CairoProgramToHash<'a> {

/// Computes the hash of the contract class, including hints.
/// We are not supporting backward compatibility now.
fn compute_hinted_class_hash(
contract_class: &ContractClass,
pub(crate) fn compute_hinted_class_hash(
contract_class: &serde_json::Value,
) -> Result<Felt252, ContractAddressError> {
let program_as_string = contract_class.program_json.to_string();
let program_as_string = contract_class.to_string();
let mut cairo_program_hash: CairoContractDefinition = serde_json::from_str(&program_as_string)
.map_err(|err| ContractAddressError::InvalidProgramJson(err.to_string()))?;

Expand Down Expand Up @@ -322,7 +322,7 @@ pub fn compute_deprecated_class_hash(

let builtin_list = compute_hash_on_elements(&builtin_list_vec)?;

let hinted_class_hash = compute_hinted_class_hash(contract_class)?;
let hinted_class_hash = contract_class.hinted_class_hash();

let mut bytecode_vector = Vec::new();

Expand All @@ -342,7 +342,7 @@ pub fn compute_deprecated_class_hash(
l1_handlers,
constructors,
builtin_list,
hinted_class_hash,
hinted_class_hash.clone(),
bytecode,
];

Expand All @@ -364,8 +364,8 @@ mod tests {
.unwrap();

assert_eq!(
compute_hinted_class_hash(&contract_class).unwrap(),
Felt252::from_str_radix(
contract_class.hinted_class_hash(),
&Felt252::from_str_radix(
"1164033593603051336816641706326288678020608687718343927364853957751413025239",
10
)
Expand Down
1 change: 1 addition & 0 deletions src/core/contract_address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ mod deprecated_contract_address;
mod sierra_contract_address;

pub use deprecated_contract_address::compute_deprecated_class_hash;
pub(crate) use deprecated_contract_address::compute_hinted_class_hash;
pub use sierra_contract_address::compute_sierra_class_hash;
11 changes: 6 additions & 5 deletions src/services/api/contract_classes/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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::{
Expand Down Expand Up @@ -75,7 +76,7 @@ pub struct ContractClass {
#[getset(get = "pub")]
pub(crate) program: Program,
#[getset(get = "pub")]
pub(crate) program_json: serde_json::Value,
pub(crate) hinted_class_hash: Felt252,
#[getset(get = "pub")]
pub(crate) entry_points_by_type: HashMap<EntryPointType, Vec<ContractEntryPoint>>,
#[getset(get = "pub")]
Expand All @@ -96,9 +97,9 @@ impl ContractClass {
}
}
}

let hinted_class_hash = compute_hinted_class_hash(&program_json).unwrap();
Ok(ContractClass {
program_json,
hinted_class_hash,
program,
entry_points_by_type,
abi,
Expand Down Expand Up @@ -126,9 +127,9 @@ impl TryFrom<&str> for ContractClass {
let program = to_cairo_runner_program(&contract_class.program)?;
let entry_points_by_type =
convert_entry_points(contract_class.clone().entry_points_by_type);
let program_json = serde_json::from_str(s)?;
let hinted_class_hash = compute_hinted_class_hash(&serde_json::from_str(s)?).unwrap();
Ok(ContractClass {
program_json,
hinted_class_hash,
program,
entry_points_by_type,
abi: contract_class.abi,
Expand Down
20 changes: 5 additions & 15 deletions src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,9 @@ impl<T: StateReader> State for CachedState<T> {
#[cfg(test)]
mod tests {
use super::*;
use crate::services::api::contract_classes::deprecated_contract_class::{
ContractEntryPoint, EntryPointType,
};

use crate::state::in_memory_state_reader::InMemoryStateReader;
use cairo_vm::types::program::Program;

use num_traits::One;

#[test]
Expand Down Expand Up @@ -395,18 +393,10 @@ mod tests {
HashMap::new(),
HashMap::new(),
);

let program_json: serde_json::Value = serde_json::Value::from("{}");
let contract_class = ContractClass::new(
program_json,
Program::default(),
HashMap::from([(
EntryPointType::Constructor,
vec![ContractEntryPoint::default()],
)]),
None,
let contract_class = ContractClass::new_from_path(
"starknet_programs/raw_contract_classes/class_with_abi.json",
)
.expect("Error creating contract class");
.unwrap();

state_reader
.class_hash_to_contract_class
Expand Down
17 changes: 3 additions & 14 deletions src/state/in_memory_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ impl StateReader for InMemoryStateReader {
#[cfg(test)]
mod tests {
use super::*;
use crate::services::api::contract_classes::deprecated_contract_class::{
ContractEntryPoint, EntryPointType,
};
use cairo_vm::types::program::Program;

#[test]
fn get_contract_state_test() {
Expand Down Expand Up @@ -170,18 +166,11 @@ mod tests {
HashMap::new(),
);

let program_json: serde_json::Value = serde_json::Value::from("{}");
let contract_class_key = [0; 32];
let contract_class = ContractClass::new(
program_json,
Program::default(),
HashMap::from([(
EntryPointType::Constructor,
vec![ContractEntryPoint::default()],
)]),
None,
let contract_class = ContractClass::new_from_path(
"starknet_programs/raw_contract_classes/class_with_abi.json",
)
.expect("Error creating contract class");
.unwrap();

state_reader
.class_hash_to_contract_class
Expand Down
12 changes: 5 additions & 7 deletions src/state/state_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ impl StateCache {

#[cfg(test)]
mod tests {

use cairo_vm::types::program::Program;

use crate::services::api::contract_classes::deprecated_contract_class::ContractClass;

use super::*;
Expand All @@ -193,10 +190,11 @@ mod tests {
fn state_chache_set_initial_values() {
let mut state_cache = StateCache::default();
let address_to_class_hash = HashMap::from([(Address(10.into()), [8; 32])]);
let program_json: serde_json::Value = serde_json::Value::from("{}");
let compiled_class = CompiledClass::Deprecated(Box::new(
ContractClass::new(program_json, Program::default(), HashMap::new(), None).unwrap(),
));
let contract_class = ContractClass::new_from_path(
"starknet_programs/raw_contract_classes/class_with_abi.json",
)
.unwrap();
let compiled_class = CompiledClass::Deprecated(Box::new(contract_class));
let class_hash_to_compiled_class_hash = HashMap::from([([8; 32], compiled_class)]);
let address_to_nonce = HashMap::from([(Address(9.into()), 12.into())]);
let storage_updates = HashMap::from([((Address(4.into()), [1; 32]), 18.into())]);
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ mod tests {

// Make a new contract class with the same program but with errors
let error_contract_class = ContractClass {
program_json: contract_class.program_json,
hinted_class_hash: contract_class.hinted_class_hash,
program: contract_class.program,
entry_points_by_type: HashMap::new(),
abi: None,
Expand Down