Skip to content

Commit

Permalink
refactoring cost-model internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
apfitzge committed Dec 6, 2024
1 parent 0782a1f commit 3140a23
Showing 1 changed file with 60 additions and 26 deletions.
86 changes: 60 additions & 26 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use {
DEFAULT_HEAP_COST, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, MAX_COMPUTE_UNIT_LIMIT,
},
solana_feature_set::{self as feature_set, FeatureSet},
solana_runtime_transaction::transaction_with_meta::TransactionWithMeta,
solana_runtime_transaction::{
transaction_meta::StaticMeta, transaction_with_meta::TransactionWithMeta,
},
solana_sdk::{
borsh1::try_from_slice_unchecked,
compute_budget::{self, ComputeBudgetInstruction},
Expand Down Expand Up @@ -49,7 +51,11 @@ impl CostModel {
let signature_cost = Self::get_signature_cost(transaction, feature_set);
let write_lock_cost = Self::get_write_lock_cost(transaction, feature_set);
let (programs_execution_cost, loaded_accounts_data_size_cost, data_bytes_cost) =
Self::get_transaction_cost(transaction, feature_set);
Self::get_transaction_cost(
transaction,
transaction.program_instructions_iter(),
feature_set,
);
let allocated_accounts_data_size =
Self::calculate_allocated_accounts_data_size(transaction);

Expand Down Expand Up @@ -81,7 +87,8 @@ impl CostModel {
let signature_cost = Self::get_signature_cost(transaction, feature_set);
let write_lock_cost = Self::get_write_lock_cost(transaction, feature_set);

let instructions_data_cost = Self::get_instructions_data_cost(transaction);
let instructions_data_cost =
Self::get_instructions_data_cost(transaction.program_instructions_iter());
let allocated_accounts_data_size =
Self::calculate_allocated_accounts_data_size(transaction);

Expand All @@ -106,7 +113,7 @@ impl CostModel {
}

/// Returns signature details and the total signature cost
fn get_signature_cost(transaction: &impl TransactionWithMeta, feature_set: &FeatureSet) -> u64 {
fn get_signature_cost(transaction: &impl StaticMeta, feature_set: &FeatureSet) -> u64 {
let signatures_count_detail = transaction.signature_details();

let ed25519_verify_cost =
Expand Down Expand Up @@ -151,26 +158,28 @@ impl CostModel {
}

/// Return (programs_execution_cost, loaded_accounts_data_size_cost, data_bytes_cost)
fn get_transaction_cost(
transaction: &impl TransactionWithMeta,
fn get_transaction_cost<'a>(
meta: &impl StaticMeta,
instructions: impl Iterator<Item = (&'a Pubkey, SVMInstruction<'a>)>,
feature_set: &FeatureSet,
) -> (u64, u64, u64) {
if feature_set.is_active(&feature_set::reserve_minimal_cus_for_builtin_instructions::id()) {
let data_bytes_cost = Self::get_instructions_data_cost(transaction);
let data_bytes_cost = Self::get_instructions_data_cost(instructions);
let (programs_execution_cost, loaded_accounts_data_size_cost) =
Self::get_estimated_execution_cost(transaction, feature_set);
Self::get_estimated_execution_cost(meta, feature_set);
(
programs_execution_cost,
loaded_accounts_data_size_cost,
data_bytes_cost,
)
} else {
Self::get_transaction_cost_without_minimal_builtin_cus(transaction, feature_set)
Self::get_transaction_cost_without_minimal_builtin_cus(meta, instructions, feature_set)
}
}

fn get_transaction_cost_without_minimal_builtin_cus(
transaction: &impl TransactionWithMeta,
fn get_transaction_cost_without_minimal_builtin_cus<'a>(
meta: &impl StaticMeta,
instructions: impl Iterator<Item = (&'a Pubkey, SVMInstruction<'a>)>,
feature_set: &FeatureSet,
) -> (u64, u64, u64) {
let mut programs_execution_costs = 0u64;
Expand All @@ -179,7 +188,7 @@ impl CostModel {
let mut compute_unit_limit_is_set = false;
let mut has_user_space_instructions = false;

for (program_id, instruction) in transaction.program_instructions_iter() {
for (program_id, instruction) in instructions {
let ix_execution_cost =
if let Some(builtin_cost) = get_builtin_instruction_cost(program_id, feature_set) {
builtin_cost
Expand Down Expand Up @@ -207,7 +216,7 @@ impl CostModel {
// if failed to process compute budget instructions, the transaction
// will not be executed by `bank`, therefore it should be considered
// as no execution cost by cost model.
match transaction
match meta
.compute_budget_instruction_details()
.sanitize_and_convert_to_compute_budget_limits(feature_set)
{
Expand Down Expand Up @@ -244,7 +253,7 @@ impl CostModel {

/// Return (programs_execution_cost, loaded_accounts_data_size_cost)
fn get_estimated_execution_cost(
transaction: &impl TransactionWithMeta,
transaction: &impl StaticMeta,
feature_set: &FeatureSet,
) -> (u64, u64) {
// if failed to process compute_budget instructions, the transaction will not be executed
Expand All @@ -267,10 +276,11 @@ impl CostModel {
}

/// Return the instruction data bytes cost.
fn get_instructions_data_cost(transaction: &impl SVMMessage) -> u64 {
let ix_data_bytes_len_total: u64 = transaction
.instructions_iter()
.map(|instruction| instruction.data.len() as u64)
fn get_instructions_data_cost<'a>(
instructions: impl Iterator<Item = (&'a Pubkey, SVMInstruction<'a>)>,
) -> u64 {
let ix_data_bytes_len_total: u64 = instructions
.map(|(_, instruction)| instruction.data.len() as u64)
.sum();

ix_data_bytes_len_total / INSTRUCTION_DATA_BYTES_COST
Expand Down Expand Up @@ -571,7 +581,11 @@ mod tests {
),
] {
let (program_execution_cost, _loaded_accounts_data_size_cost, _data_bytes_cost) =
CostModel::get_transaction_cost(&simple_transaction, &feature_set);
CostModel::get_transaction_cost(
&simple_transaction,
simple_transaction.program_instructions_iter(),
&feature_set,
);

assert_eq!(expected_execution_cost, program_execution_cost);
}
Expand Down Expand Up @@ -605,7 +619,11 @@ mod tests {
),
] {
let (program_execution_cost, _loaded_accounts_data_size_cost, data_bytes_cost) =
CostModel::get_transaction_cost(&token_transaction, &feature_set);
CostModel::get_transaction_cost(
&token_transaction,
token_transaction.program_instructions_iter(),
&feature_set,
);

assert_eq!(expected_execution_cost, program_execution_cost);
assert_eq!(0, data_bytes_cost);
Expand Down Expand Up @@ -671,7 +689,11 @@ mod tests {
(FeatureSet::all_enabled(), expected_cu_limit as u64),
] {
let (program_execution_cost, _loaded_accounts_data_size_cost, data_bytes_cost) =
CostModel::get_transaction_cost(&token_transaction, &feature_set);
CostModel::get_transaction_cost(
&token_transaction,
token_transaction.program_instructions_iter(),
&feature_set,
);

assert_eq!(expected_execution_cost, program_execution_cost);
assert_eq!(1, data_bytes_cost);
Expand Down Expand Up @@ -714,7 +736,11 @@ mod tests {

for feature_set in [FeatureSet::default(), FeatureSet::all_enabled()] {
let (program_execution_cost, _loaded_accounts_data_size_cost, _data_bytes_cost) =
CostModel::get_transaction_cost(&token_transaction, &feature_set);
CostModel::get_transaction_cost(
&token_transaction,
token_transaction.program_instructions_iter(),
&feature_set,
);
assert_eq!(0, program_execution_cost);
}
}
Expand Down Expand Up @@ -746,7 +772,7 @@ mod tests {
),
] {
let (programs_execution_cost, _loaded_accounts_data_size_cost, data_bytes_cost) =
CostModel::get_transaction_cost(&tx, &feature_set);
CostModel::get_transaction_cost(&tx, tx.program_instructions_iter(), &feature_set);
assert_eq!(expected_execution_cost, programs_execution_cost);
assert_eq!(6, data_bytes_cost);
}
Expand Down Expand Up @@ -786,7 +812,7 @@ mod tests {
),
] {
let (program_execution_cost, _loaded_accounts_data_size_cost, data_bytes_cost) =
CostModel::get_transaction_cost(&tx, &feature_set);
CostModel::get_transaction_cost(&tx, tx.program_instructions_iter(), &feature_set);
assert_eq!(expected_cost, program_execution_cost);
assert_eq!(0, data_bytes_cost);
}
Expand Down Expand Up @@ -932,7 +958,11 @@ mod tests {
),
] {
let (programs_execution_cost, _loaded_accounts_data_size_cost, _data_bytes_cost) =
CostModel::get_transaction_cost(&transaction, &feature_set);
CostModel::get_transaction_cost(
&transaction,
transaction.program_instructions_iter(),
&feature_set,
);

assert_eq!(expected_execution_cost, programs_execution_cost);
}
Expand Down Expand Up @@ -962,7 +992,11 @@ mod tests {
(FeatureSet::all_enabled(), cu_limit as u64),
] {
let (programs_execution_cost, _loaded_accounts_data_size_cost, _data_bytes_cost) =
CostModel::get_transaction_cost(&transaction, &feature_set);
CostModel::get_transaction_cost(
&transaction,
transaction.program_instructions_iter(),
&feature_set,
);

assert_eq!(expected_execution_cost, programs_execution_cost);
}
Expand Down

0 comments on commit 3140a23

Please sign in to comment.