From 10e32132381533f8297645a394abd34c883424d0 Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Wed, 15 Jan 2025 10:57:31 -0600 Subject: [PATCH] calculate_signature_fee --- fee/src/lib.rs | 133 ++++++++++++++++--------------------------------- 1 file changed, 42 insertions(+), 91 deletions(-) diff --git a/fee/src/lib.rs b/fee/src/lib.rs index 9f91bf913a7892..7f2f7ec072a492 100644 --- a/fee/src/lib.rs +++ b/fee/src/lib.rs @@ -29,35 +29,27 @@ pub fn calculate_fee_details( return FeeDetails::default(); } - calculate_fee_details_internal( - SignatureCounts::from(message), - lamports_per_signature, + FeeDetails::new( + calculate_signature_fee(SignatureCounts::from(message), lamports_per_signature), prioritization_fee, remove_rounding_in_fee_calculation, ) } -/// This function has the actual fee calculation. -fn calculate_fee_details_internal( +/// Calculate fees from signatures. +fn calculate_signature_fee( SignatureCounts { num_transaction_signatures, num_ed25519_signatures, num_secp256k1_signatures, }: SignatureCounts, lamports_per_signature: u64, - prioritization_fee: u64, - remove_rounding_in_fee_calculation: bool, -) -> FeeDetails { - let signature_fee = (num_transaction_signatures +) -> u64 { + let signature_count = num_transaction_signatures .saturating_add(num_ed25519_signatures) - .saturating_add(num_secp256k1_signatures)) - .saturating_mul(lamports_per_signature); + .saturating_add(num_secp256k1_signatures); - FeeDetails::new( - signature_fee, - prioritization_fee, - remove_rounding_in_fee_calculation, - ) + signature_count.saturating_mul(lamports_per_signature) } struct SignatureCounts { @@ -81,87 +73,46 @@ mod tests { use super::*; #[test] - fn test_calculate_fee_details_internal() { + fn test_calculate_signature_fee() { const LAMPORTS_PER_SIGNATURE: u64 = 5_000; // Impossible case - 0 signatures. - { - let signature_counts = SignatureCounts { - num_transaction_signatures: 0, - num_ed25519_signatures: 0, - num_secp256k1_signatures: 0, - }; - let prioritization_fee = 0; - - assert_eq!( - calculate_fee_details_internal( - signature_counts, - LAMPORTS_PER_SIGNATURE, - prioritization_fee, - false - ), - FeeDetails::new(0, 0, false) - ); - } + assert_eq!( + calculate_signature_fee( + SignatureCounts { + num_transaction_signatures: 0, + num_ed25519_signatures: 0, + num_secp256k1_signatures: 0, + }, + LAMPORTS_PER_SIGNATURE, + ), + 0 + ); // Simple signature - { - let signature_counts = SignatureCounts { - num_transaction_signatures: 1, - num_ed25519_signatures: 0, - num_secp256k1_signatures: 0, - }; - let prioritization_fee = 0; - - assert_eq!( - calculate_fee_details_internal( - signature_counts, - LAMPORTS_PER_SIGNATURE, - prioritization_fee, - false - ), - FeeDetails::new(LAMPORTS_PER_SIGNATURE, 0, false) - ); - } - - // Simple signature and prioritization. - { - let signature_counts = SignatureCounts { - num_transaction_signatures: 1, - num_ed25519_signatures: 0, - num_secp256k1_signatures: 0, - }; - let prioritization_fee = 1_000; - - assert_eq!( - calculate_fee_details_internal( - signature_counts, - LAMPORTS_PER_SIGNATURE, - prioritization_fee, - false - ), - FeeDetails::new(LAMPORTS_PER_SIGNATURE, 1_000, false) - ); - } + assert_eq!( + calculate_signature_fee( + SignatureCounts { + num_transaction_signatures: 1, + num_ed25519_signatures: 0, + num_secp256k1_signatures: 0, + }, + LAMPORTS_PER_SIGNATURE, + ), + LAMPORTS_PER_SIGNATURE + ); // Pre-compile signatures. - { - let signature_counts = SignatureCounts { - num_transaction_signatures: 1, - num_ed25519_signatures: 2, - num_secp256k1_signatures: 3, - }; - let prioritization_fee = 4_000; - - assert_eq!( - calculate_fee_details_internal( - signature_counts, - LAMPORTS_PER_SIGNATURE, - prioritization_fee, - false - ), - FeeDetails::new(6 * LAMPORTS_PER_SIGNATURE, 4_000, false) - ); - } + assert_eq!( + calculate_signature_fee( + SignatureCounts { + num_transaction_signatures: 1, + num_ed25519_signatures: 2, + num_secp256k1_signatures: 3, + }, + LAMPORTS_PER_SIGNATURE, + ), + 6 * LAMPORTS_PER_SIGNATURE + ); } }