From c64677100e2c26608e84b0f9b4eefb40d422ef0f Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Mon, 24 Jun 2024 21:21:27 +0000 Subject: [PATCH] rename cost_tracker.account_data_size to better describe its purpose is to tracker per-block new account allocation --- cost-model/src/cost_model.rs | 5 ++-- cost-model/src/cost_tracker.rs | 45 +++++++++++++++++------------- cost-model/src/transaction_cost.rs | 10 +++---- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/cost-model/src/cost_model.rs b/cost-model/src/cost_model.rs index d319bbac1f2530..a6ba581f05b78b 100644 --- a/cost-model/src/cost_model.rs +++ b/cost-model/src/cost_model.rs @@ -43,7 +43,8 @@ impl CostModel { Self::get_signature_cost(&mut tx_cost, transaction); Self::get_write_lock_cost(&mut tx_cost, transaction, feature_set); Self::get_transaction_cost(&mut tx_cost, transaction, feature_set); - tx_cost.account_data_size = Self::calculate_account_data_size(transaction); + tx_cost.allocated_accounts_data_size = + Self::calculate_allocated_accounts_data_size(transaction); debug!("transaction {:?} has cost {:?}", transaction, tx_cost); TransactionCost::Transaction(tx_cost) @@ -218,7 +219,7 @@ impl CostModel { /// eventually, potentially determine account data size of all writable accounts /// at the moment, calculate account data size of account creation - fn calculate_account_data_size(transaction: &SanitizedTransaction) -> u64 { + fn calculate_allocated_accounts_data_size(transaction: &SanitizedTransaction) -> u64 { transaction .message() .program_instructions_iter() diff --git a/cost-model/src/cost_tracker.rs b/cost-model/src/cost_tracker.rs index 967a4afa43f070..0c731f946ec3b8 100644 --- a/cost-model/src/cost_tracker.rs +++ b/cost-model/src/cost_tracker.rs @@ -67,7 +67,7 @@ pub struct CostTracker { block_cost: u64, vote_cost: u64, transaction_count: u64, - account_data_size: u64, + allocated_accounts_data_size: u64, transaction_signature_count: u64, secp256k1_instruction_signature_count: u64, ed25519_instruction_signature_count: u64, @@ -96,7 +96,7 @@ impl Default for CostTracker { block_cost: 0, vote_cost: 0, transaction_count: 0, - account_data_size: 0, + allocated_accounts_data_size: 0, transaction_signature_count: 0, secp256k1_instruction_signature_count: 0, ed25519_instruction_signature_count: 0, @@ -111,7 +111,7 @@ impl CostTracker { self.block_cost = 0; self.vote_cost = 0; self.transaction_count = 0; - self.account_data_size = 0; + self.allocated_accounts_data_size = 0; self.transaction_signature_count = 0; self.secp256k1_instruction_signature_count = 0; self.ed25519_instruction_signature_count = 0; @@ -213,7 +213,11 @@ impl CostTracker { ("number_of_accounts", self.number_of_accounts() as i64, i64), ("costliest_account", costliest_account.to_string(), String), ("costliest_account_cost", costliest_account_cost as i64, i64), - ("account_data_size", self.account_data_size, i64), + ( + "allocated_accounts_data_size", + self.allocated_accounts_data_size, + i64 + ), ( "transaction_signature_count", self.transaction_signature_count, @@ -265,11 +269,11 @@ impl CostTracker { return Err(CostTrackerError::WouldExceedAccountMaxLimit); } - let account_data_size = self - .account_data_size - .saturating_add(tx_cost.account_data_size()); + let allocated_accounts_data_size = self + .allocated_accounts_data_size + .saturating_add(tx_cost.allocated_accounts_data_size()); - if account_data_size > MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA { + if allocated_accounts_data_size > MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA { return Err(CostTrackerError::WouldExceedAccountDataBlockLimit); } @@ -292,7 +296,10 @@ impl CostTracker { // 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.allocated_accounts_data_size, + tx_cost.allocated_accounts_data_size() + ); saturating_add_assign!(self.transaction_count, 1); saturating_add_assign!( self.transaction_signature_count, @@ -312,9 +319,9 @@ impl CostTracker { fn remove_transaction_cost(&mut self, tx_cost: &TransactionCost) { let cost = tx_cost.sum(); self.sub_transaction_execution_cost(tx_cost, cost); - self.account_data_size = self - .account_data_size - .saturating_sub(tx_cost.account_data_size()); + self.allocated_accounts_data_size = self + .allocated_accounts_data_size + .saturating_sub(tx_cost.allocated_accounts_data_size()); self.transaction_count = self.transaction_count.saturating_sub(1); self.transaction_signature_count = self .transaction_signature_count @@ -503,7 +510,7 @@ mod tests { let (mint_keypair, start_hash) = test_setup(); let (_tx, mut tx_cost) = build_simple_transaction(&mint_keypair, &start_hash); if let TransactionCost::Transaction(ref mut usage_cost) = tx_cost { - usage_cost.account_data_size = 1; + usage_cost.allocated_accounts_data_size = 1; } else { unreachable!(); } @@ -512,9 +519,9 @@ mod tests { // build testee to have capacity for one simple transaction let mut testee = CostTracker::new(cost, cost, cost); assert!(testee.would_fit(&tx_cost).is_ok()); - let old = testee.account_data_size; + let old = testee.allocated_accounts_data_size; testee.add_transaction_cost(&tx_cost); - assert_eq!(old + 1, testee.account_data_size); + assert_eq!(old + 1, testee.allocated_accounts_data_size); } #[test] @@ -651,12 +658,12 @@ mod tests { let (_tx1, mut tx_cost1) = build_simple_transaction(&mint_keypair, &start_hash); let (_tx2, mut tx_cost2) = build_simple_transaction(&second_account, &start_hash); if let TransactionCost::Transaction(ref mut usage_cost) = tx_cost1 { - usage_cost.account_data_size = MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA; + usage_cost.allocated_accounts_data_size = MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA; } else { unreachable!(); } if let TransactionCost::Transaction(ref mut usage_cost) = tx_cost2 { - usage_cost.account_data_size = MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA + 1; + usage_cost.allocated_accounts_data_size = MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA + 1; } else { unreachable!(); } @@ -944,7 +951,7 @@ mod tests { assert_eq!(1, cost_tracker.number_of_accounts()); assert_eq!(cost, cost_tracker.block_cost); assert_eq!(0, cost_tracker.vote_cost); - assert_eq!(0, cost_tracker.account_data_size); + assert_eq!(0, cost_tracker.allocated_accounts_data_size); cost_tracker.remove_transaction_cost(&tx_cost); // assert cost_tracker is reverted to default @@ -952,6 +959,6 @@ mod tests { assert_eq!(0, cost_tracker.number_of_accounts()); assert_eq!(0, cost_tracker.block_cost); assert_eq!(0, cost_tracker.vote_cost); - assert_eq!(0, cost_tracker.account_data_size); + assert_eq!(0, cost_tracker.allocated_accounts_data_size); } } diff --git a/cost-model/src/transaction_cost.rs b/cost-model/src/transaction_cost.rs index ee280c873312f9..4951e50036ca8b 100644 --- a/cost-model/src/transaction_cost.rs +++ b/cost-model/src/transaction_cost.rs @@ -56,10 +56,10 @@ impl TransactionCost { } } - pub fn account_data_size(&self) -> u64 { + pub fn allocated_accounts_data_size(&self) -> u64 { match self { Self::SimpleVote { .. } => 0, - Self::Transaction(usage_cost) => usage_cost.account_data_size, + Self::Transaction(usage_cost) => usage_cost.allocated_accounts_data_size, } } @@ -125,7 +125,7 @@ pub struct UsageCostDetails { pub data_bytes_cost: u64, pub programs_execution_cost: u64, pub loaded_accounts_data_size_cost: u64, - pub account_data_size: u64, + pub allocated_accounts_data_size: u64, pub num_transaction_signatures: u64, pub num_secp256k1_instruction_signatures: u64, pub num_ed25519_instruction_signatures: u64, @@ -140,7 +140,7 @@ impl Default for UsageCostDetails { data_bytes_cost: 0u64, programs_execution_cost: 0u64, loaded_accounts_data_size_cost: 0u64, - account_data_size: 0u64, + allocated_accounts_data_size: 0u64, num_transaction_signatures: 0u64, num_secp256k1_instruction_signatures: 0u64, num_ed25519_instruction_signatures: 0u64, @@ -160,7 +160,7 @@ impl PartialEq for UsageCostDetails { && self.data_bytes_cost == other.data_bytes_cost && self.programs_execution_cost == other.programs_execution_cost && self.loaded_accounts_data_size_cost == other.loaded_accounts_data_size_cost - && self.account_data_size == other.account_data_size + && self.allocated_accounts_data_size == other.allocated_accounts_data_size && self.num_transaction_signatures == other.num_transaction_signatures && self.num_secp256k1_instruction_signatures == other.num_secp256k1_instruction_signatures