Skip to content

Commit

Permalink
Remove AccountInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen committed May 5, 2023
1 parent 625c72d commit 0341e29
Show file tree
Hide file tree
Showing 31 changed files with 742 additions and 723 deletions.
2 changes: 1 addition & 1 deletion docs/src/pages/docs/the-accounts-struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ You can find information about all constraints in the reference. We will cover s

## Safety checks

Two of the Anchor account types, [AccountInfo](https://docs.rs/anchor-lang/latest/anchor_lang/accounts/account_info/index.html) and [UncheckedAccount](https://docs.rs/anchor-lang/latest/anchor_lang/accounts/unchecked_account/index.html) do not implement any checks on the account being passed. Anchor implements safety checks that encourage additional documentation describing why additional checks are not necesssary.
One of the Anchor account types, [UncheckedAccount](https://docs.rs/anchor-lang/latest/anchor_lang/accounts/unchecked_account/index.html) does not implement any checks on the account being passed. Anchor implements safety checks that encourage additional documentation describing why additional checks are not necesssary.

Attempting to build a program containing the following excerpt with `anchor build`:

Expand Down
22 changes: 11 additions & 11 deletions lang/derive/accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ use syn::parse_macro_input;
/// Example:
/// <pre><code>
/// #[account(signer)]
/// pub authority: AccountInfo<'info>,
/// pub authority: UncheckedAccount<'info>,
/// #[account(signer @ MyError::MyErrorCode)]
/// pub payer: AccountInfo<'info>
/// pub payer: UncheckedAccount<'info>
/// </code></pre>
/// </td>
/// </tr>
Expand Down Expand Up @@ -173,13 +173,13 @@ use syn::parse_macro_input;
/// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init, payer = payer,
/// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;space = 8 + 8, owner = other_program.key()
/// &nbsp;&nbsp;&nbsp;&nbsp;)]
/// &nbsp;&nbsp;&nbsp;&nbsp;pub account_for_other_program: AccountInfo<'info>,
/// &nbsp;&nbsp;&nbsp;&nbsp;pub account_for_other_program: UncheckedAccount<'info>,
/// &nbsp;&nbsp;&nbsp;&nbsp;#[account(
/// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init, payer = payer, space = 8 + 8,
/// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner = other_program.key(),
/// &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seeds = [b"other_seed"], bump
/// &nbsp;&nbsp;&nbsp;&nbsp;)]
/// &nbsp;&nbsp;&nbsp;&nbsp;pub pda_for_other_program: AccountInfo<'info>,
/// &nbsp;&nbsp;&nbsp;&nbsp;pub pda_for_other_program: UncheckedAccount<'info>,
/// &nbsp;&nbsp;&nbsp;&nbsp;#[account(mut)]
/// &nbsp;&nbsp;&nbsp;&nbsp;pub payer: Signer<'info>,
/// &nbsp;&nbsp;&nbsp;&nbsp;pub system_program: Program<'info, System>,
Expand Down Expand Up @@ -254,21 +254,21 @@ use syn::parse_macro_input;
/// #[instruction(first_bump: u8, second_bump: u8)]
/// pub struct Example {
/// #[account(seeds = [b"example_seed"], bump)]
/// pub canonical_pda: AccountInfo<'info>,
/// pub canonical_pda: UncheckedAccount<'info>,
/// #[account(
/// seeds = [b"example_seed"],
/// bump,
/// seeds::program = other_program.key()
/// )]
/// pub canonical_pda_two: AccountInfo<'info>,
/// pub canonical_pda_two: UncheckedAccount<'info>,
/// #[account(seeds = [b"other_seed"], bump = first_bump)]
/// pub arbitrary_pda: AccountInfo<'info>
/// pub arbitrary_pda: UncheckedAccount<'info>
/// #[account(
/// seeds = [b"other_seed"],
/// bump = second_bump,
/// seeds::program = other_program.key()
/// )]
/// pub arbitrary_pda_two: AccountInfo<'info>,
/// pub arbitrary_pda_two: UncheckedAccount<'info>,
/// pub other_program: Program<'info, OtherProgram>
/// }
/// </code></pre>
Expand Down Expand Up @@ -337,7 +337,7 @@ use syn::parse_macro_input;
/// Example:
/// <pre><code>
/// #[account(executable)]
/// pub my_program: AccountInfo<'info>
/// pub my_program: UncheckedAccount<'info>
/// </code></pre>
/// </td>
/// </tr>
Expand All @@ -356,7 +356,7 @@ use syn::parse_macro_input;
/// #[account(zero, rent_exempt = skip)]
/// pub skipped_account: Account<'info, MyData>,
/// #[account(rent_exempt = enforce)]
/// pub enforced_account: AccountInfo<'info>
/// pub enforced_account: UncheckedAccount<'info>
/// </code></pre>
/// </td>
/// </tr>
Expand Down Expand Up @@ -629,7 +629,7 @@ use syn::parse_macro_input;
/// </tr>
/// <tbody>
/// </table>
#[proc_macro_derive(Accounts, attributes(account, instruction, only_cpi))]
#[proc_macro_derive(Accounts, attributes(account, instruction))]
pub fn derive_anchor_deserialize(item: TokenStream) -> TokenStream {
parse_macro_input!(item as anchor_syn::AccountsStruct)
.to_token_stream()
Expand Down
9 changes: 9 additions & 0 deletions lang/src/accounts/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::bpf_writer::BpfWriter;
use crate::error::{Error, ErrorCode};
use crate::prelude::UncheckedAccount;
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, Key, Owner,
Result, ToAccountInfo, ToAccountInfos, ToAccountMetas,
Expand Down Expand Up @@ -429,3 +430,11 @@ impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for Account<'i
*self.info.key
}
}

impl<'info, T: AccountSerialize + AccountDeserialize + Clone> From<Account<'info, T>>
for UncheckedAccount<'info>
{
fn from(value: Account<'info, T>) -> Self {
UncheckedAccount::try_from(value.info)
}
}
29 changes: 2 additions & 27 deletions lang/src/accounts/account_info.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
//! AccountInfo can be used as a type but
//! [Unchecked Account](crate::accounts::unchecked_account::UncheckedAccount)
//! should be used instead.

use crate::{AccountsExit, Key, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;

impl<'info> ToAccountMetas for AccountInfo<'info> {
fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
let is_signer = is_signer.unwrap_or(self.is_signer);
let meta = match self.is_writable {
false => AccountMeta::new_readonly(*self.key, is_signer),
true => AccountMeta::new(*self.key, is_signer),
};
vec![meta]
}
}

impl<'info> ToAccountInfos<'info> for AccountInfo<'info> {
fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
vec![self.clone()]
}
}

impl<'info> AccountsExit<'info> for AccountInfo<'info> {}
use crate::Key;
use solana_program::{account_info::AccountInfo, pubkey::Pubkey};

impl<'info> Key for AccountInfo<'info> {
fn key(&self) -> Pubkey {
Expand Down
9 changes: 8 additions & 1 deletion lang/src/accounts/account_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::bpf_writer::BpfWriter;
use crate::error::{Error, ErrorCode};
use crate::prelude::UncheckedAccount;
use crate::{
Accounts, AccountsClose, AccountsExit, Key, Owner, Result, ToAccountInfo, ToAccountInfos,
ToAccountMetas, ZeroCopy,
Expand Down Expand Up @@ -80,7 +81,7 @@ use std::ops::DerefMut;
/// bar: AccountLoader<'info, Bar>,
/// #[account(mut)]
/// authority: Signer<'info>,
/// system_program: AccountInfo<'info>,
/// system_program: UncheckedAccount<'info>,
/// }
///
/// #[derive(Accounts)]
Expand Down Expand Up @@ -281,3 +282,9 @@ impl<'info, T: ZeroCopy + Owner> Key for AccountLoader<'info, T> {
*self.acc_info.key
}
}

impl<'info, T: ZeroCopy + Owner> From<AccountLoader<'info, T>> for UncheckedAccount<'info> {
fn from(value: AccountLoader<'info, T>) -> Self {
UncheckedAccount::try_from(value.acc_info)
}
}
7 changes: 7 additions & 0 deletions lang/src/accounts/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//! }
//! ```

use crate::prelude::UncheckedAccount;
use crate::{Accounts, AccountsClose, AccountsExit, Result, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
Expand Down Expand Up @@ -55,3 +56,9 @@ impl<'info, T: AccountsClose<'info>> AccountsClose<'info> for Box<T> {
T::close(self, sol_destination)
}
}

impl<'info, T: Into<UncheckedAccount<'info>>> From<Box<T>> for UncheckedAccount<'info> {
fn from(value: Box<T>) -> Self {
value.into()
}
}
7 changes: 7 additions & 0 deletions lang/src/accounts/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::accounts::program::Program;
use crate::error::{Error, ErrorCode};
use crate::prelude::UncheckedAccount;
use crate::{
AccountDeserialize, Accounts, AccountsExit, CheckId, Key, Result, ToAccountInfos,
ToAccountMetas,
Expand Down Expand Up @@ -142,3 +143,9 @@ impl<'info, T: AccountDeserialize> Key for Interface<'info, T> {
self.0.key()
}
}

impl<'info, T> From<Interface<'info, T>> for UncheckedAccount<'info> {
fn from(value: Interface<'info, T>) -> Self {
value.0.into()
}
}
9 changes: 9 additions & 0 deletions lang/src/accounts/interface_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::accounts::account::Account;
use crate::error::ErrorCode;
use crate::prelude::UncheckedAccount;
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, CheckOwner, Key,
Owners, Result, ToAccountInfos, ToAccountMetas,
Expand Down Expand Up @@ -329,3 +330,11 @@ impl<'info, T: AccountSerialize + AccountDeserialize + Clone> Key for InterfaceA
self.account.key()
}
}

impl<'info, T: AccountSerialize + AccountDeserialize + Clone> From<InterfaceAccount<'info, T>>
for UncheckedAccount<'info>
{
fn from(value: InterfaceAccount<'info, T>) -> Self {
value.account.into()
}
}
1 change: 1 addition & 0 deletions lang/src/accounts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Account types that can be used in the account validation struct.

pub mod account;
#[doc(hidden)]
pub mod account_info;
pub mod account_loader;
pub mod boxed;
Expand Down
7 changes: 7 additions & 0 deletions lang/src/accounts/program.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Type validating that the account is the given Program

use crate::error::{Error, ErrorCode};
use crate::prelude::UncheckedAccount;
use crate::{
AccountDeserialize, Accounts, AccountsExit, Id, Key, Result, ToAccountInfos, ToAccountMetas,
};
Expand Down Expand Up @@ -196,3 +197,9 @@ impl<'info, T: AccountDeserialize> Key for Program<'info, T> {
*self.info.key
}
}

impl<'info, T> From<Program<'info, T>> for UncheckedAccount<'info> {
fn from(value: Program<'info, T>) -> Self {
UncheckedAccount::try_from(value.info)
}
}
7 changes: 7 additions & 0 deletions lang/src/accounts/signer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Type validating that the account signed the transaction
use crate::error::ErrorCode;
use crate::prelude::UncheckedAccount;
use crate::{Accounts, AccountsExit, Key, Result, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
Expand Down Expand Up @@ -110,3 +111,9 @@ impl<'info> Key for Signer<'info> {
*self.info.key
}
}

impl<'info> From<Signer<'info>> for UncheckedAccount<'info> {
fn from(value: Signer<'info>) -> Self {
UncheckedAccount::try_from(value.info)
}
}
7 changes: 7 additions & 0 deletions lang/src/accounts/system_account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Type validating that the account is owned by the system program

use crate::error::ErrorCode;
use crate::prelude::UncheckedAccount;
use crate::*;
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
Expand Down Expand Up @@ -89,3 +90,9 @@ impl<'info> Key for SystemAccount<'info> {
*self.info.key
}
}

impl<'info> From<SystemAccount<'info>> for UncheckedAccount<'info> {
fn from(value: SystemAccount<'info>) -> Self {
UncheckedAccount::try_from(value.info)
}
}
7 changes: 7 additions & 0 deletions lang/src/accounts/sysvar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Type validating that the account is a sysvar and deserializing it

use crate::error::ErrorCode;
use crate::prelude::UncheckedAccount;
use crate::{Accounts, AccountsExit, Key, Result, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
Expand Down Expand Up @@ -121,3 +122,9 @@ impl<'info, T: solana_program::sysvar::Sysvar> Key for Sysvar<'info, T> {
*self.info.key
}
}

impl<'info, T: solana_program::sysvar::Sysvar> From<Sysvar<'info, T>> for UncheckedAccount<'info> {
fn from(value: Sysvar<'info, T>) -> Self {
UncheckedAccount::try_from(value.info)
}
}
6 changes: 6 additions & 0 deletions lang/src/accounts/unchecked_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,9 @@ impl<'info> Key for UncheckedAccount<'info> {
*self.0.key
}
}

impl<'info> From<AccountInfo<'info>> for UncheckedAccount<'info> {
fn from(value: AccountInfo<'info>) -> Self {
UncheckedAccount::try_from(value)
}
}
Loading

0 comments on commit 0341e29

Please sign in to comment.