Skip to content

Commit 49dcd17

Browse files
authored
tx cost tests cleanup (anza-xyz#5190)
1 parent 99341c2 commit 49dcd17

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

cost-model/src/transaction_cost.rs

+50-27
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ use {
66
solana_svm_transaction::svm_message::SVMMessage,
77
};
88

9-
/// TransactionCost is used to represent resources required to process
10-
/// a transaction, denominated in CU (eg. Compute Units).
11-
/// Resources required to process a regular transaction often include
12-
/// an array of variables, such as execution cost, loaded bytes, write
13-
/// lock and read lock etc.
14-
/// SimpleVote has a simpler and pre-determined format: it has 1 or 2 signatures,
15-
/// 2 write locks, a vote instruction and less than 32k (page size) accounts to load.
16-
/// It's cost therefore can be static #33269.
9+
/// `TransactionCost`` is used to represent resources required to process a
10+
/// transaction, denominated in Compute Units (CUs). Resources required to
11+
/// process a regular transaction often include an array of variables, such as
12+
/// execution cost, loaded bytes, write lock and read lock etc.
13+
///
14+
/// SimpleVote has a simpler and pre-determined format. It has:
15+
/// - 1 or 2 signatures
16+
/// - 2 write locks
17+
/// - 1 vote instruction
18+
/// - less than 32k (page size) accounts to load
19+
///
20+
/// Its cost therefore can be static #33269.
1721
const SIMPLE_VOTE_USAGE_COST: u64 = 3428;
1822

1923
#[derive(Debug)]
@@ -302,9 +306,7 @@ mod tests {
302306
solana_vote_program::vote_state::TowerSync,
303307
};
304308

305-
#[test]
306-
fn test_vote_transaction_cost() {
307-
solana_logger::setup();
309+
fn get_example_transaction() -> VersionedTransaction {
308310
let node_keypair = Keypair::new();
309311
let vote_keypair = Keypair::new();
310312
let auth_keypair = Keypair::new();
@@ -317,36 +319,57 @@ mod tests {
317319
None,
318320
);
319321

320-
// create a sanitized vote transaction
322+
VersionedTransaction::from(transaction)
323+
}
324+
325+
#[test]
326+
fn test_vote_transaction_cost() {
327+
solana_logger::setup();
328+
329+
// Create a sanitized vote transaction.
321330
let vote_transaction = RuntimeTransaction::try_create(
322-
VersionedTransaction::from(transaction.clone()),
331+
get_example_transaction(),
323332
MessageHash::Compute,
324333
Some(true),
325334
SimpleAddressLoader::Disabled,
326335
&ReservedAccountKeys::empty_key_set(),
327336
)
328337
.unwrap();
329338

330-
// create a identical sanitized transaction, but identified as non-vote
331-
let none_vote_transaction = RuntimeTransaction::try_create(
332-
VersionedTransaction::from(transaction),
339+
// Verify actual cost matches expected.
340+
let vote_cost = CostModel::calculate_cost(&vote_transaction, &FeatureSet::all_enabled());
341+
assert_eq!(SIMPLE_VOTE_USAGE_COST, vote_cost.sum());
342+
}
343+
344+
#[test]
345+
fn test_non_vote_transaction_cost() {
346+
solana_logger::setup();
347+
348+
// Create a sanitized non-vote transaction.
349+
let non_vote_transaction = RuntimeTransaction::try_create(
350+
get_example_transaction(),
333351
MessageHash::Compute,
334352
Some(false),
335353
SimpleAddressLoader::Disabled,
336354
&ReservedAccountKeys::empty_key_set(),
337355
)
338356
.unwrap();
339357

340-
// expected vote tx cost: 2 write locks, 1 sig, 1 vote ix, 8cu of loaded accounts size,
341-
let expected_vote_cost = SIMPLE_VOTE_USAGE_COST;
342-
// expected non-vote tx cost would include default loaded accounts size cost (16384) additionally, and 3_000 for instruction
343-
let expected_none_vote_cost = 21443;
344-
345-
let vote_cost = CostModel::calculate_cost(&vote_transaction, &FeatureSet::all_enabled());
346-
let none_vote_cost =
347-
CostModel::calculate_cost(&none_vote_transaction, &FeatureSet::all_enabled());
348-
349-
assert_eq!(expected_vote_cost, vote_cost.sum());
350-
assert_eq!(expected_none_vote_cost, none_vote_cost.sum());
358+
// Compute expected cost.
359+
let signature_cost = 1440;
360+
let write_lock_cost = 600;
361+
let data_bytes_cost = 19;
362+
let programs_execution_cost = 3000;
363+
let loaded_accounts_data_size_cost = 16384;
364+
let expected_non_vote_cost = signature_cost
365+
+ write_lock_cost
366+
+ data_bytes_cost
367+
+ programs_execution_cost
368+
+ loaded_accounts_data_size_cost;
369+
370+
// Verify actual cost matches expected.
371+
let non_vote_cost =
372+
CostModel::calculate_cost(&non_vote_transaction, &FeatureSet::all_enabled());
373+
assert_eq!(expected_non_vote_cost, non_vote_cost.sum());
351374
}
352375
}

0 commit comments

Comments
 (0)