|
8 | 8 | use {
|
9 | 9 | crate::{block_cost_limits::*, transaction_cost::*},
|
10 | 10 | log::*,
|
11 |
| - solana_program_runtime::compute_budget::{ |
12 |
| - ComputeBudget, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, MAX_COMPUTE_UNIT_LIMIT, |
| 11 | + solana_program_runtime::{ |
| 12 | + compute_budget::DEFAULT_HEAP_COST, |
| 13 | + compute_budget_processor::{ |
| 14 | + process_compute_budget_instructions, ComputeBudgetLimits, |
| 15 | + DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, MAX_COMPUTE_UNIT_LIMIT, |
| 16 | + }, |
13 | 17 | },
|
14 | 18 | solana_sdk::{
|
15 | 19 | borsh0_10::try_from_slice_unchecked,
|
16 | 20 | compute_budget::{self, ComputeBudgetInstruction},
|
17 |
| - feature_set::{ |
18 |
| - add_set_tx_loaded_accounts_data_size_instruction, |
19 |
| - include_loaded_accounts_data_size_in_fee_calculation, |
20 |
| - remove_deprecated_request_unit_ix, FeatureSet, |
21 |
| - }, |
| 21 | + feature_set::{include_loaded_accounts_data_size_in_fee_calculation, FeatureSet}, |
22 | 22 | fee::FeeStructure,
|
23 | 23 | instruction::CompiledInstruction,
|
24 | 24 | program_utils::limited_deserialize,
|
@@ -62,10 +62,12 @@ impl CostModel {
|
62 | 62 | // to set limit, `compute_budget.loaded_accounts_data_size_limit` is set to default
|
63 | 63 | // limit of 64MB; which will convert to (64M/32K)*8CU = 16_000 CUs
|
64 | 64 | //
|
65 |
| - pub fn calculate_loaded_accounts_data_size_cost(compute_budget: &ComputeBudget) -> u64 { |
| 65 | + pub fn calculate_loaded_accounts_data_size_cost( |
| 66 | + compute_budget_limits: &ComputeBudgetLimits, |
| 67 | + ) -> u64 { |
66 | 68 | FeeStructure::calculate_memory_usage_cost(
|
67 |
| - compute_budget.loaded_accounts_data_size_limit, |
68 |
| - compute_budget.heap_cost, |
| 69 | + usize::try_from(compute_budget_limits.loaded_accounts_bytes).unwrap(), |
| 70 | + DEFAULT_HEAP_COST, |
69 | 71 | )
|
70 | 72 | }
|
71 | 73 |
|
@@ -128,32 +130,28 @@ impl CostModel {
|
128 | 130 | }
|
129 | 131 |
|
130 | 132 | // calculate bpf cost based on compute budget instructions
|
131 |
| - let mut compute_budget = ComputeBudget::default(); |
132 |
| - |
133 |
| - let result = compute_budget.process_instructions( |
134 |
| - transaction.message().program_instructions_iter(), |
135 |
| - !feature_set.is_active(&remove_deprecated_request_unit_ix::id()), |
136 |
| - feature_set.is_active(&add_set_tx_loaded_accounts_data_size_instruction::id()), |
137 |
| - ); |
138 | 133 |
|
139 | 134 | // if failed to process compute_budget instructions, the transaction will not be executed
|
140 | 135 | // by `bank`, therefore it should be considered as no execution cost by cost model.
|
141 |
| - match result { |
142 |
| - Ok(_) => { |
| 136 | + match process_compute_budget_instructions( |
| 137 | + transaction.message().program_instructions_iter(), |
| 138 | + feature_set, |
| 139 | + ) { |
| 140 | + Ok(compute_budget_limits) => { |
143 | 141 | // if tx contained user-space instructions and a more accurate estimate available correct it,
|
144 | 142 | // where "user-space instructions" must be specifically checked by
|
145 | 143 | // 'compute_unit_limit_is_set' flag, because compute_budget does not distinguish
|
146 | 144 | // builtin and bpf instructions when calculating default compute-unit-limit. (see
|
147 | 145 | // compute_budget.rs test `test_process_mixed_instructions_without_compute_budget`)
|
148 | 146 | if bpf_costs > 0 && compute_unit_limit_is_set {
|
149 |
| - bpf_costs = compute_budget.compute_unit_limit |
| 147 | + bpf_costs = u64::from(compute_budget_limits.compute_unit_limit); |
150 | 148 | }
|
151 | 149 |
|
152 | 150 | if feature_set
|
153 | 151 | .is_active(&include_loaded_accounts_data_size_in_fee_calculation::id())
|
154 | 152 | {
|
155 | 153 | loaded_accounts_data_size_cost =
|
156 |
| - Self::calculate_loaded_accounts_data_size_cost(&compute_budget); |
| 154 | + Self::calculate_loaded_accounts_data_size_cost(&compute_budget_limits); |
157 | 155 | }
|
158 | 156 | }
|
159 | 157 | Err(_) => {
|
@@ -545,7 +543,8 @@ mod tests {
|
545 | 543 | // default loaded_accounts_data_size_limit
|
546 | 544 | const DEFAULT_PAGE_COST: u64 = 8;
|
547 | 545 | let expected_loaded_accounts_data_size_cost =
|
548 |
| - solana_program_runtime::compute_budget::MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES as u64 |
| 546 | + solana_program_runtime::compute_budget_processor::MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES |
| 547 | + as u64 |
549 | 548 | / ACCOUNT_DATA_COST_PAGE_SIZE
|
550 | 549 | * DEFAULT_PAGE_COST;
|
551 | 550 |
|
@@ -663,36 +662,36 @@ mod tests {
|
663 | 662 | #[allow(clippy::field_reassign_with_default)]
|
664 | 663 | #[test]
|
665 | 664 | fn test_calculate_loaded_accounts_data_size_cost() {
|
666 |
| - let mut compute_budget = ComputeBudget::default(); |
| 665 | + let mut compute_budget_limits = ComputeBudgetLimits::default(); |
667 | 666 |
|
668 | 667 | // accounts data size are priced in block of 32K, ...
|
669 | 668 |
|
670 | 669 | // ... requesting less than 32K should still be charged as one block
|
671 |
| - compute_budget.loaded_accounts_data_size_limit = 31_usize * 1024; |
| 670 | + compute_budget_limits.loaded_accounts_bytes = 31 * 1024; |
672 | 671 | assert_eq!(
|
673 |
| - compute_budget.heap_cost, |
674 |
| - CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget) |
| 672 | + DEFAULT_HEAP_COST, |
| 673 | + CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget_limits) |
675 | 674 | );
|
676 | 675 |
|
677 | 676 | // ... requesting exact 32K should be charged as one block
|
678 |
| - compute_budget.loaded_accounts_data_size_limit = 32_usize * 1024; |
| 677 | + compute_budget_limits.loaded_accounts_bytes = 32 * 1024; |
679 | 678 | assert_eq!(
|
680 |
| - compute_budget.heap_cost, |
681 |
| - CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget) |
| 679 | + DEFAULT_HEAP_COST, |
| 680 | + CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget_limits) |
682 | 681 | );
|
683 | 682 |
|
684 | 683 | // ... requesting slightly above 32K should be charged as 2 block
|
685 |
| - compute_budget.loaded_accounts_data_size_limit = 33_usize * 1024; |
| 684 | + compute_budget_limits.loaded_accounts_bytes = 33 * 1024; |
686 | 685 | assert_eq!(
|
687 |
| - compute_budget.heap_cost * 2, |
688 |
| - CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget) |
| 686 | + DEFAULT_HEAP_COST * 2, |
| 687 | + CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget_limits) |
689 | 688 | );
|
690 | 689 |
|
691 | 690 | // ... requesting exact 64K should be charged as 2 block
|
692 |
| - compute_budget.loaded_accounts_data_size_limit = 64_usize * 1024; |
| 691 | + compute_budget_limits.loaded_accounts_bytes = 64 * 1024; |
693 | 692 | assert_eq!(
|
694 |
| - compute_budget.heap_cost * 2, |
695 |
| - CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget) |
| 693 | + DEFAULT_HEAP_COST * 2, |
| 694 | + CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget_limits) |
696 | 695 | );
|
697 | 696 | }
|
698 | 697 |
|
|
0 commit comments