Skip to content

Commit

Permalink
chore(blockifier): make the utility felt_to_u128 private (#2095)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Nov 17, 2024
1 parent 5d44145 commit f0436f8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
20 changes: 0 additions & 20 deletions crates/blockifier/src/abi/abi_utils_test.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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'."
);
}
8 changes: 6 additions & 2 deletions crates/blockifier/src/abi/sierra_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = Result<T, SierraTypeError>;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -41,8 +45,8 @@ pub trait SierraType: Sized {

// Utils.

pub fn felt_to_u128(felt: &Felt) -> Result<u128, SierraTypeError> {
felt.to_u128().ok_or_else(|| SierraTypeError::ValueTooLargeForType { val: *felt, ty: "u128" })
fn felt_to_u128(felt: &Felt) -> Result<u128, SierraTypeError> {
felt.to_u128().ok_or(SierraTypeError::ValueTooLargeForType { val: *felt, ty: "u128" })
}

// Implementations.
Expand Down
21 changes: 21 additions & 0 deletions crates/blockifier/src/abi/sierra_types_test.rs
Original file line number Diff line number Diff line change
@@ -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'."
);
}
5 changes: 3 additions & 2 deletions crates/starknet_api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use crate::{impl_from_through_intermediate, StarknetApiError};

/// Felt.
pub fn ascii_as_felt(ascii_str: &str) -> Result<Felt, StarknetApiError> {
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.
Expand Down

0 comments on commit f0436f8

Please sign in to comment.