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 4 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
58 changes: 36 additions & 22 deletions crates/sncast/src/helpers/fee.rs
cptartur marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,20 @@ impl FeeArgs {
{
bail!("--max-fee should be greater than or equal to --max-gas-unit-price")
}
(None, _, _) => FeeSettings::Strk {
max_gas: self.max_gas.map(TryIntoConv::try_into_).transpose()?,
max_gas_unit_price: self
.max_gas_unit_price
.map(TryIntoConv::try_into_)
.transpose()?,
},
(None, _, _) => {
if let Some(max_gas) = self.max_gas {
if max_gas == Felt::ZERO {
bail!("--max-gas should be greater than 0")
}
}
FeeSettings::Strk {
max_gas: self.max_gas.map(TryIntoConv::try_into_).transpose()?,
max_gas_unit_price: self
.max_gas_unit_price
.map(TryIntoConv::try_into_)
.transpose()?,
}
}
(Some(max_fee), None, Some(max_gas_unit_price)) => FeeSettings::Strk {
max_gas: Some(
max_fee
Expand All @@ -106,29 +113,36 @@ impl FeeArgs {
),
max_gas_unit_price: Some(max_gas_unit_price.try_into_()?),
},
(Some(max_fee), Some(max_gas), None) => FeeSettings::Strk {
max_gas: Some(max_gas.try_into_()?),
max_gas_unit_price: Some(
max_fee
.floor_div(&NonZeroFelt::from_felt_unchecked(max_gas))
.try_into_()?,
),
},
(Some(max_fee), Some(max_gas), None) => {
let max_gas_unit_price =
max_fee.floor_div(&NonZeroFelt::from_felt_unchecked(max_gas));
if max_gas_unit_price == Felt::ZERO {
ksew1 marked this conversation as resolved.
Show resolved Hide resolved
bail!("--max-gas calculated from --max-fee should be greater than 0. Please increase --max-fee")
}

FeeSettings::Strk {
max_gas: Some(max_gas.try_into_()?),
max_gas_unit_price: Some(
max_fee
.floor_div(&NonZeroFelt::from_felt_unchecked(max_gas))
.try_into_()?,
),
}
}
(Some(max_fee), None, None) => {
let max_gas_unit_price = provider
.get_block_with_tx_hashes(block_id)
.await?
.l1_gas_price()
.price_in_fri;
let max_gas = max_fee
.floor_div(&NonZeroFelt::from_felt_unchecked(max_gas_unit_price));
if max_gas == Felt::ZERO {
bail!("--max-gas calculated from --max-fee should be greater than 0. Please increase --max-fee")
}

FeeSettings::Strk {
max_gas: Some(
max_fee
.floor_div(&NonZeroFelt::from_felt_unchecked(
max_gas_unit_price,
))
.try_into_()?,
),
max_gas: Some(max_gas.try_into_()?),
max_gas_unit_price: Some(max_gas_unit_price.try_into_()?),
}
}
Expand Down
72 changes: 72 additions & 0 deletions crates/sncast/tests/e2e/invoke.rs
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do these tests refer to the reported issue?
#2697
I suppose that even if max_fee is greater than 0 an error may occur

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These ones refer to situation when user explicitly passes 0 to fee args.
The case when max gas or max gas unit price is calculated based on other values, is handled e.g. here.

Copy link
Member

Choose a reason for hiding this comment

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

The most important cases to test here are the ones where values calculated from --max-fee are 0. We should add these.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fine, so let's break it down:

Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,78 @@ fn test_too_low_max_fee() {
);
}

#[test]
fn test_max_gas_equal_to_zero() {
let args = vec![
"--accounts-file",
ACCOUNT_FILE_PATH,
"--account",
"user11",
"--wait",
"invoke",
"--url",
URL,
"--contract-address",
MAP_CONTRACT_ADDRESS_SEPOLIA,
"--function",
"put",
"--calldata",
"0x1",
"0x2",
"--max-gas",
"0",
"--fee-token",
"strk",
];

let snapbox = runner(&args);
let output = snapbox.assert().success();

assert_stderr_contains(
output,
indoc! {r"
command: invoke
error: --max-gas should be greater than 0
"},
);
}

#[test]
fn test_max_gas_calculated_from_max_fee_equal_to_zero() {
let args = vec![
"--accounts-file",
ACCOUNT_FILE_PATH,
"--account",
"user11",
"--wait",
"invoke",
"--url",
URL,
"--contract-address",
MAP_CONTRACT_ADDRESS_SEPOLIA,
"--function",
"put",
"--calldata",
"0x1",
"0x2",
"--max-fee",
"0",
"--fee-token",
"strk",
];

let snapbox = runner(&args);
let output = snapbox.assert().success();

assert_stderr_contains(
output,
indoc! {r"
command: invoke
error: --max-gas calculated from --max-fee should be greater than 0. Please increase --max-fee
"},
);
}

#[tokio::test]
async fn test_happy_case_cairo_expression_calldata() {
let tempdir = create_and_deploy_oz_account().await;
Expand Down
Loading