Skip to content

Commit 9acf172

Browse files
authored
refactor: optimize some checks on upd_price (#403)
- 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.
1 parent 87ca5ac commit 9acf172

File tree

2 files changed

+4
-27
lines changed

2 files changed

+4
-27
lines changed

program/rust/src/tests/test_add_publisher.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,7 @@ fn test_add_publisher() {
5858
permissions_account_data.security_authority = *funding_account.key;
5959
}
6060

61-
// Expect the instruction to fail, because the price account isn't rent exempt
62-
assert_eq!(
63-
process_instruction(
64-
&program_id,
65-
&[
66-
funding_account.clone(),
67-
price_account.clone(),
68-
permissions_account.clone(),
69-
],
70-
instruction_data
71-
),
72-
Err(OracleError::InvalidWritableAccount.into())
73-
);
74-
75-
// Now give the price account enough lamports to be rent exempt
61+
// Give the price account enough lamports to be rent exempt
7662
**price_account.try_borrow_mut_lamports().unwrap() =
7763
Rent::minimum_balance(&Rent::default(), PriceAccount::MINIMUM_SIZE);
7864

program/rust/src/utils.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ fn valid_writable_account(
115115
program_id: &Pubkey,
116116
account: &AccountInfo,
117117
) -> Result<bool, ProgramError> {
118-
Ok(account.is_writable
119-
&& account.owner == program_id
120-
&& get_rent()?.is_exempt(account.lamports(), account.data_len()))
118+
Ok(account.is_writable && account.owner == program_id)
121119
}
122120

123121
pub fn check_valid_writable_account(
@@ -134,10 +132,7 @@ fn valid_readable_account(
134132
program_id: &Pubkey,
135133
account: &AccountInfo,
136134
) -> Result<bool, ProgramError> {
137-
Ok(
138-
account.owner == program_id
139-
&& get_rent()?.is_exempt(account.lamports(), account.data_len()),
140-
)
135+
Ok(account.owner == program_id)
141136
}
142137

143138
pub fn check_valid_readable_account(
@@ -180,11 +175,7 @@ pub fn get_status_for_conf_price_ratio(
180175
confidence: u64,
181176
status: u32,
182177
) -> Result<u32, OracleError> {
183-
let mut threshold_conf = price / MAX_CI_DIVISOR;
184-
185-
if threshold_conf < 0 {
186-
threshold_conf = -threshold_conf;
187-
}
178+
let threshold_conf = price.abs() / MAX_CI_DIVISOR;
188179

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

0 commit comments

Comments
 (0)