Skip to content

Commit

Permalink
loaded accounts data size cost does not apply to vote transaction (so…
Browse files Browse the repository at this point in the history
…lana-labs#33235)

* loaded accounts data size cost does not apply to vote transaction

* add a test for vote cost
  • Loading branch information
tao-stones authored Sep 14, 2023
1 parent 99b24bd commit dfaec78
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cost-model/src/cost_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ mod tests {
)
.unwrap();
let mut tx_cost = TransactionCost::new_with_capacity(1);
tx_cost.bpf_execution_cost = 10;
tx_cost.builtins_execution_cost = 10;
tx_cost.writable_accounts.push(mint_keypair.pubkey());
tx_cost.is_simple_vote = true;

Expand Down
81 changes: 75 additions & 6 deletions cost-model/src/transaction_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,80 @@ impl TransactionCost {
}

pub fn sum(&self) -> u64 {
self.signature_cost
.saturating_add(self.write_lock_cost)
.saturating_add(self.data_bytes_cost)
.saturating_add(self.builtins_execution_cost)
.saturating_add(self.bpf_execution_cost)
.saturating_add(self.loaded_accounts_data_size_cost)
if self.is_simple_vote {
self.signature_cost
.saturating_add(self.write_lock_cost)
.saturating_add(self.data_bytes_cost)
.saturating_add(self.builtins_execution_cost)
} else {
self.signature_cost
.saturating_add(self.write_lock_cost)
.saturating_add(self.data_bytes_cost)
.saturating_add(self.builtins_execution_cost)
.saturating_add(self.bpf_execution_cost)
.saturating_add(self.loaded_accounts_data_size_cost)
}
}
}

#[cfg(test)]
mod tests {
use {
crate::cost_model::CostModel,
solana_sdk::{
feature_set::FeatureSet,
hash::Hash,
message::SimpleAddressLoader,
signer::keypair::Keypair,
transaction::{MessageHash, SanitizedTransaction, VersionedTransaction},
},
solana_vote_program::vote_transaction,
};

#[test]
fn test_vote_transaction_cost() {
solana_logger::setup();
let node_keypair = Keypair::new();
let vote_keypair = Keypair::new();
let auth_keypair = Keypair::new();
let transaction = vote_transaction::new_vote_transaction(
vec![],
Hash::default(),
Hash::default(),
&node_keypair,
&vote_keypair,
&auth_keypair,
None,
);

// create a sanitized vote transaction
let vote_transaction = SanitizedTransaction::try_create(
VersionedTransaction::from(transaction.clone()),
MessageHash::Compute,
Some(true),
SimpleAddressLoader::Disabled,
)
.unwrap();

// create a identical sanitized transaction, but identified as non-vote
let none_vote_transaction = SanitizedTransaction::try_create(
VersionedTransaction::from(transaction),
MessageHash::Compute,
Some(false),
SimpleAddressLoader::Disabled,
)
.unwrap();

// expected vote tx cost: 2 write locks, 2 sig, 1 vite ix, and 11 CU tx data cost
let expected_vote_cost = 4151;
// expected non-vote tx cost would include default loaded accounts size cost (16384) additionally
let expected_none_vote_cost = 20535;

let vote_cost = CostModel::calculate_cost(&vote_transaction, &FeatureSet::all_enabled());
let none_vote_cost =
CostModel::calculate_cost(&none_vote_transaction, &FeatureSet::all_enabled());

assert_eq!(expected_vote_cost, vote_cost.sum());
assert_eq!(expected_none_vote_cost, none_vote_cost.sum());
}
}

0 comments on commit dfaec78

Please sign in to comment.