Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: Remove AccountInfo from Context #2470

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The minor version will be incremented upon a breaking change and the patch versi

### Breaking

- lang: Remove the use of `AccountInfo` in the context of the accounts. Use `UncheckedAccount` instead ([#2470](https://github.com/coral-xyz/anchor/pull/2470)).
- lang: Identifiers that are intended for internal usage(`program_id`, `accounts`, `ix_data`, `remaining_accounts`) have been renamed with `__` prefix ([#2464](https://github.com/coral-xyz/anchor/pull/2464))

## [0.27.0] - 2023-03-08
Expand Down
8 changes: 0 additions & 8 deletions bench/COMPUTE_UNITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ The programs and their tests are located in [/tests/bench](https://github.com/co

| Instruction | Compute Units | +/- |
| --------------------------- | ------------- | --- |
| accountInfo1 | 954 | - |
| accountInfo2 | 1567 | - |
| accountInfo4 | 2059 | - |
| accountInfo8 | 3856 | - |
| accountEmptyInit1 | 5958 | - |
| accountEmpty1 | 1090 | - |
| accountEmptyInit2 | 10583 | - |
Expand Down Expand Up @@ -109,10 +105,6 @@ The programs and their tests are located in [/tests/bench](https://github.com/co

| Instruction | Compute Units | +/- |
| --------------------------- | ------------- | --- |
| accountInfo1 | 954 | N/A |
| accountInfo2 | 1567 | N/A |
| accountInfo4 | 2059 | N/A |
| accountInfo8 | 3856 | N/A |
| accountEmptyInit1 | 5958 | N/A |
| accountEmpty1 | 1090 | N/A |
| accountEmptyInit2 | 10574 | N/A |
Expand Down
2 changes: 1 addition & 1 deletion lang/derive/accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ use syn::parse_macro_input;
/// </tr>
/// <tbody>
/// </table>
#[proc_macro_derive(Accounts, attributes(account, instruction))]
#[proc_macro_derive(Accounts, attributes(account, instruction, only_cpi))]
Aursen marked this conversation as resolved.
Show resolved Hide resolved
pub fn derive_anchor_deserialize(item: TokenStream) -> TokenStream {
parse_macro_input!(item as anchor_syn::AccountsStruct)
.to_token_stream()
Expand Down
21 changes: 1 addition & 20 deletions lang/src/accounts/account_info.rs
Aursen marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,10 @@
//! [Unchecked Account](crate::accounts::unchecked_account::UncheckedAccount)
//! should be used instead.

use crate::error::ErrorCode;
use crate::{Accounts, AccountsExit, Key, Result, ToAccountInfos, ToAccountMetas};
use crate::{AccountsExit, Key, ToAccountInfos, ToAccountMetas};
use solana_program::account_info::AccountInfo;
use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;
use std::collections::{BTreeMap, BTreeSet};

impl<'info> Accounts<'info> for AccountInfo<'info> {
fn try_accounts(
_program_id: &Pubkey,
accounts: &mut &[AccountInfo<'info>],
_ix_data: &[u8],
_bumps: &mut BTreeMap<String, u8>,
_reallocs: &mut BTreeSet<Pubkey>,
) -> Result<Self> {
if accounts.is_empty() {
return Err(ErrorCode::AccountNotEnoughKeys.into());
}
let account = &accounts[0];
*accounts = &accounts[1..];
Ok(account.clone())
}
}

impl<'info> ToAccountMetas for AccountInfo<'info> {
fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
Expand Down
13 changes: 13 additions & 0 deletions lang/src/system_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn advance_nonce_account<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct AdvanceNonceAccount<'info> {
pub nonce: AccountInfo<'info>,
pub authorized: AccountInfo<'info>,
Expand All @@ -55,6 +56,7 @@ pub fn allocate<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct Allocate<'info> {
pub account_to_allocate: AccountInfo<'info>,
}
Expand All @@ -81,6 +83,7 @@ pub fn allocate_with_seed<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct AllocateWithSeed<'info> {
pub account_to_allocate: AccountInfo<'info>,
pub base: AccountInfo<'info>,
Expand All @@ -103,6 +106,7 @@ pub fn assign<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct Assign<'info> {
pub account_to_assign: AccountInfo<'info>,
}
Expand All @@ -127,6 +131,7 @@ pub fn assign_with_seed<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct AssignWithSeed<'info> {
pub account_to_assign: AccountInfo<'info>,
pub base: AccountInfo<'info>,
Expand All @@ -150,6 +155,7 @@ pub fn authorize_nonce_account<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct AuthorizeNonceAccount<'info> {
pub nonce: AccountInfo<'info>,
pub authorized: AccountInfo<'info>,
Expand Down Expand Up @@ -177,6 +183,7 @@ pub fn create_account<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct CreateAccount<'info> {
pub from: AccountInfo<'info>,
pub to: AccountInfo<'info>,
Expand Down Expand Up @@ -207,6 +214,7 @@ pub fn create_account_with_seed<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct CreateAccountWithSeed<'info> {
pub from: AccountInfo<'info>,
pub to: AccountInfo<'info>,
Expand Down Expand Up @@ -243,6 +251,7 @@ pub fn create_nonce_account<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct CreateNonceAccount<'info> {
pub from: AccountInfo<'info>,
pub nonce: AccountInfo<'info>,
Expand Down Expand Up @@ -287,6 +296,7 @@ pub fn create_nonce_account_with_seed<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct CreateNonceAccountWithSeed<'info> {
pub from: AccountInfo<'info>,
pub nonce: AccountInfo<'info>,
Expand All @@ -313,6 +323,7 @@ pub fn transfer<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct Transfer<'info> {
pub from: AccountInfo<'info>,
pub to: AccountInfo<'info>,
Expand Down Expand Up @@ -341,6 +352,7 @@ pub fn transfer_with_seed<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct TransferWithSeed<'info> {
pub from: AccountInfo<'info>,
pub base: AccountInfo<'info>,
Expand Down Expand Up @@ -372,6 +384,7 @@ pub fn withdraw_nonce_account<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct WithdrawNonceAccount<'info> {
pub nonce: AccountInfo<'info>,
pub to: AccountInfo<'info>,
Expand Down
4 changes: 3 additions & 1 deletion lang/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ mod tests {
use solana_program::clock::Epoch;
use solana_program::pubkey::Pubkey;

use crate::{prelude::UncheckedAccount, ToAccountInfo};

use super::*;

#[derive(Accounts)]
pub struct Test<'info> {
#[account(signer)]
test: AccountInfo<'info>,
test: UncheckedAccount<'info>,
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pub fn generate_constraint_has_one(
pub fn generate_constraint_signer(f: &Field, c: &ConstraintSigner) -> proc_macro2::TokenStream {
let ident = &f.ident;
let info = match f.ty {
Ty::AccountInfo => quote! { #ident },
Ty::UncheckedAccount => quote! { #ident.to_account_info() },
Ty::Account(_) => quote! { #ident.to_account_info() },
Ty::InterfaceAccount(_) => quote! { #ident.to_account_info() },
Ty::AccountLoader(_) => quote! { #ident.to_account_info() },
Expand Down
21 changes: 14 additions & 7 deletions lang/syn/src/codegen/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
let __client_accounts_mod = __client_accounts::generate(accs);
let __cpi_client_accounts_mod = __cpi_client_accounts::generate(accs);

quote! {
#impl_try_accounts
#impl_to_account_infos
#impl_to_account_metas
#impl_exit
if accs.only_cpi {
Aursen marked this conversation as resolved.
Show resolved Hide resolved
quote! {
#impl_to_account_infos
#impl_to_account_metas
}
} else {
quote! {
#impl_try_accounts
#impl_to_account_infos
#impl_to_account_metas
#impl_exit

#__client_accounts_mod
#__cpi_client_accounts_mod
#__client_accounts_mod
#__cpi_client_accounts_mod
}
}
}

Expand Down
20 changes: 10 additions & 10 deletions lang/syn/src/codegen/program/idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
pub struct IdlCreateAccounts<'info> {
// Payer of the transaction.
#[account(signer)]
pub from: AccountInfo<'info>,
pub from: UncheckedAccount<'info>,
// The deterministically defined "state" account being created via
// `create_account_with_seed`.
#[account(mut)]
pub to: AccountInfo<'info>,
pub to: UncheckedAccount<'info>,
// The program-derived-address signing off on the account creation.
// Seeds = &[] + bump seed.
#[account(seeds = [], bump)]
pub base: AccountInfo<'info>,
pub base: UncheckedAccount<'info>,
// The system program.
pub system_program: Program<'info, System>,
// The program whose state is being constructed.
#[account(executable)]
pub program: AccountInfo<'info>,
pub program: UncheckedAccount<'info>,
}

// Accounts for Idl instructions.
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
#[account(constraint = authority.key != &ERASED_AUTHORITY)]
pub authority: Signer<'info>,
#[account(mut)]
pub sol_destination: AccountInfo<'info>,
pub sol_destination: UncheckedAccount<'info>,
}


Expand Down Expand Up @@ -163,10 +163,10 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
anchor_lang::solana_program::program::invoke_signed(
&ix,
&[
accounts.from.clone(),
accounts.to.clone(),
accounts.base.clone(),
accounts.system_program.to_account_info().clone(),
accounts.from.to_account_info(),
accounts.to.to_account_info(),
accounts.base.to_account_info(),
accounts.system_program.to_account_info(),
],
&[seeds],
)?;
Expand Down Expand Up @@ -225,7 +225,7 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
accounts.system_program.to_account_info(),
anchor_lang::system_program::Transfer {
from: accounts.authority.to_account_info(),
to: accounts.idl.to_account_info().clone(),
to: accounts.idl.to_account_info(),
},
),
new_rent_minimum
Expand Down
4 changes: 4 additions & 0 deletions lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub struct AccountsStruct {
pub fields: Vec<AccountField>,
// Instruction data api expression.
instruction_api: Option<Punctuated<Expr, Comma>>,
// Used internally to limit the codegen
only_cpi: bool,
}

impl Parse for AccountsStruct {
Expand All @@ -119,6 +121,7 @@ impl AccountsStruct {
strct: ItemStruct,
fields: Vec<AccountField>,
instruction_api: Option<Punctuated<Expr, Comma>>,
only_cpi: bool,
) -> Self {
let ident = strct.ident.clone();
let generics = strct.generics;
Expand All @@ -127,6 +130,7 @@ impl AccountsStruct {
generics,
fields,
instruction_api,
only_cpi,
}
}

Expand Down
27 changes: 26 additions & 1 deletion lang/syn/src/parser/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn parse(strct: &syn::ItemStruct) -> ParseResult<AccountsStruct> {
})
.map(|ix_attr| ix_attr.parse_args_with(Punctuated::<Expr, Comma>::parse_terminated))
.transpose()?;
let only_cpi = strct.attrs.iter().any(|a| a.path.is_ident("only_cpi"));
let fields = match &strct.fields {
syn::Fields::Named(fields) => fields
.named
Expand All @@ -34,9 +35,33 @@ pub fn parse(strct: &syn::ItemStruct) -> ParseResult<AccountsStruct> {
}
};

if !only_cpi {
prevent_account_info(&fields)?;
}

constraints_cross_checks(&fields)?;

Ok(AccountsStruct::new(strct.clone(), fields, instruction_api))
Ok(AccountsStruct::new(
strct.clone(),
fields,
instruction_api,
only_cpi,
))
}

fn prevent_account_info(fields: &[AccountField]) -> ParseResult<()> {
let field = fields.iter().find(|f| match f {
AccountField::Field(acc_f) => acc_f.ty == Ty::AccountInfo,
_ => false,
});

match field {
Some(f) => Err(ParseError::new(
f.ident().span(),
"AccountInfo can no longer be used in the context. Please use UncheckedAccount instead.",
)),
None => Ok(()),
}
}

fn constraints_cross_checks(fields: &[AccountField]) -> ParseResult<()> {
Expand Down
5 changes: 1 addition & 4 deletions lang/syn/src/parser/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ impl ParsedModule {
syn::Type::Path(syn::TypePath {
path: syn::Path { segments, .. },
..
}) => {
segments.len() == 1 && segments[0].ident == "UncheckedAccount"
|| segments[0].ident == "AccountInfo"
}
}) => segments.len() == 1 && segments[0].ident == "UncheckedAccount",
_ => false,
})
}
Expand Down
2 changes: 1 addition & 1 deletion lang/tests/generics_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
T: AccountSerialize + AccountDeserialize + Owner + Clone,
U: BorshSerialize + BorshDeserialize + Default + Clone,
{
pub non_generic: AccountInfo<'info>,
pub non_generic: UncheckedAccount<'info>,
pub generic: Account<'info, T>,

pub const_generic: AccountLoader<'info, FooAccount<N>>,
Expand Down
1 change: 1 addition & 0 deletions spl/src/associated_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn create_idempotent<'info>(
}

#[derive(Accounts)]
#[only_cpi]
pub struct Create<'info> {
pub payer: AccountInfo<'info>,
pub associated_token: AccountInfo<'info>,
Expand Down
Loading