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

Validate max gas is greater than zero for strk fee settings #2796

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e843e47
Validate max gas is greater than zero for in for strk fee settings
franciszekjob Dec 18, 2024
ea50757
Merge branch 'master' into franciszekjob/2706-validate-fee-args-great…
franciszekjob Dec 18, 2024
8dce237
Check if max gas unit price > 0 when it's passed with max fee
franciszekjob Dec 18, 2024
72f2fe8
Merge branch 'franciszekjob/2706-validate-fee-args-greater-than-zero'…
franciszekjob Dec 18, 2024
cb3d529
Refactor checking if feee args are greater than zero
franciszekjob Dec 18, 2024
a411a4e
Update error messages
franciszekjob Dec 18, 2024
397a8ba
Update docs and changelog
franciszekjob Dec 19, 2024
77f4d4d
Update changelog
franciszekjob Dec 19, 2024
e5c22d2
Refactor checking if calculated value is zero
franciszekjob Dec 19, 2024
969e37b
Cover case when max fee and max gas unit price are passed
franciszekjob Dec 19, 2024
4788a9b
Refactor `ScriptFeeSettings` and `FeeSettings`
franciszekjob Dec 19, 2024
e23e54e
Merge branch 'master' into franciszekjob/2706-validate-fee-args-great…
franciszekjob Dec 19, 2024
31168e4
Format
franciszekjob Dec 20, 2024
69af0af
Merge branch 'franciszekjob/2706-validate-fee-args-greater-than-zero'…
franciszekjob Dec 20, 2024
7b7337e
Rename `zero` to `0`
franciszekjob Dec 20, 2024
2db18e6
Update changelog
franciszekjob Dec 20, 2024
41e4cd5
Add `impl_deserialize_for_nonzero_num_type` macro
franciszekjob Dec 20, 2024
73e085f
Apply other code review suggestions
franciszekjob Dec 20, 2024
7d4bbed
Update changelog
franciszekjob Jan 7, 2025
0779442
Add conversions tests
franciszekjob Jan 7, 2025
45cf019
Merge branch 'master' of https://github.com/foundry-rs/starknet-found…
franciszekjob Jan 7, 2025
0c80350
Update changelog - remove duplicated entry
franciszekjob Jan 7, 2025
0e185b6
Fix headings in changelog
franciszekjob Jan 7, 2025
a784e69
Refactor `FeeArgs.try_into_fee_settings()`
franciszekjob Jan 7, 2025
091e484
Update `unreachable!` messages in `FeeArgs.try_into_fee_settings()`
franciszekjob Jan 7, 2025
9682eab
Use `Felt::TWO.pow`
franciszekjob Jan 10, 2025
a7c0acf
Remove use of `to_bigint()` in `impl_deserialize_for_num_type`
franciszekjob Jan 10, 2025
2eb48ae
Merge branch 'master' of https://github.com/foundry-rs/starknet-found…
franciszekjob Jan 10, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

#### Added

- When using `--max-fee` with transactions v3, calculated max gas and max gas unit price are automatically validated to ensure they are greater than 0 after conversion
- interactive interface that allows setting created or imported account as the default

#### Changed

- Values passed to the `--max-fee`, `--max-gas`, and `--max-gas-unit-price` flags must be greater than 0
ddoktorski marked this conversation as resolved.
Show resolved Hide resolved

## [0.35.1] - 2024-12-16

Expand Down
3 changes: 3 additions & 0 deletions crates/conversions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pub mod contract_address;
pub mod entrypoint_selector;
pub mod eth_address;
pub mod felt;
pub mod non_zero_felt;
pub mod non_zero_u128;
pub mod non_zero_u64;
pub mod nonce;
pub mod padded_felt;
pub mod primitive;
Expand Down
23 changes: 23 additions & 0 deletions crates/conversions/src/non_zero_felt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::FromConv;
use starknet_types_core::felt::{Felt, NonZeroFelt};
use std::num::{NonZeroU128, NonZeroU64};

impl FromConv<NonZeroU64> for NonZeroFelt {
fn from_(value: NonZeroU64) -> Self {
NonZeroFelt::try_from(Felt::from(value.get())).unwrap_or_else(|_| {
unreachable!(
"NonZeroU64 is always greater than 0, so it should be convertible to NonZeroFelt"
)
})
}
}

impl FromConv<NonZeroU128> for NonZeroFelt {
fn from_(value: NonZeroU128) -> Self {
NonZeroFelt::try_from(Felt::from(value.get())).unwrap_or_else(|_| {
unreachable!(
"NonZeroU128 is always greater than 0, so it should be convertible to NonZeroFelt"
)
})
}
}
14 changes: 14 additions & 0 deletions crates/conversions/src/non_zero_u128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::TryFromConv;
use starknet_types_core::felt::{Felt, NonZeroFelt};
use std::num::{NonZero, NonZeroU128};

impl TryFromConv<NonZeroFelt> for NonZeroU128 {
type Error = String;
fn try_from_(value: NonZeroFelt) -> Result<Self, Self::Error> {
let value: u128 = Felt::from(value)
.try_into()
.map_err(|_| "felt was too large to fit in u128")?;
Ok(NonZero::new(value)
.unwrap_or_else(|| unreachable!("non zero felt is always greater than 0")))
}
}
14 changes: 14 additions & 0 deletions crates/conversions/src/non_zero_u64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::TryFromConv;
use starknet_types_core::felt::{Felt, NonZeroFelt};
use std::num::{NonZero, NonZeroU64};

impl TryFromConv<NonZeroFelt> for NonZeroU64 {
type Error = String;
fn try_from_(value: NonZeroFelt) -> Result<Self, Self::Error> {
let value: u64 = Felt::from(value)
.try_into()
.map_err(|_| "felt was too large to fit in u64")?;
Ok(NonZero::new(value)
.unwrap_or_else(|| unreachable!("non zero felt is always greater than 0")))
}
}
32 changes: 24 additions & 8 deletions crates/conversions/src/serde/deserialize/deserialize_impl.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @Draggu please take a look at these

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{byte_array::ByteArray, IntoConv};
use num_traits::cast::ToPrimitive;
use starknet::providers::Url;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, Nonce};
use starknet_types_core::felt::Felt;
use std::num::NonZeroU32;
use starknet_types_core::felt::{Felt, NonZeroFelt};
use std::num::NonZero;

impl CairoDeserialize for Url {
fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
Expand All @@ -13,12 +13,6 @@ impl CairoDeserialize for Url {
}
}

impl CairoDeserialize for NonZeroU32 {
fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
NonZeroU32::new(reader.read()?).ok_or(BufferReadError::ParseFailed)
}
}

impl CairoDeserialize for Felt {
fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
reader.read_felt()
Expand Down Expand Up @@ -71,6 +65,24 @@ impl CairoDeserialize for bool {
}
}

impl CairoDeserialize for NonZeroFelt {
fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
let felt = reader.read::<Felt>()?;
NonZeroFelt::try_from(felt).map_err(|_| BufferReadError::ParseFailed)
}
}

macro_rules! impl_deserialize_for_nonzero_num_type {
($type:ty) => {
impl CairoDeserialize for NonZero<$type> {
fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
let val = <$type>::deserialize(reader)?;
NonZero::new(val).ok_or(BufferReadError::ParseFailed)
}
}
};
}

franciszekjob marked this conversation as resolved.
Show resolved Hide resolved
macro_rules! impl_deserialize_for_felt_type {
($type:ty) => {
impl CairoDeserialize for $type {
Expand Down Expand Up @@ -99,6 +111,10 @@ impl_deserialize_for_felt_type!(ContractAddress);
impl_deserialize_for_felt_type!(Nonce);
impl_deserialize_for_felt_type!(EntryPointSelector);

impl_deserialize_for_nonzero_num_type!(u32);
impl_deserialize_for_nonzero_num_type!(u64);
impl_deserialize_for_nonzero_num_type!(u128);

impl_deserialize_for_num_type!(u8);
impl_deserialize_for_num_type!(u16);
impl_deserialize_for_num_type!(u32);
Expand Down
3 changes: 3 additions & 0 deletions crates/conversions/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod contract_address;
mod entrypoint_selector;
mod felt;
mod field_elements;
mod non_zero_felt;
mod non_zero_u128;
mod non_zero_u64;
mod nonce;
mod padded_felt;
mod string;
21 changes: 21 additions & 0 deletions crates/conversions/tests/e2e/non_zero_felt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[cfg(test)]
mod tests_non_zero_felt {
use std::num::{NonZeroU128, NonZeroU64};

use conversions::FromConv;
use starknet_types_core::felt::{Felt, NonZeroFelt};

#[test]
fn test_happy_case() {
let non_zero_felt = NonZeroFelt::try_from(Felt::from(1_u8)).unwrap();

assert_eq!(
non_zero_felt,
NonZeroFelt::from_(NonZeroU64::new(1).unwrap())
);
assert_eq!(
non_zero_felt,
NonZeroFelt::from_(NonZeroU128::new(1).unwrap())
);
}
}
36 changes: 36 additions & 0 deletions crates/conversions/tests/e2e/non_zero_u128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[cfg(test)]
mod tests_non_zero_u128 {
use conversions::TryFromConv;
use starknet_types_core::felt::{Felt, NonZeroFelt};
use std::num::NonZeroU128;

#[test]
fn test_happy_case() {
let non_zero_u128 = NonZeroU128::new(1).unwrap();

assert_eq!(
non_zero_u128,
NonZeroU128::try_from_(NonZeroFelt::try_from(Felt::from(1_u8)).unwrap()).unwrap()
);
}

#[test]
fn test_limit() {
let felt = Felt::from_dec_str(&u128::MAX.to_string()).unwrap();
let non_zero_felt = NonZeroFelt::try_from(felt).unwrap();

let result = NonZeroU128::try_from_(non_zero_felt);
assert!(result.is_ok());
assert_eq!(result.unwrap().get(), u128::MAX);
}

#[test]
fn test_felt_too_large() {
let large_felt = Felt::from_dec_str("340282366920938463463374607431768211456").unwrap(); // 2^128
franciszekjob marked this conversation as resolved.
Show resolved Hide resolved
let non_zero_felt = NonZeroFelt::try_from(large_felt).unwrap();

let result = NonZeroU128::try_from_(non_zero_felt);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "felt was too large to fit in u128");
}
}
36 changes: 36 additions & 0 deletions crates/conversions/tests/e2e/non_zero_u64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[cfg(test)]
mod tests_non_zero_u64 {
use conversions::TryFromConv;
use starknet_types_core::felt::{Felt, NonZeroFelt};
use std::num::NonZeroU64;

#[test]
fn test_happy_case() {
let non_zero_u64 = NonZeroU64::new(1).unwrap();

assert_eq!(
non_zero_u64,
NonZeroU64::try_from_(NonZeroFelt::try_from(Felt::from(1_u8)).unwrap()).unwrap()
);
}

#[test]
fn test_limit() {
let felt = Felt::from_dec_str(&u64::MAX.to_string()).unwrap();
let non_zero_felt = NonZeroFelt::try_from(felt).unwrap();

let result = NonZeroU64::try_from_(non_zero_felt);
assert!(result.is_ok());
assert_eq!(result.unwrap().get(), u64::MAX);
}

#[test]
fn test_felt_too_large() {
let large_felt = Felt::from_dec_str("18446744073709551616").unwrap(); // 2^64
franciszekjob marked this conversation as resolved.
Show resolved Hide resolved
let non_zero_felt = NonZeroFelt::try_from(large_felt).unwrap();

let result = NonZeroU64::try_from_(non_zero_felt);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "felt was too large to fit in u64");
}
}
Loading
Loading