From f0436f8a64fe7ed9c38ad16e063e2bfacc16afd7 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Sun, 17 Nov 2024 11:47:33 +0200 Subject: [PATCH] chore(blockifier): make the utility felt_to_u128 private (#2095) --- crates/blockifier/src/abi/abi_utils_test.rs | 20 ------------------ crates/blockifier/src/abi/sierra_types.rs | 8 +++++-- .../blockifier/src/abi/sierra_types_test.rs | 21 +++++++++++++++++++ crates/starknet_api/src/core.rs | 5 +++-- 4 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 crates/blockifier/src/abi/sierra_types_test.rs 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..121a5b84b3 100644 --- a/crates/blockifier/src/abi/sierra_types.rs +++ b/crates/blockifier/src/abi/sierra_types.rs @@ -13,6 +13,10 @@ use thiserror::Error; use crate::state::errors::StateError; use crate::state::state_api::StateReader; +#[cfg(test)] +#[path = "sierra_types_test.rs"] +mod test; + pub type SierraTypeResult = Result; #[derive(Debug, Error)] @@ -41,8 +45,8 @@ pub trait SierraType: Sized { // Utils. -pub fn felt_to_u128(felt: &Felt) -> Result { - felt.to_u128().ok_or_else(|| SierraTypeError::ValueTooLargeForType { val: *felt, ty: "u128" }) +fn felt_to_u128(felt: &Felt) -> Result { + felt.to_u128().ok_or(SierraTypeError::ValueTooLargeForType { val: *felt, ty: "u128" }) } // Implementations. diff --git a/crates/blockifier/src/abi/sierra_types_test.rs b/crates/blockifier/src/abi/sierra_types_test.rs new file mode 100644 index 0000000000..a78b1e3532 --- /dev/null +++ b/crates/blockifier/src/abi/sierra_types_test.rs @@ -0,0 +1,21 @@ +use num_bigint::BigUint; +use starknet_types_core::felt::Felt; + +use crate::abi::sierra_types::felt_to_u128; + +#[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/starknet_api/src/core.rs b/crates/starknet_api/src/core.rs index ec2ac3ed8e..4841e01a91 100644 --- a/crates/starknet_api/src/core.rs +++ b/crates/starknet_api/src/core.rs @@ -18,8 +18,9 @@ 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), + }) } /// A chain id.