Skip to content

Commit

Permalink
feat(blockifier): safe addition of fee amounts in business logic (#1255)
Browse files Browse the repository at this point in the history
<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/starkware-libs/sequencer/1255)
<!-- Reviewable:end -->
  • Loading branch information
dorimedini-starkware authored Oct 9, 2024
1 parent bc03b4b commit ce56299
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
21 changes: 1 addition & 20 deletions crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,7 @@ pub fn verify_can_pay_committed_bounds(
) -> TransactionFeeResult<()> {
let tx_info = &tx_context.tx_info;
let committed_fee = match tx_info {
TransactionInfo::Current(context) => {
match &context.resource_bounds {
L1Gas(l1_gas) =>
// Sender will not be charged by `max_price_per_unit`, but this check should not
// depend on the current gas price.
{
l1_gas.max_amount.saturating_mul(l1_gas.max_price_per_unit)
}
// TODO!(Aner): add tests
AllResources(AllResourceBounds { l1_gas, l2_gas, l1_data_gas }) => {
// Committed fee is sum of products (resource_max_amount * resource_max_price)
// of the different resources.
// The Sender will not be charged by`max_price_per_unit`, but this check should
// not depend on the current gas price
l1_gas.max_amount.saturating_mul(l1_gas.max_price_per_unit)
+ l1_data_gas.max_amount.saturating_mul(l1_data_gas.max_price_per_unit)
+ l2_gas.max_amount.saturating_mul(l2_gas.max_price_per_unit)
}
}
}
TransactionInfo::Current(context) => context.resource_bounds.max_possible_fee(),
TransactionInfo::Deprecated(context) => context.max_fee,
};
let (balance_low, balance_high, can_pay) =
Expand Down
19 changes: 12 additions & 7 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ pub struct RevertedTransactionExecutionStatus {
}

/// A fee.
#[cfg_attr(any(test, feature = "testing"), derive(derive_more::Add, derive_more::Deref))]
#[derive(
Debug,
Copy,
Expand All @@ -736,8 +737,6 @@ pub struct RevertedTransactionExecutionStatus {
Serialize,
PartialOrd,
Ord,
derive_more::Add,
derive_more::Deref,
)]
#[serde(from = "PrefixedBytesAsHex<16_usize>", into = "PrefixedBytesAsHex<16_usize>")]
pub struct Fee(pub u128);
Expand All @@ -747,6 +746,10 @@ impl Fee {
self.0.checked_add(rhs.0).map(Fee)
}

pub fn saturating_add(self, rhs: Self) -> Self {
Self(self.0.saturating_add(rhs.0))
}

pub fn checked_div_ceil(self, rhs: NonzeroGasPrice) -> Option<GasAmount> {
match self.checked_div(rhs) {
Some(value) => Some(if value.nonzero_saturating_mul(rhs) < self {
Expand Down Expand Up @@ -1119,11 +1122,13 @@ impl ValidResourceBounds {
l1_gas,
l2_gas,
l1_data_gas,
}) => {
l1_gas.max_amount.saturating_mul(l1_gas.max_price_per_unit)
+ l2_gas.max_amount.saturating_mul(l2_gas.max_price_per_unit)
+ l1_data_gas.max_amount.saturating_mul(l1_data_gas.max_price_per_unit)
}
}) => l1_gas
.max_amount
.saturating_mul(l1_gas.max_price_per_unit)
.saturating_add(l2_gas.max_amount.saturating_mul(l2_gas.max_price_per_unit))
.saturating_add(
l1_data_gas.max_amount.saturating_mul(l1_data_gas.max_price_per_unit),
),
}
}

Expand Down

0 comments on commit ce56299

Please sign in to comment.