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
 (solana-labs#33243)

* cleanup feature: enable request heap frame instruction solana-labs#30076

* update sbf tests

* removed out dated comments and test
  • Loading branch information
tao-stones authored Sep 18, 2023
1 parent 17c3930 commit 8b8a21a
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 199 deletions.
15 changes: 3 additions & 12 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 @@ -1758,11 +1757,7 @@ mod tests {
let fee = FeeStructure::default().calculate_fee(
&message,
lamports_per_signature,
&ComputeBudget::fee_budget_limits(
message.program_instructions_iter(),
&feature_set,
None,
),
&ComputeBudget::fee_budget_limits(message.program_instructions_iter(), &feature_set),
true,
false,
);
Expand Down Expand Up @@ -4327,11 +4322,7 @@ mod tests {
let fee = FeeStructure::default().calculate_fee(
&message,
lamports_per_signature,
&ComputeBudget::fee_budget_limits(
message.program_instructions_iter(),
&feature_set,
None,
),
&ComputeBudget::fee_budget_limits(message.program_instructions_iter(), &feature_set),
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
132 changes: 5 additions & 127 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,
remove_deprecated_request_unit_ix, FeatureSet,
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,13 @@ 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 +356,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,20 +367,13 @@ 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);
assert_eq!(compute_budget, $expected_budget);
};
( $instructions: expr, $expected_result: expr, $expected_budget: expr) => {
test!(
$instructions,
$expected_result,
$expected_budget,
true,
false
);
test!($instructions, $expected_result, $expected_budget, false);
};
}

Expand Down Expand Up @@ -654,104 +634,8 @@ mod tests {
);
}

#[test]
fn test_process_instructions_disable_request_heap_frame() {
// assert empty message results default compute budget and fee
test!(
&[],
Ok(PrioritizationFeeDetails::default()),
ComputeBudget {
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!(
&[
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
],
Ok(PrioritizationFeeDetails::default()),
ComputeBudget {
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
for support_set_loaded_accounts_data_size_limit_ix in [true, false] {
Expand All @@ -762,7 +646,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 +684,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 +722,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 +749,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 +784,6 @@ mod tests {
],
expected_result,
expected_budget,
enable_request_heap_frame_ix,
support_set_loaded_accounts_data_size_limit_ix
);
}
Expand All @@ -929,7 +808,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 programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3836,7 +3836,6 @@ fn test_program_fees() {
&ComputeBudget::fee_budget_limits(
sanitized_message.program_instructions_iter(),
&feature_set,
None,
),
true,
false,
Expand Down Expand Up @@ -3864,7 +3863,6 @@ fn test_program_fees() {
&ComputeBudget::fee_budget_limits(
sanitized_message.program_instructions_iter(),
&feature_set,
None,
),
true,
false,
Expand Down
2 changes: 0 additions & 2 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4092,7 +4092,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 @@ -5179,7 +5178,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 8b8a21a

Please sign in to comment.