Skip to content

Commit

Permalink
add feature gate logic to SchedulerController::calculate_priority_and…
Browse files Browse the repository at this point in the history
…_cost()
  • Loading branch information
tao-stones committed Apr 5, 2024
1 parent d8b3a52 commit 389bad3
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ use {
solana_program_runtime::compute_budget_processor::process_compute_budget_instructions,
solana_runtime::{bank::Bank, bank_forks::BankForks},
solana_sdk::{
clock::MAX_PROCESSING_AGE,
feature_set::{
include_loaded_accounts_data_size_in_fee_calculation,
remove_rounding_in_fee_calculation,
},
fee::FeeBudgetLimits,
saturating_add_assign,
clock::MAX_PROCESSING_AGE, fee::FeeBudgetLimits, saturating_add_assign,
transaction::SanitizedTransaction,
},
solana_svm::transaction_error_metrics::TransactionErrorMetrics,
Expand Down Expand Up @@ -489,15 +483,7 @@ impl SchedulerController {
bank: &Bank,
) -> (u64, u64) {
let cost = CostModel::calculate_cost(transaction, &bank.feature_set).sum();
let fee = bank.fee_structure.calculate_fee(
transaction.message(),
5_000, // this just needs to be non-zero
fee_budget_limits,
bank.feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
bank.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
);
let reward = bank.calculate_reward_for_transaction(transaction, fee_budget_limits);

// We need a multiplier here to avoid rounding down too aggressively.
// For many transactions, the cost will be greater than the fees in terms of raw lamports.
Expand All @@ -506,7 +492,8 @@ impl SchedulerController {
// An offset of 1 is used in the denominator to explicitly avoid division by zero.
const MULTIPLIER: u64 = 1_000_000;
(
fee.saturating_mul(MULTIPLIER)
reward
.saturating_mul(MULTIPLIER)
.saturating_div(cost.saturating_add(1)),
cost,
)
Expand Down
13 changes: 13 additions & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,19 @@ impl CollectorFeeDetails {
.priority_fee
.saturating_add(fee_details.prioritization_fee());
}

pub(crate) fn total(&self) -> u64 {
self.transaction_fee.saturating_add(self.priority_fee)
}
}

impl From<FeeDetails> for CollectorFeeDetails {
fn from(fee_details: FeeDetails) -> Self {
CollectorFeeDetails {
transaction_fee: fee_details.transaction_fee(),
priority_fee: fee_details.prioritization_fee(),
}
}
}

#[derive(Debug)]
Expand Down
74 changes: 56 additions & 18 deletions runtime/src/bank/fee_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ use {
log::{debug, warn},
solana_sdk::{
account::{ReadableAccount, WritableAccount},
feature_set::{
include_loaded_accounts_data_size_in_fee_calculation,
remove_rounding_in_fee_calculation, reward_full_priority_fee,
},
fee::FeeBudgetLimits,
pubkey::Pubkey,
reward_info::RewardInfo,
reward_type::RewardType,
system_program,
transaction::SanitizedTransaction,
},
solana_svm::account_rent_state::RentState,
solana_vote::vote_account::VoteAccountsHashMap,
Expand Down Expand Up @@ -49,43 +55,75 @@ impl Bank {
pub(super) fn distribute_transaction_fees(&self) {
let collector_fees = self.collector_fees.load(Relaxed);
if collector_fees != 0 {
let (deposit, mut burn) = self.fee_rate_governor.burn(collector_fees);
let (deposit, mut burn) = self.calculate_reward_and_burn_fees(collector_fees);
if deposit > 0 {
self.deposit_or_burn_fee(deposit, &mut burn);
}
self.capitalization.fetch_sub(burn, Relaxed);
}
}

// NOTE: to replace `distribute_transaction_fees()`, it applies different burn/reward rate
// on different fees:
// transaction fee: same fee_rate_governor rule
// priority fee: 100% reward
// next PR will call it behind a feature gate
// Replace `distribute_transaction_fees()` after Feature Gate: Reward full priority fee to
// validators #34731;
pub(super) fn distribute_transaction_fee_details(&self) {
let CollectorFeeDetails {
transaction_fee,
priority_fee,
} = *self.collector_fee_details.read().unwrap();

if transaction_fee.saturating_add(priority_fee) == 0 {
let fee_details = self.collector_fee_details.read().unwrap();
if fee_details.total() == 0 {
// nothing to distribute, exit early
return;
}

let (mut deposit, mut burn) = if transaction_fee != 0 {
self.fee_rate_governor.burn(transaction_fee)
} else {
(0, 0)
};
deposit = deposit.saturating_add(priority_fee);
let (deposit, mut burn) = self.calculate_reward_and_burn_fee_details(&fee_details);

if deposit > 0 {
self.deposit_or_burn_fee(deposit, &mut burn);
}
self.capitalization.fetch_sub(burn, Relaxed);
}

pub fn calculate_reward_for_transaction(
&self,
transaction: &SanitizedTransaction,
fee_budget_limits: &FeeBudgetLimits,
) -> u64 {
let (reward, _burn) = if self.feature_set.is_active(&reward_full_priority_fee::id()) {
let fee_details = self.fee_structure.calculate_fee_details(
transaction.message(),
fee_budget_limits,
self.feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
);
self.calculate_reward_and_burn_fee_details(&CollectorFeeDetails::from(fee_details))
} else {
let fee = self.fee_structure.calculate_fee(
transaction.message(),
5_000, // this just needs to be non-zero
fee_budget_limits,
self.feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
self.feature_set
.is_active(&remove_rounding_in_fee_calculation::id()),
);
self.calculate_reward_and_burn_fees(fee)
};
reward
}

fn calculate_reward_and_burn_fees(&self, fee: u64) -> (u64, u64) {
self.fee_rate_governor.burn(fee)
}

fn calculate_reward_and_burn_fee_details(
&self,
fee_details: &CollectorFeeDetails,
) -> (u64, u64) {
let (deposit, burn) = if fee_details.transaction_fee != 0 {
self.fee_rate_governor.burn(fee_details.transaction_fee)
} else {
(0, 0)
};
(deposit.saturating_add(fee_details.priority_fee), burn)
}

fn deposit_or_burn_fee(&self, deposit: u64, burn: &mut u64) {
let validate_fee_collector = self.validate_fee_collector_account();
match self.deposit_fees(
Expand Down

0 comments on commit 389bad3

Please sign in to comment.