1
1
use {
2
2
super :: immutable_deserialized_packet:: ImmutableDeserializedPacket ,
3
+ core:: num:: NonZeroU64 ,
3
4
solana_cost_model:: {
4
5
block_cost_limits,
5
6
cost_model:: CostModel ,
6
7
cost_tracker:: { CostTracker , UpdatedCosts } ,
7
- transaction_cost:: TransactionCost ,
8
8
} ,
9
9
solana_feature_set:: FeatureSet ,
10
10
solana_perf:: packet:: Packet ,
11
11
solana_sdk:: transaction:: SanitizedTransaction ,
12
- solana_svm_transaction:: svm_message:: SVMMessage ,
13
12
std:: sync:: Arc ,
14
13
} ;
15
14
@@ -68,9 +67,8 @@ pub struct ForwardPacketBatchesByAccounts {
68
67
cost_tracker : CostTracker ,
69
68
70
69
// Compute Unit limits for each batch
71
- batch_vote_limit : u64 ,
72
- batch_block_limit : u64 ,
73
- batch_account_limit : u64 ,
70
+ batch_block_limit : NonZeroU64 ,
71
+ batch_account_limit : NonZeroU64 ,
74
72
}
75
73
76
74
impl ForwardPacketBatchesByAccounts {
@@ -83,22 +81,32 @@ impl ForwardPacketBatchesByAccounts {
83
81
. map ( |_| ForwardBatch :: default ( ) )
84
82
. collect ( ) ;
85
83
86
- let batch_vote_limit = block_cost_limits:: MAX_VOTE_UNITS . saturating_div ( limit_ratio as u64 ) ;
84
+ let batch_vote_limit =
85
+ NonZeroU64 :: new ( block_cost_limits:: MAX_VOTE_UNITS . saturating_div ( limit_ratio as u64 ) )
86
+ . expect ( "batch vote limit must not be zero" ) ;
87
87
let batch_block_limit =
88
- block_cost_limits:: MAX_BLOCK_UNITS . saturating_div ( limit_ratio as u64 ) ;
89
- let batch_account_limit =
90
- block_cost_limits:: MAX_WRITABLE_ACCOUNT_UNITS . saturating_div ( limit_ratio as u64 ) ;
88
+ NonZeroU64 :: new ( block_cost_limits:: MAX_BLOCK_UNITS . saturating_div ( limit_ratio as u64 ) )
89
+ . expect ( "batch block limit must not be zero" ) ;
90
+ let batch_account_limit = NonZeroU64 :: new (
91
+ block_cost_limits:: MAX_WRITABLE_ACCOUNT_UNITS . saturating_div ( limit_ratio as u64 ) ,
92
+ )
93
+ . expect ( "batch account limit must not be zero" ) ;
91
94
92
95
let mut cost_tracker = CostTracker :: default ( ) ;
93
96
cost_tracker. set_limits (
94
- batch_account_limit. saturating_mul ( number_of_batches as u64 ) ,
95
- batch_block_limit. saturating_mul ( number_of_batches as u64 ) ,
96
- batch_vote_limit. saturating_mul ( number_of_batches as u64 ) ,
97
+ batch_account_limit
98
+ . get ( )
99
+ . saturating_mul ( number_of_batches as u64 ) ,
100
+ batch_block_limit
101
+ . get ( )
102
+ . saturating_mul ( number_of_batches as u64 ) ,
103
+ batch_vote_limit
104
+ . get ( )
105
+ . saturating_mul ( number_of_batches as u64 ) ,
97
106
) ;
98
107
Self {
99
108
forward_batches,
100
109
cost_tracker,
101
- batch_vote_limit,
102
110
batch_block_limit,
103
111
batch_account_limit,
104
112
}
@@ -113,7 +121,7 @@ impl ForwardPacketBatchesByAccounts {
113
121
let tx_cost = CostModel :: calculate_cost ( sanitized_transaction, feature_set) ;
114
122
115
123
if let Ok ( updated_costs) = self . cost_tracker . try_add ( & tx_cost) {
116
- let batch_index = self . get_batch_index_by_updated_costs ( & tx_cost , & updated_costs) ;
124
+ let batch_index = self . get_batch_index_by_updated_costs ( & updated_costs) ;
117
125
118
126
if let Some ( forward_batch) = self . forward_batches . get_mut ( batch_index) {
119
127
forward_batch. forwardable_packets . push ( immutable_packet) ;
@@ -145,24 +153,12 @@ impl ForwardPacketBatchesByAccounts {
145
153
// would be exceeded. Eg, if by block limit, it can be put into batch #1; by vote limit, it can
146
154
// be put into batch #2; and by account limit, it can be put into batch #3; then it should be
147
155
// put into batch #3 to satisfy all batch limits.
148
- fn get_batch_index_by_updated_costs (
149
- & self ,
150
- tx_cost : & TransactionCost < impl SVMMessage > ,
151
- updated_costs : & UpdatedCosts ,
152
- ) -> usize {
153
- let Some ( batch_index_by_block_limit) =
154
- updated_costs. updated_block_cost . checked_div ( match tx_cost {
155
- TransactionCost :: SimpleVote { .. } => self . batch_vote_limit ,
156
- TransactionCost :: Transaction ( _) => self . batch_block_limit ,
157
- } )
158
- else {
159
- unreachable ! ( "batch vote limit or block limit must not be zero" )
160
- } ;
161
-
156
+ fn get_batch_index_by_updated_costs ( & self , updated_costs : & UpdatedCosts ) -> usize {
157
+ let batch_index_by_block_cost =
158
+ updated_costs. updated_block_cost / self . batch_block_limit . get ( ) ;
162
159
let batch_index_by_account_limit =
163
- updated_costs. updated_costliest_account_cost / self . batch_account_limit ;
164
-
165
- batch_index_by_block_limit. max ( batch_index_by_account_limit) as usize
160
+ updated_costs. updated_costliest_account_cost / self . batch_account_limit . get ( ) ;
161
+ batch_index_by_block_cost. max ( batch_index_by_account_limit) as usize
166
162
}
167
163
}
168
164
@@ -171,14 +167,10 @@ mod tests {
171
167
use {
172
168
super :: * ,
173
169
crate :: banking_stage:: unprocessed_packet_batches:: DeserializedPacket ,
174
- solana_cost_model:: transaction_cost:: { UsageCostDetails , WritableKeysTransaction } ,
175
170
solana_feature_set:: FeatureSet ,
176
171
solana_sdk:: {
177
- compute_budget:: ComputeBudgetInstruction ,
178
- message:: { Message , TransactionSignatureDetails } ,
179
- pubkey:: Pubkey ,
180
- system_instruction,
181
- transaction:: Transaction ,
172
+ compute_budget:: ComputeBudgetInstruction , message:: Message , pubkey:: Pubkey ,
173
+ system_instruction, transaction:: Transaction ,
182
174
} ,
183
175
} ;
184
176
@@ -210,21 +202,6 @@ mod tests {
210
202
( sanitized_transaction, deserialized_packet, limit_ratio)
211
203
}
212
204
213
- fn zero_transaction_cost ( ) -> TransactionCost < ' static , WritableKeysTransaction > {
214
- static DUMMY_TRANSACTION : WritableKeysTransaction = WritableKeysTransaction ( vec ! [ ] ) ;
215
-
216
- TransactionCost :: Transaction ( UsageCostDetails {
217
- transaction : & DUMMY_TRANSACTION ,
218
- signature_cost : 0 ,
219
- write_lock_cost : 0 ,
220
- data_bytes_cost : 0 ,
221
- programs_execution_cost : 0 ,
222
- loaded_accounts_data_size_cost : 0 ,
223
- allocated_accounts_data_size : 0 ,
224
- signature_details : TransactionSignatureDetails :: new ( 0 , 0 , 0 ) ,
225
- } )
226
- }
227
-
228
205
#[ test]
229
206
fn test_try_add_packet_to_multiple_batches ( ) {
230
207
// setup two transactions, one has high priority that writes to hot account, the
@@ -364,49 +341,16 @@ mod tests {
364
341
fn test_get_batch_index_by_updated_costs ( ) {
365
342
let test_cost = 99 ;
366
343
367
- // check against vote limit only
368
- {
369
- let mut forward_packet_batches_by_accounts =
370
- ForwardPacketBatchesByAccounts :: new_with_default_batch_limits ( ) ;
371
- forward_packet_batches_by_accounts. batch_vote_limit = test_cost + 1 ;
372
-
373
- let dummy_transaction = WritableKeysTransaction ( vec ! [ ] ) ;
374
- let transaction_cost = TransactionCost :: SimpleVote {
375
- transaction : & dummy_transaction,
376
- } ;
377
- assert_eq ! (
378
- 0 ,
379
- forward_packet_batches_by_accounts. get_batch_index_by_updated_costs(
380
- & transaction_cost,
381
- & UpdatedCosts {
382
- updated_block_cost: test_cost,
383
- updated_costliest_account_cost: 0
384
- }
385
- )
386
- ) ;
387
- assert_eq ! (
388
- 1 ,
389
- forward_packet_batches_by_accounts. get_batch_index_by_updated_costs(
390
- & transaction_cost,
391
- & UpdatedCosts {
392
- updated_block_cost: test_cost + 1 ,
393
- updated_costliest_account_cost: 0
394
- }
395
- )
396
- ) ;
397
- }
398
-
399
344
// check against block limit only
400
345
{
401
346
let mut forward_packet_batches_by_accounts =
402
347
ForwardPacketBatchesByAccounts :: new_with_default_batch_limits ( ) ;
403
- forward_packet_batches_by_accounts. batch_block_limit = test_cost + 1 ;
348
+ forward_packet_batches_by_accounts. batch_block_limit =
349
+ NonZeroU64 :: new ( test_cost + 1 ) . unwrap ( ) ;
404
350
405
- let transaction_cost = zero_transaction_cost ( ) ;
406
351
assert_eq ! (
407
352
0 ,
408
353
forward_packet_batches_by_accounts. get_batch_index_by_updated_costs(
409
- & transaction_cost,
410
354
& UpdatedCosts {
411
355
updated_block_cost: test_cost,
412
356
updated_costliest_account_cost: 0
@@ -416,7 +360,6 @@ mod tests {
416
360
assert_eq ! (
417
361
1 ,
418
362
forward_packet_batches_by_accounts. get_batch_index_by_updated_costs(
419
- & transaction_cost,
420
363
& UpdatedCosts {
421
364
updated_block_cost: test_cost + 1 ,
422
365
updated_costliest_account_cost: 0
@@ -429,13 +372,12 @@ mod tests {
429
372
{
430
373
let mut forward_packet_batches_by_accounts =
431
374
ForwardPacketBatchesByAccounts :: new_with_default_batch_limits ( ) ;
432
- forward_packet_batches_by_accounts. batch_account_limit = test_cost + 1 ;
375
+ forward_packet_batches_by_accounts. batch_account_limit =
376
+ NonZeroU64 :: new ( test_cost + 1 ) . unwrap ( ) ;
433
377
434
- let transaction_cost = zero_transaction_cost ( ) ;
435
378
assert_eq ! (
436
379
0 ,
437
380
forward_packet_batches_by_accounts. get_batch_index_by_updated_costs(
438
- & transaction_cost,
439
381
& UpdatedCosts {
440
382
updated_block_cost: 0 ,
441
383
updated_costliest_account_cost: test_cost
@@ -445,7 +387,6 @@ mod tests {
445
387
assert_eq ! (
446
388
1 ,
447
389
forward_packet_batches_by_accounts. get_batch_index_by_updated_costs(
448
- & transaction_cost,
449
390
& UpdatedCosts {
450
391
updated_block_cost: 0 ,
451
392
updated_costliest_account_cost: test_cost + 1
@@ -461,15 +402,14 @@ mod tests {
461
402
{
462
403
let mut forward_packet_batches_by_accounts =
463
404
ForwardPacketBatchesByAccounts :: new_with_default_batch_limits ( ) ;
464
- forward_packet_batches_by_accounts. batch_block_limit = test_cost + 1 ;
465
- forward_packet_batches_by_accounts. batch_vote_limit = test_cost / 2 + 1 ;
466
- forward_packet_batches_by_accounts. batch_account_limit = test_cost / 3 + 1 ;
405
+ forward_packet_batches_by_accounts. batch_block_limit =
406
+ NonZeroU64 :: new ( test_cost + 1 ) . unwrap ( ) ;
407
+ forward_packet_batches_by_accounts. batch_account_limit =
408
+ NonZeroU64 :: new ( test_cost / 3 + 1 ) . unwrap ( ) ;
467
409
468
- let transaction_cost = zero_transaction_cost ( ) ;
469
410
assert_eq ! (
470
411
2 ,
471
412
forward_packet_batches_by_accounts. get_batch_index_by_updated_costs(
472
- & transaction_cost,
473
413
& UpdatedCosts {
474
414
updated_block_cost: test_cost,
475
415
updated_costliest_account_cost: test_cost
0 commit comments