Skip to content

Commit

Permalink
Remove prioritization_fee.rs (anza-xyz#3235)
Browse files Browse the repository at this point in the history
* Remove dead method in prioritization_fee.rs
* Remove prioritization_fee.rs
* Fix tests
* Make get_prioritization_fee private
  • Loading branch information
ksolana authored Nov 4, 2024
1 parent 9b56f30 commit faf5c73
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 136 deletions.
64 changes: 56 additions & 8 deletions compute-budget/src/compute_budget_limits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use {
crate::prioritization_fee::{PrioritizationFeeDetails, PrioritizationFeeType},
solana_fee_structure::FeeBudgetLimits,
solana_program_entrypoint::HEAP_LENGTH,
solana_fee_structure::FeeBudgetLimits, solana_program_entrypoint::HEAP_LENGTH,
std::num::NonZeroU32,
};

Expand All @@ -13,6 +11,11 @@ pub const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000;
pub const MAX_HEAP_FRAME_BYTES: u32 = 256 * 1024;
pub const MIN_HEAP_FRAME_BYTES: u32 = HEAP_LENGTH as u32;

type MicroLamports = u128;

/// There are 10^6 micro-lamports in one lamport
const MICRO_LAMPORTS_PER_LAMPORT: u64 = 1_000_000;

/// The total accounts data a transaction can load is limited to 64MiB to not break
/// anyone in Mainnet-beta today. It can be set by set_loaded_accounts_data_size_limit instruction
pub const MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES: NonZeroU32 =
Expand All @@ -37,13 +40,20 @@ impl Default for ComputeBudgetLimits {
}
}

fn get_prioritization_fee(compute_unit_price: u64, compute_unit_limit: u64) -> u64 {
let micro_lamport_fee: MicroLamports =
(compute_unit_price as u128).saturating_mul(compute_unit_limit as u128);
micro_lamport_fee
.saturating_add(MICRO_LAMPORTS_PER_LAMPORT.saturating_sub(1) as u128)
.checked_div(MICRO_LAMPORTS_PER_LAMPORT as u128)
.and_then(|fee| u64::try_from(fee).ok())
.unwrap_or(u64::MAX)
}

impl From<ComputeBudgetLimits> for FeeBudgetLimits {
fn from(val: ComputeBudgetLimits) -> Self {
let prioritization_fee_details = PrioritizationFeeDetails::new(
PrioritizationFeeType::ComputeUnitPrice(val.compute_unit_price),
u64::from(val.compute_unit_limit),
);
let prioritization_fee = prioritization_fee_details.get_fee();
let prioritization_fee =
get_prioritization_fee(val.compute_unit_price, u64::from(val.compute_unit_limit));

FeeBudgetLimits {
loaded_accounts_data_size_limit: val.loaded_accounts_bytes,
Expand All @@ -53,3 +63,41 @@ impl From<ComputeBudgetLimits> for FeeBudgetLimits {
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_new_with_no_fee() {
for compute_units in [0, 1, MICRO_LAMPORTS_PER_LAMPORT, u64::MAX] {
assert_eq!(get_prioritization_fee(0, compute_units), 0);
}
}

#[test]
fn test_new_with_compute_unit_price() {
assert_eq!(
get_prioritization_fee(MICRO_LAMPORTS_PER_LAMPORT - 1, 1),
1,
"should round up (<1.0) lamport fee to 1 lamport"
);

assert_eq!(get_prioritization_fee(MICRO_LAMPORTS_PER_LAMPORT, 1), 1);

assert_eq!(
get_prioritization_fee(MICRO_LAMPORTS_PER_LAMPORT + 1, 1),
2,
"should round up (>1.0) lamport fee to 2 lamports"
);

assert_eq!(get_prioritization_fee(200, 100_000), 20);

assert_eq!(
get_prioritization_fee(MICRO_LAMPORTS_PER_LAMPORT, u64::MAX),
u64::MAX
);

assert_eq!(get_prioritization_fee(u64::MAX, u64::MAX), u64::MAX);
}
}
1 change: 0 additions & 1 deletion compute-budget/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@

pub mod compute_budget;
pub mod compute_budget_limits;
pub mod prioritization_fee;
114 changes: 0 additions & 114 deletions compute-budget/src/prioritization_fee.rs

This file was deleted.

26 changes: 13 additions & 13 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ use {
},
solana_compute_budget::{
compute_budget::ComputeBudget,
compute_budget_limits::{self, MAX_COMPUTE_UNIT_LIMIT},
prioritization_fee::{PrioritizationFeeDetails, PrioritizationFeeType},
compute_budget_limits::{self, ComputeBudgetLimits, MAX_COMPUTE_UNIT_LIMIT},
},
solana_feature_set::{self as feature_set, FeatureSet},
solana_inline_spl::token,
Expand Down Expand Up @@ -10303,10 +10302,6 @@ fn test_calculate_fee_compute_units() {
MAX_COMPUTE_UNIT_LIMIT,
] {
const PRIORITIZATION_FEE_RATE: u64 = 42;
let prioritization_fee_details = PrioritizationFeeDetails::new(
PrioritizationFeeType::ComputeUnitPrice(PRIORITIZATION_FEE_RATE),
requested_compute_units as u64,
);
let message = new_sanitized_message(Message::new(
&[
ComputeBudgetInstruction::set_compute_unit_limit(requested_compute_units),
Expand All @@ -10316,9 +10311,14 @@ fn test_calculate_fee_compute_units() {
Some(&Pubkey::new_unique()),
));
let fee = calculate_test_fee(&message, 1, &fee_structure);
let fee_budget_limits = FeeBudgetLimits::from(ComputeBudgetLimits {
compute_unit_price: PRIORITIZATION_FEE_RATE,
compute_unit_limit: requested_compute_units,
..ComputeBudgetLimits::default()
});
assert_eq!(
fee,
lamports_per_signature + prioritization_fee_details.get_fee()
lamports_per_signature + fee_budget_limits.prioritization_fee
);
}
}
Expand All @@ -10332,11 +10332,11 @@ fn test_calculate_prioritization_fee() {

let request_units = 1_000_000_u32;
let request_unit_price = 2_000_000_000_u64;
let prioritization_fee_details = PrioritizationFeeDetails::new(
PrioritizationFeeType::ComputeUnitPrice(request_unit_price),
request_units as u64,
);
let prioritization_fee = prioritization_fee_details.get_fee();
let fee_budget_limits = FeeBudgetLimits::from(ComputeBudgetLimits {
compute_unit_price: request_unit_price,
compute_unit_limit: request_units,
..ComputeBudgetLimits::default()
});

let message = new_sanitized_message(Message::new(
&[
Expand All @@ -10353,7 +10353,7 @@ fn test_calculate_prioritization_fee() {
);
assert_eq!(
fee,
fee_structure.lamports_per_signature + prioritization_fee
fee_structure.lamports_per_signature + fee_budget_limits.prioritization_fee
);
}

Expand Down

0 comments on commit faf5c73

Please sign in to comment.