diff --git a/program/rust/src/deserialize.rs b/program/rust/src/deserialize.rs index 49f550343..6af073aad 100644 --- a/program/rust/src/deserialize.rs +++ b/program/rust/src/deserialize.rs @@ -6,7 +6,6 @@ use bytemuck::{ Pod, }; use solana_program::pubkey::Pubkey; -use solana_program::rent::Rent; use crate::c_oracle_header::{ AccountHeader, @@ -19,6 +18,7 @@ use crate::utils::{ assign_owner, check_valid_fresh_account, clear_account, + get_rent, pyth_assert, send_lamports, }; @@ -127,7 +127,7 @@ pub fn create_pda_if_needed<'a, T: PythAccount>( seeds: &[&[u8]], version: u32, ) -> Result<(), ProgramError> { - let target_rent = Rent::default().minimum_balance(T::MINIMUM_SIZE); + let target_rent = get_rent()?.minimum_balance(T::MINIMUM_SIZE); if account.lamports() < target_rent { send_lamports( funding_account, diff --git a/program/rust/src/rust_oracle.rs b/program/rust/src/rust_oracle.rs index 871c1349c..1645cc807 100644 --- a/program/rust/src/rust_oracle.rs +++ b/program/rust/src/rust_oracle.rs @@ -45,6 +45,7 @@ use crate::utils::{ check_valid_funding_account, check_valid_signable_account, check_valid_writable_account, + get_rent, is_component_update, pyth_assert, read_pc_str_t, @@ -107,7 +108,7 @@ pub fn resize_price_account( match account_len { PriceAccount::MINIMUM_SIZE => { // Ensure account is still rent exempt after resizing - let rent: Rent = Default::default(); + let rent: Rent = get_rent()?; let lamports_needed: u64 = rent .minimum_balance(size_of::()) .saturating_sub(price_account_info.lamports()); diff --git a/program/rust/src/utils.rs b/program/rust/src/utils.rs index c5a2f2ff6..65ad91638 100644 --- a/program/rust/src/utils.rs +++ b/program/rust/src/utils.rs @@ -56,11 +56,14 @@ pub fn check_valid_funding_account(account: &AccountInfo) -> Result<(), ProgramE ) } -pub fn valid_signable_account(program_id: &Pubkey, account: &AccountInfo) -> bool { - account.is_signer +pub fn valid_signable_account( + program_id: &Pubkey, + account: &AccountInfo, +) -> Result { + Ok(account.is_signer && account.is_writable && account.owner == program_id - && Rent::default().is_exempt(account.lamports(), account.data_len()) + && get_rent()?.is_exempt(account.lamports(), account.data_len())) } pub fn check_valid_signable_account( @@ -68,7 +71,7 @@ pub fn check_valid_signable_account( account: &AccountInfo, ) -> Result<(), ProgramError> { pyth_assert( - valid_signable_account(program_id, account), + valid_signable_account(program_id, account)?, OracleError::InvalidSignableAccount.into(), ) } @@ -119,10 +122,13 @@ pub fn read_pc_str_t(source: &[u8]) -> Result<&[u8], ProgramError> { } } -fn valid_writable_account(program_id: &Pubkey, account: &AccountInfo) -> bool { - account.is_writable +fn valid_writable_account( + program_id: &Pubkey, + account: &AccountInfo, +) -> Result { + Ok(account.is_writable && account.owner == program_id - && Rent::default().is_exempt(account.lamports(), account.data_len()) + && get_rent()?.is_exempt(account.lamports(), account.data_len())) } pub fn check_valid_writable_account( @@ -130,7 +136,7 @@ pub fn check_valid_writable_account( account: &AccountInfo, ) -> Result<(), ProgramError> { pyth_assert( - valid_writable_account(program_id, account), + valid_writable_account(program_id, account)?, OracleError::InvalidWritableAccount.into(), ) } @@ -244,3 +250,14 @@ pub fn assign_owner<'a>( )?; Ok(()) } + +#[cfg(not(test))] +pub fn get_rent() -> Result { + use solana_program::sysvar::Sysvar; + Rent::get() +} + +#[cfg(test)] +pub fn get_rent() -> Result { + Ok(Rent::default()) +}