diff --git a/compute-budget/src/compute_budget_limits.rs b/compute-budget/src/compute_budget_limits.rs index c4a48cab0c58e1..90652b7772b6ed 100644 --- a/compute-budget/src/compute_budget_limits.rs +++ b/compute-budget/src/compute_budget_limits.rs @@ -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, }; @@ -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 = @@ -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 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, @@ -53,3 +63,41 @@ impl From 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); + } +} diff --git a/compute-budget/src/lib.rs b/compute-budget/src/lib.rs index 46a8312dcffd27..64108a0f225183 100644 --- a/compute-budget/src/lib.rs +++ b/compute-budget/src/lib.rs @@ -3,4 +3,3 @@ pub mod compute_budget; pub mod compute_budget_limits; -pub mod prioritization_fee; diff --git a/compute-budget/src/prioritization_fee.rs b/compute-budget/src/prioritization_fee.rs deleted file mode 100644 index 398b8d310be854..00000000000000 --- a/compute-budget/src/prioritization_fee.rs +++ /dev/null @@ -1,114 +0,0 @@ -/// There are 10^6 micro-lamports in one lamport -const MICRO_LAMPORTS_PER_LAMPORT: u64 = 1_000_000; - -type MicroLamports = u128; - -pub enum PrioritizationFeeType { - ComputeUnitPrice(u64), -} - -#[derive(Default, Debug, PartialEq, Eq)] -pub struct PrioritizationFeeDetails { - fee: u64, - compute_unit_price: u64, -} - -impl PrioritizationFeeDetails { - pub fn new(fee_type: PrioritizationFeeType, compute_unit_limit: u64) -> Self { - match fee_type { - PrioritizationFeeType::ComputeUnitPrice(compute_unit_price) => { - let micro_lamport_fee: MicroLamports = - (compute_unit_price as u128).saturating_mul(compute_unit_limit as u128); - let fee = 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); - - Self { - fee, - compute_unit_price, - } - } - } - } - - pub fn get_fee(&self) -> u64 { - self.fee - } - - pub fn get_compute_unit_price(&self) -> u64 { - self.compute_unit_price - } -} - -#[cfg(test)] -mod test { - use super::{PrioritizationFeeDetails as FeeDetails, PrioritizationFeeType as FeeType, *}; - - #[test] - fn test_new_with_no_fee() { - for compute_units in [0, 1, MICRO_LAMPORTS_PER_LAMPORT, u64::MAX] { - assert_eq!( - FeeDetails::new(FeeType::ComputeUnitPrice(0), compute_units), - FeeDetails::default(), - ); - } - } - - #[test] - fn test_new_with_compute_unit_price() { - assert_eq!( - FeeDetails::new(FeeType::ComputeUnitPrice(MICRO_LAMPORTS_PER_LAMPORT - 1), 1), - FeeDetails { - fee: 1, - compute_unit_price: MICRO_LAMPORTS_PER_LAMPORT - 1, - }, - "should round up (<1.0) lamport fee to 1 lamport" - ); - - assert_eq!( - FeeDetails::new(FeeType::ComputeUnitPrice(MICRO_LAMPORTS_PER_LAMPORT), 1), - FeeDetails { - fee: 1, - compute_unit_price: MICRO_LAMPORTS_PER_LAMPORT, - }, - ); - - assert_eq!( - FeeDetails::new(FeeType::ComputeUnitPrice(MICRO_LAMPORTS_PER_LAMPORT + 1), 1), - FeeDetails { - fee: 2, - compute_unit_price: MICRO_LAMPORTS_PER_LAMPORT + 1, - }, - "should round up (>1.0) lamport fee to 2 lamports" - ); - - assert_eq!( - FeeDetails::new(FeeType::ComputeUnitPrice(200), 100_000), - FeeDetails { - fee: 20, - compute_unit_price: 200, - }, - ); - - assert_eq!( - FeeDetails::new( - FeeType::ComputeUnitPrice(MICRO_LAMPORTS_PER_LAMPORT), - u64::MAX - ), - FeeDetails { - fee: u64::MAX, - compute_unit_price: MICRO_LAMPORTS_PER_LAMPORT, - }, - ); - - assert_eq!( - FeeDetails::new(FeeType::ComputeUnitPrice(u64::MAX), u64::MAX), - FeeDetails { - fee: u64::MAX, - compute_unit_price: u64::MAX, - }, - ); - } -} diff --git a/runtime/src/bank/tests.rs b/runtime/src/bank/tests.rs index 5a3cec8193c219..57afa678eeb3f0 100644 --- a/runtime/src/bank/tests.rs +++ b/runtime/src/bank/tests.rs @@ -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, @@ -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), @@ -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 ); } } @@ -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( &[ @@ -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 ); }