-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate max gas is greater than zero for strk fee settings (#2796)
<!-- Reference any GitHub issues resolved by this PR --> Closes #2706 ## Introduced changes <!-- A brief description of the changes --> Validate if fee args are greater than 0 - Change type of `max_fee`, `max_gas` and `max_gas_unit_price` from `Option<Felt>` to `Option<NonZeroFelt>` ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md`
- Loading branch information
1 parent
8248052
commit a530f45
Showing
22 changed files
with
433 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::TWO.pow(128_u8); | ||
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::TWO.pow(64_u8); | ||
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"); | ||
} | ||
} |
Oops, something went wrong.