diff --git a/Cargo.lock b/Cargo.lock index 847f3a4c48..df5cbdb878 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10081,6 +10081,7 @@ dependencies = [ "indexmap 2.6.0", "itertools 0.12.1", "num-bigint 0.4.6", + "num-traits 0.2.19", "pretty_assertions", "primitive-types", "rstest", diff --git a/crates/blockifier/src/abi/abi_utils_test.rs b/crates/blockifier/src/abi/abi_utils_test.rs index 7c3ad40eca..278237ccce 100644 --- a/crates/blockifier/src/abi/abi_utils_test.rs +++ b/crates/blockifier/src/abi/abi_utils_test.rs @@ -1,11 +1,8 @@ -use num_bigint::BigUint; use starknet_api::core::EntryPointSelector; use starknet_api::felt; -use starknet_types_core::felt::Felt; use crate::abi::abi_utils::selector_from_name; use crate::abi::constants as abi_constants; -use crate::abi::sierra_types::felt_to_u128; use crate::transaction::constants as tx_constants; #[test] @@ -37,20 +34,3 @@ fn test_selector_from_name() { let expected_empty_selector = EntryPointSelector(felt!(expected_empty_selector)); assert_eq!(selector_from_name(""), expected_empty_selector); } - -#[test] -fn test_value_too_large_for_type() { - // Happy flow. - let n = 1991_u128; - let n_as_felt = Felt::from(n); - felt_to_u128(&n_as_felt).unwrap(); - - // Value too large for type. - let overflowed_u128: BigUint = BigUint::from(1_u8) << 128; - let overflowed_u128_as_felt = Felt::from(overflowed_u128); - let error = felt_to_u128(&overflowed_u128_as_felt).unwrap_err(); - assert_eq!( - format!("{error}"), - "Felt 340282366920938463463374607431768211456 is too big to convert to 'u128'." - ); -} diff --git a/crates/blockifier/src/abi/sierra_types.rs b/crates/blockifier/src/abi/sierra_types.rs index 58677f36fc..620fdd97a9 100644 --- a/crates/blockifier/src/abi/sierra_types.rs +++ b/crates/blockifier/src/abi/sierra_types.rs @@ -3,11 +3,9 @@ use cairo_vm::types::relocatable::Relocatable; use cairo_vm::vm::errors::memory_errors::MemoryError; use cairo_vm::vm::vm_core::VirtualMachine; use num_bigint::{BigUint, ToBigUint}; -use num_traits::ToPrimitive; -use starknet_api::core::ContractAddress; +use starknet_api::core::{felt_to_u128, ContractAddress}; use starknet_api::state::StorageKey; use starknet_api::StarknetApiError; -use starknet_types_core::felt::Felt; use thiserror::Error; use crate::state::errors::StateError; @@ -17,8 +15,6 @@ pub type SierraTypeResult = Result; #[derive(Debug, Error)] pub enum SierraTypeError { - #[error("Felt {val} is too big to convert to '{ty}'.")] - ValueTooLargeForType { val: Felt, ty: &'static str }, #[error(transparent)] MemoryError(#[from] MemoryError), #[error(transparent)] @@ -39,12 +35,6 @@ pub trait SierraType: Sized { ) -> SierraTypeResult; } -// Utils. - -pub fn felt_to_u128(felt: &Felt) -> Result { - felt.to_u128().ok_or_else(|| SierraTypeError::ValueTooLargeForType { val: *felt, ty: "u128" }) -} - // Implementations. // We implement the trait SierraType for SierraU128 and not for u128 since it's not guaranteed that diff --git a/crates/starknet_api/Cargo.toml b/crates/starknet_api/Cargo.toml index 73879008b9..beb50e9ca5 100644 --- a/crates/starknet_api/Cargo.toml +++ b/crates/starknet_api/Cargo.toml @@ -18,6 +18,7 @@ hex.workspace = true indexmap = { workspace = true, features = ["serde"] } itertools.workspace = true num-bigint.workspace = true +num-traits.workspace = true pretty_assertions.workspace = true primitive-types = { workspace = true, features = ["serde"] } serde = { workspace = true, features = ["derive", "rc"] } diff --git a/crates/starknet_api/src/core.rs b/crates/starknet_api/src/core.rs index ec2ac3ed8e..d41ebc600f 100644 --- a/crates/starknet_api/src/core.rs +++ b/crates/starknet_api/src/core.rs @@ -5,6 +5,7 @@ mod core_test; use std::fmt::Debug; use std::sync::LazyLock; +use num_traits::ToPrimitive; use primitive_types::H160; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use starknet_types_core::felt::{Felt, NonZeroFelt}; @@ -18,8 +19,15 @@ use crate::{impl_from_through_intermediate, StarknetApiError}; /// Felt. pub fn ascii_as_felt(ascii_str: &str) -> Result { - Felt::from_hex(hex::encode(ascii_str).as_str()) - .map_err(|_| StarknetApiError::OutOfRange { string: ascii_str.to_string() }) + Felt::from_hex(hex::encode(ascii_str).as_str()).map_err(|_| StarknetApiError::OutOfRange { + string: format!("The str {}, does not fit into a single felt", ascii_str), + }) +} + +pub fn felt_to_u128(felt: &Felt) -> Result { + felt.to_u128().ok_or(StarknetApiError::OutOfRange { + string: format!("Felt {} is too big to convert to 'u128'", *felt,), + }) } /// A chain id. diff --git a/crates/starknet_api/src/core_test.rs b/crates/starknet_api/src/core_test.rs index 81221cce44..0443f9ffb4 100644 --- a/crates/starknet_api/src/core_test.rs +++ b/crates/starknet_api/src/core_test.rs @@ -1,10 +1,12 @@ use assert_matches::assert_matches; +use num_bigint::BigUint; use starknet_types_core::felt::Felt; use starknet_types_core::hash::{Pedersen, StarkHash as CoreStarkHash}; use crate::core::{ ascii_as_felt, calculate_contract_address, + felt_to_u128, ChainId, ContractAddress, EthAddress, @@ -111,3 +113,21 @@ fn test_ascii_as_felt() { let expected_sn_main = Felt::from(23448594291968334_u128); assert_eq!(sn_main_felt, expected_sn_main); } + +#[test] +fn test_value_too_large_for_type() { + // Happy flow. + let n = 1991_u128; + let n_as_felt = Felt::from(n); + felt_to_u128(&n_as_felt).unwrap(); + + // Value too large for type. + let overflowed_u128: BigUint = BigUint::from(1_u8) << 128; + let overflowed_u128_as_felt = Felt::from(overflowed_u128); + let error = felt_to_u128(&overflowed_u128_as_felt).unwrap_err(); + assert_eq!( + format!("{error}"), + "Out of range Felt 340282366920938463463374607431768211456 is too big to convert to \ + 'u128'." + ); +}