6
6
solana_svm_transaction:: svm_message:: SVMMessage ,
7
7
} ;
8
8
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.
17
21
const SIMPLE_VOTE_USAGE_COST : u64 = 3428 ;
18
22
19
23
#[ derive( Debug ) ]
@@ -302,9 +306,7 @@ mod tests {
302
306
solana_vote_program:: vote_state:: TowerSync ,
303
307
} ;
304
308
305
- #[ test]
306
- fn test_vote_transaction_cost ( ) {
307
- solana_logger:: setup ( ) ;
309
+ fn get_example_transaction ( ) -> VersionedTransaction {
308
310
let node_keypair = Keypair :: new ( ) ;
309
311
let vote_keypair = Keypair :: new ( ) ;
310
312
let auth_keypair = Keypair :: new ( ) ;
@@ -317,36 +319,57 @@ mod tests {
317
319
None ,
318
320
) ;
319
321
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.
321
330
let vote_transaction = RuntimeTransaction :: try_create (
322
- VersionedTransaction :: from ( transaction . clone ( ) ) ,
331
+ get_example_transaction ( ) ,
323
332
MessageHash :: Compute ,
324
333
Some ( true ) ,
325
334
SimpleAddressLoader :: Disabled ,
326
335
& ReservedAccountKeys :: empty_key_set ( ) ,
327
336
)
328
337
. unwrap ( ) ;
329
338
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 ( ) ,
333
351
MessageHash :: Compute ,
334
352
Some ( false ) ,
335
353
SimpleAddressLoader :: Disabled ,
336
354
& ReservedAccountKeys :: empty_key_set ( ) ,
337
355
)
338
356
. unwrap ( ) ;
339
357
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( ) ) ;
351
374
}
352
375
}
0 commit comments