diff --git a/cost-model/src/cost_tracker.rs b/cost-model/src/cost_tracker.rs index ecded4c75479c2..29464adc21314f 100644 --- a/cost-model/src/cost_tracker.rs +++ b/cost-model/src/cost_tracker.rs @@ -4,7 +4,10 @@ //! - add_transaction_cost(&tx_cost), mutable function to accumulate tx_cost to tracker. //! use { - crate::{block_cost_limits::*, transaction_cost::TransactionCost}, + crate::{ + block_cost_limits::*, cost_tracker_post_analysis::CostTrackerPostAnalysis, + transaction_cost::TransactionCost, + }, solana_metrics::datapoint_info, solana_pubkey::Pubkey, solana_runtime_transaction::transaction_with_meta::TransactionWithMeta, @@ -433,6 +436,15 @@ impl CostTracker { } } +/// Implement the trait for the cost tracker +/// This is only used for post-analysis to avoid lock contention +/// Do not use in the hot path +impl CostTrackerPostAnalysis for CostTracker { + fn get_cost_by_writable_accounts(&self) -> &HashMap { + &self.cost_by_writable_accounts + } +} + #[cfg(test)] mod tests { use { @@ -998,4 +1010,20 @@ mod tests { assert_eq!(0, cost_tracker.vote_cost); assert_eq!(0, cost_tracker.allocated_accounts_data_size.0); } + + #[test] + fn test_get_cost_by_writable_accounts_post_analysis() { + let mut cost_tracker = CostTracker::default(); + let cost = 100u64; + let transaction = WritableKeysTransaction(vec![Pubkey::new_unique()]); + let tx_cost = simple_transaction_cost(&transaction, cost); + cost_tracker.add_transaction_cost(&tx_cost); + let cost_by_writable_accounts = cost_tracker.get_cost_by_writable_accounts(); + assert_eq!(1, cost_by_writable_accounts.len()); + assert_eq!(cost, *cost_by_writable_accounts.values().next().unwrap()); + assert_eq!( + *cost_by_writable_accounts, + cost_tracker.cost_by_writable_accounts + ); + } } diff --git a/cost-model/src/cost_tracker_post_analysis.rs b/cost-model/src/cost_tracker_post_analysis.rs new file mode 100644 index 00000000000000..904bad8ebd8734 --- /dev/null +++ b/cost-model/src/cost_tracker_post_analysis.rs @@ -0,0 +1,8 @@ +use {solana_pubkey::Pubkey, std::collections::HashMap}; + +/// Trait to help with post-analysis of a given block +pub trait CostTrackerPostAnalysis { + /// Only use in post-analyze to avoid lock contention + /// Do not use in the hot path + fn get_cost_by_writable_accounts(&self) -> &HashMap; +} diff --git a/cost-model/src/lib.rs b/cost-model/src/lib.rs index f408a18de3b377..872d21106801d3 100644 --- a/cost-model/src/lib.rs +++ b/cost-model/src/lib.rs @@ -4,6 +4,7 @@ pub mod block_cost_limits; pub mod cost_model; pub mod cost_tracker; +pub mod cost_tracker_post_analysis; pub mod transaction_cost; #[cfg_attr(feature = "frozen-abi", macro_use)]