Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(blockifier): share the utility felt_to_u128 to starknet api #2093

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

16 changes: 1 addition & 15 deletions crates/blockifier/src/abi/sierra_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -43,12 +35,6 @@ pub trait SierraType: Sized {
) -> SierraTypeResult<Self>;
}

// Utils.

fn felt_to_u128(felt: &Felt) -> Result<u128, SierraTypeError> {
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
Expand Down
21 changes: 0 additions & 21 deletions crates/blockifier/src/abi/sierra_types_test.rs

This file was deleted.

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
7 changes: 7 additions & 0 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 @@ -23,6 +24,12 @@ pub fn ascii_as_felt(ascii_str: &str) -> Result<Felt, StarknetApiError> {
})
}

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.
#[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub enum ChainId {
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'."
);
}
Loading