Skip to content

Commit

Permalink
cleanup feature: enable request heap frame instruction solana-labs#30076
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-stones committed Sep 13, 2023
1 parent 525e59f commit b9da7f4
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 138 deletions.
5 changes: 1 addition & 4 deletions accounts-db/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ impl Accounts {
let _process_transaction_result = compute_budget.process_instructions(
tx.message().program_instructions_iter(),
!feature_set.is_active(&remove_deprecated_request_unit_ix::id()),
true, // don't reject txs that use request heap size ix
feature_set.is_active(&add_set_tx_loaded_accounts_data_size_instruction::id()),
);
// sanitize against setting size limit to zero
Expand Down Expand Up @@ -723,7 +722,7 @@ impl Accounts {
fee_structure.calculate_fee(
tx.message(),
lamports_per_signature,
&ComputeBudget::fee_budget_limits(tx.message().program_instructions_iter(), feature_set, Some(self.accounts_db.expected_cluster_type())),
&ComputeBudget::fee_budget_limits(tx.message().program_instructions_iter(), feature_set),
feature_set.is_active(&remove_congestion_multiplier_from_fee_calculation::id()),
feature_set.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()),
)
Expand Down Expand Up @@ -1761,7 +1760,6 @@ mod tests {
&ComputeBudget::fee_budget_limits(
message.program_instructions_iter(),
&feature_set,
None,
),
true,
false,
Expand Down Expand Up @@ -4328,7 +4326,6 @@ mod tests {
&ComputeBudget::fee_budget_limits(
message.program_instructions_iter(),
&feature_set,
None,
),
true,
false,
Expand Down
8 changes: 0 additions & 8 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,9 @@ impl CostModel {
// calculate bpf cost based on compute budget instructions
let mut compute_budget = ComputeBudget::default();

// Starting from v1.15, cost model uses compute_budget.set_compute_unit_limit to
// measure bpf_costs (code below), vs earlier versions that use estimated
// bpf instruction costs. The calculated transaction costs are used by leaders
// during block packing, different costs for same transaction due to different versions
// will not impact consensus. So for v1.15+, should call compute budget with
// the feature gate `enable_request_heap_frame_ix` enabled.
let enable_request_heap_frame_ix = true;
let result = compute_budget.process_instructions(
transaction.message().program_instructions_iter(),
!feature_set.is_active(&remove_deprecated_request_unit_ix::id()),
enable_request_heap_frame_ix,
feature_set.is_active(&add_set_tx_loaded_accounts_data_size_instruction::id()),
);

Expand Down
87 changes: 4 additions & 83 deletions program-runtime/src/compute_budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use {
compute_budget::{self, ComputeBudgetInstruction},
entrypoint::HEAP_LENGTH as MIN_HEAP_FRAME_BYTES,
feature_set::{
add_set_tx_loaded_accounts_data_size_instruction, enable_request_heap_frame_ix,
add_set_tx_loaded_accounts_data_size_instruction,
remove_deprecated_request_unit_ix, FeatureSet,
},
fee::FeeBudgetLimits,
genesis_config::ClusterType,
instruction::{CompiledInstruction, InstructionError},
pubkey::Pubkey,
transaction::TransactionError,
Expand Down Expand Up @@ -191,7 +190,6 @@ impl ComputeBudget {
&mut self,
instructions: impl Iterator<Item = (&'a Pubkey, &'a CompiledInstruction)>,
support_request_units_deprecated: bool,
enable_request_heap_frame_ix: bool,
support_set_loaded_accounts_data_size_limit_ix: bool,
) -> Result<PrioritizationFeeDetails, TransactionError> {
let mut num_non_compute_budget_instructions: u32 = 0;
Expand Down Expand Up @@ -260,8 +258,7 @@ impl ComputeBudget {
}

if let Some((bytes, i)) = requested_heap_size {
if !enable_request_heap_frame_ix
|| bytes > MAX_HEAP_FRAME_BYTES
if bytes > MAX_HEAP_FRAME_BYTES
|| bytes < MIN_HEAP_FRAME_BYTES as u32
|| bytes % 1024 != 0
{
Expand Down Expand Up @@ -293,23 +290,16 @@ impl ComputeBudget {
pub fn fee_budget_limits<'a>(
instructions: impl Iterator<Item = (&'a Pubkey, &'a CompiledInstruction)>,
feature_set: &FeatureSet,
maybe_cluster_type: Option<ClusterType>,
) -> FeeBudgetLimits {
let mut compute_budget = Self::default();

// A cluster specific feature gate, when not activated it keeps v1.13 behavior in mainnet-beta;
// once activated for v1.14+, it allows compute_budget::request_heap_frame and
// compute_budget::set_compute_unit_price co-exist in same transaction.
let enable_request_heap_frame_ix = feature_set
.is_active(&enable_request_heap_frame_ix::id())
|| maybe_cluster_type
.and_then(|cluster_type| (cluster_type != ClusterType::MainnetBeta).then_some(0))
.is_some();
let prioritization_fee_details = compute_budget
.process_instructions(
instructions,
!feature_set.is_active(&remove_deprecated_request_unit_ix::id()),
enable_request_heap_frame_ix,
feature_set.is_active(&add_set_tx_loaded_accounts_data_size_instruction::id()),
)
.unwrap_or_default();
Expand Down Expand Up @@ -369,7 +359,7 @@ mod tests {
};

macro_rules! test {
( $instructions: expr, $expected_result: expr, $expected_budget: expr, $enable_request_heap_frame_ix: expr, $support_set_loaded_accounts_data_size_limit_ix: expr ) => {
( $instructions: expr, $expected_result: expr, $expected_budget: expr, $support_set_loaded_accounts_data_size_limit_ix: expr ) => {
let payer_keypair = Keypair::new();
let tx = SanitizedTransaction::from_transaction_for_tests(Transaction::new(
&[&payer_keypair],
Expand All @@ -380,7 +370,6 @@ mod tests {
let result = compute_budget.process_instructions(
tx.message().program_instructions_iter(),
false, /*not support request_units_deprecated*/
$enable_request_heap_frame_ix,
$support_set_loaded_accounts_data_size_limit_ix,
);
assert_eq!($expected_result, result);
Expand All @@ -391,7 +380,6 @@ mod tests {
$instructions,
$expected_result,
$expected_budget,
true,
false
);
};
Expand Down Expand Up @@ -664,70 +652,11 @@ mod tests {
compute_unit_limit: 0,
..ComputeBudget::default()
},
false,
false
);

// assert requesting heap frame when feature is disable will result instruction error
test!(
&[
ComputeBudgetInstruction::request_heap_frame(40 * 1024),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
],
Err(TransactionError::InstructionError(
0,
InstructionError::InvalidInstructionData
)),
ComputeBudget::default(),
false,
false
);
test!(
&[
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
ComputeBudgetInstruction::request_heap_frame(MAX_HEAP_FRAME_BYTES),
],
Err(TransactionError::InstructionError(
1,
InstructionError::InvalidInstructionData,
)),
ComputeBudget::default(),
false,
false
);
test!(
&[
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
ComputeBudgetInstruction::request_heap_frame(MAX_HEAP_FRAME_BYTES),
ComputeBudgetInstruction::set_compute_unit_limit(MAX_COMPUTE_UNIT_LIMIT),
ComputeBudgetInstruction::set_compute_unit_price(u64::MAX),
],
Err(TransactionError::InstructionError(
1,
InstructionError::InvalidInstructionData,
)),
ComputeBudget::default(),
false,
false
);
test!(
&[
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
ComputeBudgetInstruction::set_compute_unit_limit(1),
ComputeBudgetInstruction::request_heap_frame(MAX_HEAP_FRAME_BYTES),
ComputeBudgetInstruction::set_compute_unit_price(u64::MAX),
],
Err(TransactionError::InstructionError(
2,
InstructionError::InvalidInstructionData,
)),
ComputeBudget::default(),
false,
false
);

// assert normal results when not requesting heap frame when the feature is disabled
test!(
test!(
&[
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Expand All @@ -743,14 +672,12 @@ mod tests {
compute_unit_limit: DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64 * 7,
..ComputeBudget::default()
},
false,
false
);
}

#[test]
fn test_process_loaded_accounts_data_size_limit_instruction() {
let enable_request_heap_frame_ix: bool = true;

// Assert for empty instructions, change value of support_set_loaded_accounts_data_size_limit_ix
// will not change results, which should all be default
Expand All @@ -762,7 +689,6 @@ mod tests {
compute_unit_limit: 0,
..ComputeBudget::default()
},
enable_request_heap_frame_ix,
support_set_loaded_accounts_data_size_limit_ix
);
}
Expand Down Expand Up @@ -801,7 +727,6 @@ mod tests {
],
expected_result,
expected_budget,
enable_request_heap_frame_ix,
support_set_loaded_accounts_data_size_limit_ix
);
}
Expand Down Expand Up @@ -840,7 +765,6 @@ mod tests {
],
expected_result,
expected_budget,
enable_request_heap_frame_ix,
support_set_loaded_accounts_data_size_limit_ix
);
}
Expand Down Expand Up @@ -868,7 +792,6 @@ mod tests {
),],
expected_result,
expected_budget,
enable_request_heap_frame_ix,
support_set_loaded_accounts_data_size_limit_ix
);
}
Expand Down Expand Up @@ -904,7 +827,6 @@ mod tests {
],
expected_result,
expected_budget,
enable_request_heap_frame_ix,
support_set_loaded_accounts_data_size_limit_ix
);
}
Expand All @@ -929,7 +851,6 @@ mod tests {
let result = compute_budget.process_instructions(
transaction.message().program_instructions_iter(),
false, //not support request_units_deprecated
true, //enable_request_heap_frame_ix,
true, //support_set_loaded_accounts_data_size_limit_ix,
);

Expand Down
2 changes: 0 additions & 2 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4089,7 +4089,6 @@ impl Bank {
&ComputeBudget::fee_budget_limits(
message.program_instructions_iter(),
&self.feature_set,
Some(self.cluster_type()),
),
self.feature_set
.is_active(&remove_congestion_multiplier_from_fee_calculation::id()),
Expand Down Expand Up @@ -5177,7 +5176,6 @@ impl Bank {
!self
.feature_set
.is_active(&remove_deprecated_request_unit_ix::id()),
true, // don't reject txs that use request heap size ix
self.feature_set
.is_active(&add_set_tx_loaded_accounts_data_size_instruction::id()),
);
Expand Down
Loading

0 comments on commit b9da7f4

Please sign in to comment.