Skip to content

Commit

Permalink
set default to about 95 percentile thresholds;
Browse files Browse the repository at this point in the history
replace default() with new_with(feature_gate_status);
add public getter with feature_gate status and making consts private;
  • Loading branch information
tao-stones committed Jun 3, 2024
1 parent 156b507 commit a0612cc
Showing 1 changed file with 52 additions and 23 deletions.
75 changes: 52 additions & 23 deletions compute-budget/src/compute_budget_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000;

/// The total accounts data a transaction can load is limited to 64MiB to not break
/// anyone in Mainnet-beta today. It can be set by set_loaded_accounts_data_size_limit instruction
pub const MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES: u32 = 64 * 1024 * 1024;
const MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES: u32 = 64 * 1024 * 1024;
/// The default accounts data size a transaction can load if sender didn't set specific
/// value via set_loaded_accounts_data_size_limit
pub const DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES: u32 = 32 * 1024 * 1024;
const DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES: u32 = 2 * 1024 * 1024;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ComputeBudgetLimits {
Expand All @@ -33,13 +33,27 @@ pub struct ComputeBudgetLimits {
pub loaded_accounts_bytes: u32,
}

impl Default for ComputeBudgetLimits {
fn default() -> Self {
impl ComputeBudgetLimits {
// default constructor with feature gate
pub fn new_with(use_default_loaded_accounts_data_size: bool) -> Self {
ComputeBudgetLimits {
updated_heap_bytes: u32::try_from(MIN_HEAP_FRAME_BYTES).unwrap(),
compute_unit_limit: MAX_COMPUTE_UNIT_LIMIT,
compute_unit_price: 0,
loaded_accounts_bytes: DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES,
loaded_accounts_bytes: Self::get_default_loaded_accounts_data_size_bytes(
use_default_loaded_accounts_data_size,
),
}
}

// behavior before/after feature gate `default_loaded_accounts_data_size_limit`
pub fn get_default_loaded_accounts_data_size_bytes(
use_default_loaded_accounts_data_size: bool,
) -> u32 {
if use_default_loaded_accounts_data_size {
DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES
} else {
MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES
}
}
}
Expand Down Expand Up @@ -70,6 +84,7 @@ impl From<ComputeBudgetLimits> for FeeBudgetLimits {
/// are retrieved and returned,
pub fn process_compute_budget_instructions<'a>(
instructions: impl Iterator<Item = (&'a Pubkey, &'a CompiledInstruction)>,
use_default_loaded_accounts_data_size: bool,
) -> Result<ComputeBudgetLimits, TransactionError> {
let mut num_non_compute_budget_instructions: u32 = 0;
let mut updated_compute_unit_limit = None;
Expand Down Expand Up @@ -138,7 +153,11 @@ pub fn process_compute_budget_instructions<'a>(
let compute_unit_price = updated_compute_unit_price.unwrap_or(0);

let loaded_accounts_bytes = updated_loaded_accounts_data_size_limit
.unwrap_or(DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES)
.unwrap_or(
ComputeBudgetLimits::get_default_loaded_accounts_data_size_bytes(
use_default_loaded_accounts_data_size,
),
)
.min(MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES);

Ok(ComputeBudgetLimits {
Expand Down Expand Up @@ -171,17 +190,22 @@ mod tests {
};

macro_rules! test {
( $instructions: expr, $expected_result: expr) => {
( $instructions: expr, $expected_result: expr, $use_default_loaded_accounts_data_szie: expr) => {
let payer_keypair = Keypair::new();
let tx = SanitizedTransaction::from_transaction_for_tests(Transaction::new(
&[&payer_keypair],
Message::new($instructions, Some(&payer_keypair.pubkey())),
Hash::default(),
));
let result =
process_compute_budget_instructions(tx.message().program_instructions_iter());
let result = process_compute_budget_instructions(
tx.message().program_instructions_iter(),
$use_default_loaded_accounts_data_szie,
);
assert_eq!($expected_result, result);
};
( $instructions: expr, $expected_result: expr) => {
test!($instructions, $expected_result, true);
};
}

#[test]
Expand Down Expand Up @@ -441,20 +465,25 @@ mod tests {

// Assert when set_loaded_accounts_data_size_limit is not presented
// budget is set to default data size
let expected_result = Ok(ComputeBudgetLimits {
compute_unit_limit: DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT,
loaded_accounts_bytes: DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES,
..ComputeBudgetLimits::default()
});

test!(
&[Instruction::new_with_bincode(
Pubkey::new_unique(),
&0_u8,
vec![]
),],
expected_result
);
for use_default_loaded_accounts_data_size in [true, false] {
let expected_result = Ok(ComputeBudgetLimits {
compute_unit_limit: DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT,
loaded_accounts_bytes: Self::get_default_loaded_accounts_data_size_bytes(
use_default_loaded_accounts_data_size,
),
..ComputeBudgetLimits::default()
});

test!(
&[Instruction::new_with_bincode(
Pubkey::new_unique(),
&0_u8,
vec![]
),],
expected_result,
use_default_loaded_accounts_data_size
);
}

// Assert when set_loaded_accounts_data_size_limit presents more than once,
// return DuplicateInstruction
Expand Down

0 comments on commit a0612cc

Please sign in to comment.