Skip to content

Commit

Permalink
calculate_signature_fee
Browse files Browse the repository at this point in the history
  • Loading branch information
apfitzge committed Jan 15, 2025
1 parent 52cfd85 commit 10e3213
Showing 1 changed file with 42 additions and 91 deletions.
133 changes: 42 additions & 91 deletions fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
);
}
}

0 comments on commit 10e3213

Please sign in to comment.