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

changed failedHash to hashError #817

Merged
merged 5 commits into from
Jul 14, 2023
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
9 changes: 9 additions & 0 deletions src/core/errors/contract_address_errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::errors::hash_errors::HashError;
use cairo_vm::{
types::errors::program_errors::ProgramError,
vm::errors::{
Expand Down Expand Up @@ -43,4 +44,12 @@ pub enum ContractAddressError {
NoneIntMaybeRelocatable,
#[error("Invalid program JSON, message: {0}")]
InvalidProgramJson(String),
#[error("Couldn't compute hash: {0}")]
HashError(HashError),
}

impl From<HashError> for ContractAddressError {
fn from(error: HashError) -> Self {
ContractAddressError::HashError(error)
}
}
7 changes: 7 additions & 0 deletions src/core/errors/hash_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum HashError {
#[error("Failed to compute hash {0}")]
FailedToComputeHash(String),
}
1 change: 1 addition & 0 deletions src/core/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod contract_address_errors;
pub mod hash_errors;
pub mod state_errors;
8 changes: 5 additions & 3 deletions src/core/transaction_hash/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::errors::hash_errors::HashError;
use crate::{
core::contract_address::compute_deprecated_class_hash,
definitions::constants::CONSTRUCTOR_ENTRY_POINT_SELECTOR, hash_utils::compute_hash_on_elements,
Expand Down Expand Up @@ -68,7 +69,7 @@ pub fn calculate_transaction_hash_common(

data_to_hash.extend(additional_data.iter().cloned());

compute_hash_on_elements(&data_to_hash)
Ok(compute_hash_on_elements(&data_to_hash)?)
}

pub fn calculate_deploy_transaction_hash(
Expand Down Expand Up @@ -123,8 +124,9 @@ pub fn calculate_declare_transaction_hash(
version: Felt252,
nonce: Felt252,
) -> Result<Felt252, SyscallHandlerError> {
let class_hash = compute_deprecated_class_hash(contract_class)
.map_err(|_| SyscallHandlerError::FailToComputeHash)?;
let class_hash = compute_deprecated_class_hash(contract_class).map_err(|e| {
SyscallHandlerError::HashError(HashError::FailedToComputeHash(e.to_string()))
})?;

let (calldata, additional_data) = if !version.is_zero() {
(vec![class_hash], vec![nonce])
Expand Down
11 changes: 7 additions & 4 deletions src/hash_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::errors::hash_errors::HashError;
use crate::{syscalls::syscall_handler_errors::SyscallHandlerError, utils::Address};
use cairo_vm::felt::Felt252;
use num_integer::Integer;
Expand Down Expand Up @@ -109,22 +110,24 @@ pub fn calculate_contract_address(
/// }
/// }
/// ```
pub fn compute_hash_on_elements(vec: &[Felt252]) -> Result<Felt252, SyscallHandlerError> {
pub fn compute_hash_on_elements(vec: &[Felt252]) -> Result<Felt252, HashError> {
let mut felt_vec = vec
.iter()
.map(|num| {
FieldElement::from_dec_str(&num.to_str_radix(10))
.map_err(|_| SyscallHandlerError::FailToComputeHash)
.map_err(|e| HashError::FailedToComputeHash(e.to_string()))
})
.collect::<Result<Vec<FieldElement>, SyscallHandlerError>>()?;
.collect::<Result<Vec<FieldElement>, HashError>>()?;

felt_vec.push(FieldElement::from(felt_vec.len()));
felt_vec.insert(0, FieldElement::from(0_u16));

let felt_result = felt_vec
.into_iter()
.reduce(|x, y| pedersen_hash(&x, &y))
.ok_or(SyscallHandlerError::FailToComputeHash)?;
.ok_or(HashError::FailedToComputeHash(
"Failed to compute Pedersen hash.".to_string(),
))?;

let result = Felt252::from_bytes_be(&felt_result.to_bytes_be());
Ok(result)
Expand Down
5 changes: 3 additions & 2 deletions src/syscalls/syscall_handler_errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::errors::hash_errors::HashError;
use crate::core::errors::state_errors::StateError;
use cairo_vm::felt::Felt252;
use cairo_vm::{
Expand All @@ -20,8 +21,8 @@ pub enum SyscallHandlerError {
ExecutionError(String),
#[error("Couldn't convert from {0} to {1}")]
Conversion(String, String),
#[error("Couldn't compute hash")]
FailToComputeHash,
#[error("Couldn't compute hash: {0}")]
HashError(#[from] HashError),
#[error("Expected a struct of type: {0:?}, received: {1:?}")]
ExpectedStruct(String, String),
#[error("Unsopported address domain: {0}")]
Expand Down
3 changes: 2 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::core::errors::hash_errors::HashError;
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
use crate::{
definitions::transaction_type::TransactionType,
Expand Down Expand Up @@ -96,7 +97,7 @@ pub fn get_felt_range(

pub fn felt_to_field_element(value: &Felt252) -> Result<FieldElement, SyscallHandlerError> {
FieldElement::from_dec_str(&value.to_str_radix(10))
.map_err(|_| SyscallHandlerError::FailToComputeHash)
.map_err(|e| SyscallHandlerError::HashError(HashError::FailedToComputeHash(e.to_string())))
}

pub fn field_element_to_felt(felt: &FieldElement) -> Felt252 {
Expand Down