Skip to content

Commit

Permalink
Rename cost_tracker.account_data_size to better describe its purpose …
Browse files Browse the repository at this point in the history
…is to track per-block new accounts (re)allocation (anza-xyz#1849)

rename cost_tracker.account_data_size to better describe its purpose is to tracker per-block new account allocation
  • Loading branch information
tao-stones authored and samkim-crypto committed Jul 31, 2024
1 parent 744fed9 commit 45a70a0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
5 changes: 3 additions & 2 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
45 changes: 26 additions & 19 deletions cost-model/src/cost_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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!();
}
Expand All @@ -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]
Expand Down Expand Up @@ -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!();
}
Expand Down Expand Up @@ -944,14 +951,14 @@ 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
assert_eq!(0, cost_tracker.transaction_count);
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);
}
}
10 changes: 5 additions & 5 deletions cost-model/src/transaction_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 45a70a0

Please sign in to comment.