123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- //! Type facilitating on demand zero copy deserialization.
- use crate::error::ErrorCode;
- use crate::{
- Accounts, AccountsClose, AccountsExit, Bump, Owner, ToAccountInfo, ToAccountInfos,
- ToAccountMetas, ZeroCopy,
- };
- use arrayref::array_ref;
- use solana_program::account_info::AccountInfo;
- use solana_program::entrypoint::ProgramResult;
- use solana_program::instruction::AccountMeta;
- use solana_program::program_error::ProgramError;
- use solana_program::pubkey::Pubkey;
- use std::cell::{Ref, RefMut};
- use std::fmt;
- use std::marker::PhantomData;
- use std::mem;
- use std::ops::DerefMut;
- /// Type facilitating on demand zero copy deserialization.
- ///
- /// Note that using accounts in this way is distinctly different from using,
- /// for example, the [`Account`](./struct.Account.html). Namely,
- /// one must call
- /// - `load` when the account is not mutable
- /// - `load_mut` when the account is mutable
- ///
- /// For more details on zero-copy-deserialization, see the
- /// [`account`](./attr.account.html) attribute.
- /// <p style=";padding:0.75em;border: 1px solid #ee6868">
- /// <strong>⚠️ </strong> When using this type it's important to be mindful
- /// of any calls to the <code>load</code> functions so as not to
- /// induce a <code>RefCell</code> panic, especially when sharing accounts across CPI
- /// boundaries. When in doubt, one should make sure all refs resulting from
- /// a call to a <code>load</code> function are dropped before CPI.
- /// This can be done explicitly by calling <code>drop(my_var)</code> or implicitly
- /// by wrapping the code using the <code>Ref</code> in braces <code>{..}</code> or
- /// moving it into its own function.
- /// </p>
- ///
- /// # Example
- /// ```ignore
- /// use anchor_lang::prelude::*;
- ///
- /// declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
- ///
- /// #[program]
- /// pub mod bar {
- /// use super::*;
- ///
- /// pub fn create_bar(ctx: Context<CreateBar>, data: u64) -> ProgramResult {
- /// let bar = &mut ctx.accounts.bar.load_init()?;
- /// bar.authority = ctx.accounts.authority.key();
- /// bar.data = data;
- /// Ok(())
- /// }
- ///
- /// pub fn update_bar(ctx: Context<UpdateBar>, data: u64) -> ProgramResult {
- /// (*ctx.accounts.bar.load_mut()?).data = data;
- /// Ok(())
- /// }
- /// }
- ///
- /// #[account(zero_copy)]
- /// #[derive(Default)]
- /// pub struct Bar {
- /// authority: Pubkey,
- /// data: u64
- /// }
- ///
- /// #[derive(Accounts)]
- /// pub struct CreateBar<'info> {
- /// #[account(
- /// init,
- /// payer = authority
- /// )]
- /// bar: AccountLoader<'info, Bar>,
- /// #[account(mut)]
- /// authority: Signer<'info>,
- /// system_program: AccountInfo<'info>,
- /// }
- ///
- /// #[derive(Accounts)]
- /// pub struct UpdateBar<'info> {
- /// #[account(
- /// mut,
- /// has_one = authority,
- /// )]
- /// pub bar: AccountLoader<'info, Bar>,
- /// pub authority: Signer<'info>,
- /// }
- /// ```
- #[derive(Clone)]
- pub struct AccountLoader<'info, T: ZeroCopy + Owner> {
- acc_info: AccountInfo<'info>,
- phantom: PhantomData<&'info T>,
- }
- impl<'info, T: ZeroCopy + Owner + fmt::Debug> fmt::Debug for AccountLoader<'info, T> {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- f.debug_struct("AccountLoader")
- .field("acc_info", &self.acc_info)
- .field("phantom", &self.phantom)
- .finish()
- }
- }
- impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
- fn new(acc_info: AccountInfo<'info>) -> AccountLoader<'info, T> {
- Self {
- acc_info,
- phantom: PhantomData,
- }
- }
- /// Constructs a new `Loader` from a previously initialized account.
- #[inline(never)]
- pub fn try_from(
- acc_info: &AccountInfo<'info>,
- ) -> Result<AccountLoader<'info, T>, ProgramError> {
- if acc_info.owner != &T::owner() {
- return Err(ErrorCode::AccountOwnedByWrongProgram.into());
- }
- let data: &[u8] = &acc_info.try_borrow_data()?;
- // Discriminator must match.
- #[cfg(feature = "deprecated-layout")]
- let disc_bytes = array_ref![data, 0, 8];
- #[cfg(not(feature = "deprecated-layout"))]
- let disc_bytes = array_ref![data, 2, 4];
- if disc_bytes != &T::discriminator() {
- return Err(ErrorCode::AccountDiscriminatorMismatch.into());
- }
- Ok(AccountLoader::new(acc_info.clone()))
- }
- /// Constructs a new `Loader` from an uninitialized account.
- #[inline(never)]
- pub fn try_from_unchecked(
- _program_id: &Pubkey,
- acc_info: &AccountInfo<'info>,
- ) -> Result<AccountLoader<'info, T>, ProgramError> {
- if acc_info.owner != &T::owner() {
- return Err(ErrorCode::AccountOwnedByWrongProgram.into());
- }
- Ok(AccountLoader::new(acc_info.clone()))
- }
- /// Returns a Ref to the account data structure for reading.
- pub fn load(&self) -> Result<Ref<T>, ProgramError> {
- let data = self.acc_info.try_borrow_data()?;
- #[cfg(feature = "deprecated-layout")]
- let disc_bytes = array_ref![data, 0, 8];
- #[cfg(not(feature = "deprecated-layout"))]
- let disc_bytes = array_ref![data, 2, 4];
- if disc_bytes != &T::discriminator() {
- return Err(ErrorCode::AccountDiscriminatorMismatch.into());
- }
- Ok(Ref::map(data, |data| {
- bytemuck::from_bytes(&data[8..mem::size_of::<T>() + 8])
- }))
- }
- /// Returns a `RefMut` to the account data structure for reading or writing.
- pub fn load_mut(&self) -> Result<RefMut<T>, ProgramError> {
- // AccountInfo api allows you to borrow mut even if the account isn't
- // writable, so add this check for a better dev experience.
- if !self.acc_info.is_writable {
- return Err(ErrorCode::AccountNotMutable.into());
- }
- let data = self.acc_info.try_borrow_mut_data()?;
- #[cfg(feature = "deprecated-layout")]
- let disc_bytes = array_ref![data, 0, 8];
- #[cfg(not(feature = "deprecated-layout"))]
- let disc_bytes = array_ref![data, 2, 4];
- if disc_bytes != &T::discriminator() {
- return Err(ErrorCode::AccountDiscriminatorMismatch.into());
- }
- Ok(RefMut::map(data, |data| {
- bytemuck::from_bytes_mut(&mut data.deref_mut()[8..mem::size_of::<T>() + 8])
- }))
- }
- }
- impl<'info, T: ZeroCopy + Owner> Accounts<'info> for AccountLoader<'info, T> {
- #[inline(never)]
- fn try_accounts(
- _program_id: &Pubkey,
- accounts: &mut &[AccountInfo<'info>],
- _ix_data: &[u8],
- ) -> Result<Self, ProgramError> {
- if accounts.is_empty() {
- return Err(ErrorCode::AccountNotEnoughKeys.into());
- }
- let account = &accounts[0];
- *accounts = &accounts[1..];
- let l = AccountLoader::try_from(account)?;
- Ok(l)
- }
- }
- impl<'info, T: ZeroCopy + Owner> AccountsExit<'info> for AccountLoader<'info, T> {
- // The account *cannot* be loaded when this is called.
- fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
- // No-op.
- Ok(())
- }
- }
- impl<'info, T: ZeroCopy + Owner> AccountsClose<'info> for AccountLoader<'info, T> {
- fn close(&self, sol_destination: AccountInfo<'info>) -> ProgramResult {
- crate::common::close(self.to_account_info(), sol_destination)
- }
- }
- impl<'info, T: ZeroCopy + Owner> ToAccountMetas for AccountLoader<'info, T> {
- fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
- let is_signer = is_signer.unwrap_or(self.acc_info.is_signer);
- let meta = match self.acc_info.is_writable {
- false => AccountMeta::new_readonly(*self.acc_info.key, is_signer),
- true => AccountMeta::new(*self.acc_info.key, is_signer),
- };
- vec![meta]
- }
- }
- impl<'info, T: ZeroCopy + Owner> AsRef<AccountInfo<'info>> for AccountLoader<'info, T> {
- fn as_ref(&self) -> &AccountInfo<'info> {
- &self.acc_info
- }
- }
- impl<'info, T: ZeroCopy + Owner> ToAccountInfos<'info> for AccountLoader<'info, T> {
- fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
- vec![self.acc_info.clone()]
- }
- }
- #[cfg(not(feature = "deprecated-layout"))]
- impl<'info, T> Bump for T
- where
- T: AsRef<AccountInfo<'info>>,
- {
- fn seed(&self) -> u8 {
- self.as_ref().data.borrow()[1]
- }
- }
|