Skip to content

Commit

Permalink
chore(blockifier): share the utility felt_to_u128 to starknet api
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Nov 17, 2024
1 parent 846a828 commit 8c66340
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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'."
);
}
12 changes: 1 addition & 11 deletions crates/blockifier/src/abi/sierra_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,8 +15,6 @@ pub type SierraTypeResult<T> = Result<T, SierraTypeError>;

#[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)]
Expand All @@ -39,12 +35,6 @@ pub trait SierraType: Sized {
) -> SierraTypeResult<Self>;
}

// Utils.

pub fn felt_to_u128(felt: &Felt) -> Result<u128, SierraTypeError> {
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
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
12 changes: 10 additions & 2 deletions crates/starknet_api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -18,8 +19,15 @@ 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),
})
}

pub fn felt_to_u128(felt: &Felt) -> Result<u128, StarknetApiError> {
felt.to_u128().ok_or(StarknetApiError::OutOfRange {
string: format!("Felt {} is too big to convert to 'u128'", *felt,),
})
}

/// A chain id.
Expand Down
20 changes: 20 additions & 0 deletions crates/starknet_api/src/core_test.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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'."
);
}

0 comments on commit 8c66340

Please sign in to comment.