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 25 commits into
base: master
Choose a base branch
from

Conversation

franciszekjob
Copy link
Collaborator

@franciszekjob franciszekjob commented Dec 18, 2024

Closes #2706

Introduced 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

  • Linked relevant issue
  • Updated relevant documentation
  • Added relevant tests
  • Performed self-review of the code
  • Added changes to CHANGELOG.md

@franciszekjob franciszekjob marked this pull request as ready for review December 18, 2024 14:25
@franciszekjob franciszekjob requested a review from ksew1 December 19, 2024 00:27
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.

crates/sncast/src/helpers/fee.rs Outdated Show resolved Hide resolved
crates/sncast/src/helpers/fee.rs Outdated Show resolved Hide resolved
crates/sncast/src/helpers/fee.rs Outdated Show resolved Hide resolved
@franciszekjob franciszekjob requested a review from Draggu December 20, 2024 09:57
crates/conversions/src/felt.rs Outdated Show resolved Hide resolved
crates/sncast/src/helpers/fee.rs Outdated Show resolved Hide resolved
crates/conversions/src/felt.rs Outdated Show resolved Hide resolved
crates/sncast/src/helpers/fee.rs Show resolved Hide resolved
crates/sncast/src/helpers/fee.rs Outdated Show resolved Hide resolved
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:

max_fee: Some(MAX_FEE.into()),
max_gas: Some(1_000_000_u32.into()),
max_fee: Some(Felt::from(MAX_FEE).try_into().unwrap()),
max_gas: Some(Felt::from(1_000_000_u32).try_into().unwrap()),
Copy link
Member

Choose a reason for hiding this comment

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

Why are you using NonZeroU64::new in some test cases and Felt::from... in other cases for the same field? Let's pick a consistent behavior if it's possible.

Copy link
Collaborator Author

@franciszekjob franciszekjob Dec 20, 2024

Choose a reason for hiding this comment

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

It's because fee-related fields in FeeArgs use NonZeroFelt while in FeeSettings::Strk/Eth they use either NonZeroFelt, NonZeroU64 or NonZeroU128 (ofc wrapped Option).

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

crates/conversions/src/felt.rs Outdated Show resolved Hide resolved
CHANGELOG.md Show resolved Hide resolved
if value == Felt::ZERO {
Err("value should be greater than 0".to_string())
} else {
let value: u64 = value.try_into().expect("failed to convert Felt to u64");
Copy link
Contributor

Choose a reason for hiding this comment

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

As @ksew1 said ^ don't panic in fallible conversion. It is highly unexpected.

crates/sncast/src/helpers/fee.rs Show resolved Hide resolved
@ddoktorski ddoktorski removed their request for review December 20, 2024 15:30
@franciszekjob franciszekjob changed the title Validate max gas is greater than zero for in for strk fee settings Validate max gas is greater than zero for strk fee settings Jan 7, 2025
crates/conversions/tests/e2e/non_zero_u128.rs Show resolved Hide resolved
crates/conversions/tests/e2e/non_zero_u64.rs Show resolved Hide resolved
output,
indoc! {r"
command: invoke
error: Calculated max-gas from provided --max-fee and the current network gas price is 0. Please increase --max-fee to obtain a positive gas amount: Tried to create NonZeroFelt from 0
Copy link
Member

Choose a reason for hiding this comment

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

Does it need to include : Tried to create NonZeroFelt from 0?

}

let max_gas = NonZeroFelt::try_from(Felt::from(max_fee).floor_div(&max_gas_unit_price))
.unwrap_or_else(|_| unreachable!("Calculated max gas must be >= 1 because max_fee >= max_gas_unit_price ensures a positive result"));
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't some rounding errors still make this <1 in special cases?

Copy link
Collaborator Author

@franciszekjob franciszekjob Jan 10, 2025

Choose a reason for hiding this comment

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

No, because we explicitly ensure that max_fee >= max_gas_unit_price before this calculation. Since floor_div performs exact integer division (rounding down), and both max_fee and max_gas_unit_price are non-zero, the result of floor_div will always be >=1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate fee args is greater than 0
6 participants