Skip to content

Commit

Permalink
SVM: Move fee_structure to environment input (anza-xyz#1771)
Browse files Browse the repository at this point in the history
* SVM: add `fee_structure` to environment arg

* runtime: add `fee_structure` to bank

* SVM: drop `fee_structure` from global configs
  • Loading branch information
buffalojoec authored and samkim-crypto committed Jul 31, 2024
1 parent c6d5e50 commit bcbe44b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
12 changes: 10 additions & 2 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ impl PartialEq for Bank {
collector_fee_details: _,
compute_budget: _,
transaction_account_lock_limit: _,
fee_structure: _,
// Ignore new fields explicitly if they do not impact PartialEq.
// Adding ".." will remove compile-time checks that if a new field
// is added to the struct, this PartialEq is accordingly updated.
Expand Down Expand Up @@ -887,6 +888,9 @@ pub struct Bank {

/// The max number of accounts that a transaction may lock.
transaction_account_lock_limit: Option<usize>,

/// Fee structure to use for assessing transaction fees.
fee_structure: FeeStructure,
}

struct VoteWithStakeDelegations {
Expand Down Expand Up @@ -1004,6 +1008,7 @@ impl Bank {
collector_fee_details: RwLock::new(CollectorFeeDetails::default()),
compute_budget: None,
transaction_account_lock_limit: None,
fee_structure: FeeStructure::default(),
};

bank.transaction_processor =
Expand Down Expand Up @@ -1249,6 +1254,7 @@ impl Bank {
collector_fee_details: RwLock::new(CollectorFeeDetails::default()),
compute_budget: parent.compute_budget,
transaction_account_lock_limit: parent.transaction_account_lock_limit,
fee_structure: parent.fee_structure.clone(),
};

let (_, ancestors_time_us) = measure_us!({
Expand Down Expand Up @@ -1640,6 +1646,7 @@ impl Bank {
collector_fee_details: RwLock::new(CollectorFeeDetails::default()),
compute_budget: runtime_config.compute_budget,
transaction_account_lock_limit: runtime_config.transaction_account_lock_limit,
fee_structure: FeeStructure::default(),
};

bank.transaction_processor =
Expand Down Expand Up @@ -3716,6 +3723,7 @@ impl Bank {
epoch_total_stake: self.epoch_total_stake(self.epoch()),
epoch_vote_accounts: self.epoch_vote_accounts(self.epoch()),
feature_set: Arc::clone(&self.feature_set),
fee_structure: Some(&self.fee_structure),
lamports_per_signature,
rent_collector: Some(&self.rent_collector),
};
Expand Down Expand Up @@ -6827,7 +6835,7 @@ impl Bank {
}

pub fn fee_structure(&self) -> &FeeStructure {
&self.transaction_processor.fee_structure
&self.fee_structure
}

pub fn compute_budget(&self) -> Option<ComputeBudget> {
Expand Down Expand Up @@ -7149,7 +7157,7 @@ impl Bank {
}

pub fn set_fee_structure(&mut self, fee_structure: &FeeStructure) {
self.transaction_processor.fee_structure = fee_structure.clone();
self.fee_structure = fee_structure.clone();
}

pub fn load_program(
Expand Down
25 changes: 17 additions & 8 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub struct TransactionProcessingEnvironment<'a> {
pub epoch_vote_accounts: Option<&'a VoteAccountsHashMap>,
/// Runtime feature set to use for the transaction batch.
pub feature_set: Arc<FeatureSet>,
/// Fee structure to use for assessing transaction fees.
pub fee_structure: Option<&'a FeeStructure>,
/// Lamports per signature to charge per transaction.
pub lamports_per_signature: u64,
/// Rent collector to use for the transaction batch.
Expand All @@ -142,9 +144,6 @@ pub struct TransactionBatchProcessor<FG: ForkGraph> {
/// Bank epoch
epoch: Epoch,

/// Transaction fee structure
pub fee_structure: FeeStructure,

/// SysvarCache is a collection of system variables that are
/// accessible from on chain programs. It is passed to SVM from
/// client code (e.g. Bank) and forwarded to the MessageProcessor.
Expand All @@ -162,7 +161,6 @@ impl<FG: ForkGraph> Debug for TransactionBatchProcessor<FG> {
f.debug_struct("TransactionBatchProcessor")
.field("slot", &self.slot)
.field("epoch", &self.epoch)
.field("fee_structure", &self.fee_structure)
.field("sysvar_cache", &self.sysvar_cache)
.field("program_cache", &self.program_cache)
.finish()
Expand All @@ -174,7 +172,6 @@ impl<FG: ForkGraph> Default for TransactionBatchProcessor<FG> {
Self {
slot: Slot::default(),
epoch: Epoch::default(),
fee_structure: FeeStructure::default(),
sysvar_cache: RwLock::<SysvarCache>::default(),
program_cache: Arc::new(RwLock::new(ProgramCache::new(
Slot::default(),
Expand All @@ -190,7 +187,6 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
Self {
slot,
epoch,
fee_structure: FeeStructure::default(),
sysvar_cache: RwLock::<SysvarCache>::default(),
program_cache: Arc::new(RwLock::new(ProgramCache::new(slot, epoch))),
builtin_program_ids: RwLock::new(builtin_program_ids),
Expand All @@ -201,7 +197,6 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
Self {
slot,
epoch,
fee_structure: self.fee_structure.clone(),
sysvar_cache: RwLock::<SysvarCache>::default(),
program_cache: self.program_cache.clone(),
builtin_program_ids: RwLock::new(self.builtin_program_ids.read().unwrap().clone()),
Expand Down Expand Up @@ -236,6 +231,9 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
sanitized_txs,
check_results,
&environment.feature_set,
environment
.fee_structure
.unwrap_or(&FeeStructure::default()),
environment
.rent_collector
.unwrap_or(&RentCollector::default()),
Expand Down Expand Up @@ -399,6 +397,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
sanitized_txs: &[impl core::borrow::Borrow<SanitizedTransaction>],
check_results: Vec<TransactionCheckResult>,
feature_set: &FeatureSet,
fee_structure: &FeeStructure,
rent_collector: &RentCollector,
error_counters: &mut TransactionErrorMetrics,
) -> Vec<TransactionValidationResult> {
Expand All @@ -417,6 +416,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
callbacks,
message,
feature_set,
fee_structure,
lamports_per_signature,
rent_collector,
error_counters,
Expand Down Expand Up @@ -453,6 +453,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
callbacks: &CB,
message: &SanitizedMessage,
feature_set: &FeatureSet,
fee_structure: &FeeStructure,
lamports_per_signature: u64,
rent_collector: &RentCollector,
error_counters: &mut TransactionErrorMetrics,
Expand All @@ -472,7 +473,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
)
.rent_amount;

let fee_details = self.fee_structure.calculate_fee_details(
let fee_details = fee_structure.calculate_fee_details(
message,
lamports_per_signature,
&process_compute_budget_instructions(message.program_instructions_iter())
Expand Down Expand Up @@ -1975,6 +1976,7 @@ mod tests {
&mock_bank,
&message,
&FeatureSet::default(),
&FeeStructure::default(),
lamports_per_signature,
&rent_collector,
&mut error_counters,
Expand Down Expand Up @@ -2033,6 +2035,7 @@ mod tests {
&mock_bank,
&message,
&FeatureSet::default(),
&FeeStructure::default(),
lamports_per_signature,
&rent_collector,
&mut error_counters,
Expand Down Expand Up @@ -2068,6 +2071,7 @@ mod tests {
&mock_bank,
&message,
&FeatureSet::default(),
&FeeStructure::default(),
lamports_per_signature,
&RentCollector::default(),
&mut error_counters,
Expand Down Expand Up @@ -2096,6 +2100,7 @@ mod tests {
&mock_bank,
&message,
&FeatureSet::default(),
&FeeStructure::default(),
lamports_per_signature,
&RentCollector::default(),
&mut error_counters,
Expand Down Expand Up @@ -2128,6 +2133,7 @@ mod tests {
&mock_bank,
&message,
&FeatureSet::default(),
&FeeStructure::default(),
lamports_per_signature,
&rent_collector,
&mut error_counters,
Expand Down Expand Up @@ -2158,6 +2164,7 @@ mod tests {
&mock_bank,
&message,
&FeatureSet::default(),
&FeeStructure::default(),
lamports_per_signature,
&RentCollector::default(),
&mut error_counters,
Expand Down Expand Up @@ -2209,6 +2216,7 @@ mod tests {
&mock_bank,
&message,
&feature_set,
&FeeStructure::default(),
lamports_per_signature,
&rent_collector,
&mut error_counters,
Expand Down Expand Up @@ -2254,6 +2262,7 @@ mod tests {
&mock_bank,
&message,
&feature_set,
&FeeStructure::default(),
lamports_per_signature,
&rent_collector,
&mut error_counters,
Expand Down

0 comments on commit bcbe44b

Please sign in to comment.