Skip to content

Commit

Permalink
separate priority fee and transaction fee from fee calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-stones committed Jan 23, 2024
1 parent 73d3973 commit 9cd885f
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 75 deletions.
1 change: 1 addition & 0 deletions banks-server/src/banks_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ impl Banks for BanksServer {
let bank = self.bank(commitment);
let sanitized_message = SanitizedMessage::try_from(message).ok()?;
bank.get_fee_for_message(&sanitized_message)
.map(|fee_details| fee_details.sum())
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/banking_stage/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ impl Consumer {
bank.feature_set.is_active(
&feature_set::include_loaded_accounts_data_size_in_fee_calculation::id(),
),
);
).sum();
let (mut fee_payer_account, _slot) = bank
.rc
.accounts
Expand Down
46 changes: 26 additions & 20 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,12 @@ fn execute_transactions(
),
}
.expect("lamports_per_signature must be available");
let fee = bank.get_fee_for_message_with_lamports_per_signature(
&SanitizedMessage::try_from(tx.message().clone()).unwrap(),
lamports_per_signature,
);
let fee = bank
.get_fee_for_message_with_lamports_per_signature(
&SanitizedMessage::try_from(tx.message().clone()).unwrap(),
lamports_per_signature,
)
.sum();

let inner_instructions = inner_instructions.map(|inner_instructions| {
map_inner_instructions(inner_instructions).collect()
Expand Down Expand Up @@ -3983,14 +3985,16 @@ fn test_program_fees() {
);

let sanitized_message = SanitizedMessage::try_from(message.clone()).unwrap();
let expected_normal_fee = fee_structure.calculate_fee(
&sanitized_message,
congestion_multiplier,
&process_compute_budget_instructions(sanitized_message.program_instructions_iter())
.unwrap_or_default()
.into(),
false,
);
let expected_normal_fee = fee_structure
.calculate_fee(
&sanitized_message,
congestion_multiplier,
&process_compute_budget_instructions(sanitized_message.program_instructions_iter())
.unwrap_or_default()
.into(),
false,
)
.sum();
bank_client
.send_and_confirm_message(&[&mint_keypair], message)
.unwrap();
Expand All @@ -4006,14 +4010,16 @@ fn test_program_fees() {
Some(&mint_keypair.pubkey()),
);
let sanitized_message = SanitizedMessage::try_from(message.clone()).unwrap();
let expected_prioritized_fee = fee_structure.calculate_fee(
&sanitized_message,
congestion_multiplier,
&process_compute_budget_instructions(sanitized_message.program_instructions_iter())
.unwrap_or_default()
.into(),
false,
);
let expected_prioritized_fee = fee_structure
.calculate_fee(
&sanitized_message,
congestion_multiplier,
&process_compute_budget_instructions(sanitized_message.program_instructions_iter())
.unwrap_or_default()
.into(),
false,
)
.sum();
assert!(expected_normal_fee < expected_prioritized_fee);

bank_client
Expand Down
4 changes: 3 additions & 1 deletion rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4017,7 +4017,9 @@ pub mod rpc_full {
.map_err(|err| {
Error::invalid_params(format!("invalid transaction message: {err}"))
})?;
let fee = bank.get_fee_for_message(&sanitized_message);
let fee = bank
.get_fee_for_message(&sanitized_message)
.map(|fee_details| fee_details.sum());
Ok(new_response(bank, fee))
}

Expand Down
10 changes: 6 additions & 4 deletions rpc/src/transaction_status_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ impl TransactionStatusService {
),
}
.expect("lamports_per_signature must be available");
let fee = bank.get_fee_for_message_with_lamports_per_signature(
transaction.message(),
lamports_per_signature,
);
let fee = bank
.get_fee_for_message_with_lamports_per_signature(
transaction.message(),
lamports_per_signature,
)
.sum();
let tx_account_locks = transaction.get_account_locks_unchecked();

let inner_instructions = inner_instructions.map(|inner_instructions| {
Expand Down
59 changes: 33 additions & 26 deletions runtime/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,20 @@ pub(super) fn load_accounts(
hash_queue.get_lamports_per_signature(tx.message().recent_blockhash())
});
let fee = if let Some(lamports_per_signature) = lamports_per_signature {
fee_structure.calculate_fee(
tx.message(),
lamports_per_signature,
&process_compute_budget_instructions(
tx.message().program_instructions_iter(),
fee_structure
.calculate_fee(
tx.message(),
lamports_per_signature,
&process_compute_budget_instructions(
tx.message().program_instructions_iter(),
)
.unwrap_or_default()
.into(),
feature_set.is_active(
&include_loaded_accounts_data_size_in_fee_calculation::id(),
),
)
.unwrap_or_default()
.into(),
feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
)
.sum()
} else {
return (Err(TransactionError::BlockhashNotFound), None);
};
Expand Down Expand Up @@ -688,14 +691,16 @@ mod tests {
);

let message = SanitizedMessage::try_from(tx.message().clone()).unwrap();
let fee = FeeStructure::default().calculate_fee(
&message,
lamports_per_signature,
&process_compute_budget_instructions(message.program_instructions_iter())
.unwrap_or_default()
.into(),
false,
);
let fee = FeeStructure::default()
.calculate_fee(
&message,
lamports_per_signature,
&process_compute_budget_instructions(message.program_instructions_iter())
.unwrap_or_default()
.into(),
false,
)
.sum();
assert_eq!(fee, lamports_per_signature);

let loaded_accounts = load_accounts_with_fee(
Expand Down Expand Up @@ -1222,14 +1227,16 @@ mod tests {
);

let message = SanitizedMessage::try_from(tx.message().clone()).unwrap();
let fee = FeeStructure::default().calculate_fee(
&message,
lamports_per_signature,
&process_compute_budget_instructions(message.program_instructions_iter())
.unwrap_or_default()
.into(),
false,
);
let fee = FeeStructure::default()
.calculate_fee(
&message,
lamports_per_signature,
&process_compute_budget_instructions(message.program_instructions_iter())
.unwrap_or_default()
.into(),
false,
)
.sum();
assert_eq!(fee, lamports_per_signature + prioritization_fee);

// assert fail to load account with 2B lamport balance for transaction asking for 2B
Expand Down
16 changes: 9 additions & 7 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ use {
epoch_schedule::EpochSchedule,
feature,
feature_set::{self, include_loaded_accounts_data_size_in_fee_calculation, FeatureSet},
fee::FeeStructure,
fee::{FeeDetails, FeeStructure},
fee_calculator::{FeeCalculator, FeeRateGovernor},
genesis_config::{ClusterType, GenesisConfig},
hard_forks::HardForks,
Expand Down Expand Up @@ -3998,7 +3998,7 @@ impl Bank {
&self.fee_rate_governor
}

pub fn get_fee_for_message(&self, message: &SanitizedMessage) -> Option<u64> {
pub fn get_fee_for_message(&self, message: &SanitizedMessage) -> Option<FeeDetails> {
let lamports_per_signature = {
let blockhash_queue = self.blockhash_queue.read().unwrap();
blockhash_queue.get_lamports_per_signature(message.recent_blockhash())
Expand Down Expand Up @@ -4047,7 +4047,7 @@ impl Bank {
&self,
message: &SanitizedMessage,
lamports_per_signature: u64,
) -> u64 {
) -> FeeDetails {
self.fee_structure.calculate_fee(
message,
lamports_per_signature,
Expand Down Expand Up @@ -5500,10 +5500,12 @@ impl Bank {

let lamports_per_signature =
lamports_per_signature.ok_or(TransactionError::BlockhashNotFound)?;
let fee = self.get_fee_for_message_with_lamports_per_signature(
tx.message(),
lamports_per_signature,
);
let fee = self
.get_fee_for_message_with_lamports_per_signature(
tx.message(),
lamports_per_signature,
)
.sum();

// In case of instruction error, even though no accounts
// were stored we still need to charge the payer the
Expand Down
24 changes: 17 additions & 7 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5278,7 +5278,8 @@ fn test_nonce_transaction() {
let mut expected_balance = 4_650_000
- bank
.get_fee_for_message(&recent_message.try_into().unwrap())
.unwrap();
.unwrap()
.sum();
assert_eq!(bank.get_balance(&custodian_pubkey), expected_balance);
assert_eq!(bank.get_balance(&nonce_pubkey), 250_000);
assert_eq!(bank.get_balance(&alice_pubkey), 100_000);
Expand Down Expand Up @@ -5337,7 +5338,8 @@ fn test_nonce_transaction() {
recent_message.recent_blockhash = bank.last_blockhash();
expected_balance -= bank
.get_fee_for_message(&SanitizedMessage::try_from(recent_message).unwrap())
.unwrap();
.unwrap()
.sum();
assert_eq!(bank.get_balance(&custodian_pubkey), expected_balance);
assert_ne!(
nonce_hash,
Expand Down Expand Up @@ -5405,7 +5407,8 @@ fn test_nonce_transaction_with_tx_wide_caps() {
let mut expected_balance = 4_650_000
- bank
.get_fee_for_message(&recent_message.try_into().unwrap())
.unwrap();
.unwrap()
.sum();
assert_eq!(bank.get_balance(&custodian_pubkey), expected_balance);
assert_eq!(bank.get_balance(&nonce_pubkey), 250_000);
assert_eq!(bank.get_balance(&alice_pubkey), 100_000);
Expand Down Expand Up @@ -5464,7 +5467,8 @@ fn test_nonce_transaction_with_tx_wide_caps() {
recent_message.recent_blockhash = bank.last_blockhash();
expected_balance -= bank
.get_fee_for_message(&SanitizedMessage::try_from(recent_message).unwrap())
.unwrap();
.unwrap()
.sum();
assert_eq!(bank.get_balance(&custodian_pubkey), expected_balance);
assert_ne!(
nonce_hash,
Expand Down Expand Up @@ -5597,6 +5601,7 @@ fn test_nonce_payer() {
- bank
.get_fee_for_message(&recent_message.try_into().unwrap())
.unwrap()
.sum()
);
assert_ne!(
nonce_hash,
Expand Down Expand Up @@ -5664,6 +5669,7 @@ fn test_nonce_payer_tx_wide_cap() {
- bank
.get_fee_for_message(&recent_message.try_into().unwrap())
.unwrap()
.sum()
);
assert_ne!(
nonce_hash,
Expand Down Expand Up @@ -10028,7 +10034,9 @@ fn calculate_test_fee(
.unwrap_or_default()
.into();

fee_structure.calculate_fee(message, lamports_per_signature, &budget_limits, false)
fee_structure
.calculate_fee(message, lamports_per_signature, &budget_limits, false)
.sum()
}

#[test]
Expand Down Expand Up @@ -10755,7 +10763,7 @@ fn test_invalid_rent_state_changes_fee_payer() {
&recent_blockhash,
))
.unwrap();
let fee = bank.get_fee_for_message(&dummy_message).unwrap();
let fee = bank.get_fee_for_message(&dummy_message).unwrap().sum();

// RentPaying fee-payer can remain RentPaying
let tx = Transaction::new(
Expand Down Expand Up @@ -12126,7 +12134,9 @@ fn test_bank_verify_accounts_hash_with_base() {
let transaction = SanitizedTransaction::from_transaction_for_tests(
system_transaction::transfer(&key2, &key3.pubkey(), amount, blockhash),
);
bank.get_fee_for_message(transaction.message()).unwrap()
bank.get_fee_for_message(transaction.message())
.unwrap()
.sum()
};
bank.transfer(amount + fee, &mint, &key1.pubkey()).unwrap();
bank.transfer(amount + fee, &mint, &key2.pubkey()).unwrap();
Expand Down
1 change: 1 addition & 0 deletions runtime/src/bank_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ impl SyncClient for BankClient {
SanitizedMessage::try_from(message.clone())
.ok()
.and_then(|sanitized_message| self.bank.get_fee_for_message(&sanitized_message))
.map(|fee_details| fee_details.sum())
.ok_or_else(|| {
TransportError::IoError(io::Error::new(
io::ErrorKind::Other,
Expand Down
6 changes: 4 additions & 2 deletions runtime/src/snapshot_bank_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,7 @@ mod tests {
lamports_to_transfer,
blockhash,
));
let fee = bank2.get_fee_for_message(tx.message()).unwrap();
let fee = bank2.get_fee_for_message(tx.message()).unwrap().sum();
let tx = system_transaction::transfer(
&key1,
&key2.pubkey(),
Expand Down Expand Up @@ -2184,7 +2184,9 @@ mod tests {
let transaction = SanitizedTransaction::from_transaction_for_tests(
system_transaction::transfer(&key2, &key3.pubkey(), amount, blockhash),
);
bank.get_fee_for_message(transaction.message()).unwrap()
bank.get_fee_for_message(transaction.message())
.unwrap()
.sum()
};
bank.transfer(amount + fee, mint, &key1.pubkey()).unwrap();
bank.transfer(amount + fee, mint, &key2.pubkey()).unwrap();
Expand Down
Loading

0 comments on commit 9cd885f

Please sign in to comment.