Skip to content

Commit

Permalink
refactor: optimize some checks on upd_price (#403)
Browse files Browse the repository at this point in the history
- Remove rent check because it costs 500CU (+10% of upd_price without aggregate) and it really is not needed.
- Use abs() on checking confidence threshold because it's incredibly slow on negative prices (2000CU). We don't have negative prices but will guard us in the future.
  • Loading branch information
ali-bahjati authored May 13, 2024
1 parent 87ca5ac commit 9acf172
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 27 deletions.
16 changes: 1 addition & 15 deletions program/rust/src/tests/test_add_publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,7 @@ fn test_add_publisher() {
permissions_account_data.security_authority = *funding_account.key;
}

// Expect the instruction to fail, because the price account isn't rent exempt
assert_eq!(
process_instruction(
&program_id,
&[
funding_account.clone(),
price_account.clone(),
permissions_account.clone(),
],
instruction_data
),
Err(OracleError::InvalidWritableAccount.into())
);

// Now give the price account enough lamports to be rent exempt
// Give the price account enough lamports to be rent exempt
**price_account.try_borrow_mut_lamports().unwrap() =
Rent::minimum_balance(&Rent::default(), PriceAccount::MINIMUM_SIZE);

Expand Down
15 changes: 3 additions & 12 deletions program/rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ fn valid_writable_account(
program_id: &Pubkey,
account: &AccountInfo,
) -> Result<bool, ProgramError> {
Ok(account.is_writable
&& account.owner == program_id
&& get_rent()?.is_exempt(account.lamports(), account.data_len()))
Ok(account.is_writable && account.owner == program_id)
}

pub fn check_valid_writable_account(
Expand All @@ -134,10 +132,7 @@ fn valid_readable_account(
program_id: &Pubkey,
account: &AccountInfo,
) -> Result<bool, ProgramError> {
Ok(
account.owner == program_id
&& get_rent()?.is_exempt(account.lamports(), account.data_len()),
)
Ok(account.owner == program_id)
}

pub fn check_valid_readable_account(
Expand Down Expand Up @@ -180,11 +175,7 @@ pub fn get_status_for_conf_price_ratio(
confidence: u64,
status: u32,
) -> Result<u32, OracleError> {
let mut threshold_conf = price / MAX_CI_DIVISOR;

if threshold_conf < 0 {
threshold_conf = -threshold_conf;
}
let threshold_conf = price.abs() / MAX_CI_DIVISOR;

if confidence > try_convert::<_, u64>(threshold_conf)? {
Ok(PC_STATUS_IGNORED)
Expand Down

0 comments on commit 9acf172

Please sign in to comment.