program_account.rs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #[allow(deprecated)]
  2. use crate::accounts::cpi_account::CpiAccount;
  3. use crate::bpf_writer::BpfWriter;
  4. use crate::error::{Error, ErrorCode};
  5. use crate::{
  6. AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, Key, Result,
  7. ToAccountInfo, ToAccountInfos, ToAccountMetas,
  8. };
  9. use solana_program::account_info::AccountInfo;
  10. use solana_program::instruction::AccountMeta;
  11. use solana_program::pubkey::Pubkey;
  12. use std::collections::BTreeMap;
  13. use std::ops::{Deref, DerefMut};
  14. /// Boxed container for a deserialized `account`. Use this to reference any
  15. /// account owned by the currently executing program.
  16. #[derive(Clone)]
  17. #[deprecated(since = "0.15.0", note = "Please use Account instead")]
  18. pub struct ProgramAccount<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  19. inner: Box<Inner<'info, T>>,
  20. }
  21. #[derive(Clone)]
  22. struct Inner<'info, T: AccountSerialize + AccountDeserialize + Clone> {
  23. info: AccountInfo<'info>,
  24. account: T,
  25. }
  26. #[allow(deprecated)]
  27. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> ProgramAccount<'a, T> {
  28. fn new(info: AccountInfo<'a>, account: T) -> ProgramAccount<'a, T> {
  29. Self {
  30. inner: Box::new(Inner { info, account }),
  31. }
  32. }
  33. /// Deserializes the given `info` into a `ProgramAccount`.
  34. #[inline(never)]
  35. pub fn try_from(program_id: &Pubkey, info: &AccountInfo<'a>) -> Result<ProgramAccount<'a, T>> {
  36. if info.owner != program_id {
  37. return Err(Error::from(ErrorCode::AccountOwnedByWrongProgram)
  38. .with_pubkeys((*info.owner, *program_id)));
  39. }
  40. let mut data: &[u8] = &info.try_borrow_data()?;
  41. Ok(ProgramAccount::new(
  42. info.clone(),
  43. T::try_deserialize(&mut data)?,
  44. ))
  45. }
  46. /// Deserializes the given `info` into a `ProgramAccount` without checking
  47. /// the account discriminator. Be careful when using this and avoid it if
  48. /// possible.
  49. #[inline(never)]
  50. pub fn try_from_unchecked(
  51. program_id: &Pubkey,
  52. info: &AccountInfo<'a>,
  53. ) -> Result<ProgramAccount<'a, T>> {
  54. if info.owner != program_id {
  55. return Err(Error::from(ErrorCode::AccountOwnedByWrongProgram)
  56. .with_pubkeys((*info.owner, *program_id)));
  57. }
  58. let mut data: &[u8] = &info.try_borrow_data()?;
  59. Ok(ProgramAccount::new(
  60. info.clone(),
  61. T::try_deserialize_unchecked(&mut data)?,
  62. ))
  63. }
  64. pub fn into_inner(self) -> T {
  65. self.inner.account
  66. }
  67. }
  68. #[allow(deprecated)]
  69. impl<'info, T> Accounts<'info> for ProgramAccount<'info, T>
  70. where
  71. T: AccountSerialize + AccountDeserialize + Clone,
  72. {
  73. #[inline(never)]
  74. fn try_accounts(
  75. program_id: &Pubkey,
  76. accounts: &mut &[AccountInfo<'info>],
  77. _ix_data: &[u8],
  78. _bumps: &mut BTreeMap<String, u8>,
  79. ) -> Result<Self> {
  80. if accounts.is_empty() {
  81. return Err(ErrorCode::AccountNotEnoughKeys.into());
  82. }
  83. let account = &accounts[0];
  84. *accounts = &accounts[1..];
  85. ProgramAccount::try_from(program_id, account)
  86. }
  87. }
  88. #[allow(deprecated)]
  89. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsExit<'info>
  90. for ProgramAccount<'info, T>
  91. {
  92. fn exit(&self, _program_id: &Pubkey) -> Result<()> {
  93. let info = self.to_account_info();
  94. let mut data = info.try_borrow_mut_data()?;
  95. let dst: &mut [u8] = &mut data;
  96. let mut writer = BpfWriter::new(dst);
  97. self.inner.account.try_serialize(&mut writer)?;
  98. Ok(())
  99. }
  100. }
  101. /// This function is for INTERNAL USE ONLY.
  102. /// Do NOT use this function in a program.
  103. /// Manual closing of `ProgramAccount<'info, T>` types is NOT supported.
  104. ///
  105. /// Details: Using `close` with `ProgramAccount<'info, T>` is not safe because
  106. /// it requires the `mut` constraint but for that type the constraint
  107. /// overwrites the "closed account" discriminator at the end of the instruction.
  108. #[allow(deprecated)]
  109. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AccountsClose<'info>
  110. for ProgramAccount<'info, T>
  111. {
  112. fn close(&self, sol_destination: AccountInfo<'info>) -> Result<()> {
  113. crate::common::close(self.to_account_info(), sol_destination)
  114. }
  115. }
  116. #[allow(deprecated)]
  117. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountMetas
  118. for ProgramAccount<'info, T>
  119. {
  120. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  121. let is_signer = is_signer.unwrap_or(self.inner.info.is_signer);
  122. let meta = match self.inner.info.is_writable {
  123. false => AccountMeta::new_readonly(*self.inner.info.key, is_signer),
  124. true => AccountMeta::new(*self.inner.info.key, is_signer),
  125. };
  126. vec![meta]
  127. }
  128. }
  129. #[allow(deprecated)]
  130. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> ToAccountInfos<'info>
  131. for ProgramAccount<'info, T>
  132. {
  133. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  134. vec![self.inner.info.clone()]
  135. }
  136. }
  137. #[allow(deprecated)]
  138. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> AsRef<AccountInfo<'info>>
  139. for ProgramAccount<'info, T>
  140. {
  141. fn as_ref(&self) -> &AccountInfo<'info> {
  142. &self.inner.info
  143. }
  144. }
  145. #[allow(deprecated)]
  146. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Deref for ProgramAccount<'a, T> {
  147. type Target = T;
  148. fn deref(&self) -> &Self::Target {
  149. &(*self.inner).account
  150. }
  151. }
  152. #[allow(deprecated)]
  153. impl<'a, T: AccountSerialize + AccountDeserialize + Clone> DerefMut for ProgramAccount<'a, T> {
  154. fn deref_mut(&mut self) -> &mut Self::Target {
  155. #[cfg(feature = "anchor-debug")]
  156. if !self.inner.info.is_writable {
  157. solana_program::msg!("The given ProgramAccount is not mutable");
  158. panic!();
  159. }
  160. &mut DerefMut::deref_mut(&mut self.inner).account
  161. }
  162. }
  163. #[allow(deprecated)]
  164. impl<'info, T> From<CpiAccount<'info, T>> for ProgramAccount<'info, T>
  165. where
  166. T: AccountSerialize + AccountDeserialize + Clone,
  167. {
  168. fn from(a: CpiAccount<'info, T>) -> Self {
  169. Self::new(a.to_account_info(), Deref::deref(&a).clone())
  170. }
  171. }
  172. #[allow(deprecated)]
  173. impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for ProgramAccount<'info, T> {
  174. fn key(&self) -> Pubkey {
  175. *self.inner.info.key
  176. }
  177. }