Skip to content

Commit

Permalink
Update cost-tracker to return more updated costs after successful try…
Browse files Browse the repository at this point in the history
…_add call (anza-xyz#1141)
  • Loading branch information
tao-stones authored May 2, 2024
1 parent 12d009e commit b1b6b9d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions core/src/banking_stage/forward_packet_batches_by_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
solana_cost_model::{
block_cost_limits,
cost_model::CostModel,
cost_tracker::{CostTracker, CostTrackerError},
cost_tracker::{CostTracker, CostTrackerError, UpdatedCosts},
},
solana_perf::packet::Packet,
solana_sdk::{feature_set::FeatureSet, transaction::SanitizedTransaction},
Expand Down Expand Up @@ -61,7 +61,7 @@ impl ForwardBatch {
sanitized_transaction: &SanitizedTransaction,
immutable_packet: Arc<ImmutableDeserializedPacket>,
feature_set: &FeatureSet,
) -> Result<u64, CostTrackerError> {
) -> Result<UpdatedCosts, CostTrackerError> {
let tx_cost = CostModel::calculate_cost(sanitized_transaction, feature_set);
let res = self.cost_tracker.try_add(&tx_cost);
if res.is_ok() {
Expand Down
8 changes: 5 additions & 3 deletions core/src/banking_stage/qos_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use {
super::{committer::CommitTransactionDetails, BatchedTransactionDetails},
solana_cost_model::{cost_model::CostModel, transaction_cost::TransactionCost},
solana_cost_model::{
cost_model::CostModel, cost_tracker::UpdatedCosts, transaction_cost::TransactionCost,
},
solana_measure::measure::Measure,
solana_runtime::bank::Bank,
solana_sdk::{
Expand Down Expand Up @@ -104,8 +106,8 @@ impl QosService {
match cost {
Ok(cost) => {
match cost_tracker.try_add(&cost) {
Ok(current_block_cost) => {
debug!("slot {:?}, transaction {:?}, cost {:?}, fit into current block, current block cost {}", bank.slot(), tx, cost, current_block_cost);
Ok(UpdatedCosts{updated_block_cost, updated_costliest_account_cost}) => {
debug!("slot {:?}, transaction {:?}, cost {:?}, fit into current block, current block cost {}, updated costliest account cost {}", bank.slot(), tx, cost, updated_block_cost, updated_costliest_account_cost);
self.metrics.stats.selected_txs_count.fetch_add(1, Ordering::Relaxed);
num_included += 1;
Ok(cost)
Expand Down
34 changes: 28 additions & 6 deletions cost-model/src/cost_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ impl From<CostTrackerError> for TransactionError {
}
}

/// Relevant block costs that were updated after successful `try_add()`
#[derive(Debug, Default)]
pub struct UpdatedCosts {
pub updated_block_cost: u64,
// for all write-locked accounts `try_add()` successfully updated, the highest account cost
// can be useful info.
pub updated_costliest_account_cost: u64,
}

#[derive(AbiExample, Debug)]
pub struct CostTracker {
account_cost_limit: u64,
Expand Down Expand Up @@ -122,10 +131,13 @@ impl CostTracker {
.saturating_sub(in_flight_transaction_count);
}

pub fn try_add(&mut self, tx_cost: &TransactionCost) -> Result<u64, CostTrackerError> {
pub fn try_add(&mut self, tx_cost: &TransactionCost) -> Result<UpdatedCosts, CostTrackerError> {
self.would_fit(tx_cost)?;
self.add_transaction_cost(tx_cost);
Ok(self.block_cost)
let updated_costliest_account_cost = self.add_transaction_cost(tx_cost);
Ok(UpdatedCosts {
updated_block_cost: self.block_cost,
updated_costliest_account_cost,
})
}

pub fn update_execution_cost(
Expand Down Expand Up @@ -257,8 +269,8 @@ impl CostTracker {
Ok(())
}

fn add_transaction_cost(&mut self, tx_cost: &TransactionCost) {
self.add_transaction_execution_cost(tx_cost, tx_cost.sum());
// Returns the highest account cost for all write-lock accounts `TransactionCost` updated
fn add_transaction_cost(&mut self, tx_cost: &TransactionCost) -> u64 {
saturating_add_assign!(self.account_data_size, tx_cost.account_data_size());
saturating_add_assign!(self.transaction_count, 1);
saturating_add_assign!(
Expand All @@ -273,6 +285,7 @@ impl CostTracker {
self.ed25519_instruction_signature_count,
tx_cost.num_ed25519_instruction_signatures()
);
self.add_transaction_execution_cost(tx_cost, tx_cost.sum())
}

fn remove_transaction_cost(&mut self, tx_cost: &TransactionCost) {
Expand All @@ -294,18 +307,27 @@ impl CostTracker {
}

/// Apply additional actual execution units to cost_tracker
fn add_transaction_execution_cost(&mut self, tx_cost: &TransactionCost, adjustment: u64) {
/// Return the costliest account cost that were updated by `TransactionCost`
fn add_transaction_execution_cost(
&mut self,
tx_cost: &TransactionCost,
adjustment: u64,
) -> u64 {
let mut costliest_account_cost = 0;
for account_key in tx_cost.writable_accounts().iter() {
let account_cost = self
.cost_by_writable_accounts
.entry(*account_key)
.or_insert(0);
*account_cost = account_cost.saturating_add(adjustment);
costliest_account_cost = costliest_account_cost.max(*account_cost);
}
self.block_cost = self.block_cost.saturating_add(adjustment);
if tx_cost.is_simple_vote() {
self.vote_cost = self.vote_cost.saturating_add(adjustment);
}

costliest_account_cost
}

/// Subtract extra execution units from cost_tracker
Expand Down

0 comments on commit b1b6b9d

Please sign in to comment.