Skip to content

Commit

Permalink
refactor: remove feature gate from total_fee function (#1463)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored May 23, 2024
1 parent e3d8217 commit cd7710b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
7 changes: 3 additions & 4 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3950,16 +3950,15 @@ impl Bank {
.into(),
self.feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
self.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
);

self.check_execution_status_and_charge_fee(
message,
execution_status,
is_nonce,
fee_details.total_fee(
self.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
),
fee_details.total_fee(),
)?;

accumulated_fee_details.accumulate(&fee_details);
Expand Down
24 changes: 9 additions & 15 deletions runtime/src/bank/fee_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,18 @@ impl Bank {
transaction: &SanitizedTransaction,
fee_budget_limits: &FeeBudgetLimits,
) -> u64 {
let fee_details = self.fee_structure().calculate_fee_details(
transaction.message(),
fee_budget_limits,
self.feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
self.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
);
let (reward, _burn) = if self.feature_set.is_active(&reward_full_priority_fee::id()) {
let fee_details = self.fee_structure().calculate_fee_details(
transaction.message(),
fee_budget_limits,
self.feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
);
self.calculate_reward_and_burn_fee_details(&CollectorFeeDetails::from(fee_details))
} else {
let fee = self.fee_structure().calculate_fee(
transaction.message(),
5_000, // this just needs to be non-zero
fee_budget_limits,
self.feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
self.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
);
let fee = fee_details.total_fee();
self.calculate_reward_and_burn_fees(fee)
};
reward
Expand Down
33 changes: 23 additions & 10 deletions sdk/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ pub struct FeeStructure {
pub compute_fee_bins: Vec<FeeBin>,
}

#[derive(Debug, Default, Clone, Eq, PartialEq)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub struct FeeDetails {
transaction_fee: u64,
prioritization_fee: u64,
remove_rounding_in_fee_calculation: bool,
}

impl FeeDetails {
pub fn total_fee(&self, remove_rounding_in_fee_calculation: bool) -> u64 {
pub fn total_fee(&self) -> u64 {
let total_fee = self.transaction_fee.saturating_add(self.prioritization_fee);
if remove_rounding_in_fee_calculation {
if self.remove_rounding_in_fee_calculation {
total_fee
} else {
// backward compatible behavior
Expand Down Expand Up @@ -133,8 +134,9 @@ impl FeeStructure {
message,
budget_limits,
include_loaded_account_data_size_in_fee,
remove_rounding_in_fee_calculation,
)
.total_fee(remove_rounding_in_fee_calculation)
.total_fee()
}
}

Expand All @@ -145,6 +147,7 @@ impl FeeStructure {
message: &SanitizedMessage,
budget_limits: &FeeBudgetLimits,
include_loaded_account_data_size_in_fee: bool,
remove_rounding_in_fee_calculation: bool,
) -> FeeDetails {
let signature_fee = message
.num_signatures()
Expand Down Expand Up @@ -182,6 +185,7 @@ impl FeeStructure {
.saturating_add(write_lock_fee)
.saturating_add(compute_fee),
prioritization_fee: budget_limits.prioritization_fee,
remove_rounding_in_fee_calculation,
}
}
}
Expand Down Expand Up @@ -240,13 +244,22 @@ mod tests {
// round large `f64` can lost precision, see feature gate:
// "Removing unwanted rounding in fee calculation #34982"

let large_fee_details = FeeDetails {
transaction_fee: u64::MAX - 11,
prioritization_fee: 1,
};
let transaction_fee = u64::MAX - 11;
let prioritization_fee = 1;
let expected_large_fee = u64::MAX - 10;

assert_eq!(large_fee_details.total_fee(true), expected_large_fee);
assert_ne!(large_fee_details.total_fee(false), expected_large_fee);
let details_with_rounding = FeeDetails {
transaction_fee,
prioritization_fee,
remove_rounding_in_fee_calculation: false,
};
let details_without_rounding = FeeDetails {
transaction_fee,
prioritization_fee,
remove_rounding_in_fee_calculation: true,
};

assert_eq!(details_without_rounding.total_fee(), expected_large_fee);
assert_ne!(details_with_rounding.total_fee(), expected_large_fee);
}
}

0 comments on commit cd7710b

Please sign in to comment.