From 28a0f67e9124fedfe8bf11e35537b5ee73bbe738 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Wed, 18 Dec 2024 17:17:15 +0200 Subject: [PATCH] chore(blockifier): share the utility felt_to_u128 to starknet api (#2093) --- Cargo.lock | 1 + crates/blockifier/src/abi/sierra_types.rs | 16 +------------- .../blockifier/src/abi/sierra_types_test.rs | 21 ------------------- crates/starknet_api/Cargo.toml | 1 + crates/starknet_api/src/core.rs | 7 +++++++ crates/starknet_api/src/core_test.rs | 20 ++++++++++++++++++ 6 files changed, 30 insertions(+), 36 deletions(-) delete mode 100644 crates/blockifier/src/abi/sierra_types_test.rs diff --git a/Cargo.lock b/Cargo.lock index a54a7583fd..1f1a2ea701 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10262,6 +10262,7 @@ dependencies = [ "infra_utils", "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/sierra_types.rs b/crates/blockifier/src/abi/sierra_types.rs index 121a5b84b3..620fdd97a9 100644 --- a/crates/blockifier/src/abi/sierra_types.rs +++ b/crates/blockifier/src/abi/sierra_types.rs @@ -3,26 +3,18 @@ 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; use crate::state::state_api::StateReader; -#[cfg(test)] -#[path = "sierra_types_test.rs"] -mod test; - 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)] @@ -43,12 +35,6 @@ pub trait SierraType: Sized { ) -> SierraTypeResult; } -// Utils. - -fn felt_to_u128(felt: &Felt) -> Result { - felt.to_u128().ok_or(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/blockifier/src/abi/sierra_types_test.rs b/crates/blockifier/src/abi/sierra_types_test.rs deleted file mode 100644 index a78b1e3532..0000000000 --- a/crates/blockifier/src/abi/sierra_types_test.rs +++ /dev/null @@ -1,21 +0,0 @@ -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/Cargo.toml b/crates/starknet_api/Cargo.toml index 7efd7a2610..4ac9b5bc35 100644 --- a/crates/starknet_api/Cargo.toml +++ b/crates/starknet_api/Cargo.toml @@ -19,6 +19,7 @@ indexmap = { workspace = true, features = ["serde"] } infra_utils = { workspace = true, optional = true } itertools.workspace = true num-bigint.workspace = true +num-traits.workspace = true pretty_assertions.workspace = true primitive-types = { workspace = true, features = ["serde"] } semver.workspace = true diff --git a/crates/starknet_api/src/core.rs b/crates/starknet_api/src/core.rs index 8aaa23f66e..7716393bb6 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}; @@ -23,6 +24,12 @@ pub fn ascii_as_felt(ascii_str: &str) -> Result { }) } +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. #[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)] pub enum ChainId { 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'." + ); +}