|
| 1 | +use { |
| 2 | + crate::{ |
| 3 | + accounts::{ |
| 4 | + AccountHeader, |
| 5 | + MappingAccount, |
| 6 | + PythAccount, |
| 7 | + }, |
| 8 | + c_oracle_header::PC_MAGIC, |
| 9 | + deserialize::{ |
| 10 | + load, |
| 11 | + load_account_as, |
| 12 | + }, |
| 13 | + instruction::CommandHeader, |
| 14 | + utils::{ |
| 15 | + check_valid_writable_account, |
| 16 | + pyth_assert, |
| 17 | + }, |
| 18 | + OracleError, |
| 19 | + }, |
| 20 | + solana_program::{ |
| 21 | + account_info::AccountInfo, |
| 22 | + entrypoint::{ |
| 23 | + ProgramResult, |
| 24 | + MAX_PERMITTED_DATA_INCREASE, |
| 25 | + }, |
| 26 | + pubkey::Pubkey, |
| 27 | + }, |
| 28 | + std::{ |
| 29 | + cmp::min, |
| 30 | + mem::size_of, |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +/// Resize mapping account. |
| 35 | +// account[0] mapping account [writable] |
| 36 | +pub fn resize_mapping( |
| 37 | + program_id: &Pubkey, |
| 38 | + accounts: &[AccountInfo], |
| 39 | + instruction_data: &[u8], |
| 40 | +) -> ProgramResult { |
| 41 | + let mapping_account = match accounts { |
| 42 | + [x] => Ok(x), |
| 43 | + _ => Err(OracleError::InvalidNumberOfAccounts), |
| 44 | + }?; |
| 45 | + |
| 46 | + let hdr = load::<CommandHeader>(instruction_data)?; |
| 47 | + |
| 48 | + check_valid_writable_account(program_id, mapping_account)?; |
| 49 | + |
| 50 | + { |
| 51 | + let account_header = load_account_as::<AccountHeader>(mapping_account)?; |
| 52 | + pyth_assert( |
| 53 | + account_header.magic_number == PC_MAGIC |
| 54 | + && account_header.version == hdr.version |
| 55 | + && account_header.account_type == MappingAccount::ACCOUNT_TYPE, |
| 56 | + OracleError::InvalidAccountHeader.into(), |
| 57 | + )?; |
| 58 | + } |
| 59 | + |
| 60 | + pyth_assert( |
| 61 | + mapping_account.data_len() < size_of::<MappingAccount>(), |
| 62 | + OracleError::NoNeedToResize.into(), |
| 63 | + )?; |
| 64 | + let new_size = min( |
| 65 | + size_of::<MappingAccount>(), |
| 66 | + mapping_account.data_len() + MAX_PERMITTED_DATA_INCREASE, |
| 67 | + ); |
| 68 | + mapping_account.realloc(new_size, true)?; |
| 69 | + |
| 70 | + Ok(()) |
| 71 | +} |
0 commit comments