Skip to content

Commit

Permalink
refactor: reuse compute budget limits
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Jun 12, 2024
1 parent 3e077b7 commit 2e83153
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
28 changes: 11 additions & 17 deletions compute-budget/src/compute_budget.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use {
crate::compute_budget_processor::{
self, process_compute_budget_instructions, DEFAULT_HEAP_COST,
},
solana_sdk::{instruction::CompiledInstruction, pubkey::Pubkey, transaction::Result},
};
use crate::compute_budget_processor::{self, ComputeBudgetLimits, DEFAULT_HEAP_COST};

#[cfg(all(RUSTC_WITH_SPECIALIZATION, feature = "frozen-abi"))]
impl ::solana_frozen_abi::abi_example::AbiExample for ComputeBudget {
Expand Down Expand Up @@ -126,6 +121,16 @@ impl Default for ComputeBudget {
}
}

impl From<ComputeBudgetLimits> for ComputeBudget {
fn from(compute_budget_limits: ComputeBudgetLimits) -> Self {
ComputeBudget {
compute_unit_limit: u64::from(compute_budget_limits.compute_unit_limit),
heap_size: compute_budget_limits.updated_heap_bytes,
..ComputeBudget::default()
}
}
}

impl ComputeBudget {
pub fn new(compute_unit_limit: u64) -> Self {
ComputeBudget {
Expand Down Expand Up @@ -176,17 +181,6 @@ impl ComputeBudget {
}
}

pub fn try_from_instructions<'a>(
instructions: impl Iterator<Item = (&'a Pubkey, &'a CompiledInstruction)>,
) -> Result<Self> {
let compute_budget_limits = process_compute_budget_instructions(instructions)?;
Ok(ComputeBudget {
compute_unit_limit: u64::from(compute_budget_limits.compute_unit_limit),
heap_size: compute_budget_limits.updated_heap_bytes,
..ComputeBudget::default()
})
}

/// Returns cost of the Poseidon hash function for the given number of
/// inputs is determined by the following quadratic function:
///
Expand Down
2 changes: 1 addition & 1 deletion compute-budget/src/compute_budget_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000;
/// anyone in Mainnet-beta today. It can be set by set_loaded_accounts_data_size_limit instruction
pub const MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES: u32 = 64 * 1024 * 1024;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ComputeBudgetLimits {
pub updated_heap_bytes: u32,
pub compute_unit_limit: u32,
Expand Down
7 changes: 6 additions & 1 deletion svm/src/account_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use {
transaction_processing_callback::TransactionProcessingCallback,
},
itertools::Itertools,
solana_compute_budget::compute_budget_processor::process_compute_budget_instructions,
solana_compute_budget::compute_budget_processor::{
process_compute_budget_instructions, ComputeBudgetLimits,
},
solana_program_runtime::loaded_programs::{ProgramCacheEntry, ProgramCacheForTxBatch},
solana_sdk::{
account::{Account, AccountSharedData, ReadableAccount, WritableAccount},
Expand Down Expand Up @@ -46,6 +48,7 @@ pub struct CheckedTransactionDetails {
#[cfg_attr(feature = "dev-context-only-utils", derive(Default))]
pub struct ValidatedTransactionDetails {
pub nonce: Option<NonceFull>,
pub compute_budget_limits: ComputeBudgetLimits,
pub fee_details: FeeDetails,
pub fee_payer_account: AccountSharedData,
pub fee_payer_rent_debit: u64,
Expand All @@ -57,6 +60,7 @@ pub struct LoadedTransaction {
pub program_indices: TransactionProgramIndices,
pub nonce: Option<NonceFull>,
pub fee_details: FeeDetails,
pub compute_budget_limits: ComputeBudgetLimits,
pub rent: TransactionRent,
pub rent_debits: RentDebits,
pub loaded_accounts_data_size: usize,
Expand Down Expand Up @@ -362,6 +366,7 @@ fn load_transaction_accounts<CB: TransactionProcessingCallback>(
program_indices,
nonce: tx_details.nonce,
fee_details: tx_details.fee_details,
compute_budget_limits: tx_details.compute_budget_limits,
rent: tx_rent,
rent_debits,
loaded_accounts_data_size: accumulated_accounts_data_size,
Expand Down
53 changes: 24 additions & 29 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use {
solana_bpf_loader_program::syscalls::create_program_runtime_environment_v1,
solana_compute_budget::{
compute_budget::ComputeBudget,
compute_budget_processor::process_compute_budget_instructions,
compute_budget_processor::{process_compute_budget_instructions, ComputeBudgetLimits},
},
solana_loader_v4_program::create_program_runtime_environment_v2,
solana_measure::{measure, measure::Measure},
Expand Down Expand Up @@ -292,22 +292,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
if let Some(compute_budget) = self.runtime_config.compute_budget {
compute_budget
} else {
let mut compute_budget_process_transaction_time =
Measure::start("compute_budget_process_transaction_time");
let maybe_compute_budget = ComputeBudget::try_from_instructions(
tx.message().program_instructions_iter(),
);
compute_budget_process_transaction_time.stop();
saturating_add_assign!(
execute_timings
.execute_accessories
.compute_budget_process_transaction_us,
compute_budget_process_transaction_time.as_us()
);
if let Err(err) = maybe_compute_budget {
return TransactionExecutionResult::NotExecuted(err);
}
maybe_compute_budget.unwrap()
loaded_transaction.compute_budget_limits.into()
};

let result = self.execute_loaded_transaction(
Expand Down Expand Up @@ -403,13 +388,17 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
lamports_per_signature,
}| {
let message = sanitized_tx.borrow().message();
let (fee_details, fee_payer_account, fee_payer_rent_debit) = self
.validate_transaction_fee_payer(
callbacks,
message,
lamports_per_signature,
error_counters,
)?;
let (
compute_budget_limits,
fee_details,
fee_payer_account,
fee_payer_rent_debit,
) = self.validate_transaction_fee_payer(
callbacks,
message,
lamports_per_signature,
error_counters,
)?;

// Update nonce with fee-subtracted accounts
let fee_payer_address = message.fee_payer();
Expand All @@ -424,6 +413,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {

Ok(ValidatedTransactionDetails {
nonce,
compute_budget_limits,
fee_details,
fee_payer_account,
fee_payer_rent_debit,
Expand All @@ -443,7 +433,7 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
message: &SanitizedMessage,
lamports_per_signature: u64,
error_counters: &mut TransactionErrorMetrics,
) -> transaction::Result<(FeeDetails, AccountSharedData, u64)> {
) -> transaction::Result<(ComputeBudgetLimits, FeeDetails, AccountSharedData, u64)> {
let feature_set = callbacks.get_feature_set();
let rent_collector = callbacks.get_rent_collector();

Expand All @@ -462,12 +452,12 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
)
.rent_amount;

let compute_budget_limits =
process_compute_budget_instructions(message.program_instructions_iter())?;
let fee_details = self.fee_structure.calculate_fee_details(
message,
lamports_per_signature,
&process_compute_budget_instructions(message.program_instructions_iter())
.unwrap_or_default()
.into(),
&compute_budget_limits.into(),
feature_set.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
feature_set.is_active(&remove_rounding_in_fee_calculation::id()),
);
Expand All @@ -482,7 +472,12 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
fee_details.total_fee(),
)?;

Ok((fee_details, fee_payer_account, fee_payer_rent_debit))
Ok((
compute_budget_limits,
fee_details,
fee_payer_account,
fee_payer_rent_debit,
))
}

/// Returns a map from executable program accounts (all accounts owned by any loader)
Expand Down

0 comments on commit 2e83153

Please sign in to comment.