Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secp256r1 signature fees #4472

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core/src/banking_stage/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use {
},
itertools::Itertools,
solana_feature_set as feature_set,
solana_fee::FeeFeatures,
solana_ledger::token_balances::collect_token_balances,
solana_measure::{measure::Measure, measure_us},
solana_poh::poh_recorder::{
Expand Down Expand Up @@ -767,8 +768,7 @@ impl Consumer {
bank.get_lamports_per_signature() == 0,
bank.fee_structure().lamports_per_signature,
fee_budget_limits.prioritization_fee,
bank.feature_set
.is_active(&feature_set::remove_rounding_in_fee_calculation::id()),
FeeFeatures::from(bank.feature_set.as_ref()),
);
let (mut fee_payer_account, _slot) = bank
.rc
Expand Down
1 change: 1 addition & 0 deletions fee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ license = { workspace = true }
edition = { workspace = true }

[dependencies]
solana-feature-set = { workspace = true }
solana-fee-structure = { workspace = true }
solana-svm-transaction = { workspace = true }
75 changes: 67 additions & 8 deletions fee/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
use {solana_fee_structure::FeeDetails, solana_svm_transaction::svm_message::SVMMessage};
use {
solana_feature_set::{
enable_secp256r1_precompile, remove_rounding_in_fee_calculation, FeatureSet,
},
solana_fee_structure::FeeDetails,
solana_svm_transaction::svm_message::SVMMessage,
};

/// Bools indicating the activation of features relevant
/// to the fee calculation.
// DEVELOPER NOTE:
// This struct may become empty at some point. It is preferable to keep it
// instead of removing, since fees will naturally be changed via feature-gates
// in the future. Keeping this struct will help keep things organized.
#[derive(Copy, Clone)]
pub struct FeeFeatures {
pub remove_rounding_in_fee_calculation: bool,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goal of this structure is so that we do not have a bunch of bool arguments that we always need, but also to not introduce the entire feature set which.
Keeping this makes it extremely obvious exactly which features affect the fee-calculation; and the FeeFeatures could even be re-used to avoid re-looking up in the feature-set!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since needed features come and go, there will be a time FeeFeatures becomes an empty struct. I would think to keep it empty then (perhaps with a note or something to prevent innocent "cleanup"), so to keep calculate_fee() signature unchanged. wdyt?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I agree with keeping it with a note, so that the design idea is not lost

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub enable_secp256r1_precompile: bool,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these need to be public, since only this model needs to know which feature(s) matter to fee calculation?

}

impl From<&FeatureSet> for FeeFeatures {
fn from(feature_set: &FeatureSet) -> Self {
Self {
remove_rounding_in_fee_calculation: feature_set
.is_active(&remove_rounding_in_fee_calculation::ID),
enable_secp256r1_precompile: feature_set.is_active(&enable_secp256r1_precompile::ID),
}
}
}

/// Calculate fee for `SanitizedMessage`
pub fn calculate_fee(
message: &impl SVMMessage,
zero_fees_for_test: bool,
lamports_per_signature: u64,
prioritization_fee: u64,
remove_rounding_in_fee_calculation: bool,
fee_features: FeeFeatures,
) -> u64 {
calculate_fee_details(
message,
zero_fees_for_test,
lamports_per_signature,
prioritization_fee,
remove_rounding_in_fee_calculation,
fee_features,
)
.total_fee()
}
Expand All @@ -23,16 +51,20 @@ pub fn calculate_fee_details(
zero_fees_for_test: bool,
lamports_per_signature: u64,
prioritization_fee: u64,
remove_rounding_in_fee_calculation: bool,
fee_features: FeeFeatures,
) -> FeeDetails {
if zero_fees_for_test {
return FeeDetails::default();
}

FeeDetails::new(
calculate_signature_fee(SignatureCounts::from(message), lamports_per_signature),
calculate_signature_fee(
SignatureCounts::from(message),
lamports_per_signature,
fee_features.enable_secp256r1_precompile,
),
prioritization_fee,
remove_rounding_in_fee_calculation,
fee_features.remove_rounding_in_fee_calculation,
)
}

Expand All @@ -42,20 +74,25 @@ fn calculate_signature_fee(
num_transaction_signatures,
num_ed25519_signatures,
num_secp256k1_signatures,
num_secp256r1_signatures,
}: SignatureCounts,
lamports_per_signature: u64,
enable_secp256r1_precompile: bool,
) -> u64 {
let signature_count = num_transaction_signatures
.saturating_add(num_ed25519_signatures)
.saturating_add(num_secp256k1_signatures);

.saturating_add(num_secp256k1_signatures)
.saturating_add(
u64::from(enable_secp256r1_precompile).wrapping_mul(num_secp256r1_signatures),
);
signature_count.saturating_mul(lamports_per_signature)
}

struct SignatureCounts {
pub num_transaction_signatures: u64,
pub num_ed25519_signatures: u64,
pub num_secp256k1_signatures: u64,
pub num_secp256r1_signatures: u64,
}

impl<Tx: SVMMessage> From<&Tx> for SignatureCounts {
Expand All @@ -64,6 +101,7 @@ impl<Tx: SVMMessage> From<&Tx> for SignatureCounts {
num_transaction_signatures: message.num_transaction_signatures(),
num_ed25519_signatures: message.num_ed25519_signatures(),
num_secp256k1_signatures: message.num_secp256k1_signatures(),
num_secp256r1_signatures: message.num_secp256r1_signatures(),
}
}
}
Expand All @@ -83,8 +121,10 @@ mod tests {
num_transaction_signatures: 0,
num_ed25519_signatures: 0,
num_secp256k1_signatures: 0,
num_secp256r1_signatures: 0,
},
LAMPORTS_PER_SIGNATURE,
true,
),
0
);
Expand All @@ -96,8 +136,10 @@ mod tests {
num_transaction_signatures: 1,
num_ed25519_signatures: 0,
num_secp256k1_signatures: 0,
num_secp256r1_signatures: 0,
},
LAMPORTS_PER_SIGNATURE,
true,
),
LAMPORTS_PER_SIGNATURE
);
Expand All @@ -109,8 +151,25 @@ mod tests {
num_transaction_signatures: 1,
num_ed25519_signatures: 2,
num_secp256k1_signatures: 3,
num_secp256r1_signatures: 4,
},
LAMPORTS_PER_SIGNATURE,
true,
),
10 * LAMPORTS_PER_SIGNATURE
);

// Pre-compile signatures (no secp256r1)
assert_eq!(
calculate_signature_fee(
SignatureCounts {
num_transaction_signatures: 1,
num_ed25519_signatures: 2,
num_secp256k1_signatures: 3,
num_secp256r1_signatures: 4,
},
LAMPORTS_PER_SIGNATURE,
false,
),
6 * LAMPORTS_PER_SIGNATURE
);
Expand Down
3 changes: 2 additions & 1 deletion programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3886,7 +3886,7 @@ fn test_program_fees() {
bank.set_fee_structure(&fee_structure);
let (bank, bank_forks) = bank.wrap_with_bank_forks_for_tests();
let feature_set = bank.feature_set.clone();
let mut bank_client = BankClient::new_shared(bank);
let mut bank_client = BankClient::new_shared(bank.clone());
let authority_keypair = Keypair::new();

let (_bank, program_id) = load_upgradeable_program_and_advance_slot(
Expand Down Expand Up @@ -3920,7 +3920,7 @@ fn test_program_fees() {
congestion_multiplier == 0,
fee_structure.lamports_per_signature,
fee_budget_limits.prioritization_fee,
true,
bank.feature_set.as_ref().into(),
);
bank_client
.send_and_confirm_message(&[&mint_keypair], message)
Expand Down Expand Up @@ -3953,7 +3953,7 @@ fn test_program_fees() {
congestion_multiplier == 0,
fee_structure.lamports_per_signature,
fee_budget_limits.prioritization_fee,
true,
bank.feature_set.as_ref().into(),
);
assert!(expected_normal_fee < expected_prioritized_fee);

Expand Down
9 changes: 3 additions & 6 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ use {
solana_compute_budget::compute_budget::ComputeBudget,
solana_compute_budget_instruction::instructions_processor::process_compute_budget_instructions,
solana_cost_model::{block_cost_limits::simd_0207_block_limits, cost_tracker::CostTracker},
solana_feature_set::{
self as feature_set, remove_rounding_in_fee_calculation, reward_full_priority_fee,
FeatureSet,
},
solana_feature_set::{self as feature_set, reward_full_priority_fee, FeatureSet},
solana_fee::FeeFeatures,
solana_lattice_hash::lt_hash::LtHash,
solana_measure::{meas_dur, measure::Measure, measure_time, measure_us},
solana_program_runtime::{
Expand Down Expand Up @@ -2920,8 +2918,7 @@ impl Bank {
lamports_per_signature == 0,
self.fee_structure().lamports_per_signature,
fee_budget_limits.prioritization_fee,
self.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
FeeFeatures::from(self.feature_set.as_ref()),
)
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/src/bank/fee_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use {
super::Bank,
crate::bank::CollectorFeeDetails,
log::{debug, warn},
solana_feature_set::{remove_rounding_in_fee_calculation, reward_full_priority_fee},
solana_feature_set::reward_full_priority_fee,
solana_fee::FeeFeatures,
solana_runtime_transaction::transaction_with_meta::TransactionWithMeta,
solana_sdk::{
account::{ReadableAccount, WritableAccount},
Expand Down Expand Up @@ -81,8 +82,7 @@ impl Bank {
self.get_lamports_per_signature() == 0,
self.fee_structure().lamports_per_signature,
fee_budget_limits.prioritization_fee,
self.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
FeeFeatures::from(self.feature_set.as_ref()),
);
let (reward, _burn) = if self.feature_set.is_active(&reward_full_priority_fee::id()) {
self.calculate_reward_and_burn_fee_details(&CollectorFeeDetails::from(fee_details))
Expand Down
5 changes: 4 additions & 1 deletion runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10388,7 +10388,10 @@ fn calculate_test_fee(
lamports_per_signature == 0,
fee_structure.lamports_per_signature,
fee_budget_limits.prioritization_fee,
true,
FeeFeatures {
remove_rounding_in_fee_calculation: true,
enable_secp256r1_precompile: true,
},
)
}

Expand Down
3 changes: 2 additions & 1 deletion svm/examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ use {
solana_compute_budget::compute_budget::ComputeBudget,
solana_compute_budget_instruction::instructions_processor::process_compute_budget_instructions,
solana_feature_set::{
enable_transaction_loading_failure_fees, remove_accounts_executable_flag_checks,
remove_rounding_in_fee_calculation, FeatureSet,
enable_transaction_loading_failure_fees, remove_accounts_executable_flag_checks, FeatureSet,
},
solana_fee::FeeFeatures,
solana_fee_structure::{FeeBudgetLimits, FeeStructure},
solana_hash::Hash,
solana_instruction::TRANSACTION_LEVEL_STACK_HEIGHT,
Expand Down Expand Up @@ -606,9 +606,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
lamports_per_signature == 0,
fee_lamports_per_signature,
fee_budget_limits.prioritization_fee,
account_loader
.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
FeeFeatures::from(account_loader.feature_set.as_ref()),
);

let fee_payer_index = 0;
Expand Down
Loading