Skip to content

Commit

Permalink
lang: export accounts module, re-export its types in the prelude (#1208)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-schaaf committed Jan 6, 2022
1 parent bb20eee commit a830644
Show file tree
Hide file tree
Showing 24 changed files with 69 additions and 82 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -35,6 +35,8 @@ incremented for features.
* client: Client::new and Client::new_with_options now accept `Rc<dyn Signer>` instead of `Keypair` ([#975](https://github.com/project-serum/anchor/pull/975)).
* lang, ts: Change error enum name and message for 'wrong program ownership' account validation ([#1154](https://github.com/project-serum/anchor/pull/1154)).
* lang: Change from `#[repr(packed)]` to `#[repr(C)]` for zero copy accounts ([#1106](https://github.com/project-serum/anchor/pull/1106)).
* lang: Account types can now be found either in the `prelude` module or the `accounts` module but not longer directly under the root.
Deprecated account types are no longer imported by the prelude ([#1208](https://github.com/project-serum/anchor/pull/1208)).

## [0.19.0] - 2021-12-08

Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/access-control/src/lib.rs
Expand Up @@ -26,7 +26,7 @@ use syn::parse_macro_input;
/// #[derive(Accounts)]
/// pub struct Create {
/// #[account(init)]
/// my_account: ProgramAccount<'info, MyAccount>,
/// my_account: Account<'info, MyAccount>,
/// }
///
/// impl Create {
Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/interface/src/lib.rs
Expand Up @@ -198,7 +198,7 @@ pub fn interface(
format!("{:?}", sighash_arr).parse().unwrap();
quote! {
pub fn #method_name<'a,'b, 'c, 'info, T: anchor_lang::Accounts<'info> + anchor_lang::ToAccountMetas + anchor_lang::ToAccountInfos<'info>>(
ctx: anchor_lang::CpiContext<'a, 'b, 'c, 'info, T>,
ctx: anchor_lang::context::CpiContext<'a, 'b, 'c, 'info, T>,
#(#args),*
) -> anchor_lang::solana_program::entrypoint::ProgramResult {
#args_struct
Expand Down
4 changes: 3 additions & 1 deletion lang/src/accounts/account.rs
Expand Up @@ -26,7 +26,7 @@ use std::ops::{Deref, DerefMut};
/// - `Account.info.owner == T::owner()`
/// - `!(Account.info.owner == SystemProgram && Account.info.lamports() == 0)`
///
/// Example
/// # Example
/// ```ignore
/// use anchor_lang::prelude::*;
/// use other_program::Auth;
Expand Down Expand Up @@ -69,6 +69,8 @@ use std::ops::{Deref, DerefMut};
/// ...
/// ```
///
/// # Using Account with non-anchor programs
///
/// Account can also be used with non-anchor programs. The data types from
/// those programs are not annotated with `#[account]` so you have to
/// - create a wrapper type around the structs you want to wrap with Account
Expand Down
2 changes: 1 addition & 1 deletion lang/src/accounts/cpi_state.rs
@@ -1,6 +1,6 @@
use crate::error::ErrorCode;
#[allow(deprecated)]
use crate::{accounts::state::ProgramState, CpiStateContext};
use crate::{accounts::state::ProgramState, context::CpiStateContext};
use crate::{
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, Key, ToAccountInfo,
ToAccountInfos, ToAccountMetas,
Expand Down
2 changes: 1 addition & 1 deletion lang/src/accounts/loader.rs
Expand Up @@ -17,7 +17,7 @@ use std::ops::DerefMut;

/// Account loader facilitating on demand zero copy deserialization.
/// Note that using accounts in this way is distinctly different from using,
/// for example, the [`ProgramAccount`](./struct.ProgramAccount.html). Namely,
/// for example, the [`Account`](./struct.Account.html). Namely,
/// one must call `load`, `load_mut`, or `load_init`, before reading or writing
/// to the account. For more details on zero-copy-deserialization, see the
/// [`account`](./attr.account.html) attribute.
Expand Down
2 changes: 1 addition & 1 deletion lang/src/accounts/loader_account.rs
Expand Up @@ -17,7 +17,7 @@ use std::ops::DerefMut;

/// Account AccountLoader facilitating on demand zero copy deserialization.
/// Note that using accounts in this way is distinctly different from using,
/// for example, the [`ProgramAccount`](./struct.ProgramAccount.html). Namely,
/// for example, the [`Account`](./struct.Account.html). Namely,
/// one must call `load`, `load_mut`, or `load_init`, before reading or writing
/// to the account. For more details on zero-copy-deserialization, see the
/// [`account`](./attr.account.html) attribute.
Expand Down
2 changes: 2 additions & 0 deletions lang/src/accounts/mod.rs
@@ -1,3 +1,5 @@
//! Account types that can be used in the account validation struct.

pub mod account;
pub mod account_info;
pub mod boxed;
Expand Down
2 changes: 2 additions & 0 deletions lang/src/idl.rs
Expand Up @@ -17,6 +17,8 @@
//! Note that IDL account instructions are automatically inserted into all
//! Anchor programs. To remove them, one can use the `no-idl` feature.

#[allow(deprecated)]
use crate::accounts::program_account::ProgramAccount;
use crate::prelude::*;
use solana_program::pubkey::Pubkey;

Expand Down
61 changes: 17 additions & 44 deletions lang/src/lib.rs
Expand Up @@ -32,44 +32,19 @@ use solana_program::pubkey::Pubkey;
use std::io::Write;

mod account_meta;
mod accounts;
pub mod accounts;
mod bpf_upgradeable_state;
mod common;
mod context;
pub mod context;
mod ctor;
mod error;
#[doc(hidden)]
pub mod idl;
mod system_program;

pub use crate::accounts::account::Account;
#[doc(hidden)]
#[allow(deprecated)]
pub use crate::accounts::cpi_account::CpiAccount;
#[doc(hidden)]
#[allow(deprecated)]
pub use crate::accounts::cpi_state::CpiState;
#[allow(deprecated)]
pub use crate::accounts::loader::Loader;
pub use crate::accounts::loader_account::AccountLoader;
pub use crate::accounts::program::Program;
#[doc(hidden)]
#[allow(deprecated)]
pub use crate::accounts::program_account::ProgramAccount;
pub use crate::accounts::signer::Signer;
#[doc(hidden)]
#[allow(deprecated)]
pub use crate::accounts::state::ProgramState;
pub use crate::accounts::system_account::SystemAccount;
pub use crate::accounts::sysvar::Sysvar;
pub use crate::accounts::unchecked_account::UncheckedAccount;
pub use crate::system_program::System;
mod vec;
pub use crate::bpf_upgradeable_state::*;
#[doc(hidden)]
#[allow(deprecated)]
pub use crate::context::CpiStateContext;
pub use crate::context::{Context, CpiContext};
pub use anchor_attribute_access_control::access_control;
pub use anchor_attribute_account::{account, declare_id, zero_copy};
pub use anchor_attribute_constant::constant;
Expand Down Expand Up @@ -244,18 +219,15 @@ impl Key for Pubkey {
/// All programs should include it via `anchor_lang::prelude::*;`.
pub mod prelude {
pub use super::{
access_control, account, constant, declare_id, emit, error, event, interface, program,
access_control, account, accounts::account::Account,
accounts::loader_account::AccountLoader, accounts::program::Program,
accounts::signer::Signer, accounts::system_account::SystemAccount,
accounts::sysvar::Sysvar, accounts::unchecked_account::UncheckedAccount, constant,
context::Context, context::CpiContext, declare_id, emit, error, event, interface, program,
require, solana_program::bpf_loader_upgradeable::UpgradeableLoaderState, state, zero_copy,
Account, AccountDeserialize, AccountLoader, AccountSerialize, Accounts, AccountsExit,
AnchorDeserialize, AnchorSerialize, Context, CpiContext, Id, Key, Owner, Program,
ProgramData, Signer, System, SystemAccount, Sysvar, ToAccountInfo, ToAccountInfos,
ToAccountMetas, UncheckedAccount,
};

#[allow(deprecated)]
pub use super::{
accounts::cpi_account::CpiAccount, accounts::cpi_state::CpiState, accounts::loader::Loader,
accounts::program_account::ProgramAccount, accounts::state::ProgramState, CpiStateContext,
AccountDeserialize, AccountSerialize, Accounts, AccountsExit, AnchorDeserialize,
AnchorSerialize, Id, Key, Owner, ProgramData, System, ToAccountInfo, ToAccountInfos,
ToAccountMetas,
};

pub use borsh;
Expand All @@ -279,12 +251,18 @@ pub mod prelude {
pub use thiserror;
}

// Internal module used by macros and unstable apis.
/// Internal module used by macros and unstable apis.
pub mod __private {
// Modules with useful information for users
// don't use #[doc(hidden)] on these
pub use crate::error::ErrorCode;

/// The discriminator anchor uses to mark an account as closed.
pub const CLOSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [255, 255, 255, 255, 255, 255, 255, 255];

/// The starting point for user defined error codes.
pub const ERROR_CODE_OFFSET: u32 = 6000;

#[doc(hidden)]
pub use crate::ctor::Ctor;
#[doc(hidden)]
Expand All @@ -307,9 +285,6 @@ pub mod __private {
pub use crate::accounts::state::*;
}

/// The starting point for user defined error codes.
pub const ERROR_CODE_OFFSET: u32 = 6000;

// Calculates the size of an account, which may be larger than the deserialized
// data in it. This trait is currently only used for `#[state]` accounts.
#[doc(hidden)]
Expand All @@ -336,8 +311,6 @@ pub mod __private {

#[doc(hidden)]
pub use crate::accounts::state::PROGRAM_STATE_SEED;
#[doc(hidden)]
pub const CLOSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [255, 255, 255, 255, 255, 255, 255, 255];
}

/// Ensures a condition is true, otherwise returns the given error.
Expand Down
8 changes: 4 additions & 4 deletions lang/syn/src/codegen/accounts/constraints.rs
Expand Up @@ -447,7 +447,7 @@ pub fn generate_init(
authority: #owner.to_account_info(),
rent: rent.to_account_info(),
};
let cpi_ctx = CpiContext::new(cpi_program, accounts);
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
anchor_spl::token::initialize_account(cpi_ctx)?;
}

Expand Down Expand Up @@ -480,7 +480,7 @@ pub fn generate_init(
token_program: token_program.to_account_info(),
rent: rent.to_account_info(),
};
let cpi_ctx = CpiContext::new(cpi_program, cpi_accounts);
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, cpi_accounts);
anchor_spl::associated_token::create(cpi_ctx)?;
}
let pa: #ty_decl = #from_account_info;
Expand Down Expand Up @@ -530,7 +530,7 @@ pub fn generate_init(
mint: #field.to_account_info(),
rent: rent.to_account_info(),
};
let cpi_ctx = CpiContext::new(cpi_program, accounts);
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
anchor_spl::token::initialize_mint(cpi_ctx, #decimals, &#owner.key(), #freeze_authority)?;
}
let pa: #ty_decl = #from_account_info;
Expand Down Expand Up @@ -735,7 +735,7 @@ pub fn generate_constraint_state(f: &Field, c: &ConstraintState) -> proc_macro2:
quote! {
// Checks the given state account is the canonical state account for
// the target program.
if #ident.key() != anchor_lang::CpiState::<#account_ty>::address(&#program_target.key()) {
if #ident.key() != anchor_lang::accounts::cpi_state::CpiState::<#account_ty>::address(&#program_target.key()) {
return Err(anchor_lang::__private::ErrorCode::ConstraintState.into());
}
if #ident.to_account_info().owner != &#program_target.key() {
Expand Down
4 changes: 2 additions & 2 deletions lang/syn/src/codegen/program/cpi.rs
Expand Up @@ -29,7 +29,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {

quote! {
pub fn #method_name<'a, 'b, 'c, 'info>(
ctx: CpiStateContext<'a, 'b, 'c, 'info, #accounts_ident<'info>>,
ctx: anchor_lang::context::CpiStateContext<'a, 'b, 'c, 'info, #accounts_ident<'info>>,
#(#args),*
) -> ProgramResult {
let ix = {
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
format!("{:?}", sighash_arr).parse().unwrap();
quote! {
pub fn #method_name<'a, 'b, 'c, 'info>(
ctx: CpiContext<'a, 'b, 'c, 'info, #accounts_ident<'info>>,
ctx: anchor_lang::context::CpiContext<'a, 'b, 'c, 'info, #accounts_ident<'info>>,
#(#args),*
) -> ProgramResult {
let ix = {
Expand Down
24 changes: 12 additions & 12 deletions lang/syn/src/codegen/program/handlers.rs
Expand Up @@ -246,15 +246,15 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
)?;

// Zero copy deserialize.
let loader: anchor_lang::Loader<#mod_name::#name> = anchor_lang::Loader::try_from_unchecked(program_id, &ctor_accounts.to)?;
let loader: anchor_lang::accounts::loader::Loader<#mod_name::#name> = anchor_lang::accounts::loader::Loader::try_from_unchecked(program_id, &ctor_accounts.to)?;

// Invoke the ctor in a new lexical scope so that
// the zero-copy RefMut gets dropped. Required
// so that we can subsequently run the exit routine.
{
let mut instance = loader.load_init()?;
instance.new(
anchor_lang::Context::new(
anchor_lang::context::Context::new(
program_id,
&mut ctor_user_def_accounts,
remaining_accounts,
Expand Down Expand Up @@ -291,7 +291,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {

// Invoke the ctor.
let instance = #mod_name::#name::new(
anchor_lang::Context::new(
anchor_lang::context::Context::new(
program_id,
&mut ctor_user_def_accounts,
remaining_accounts,
Expand All @@ -302,7 +302,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
// Create the solana account for the ctor data.
let from = ctor_accounts.from.key;
let (base, nonce) = Pubkey::find_program_address(&[], ctor_accounts.program.key);
let seed = anchor_lang::ProgramState::<#name>::seed();
let seed = anchor_lang::accounts::state::ProgramState::<#name>::seed();
let owner = ctor_accounts.program.key;
let to = Pubkey::create_with_seed(&base, seed, owner).unwrap();
let space = anchor_lang::__private::AccountSize::size(&instance)?;
Expand Down Expand Up @@ -392,15 +392,15 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
if remaining_accounts.is_empty() {
return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into());
}
let loader: anchor_lang::Loader<#mod_name::#name> = anchor_lang::Loader::try_accounts(program_id, &mut remaining_accounts, &[])?;
let loader: anchor_lang::accounts::loader::Loader<#mod_name::#name> = anchor_lang::accounts::loader::Loader::try_accounts(program_id, &mut remaining_accounts, &[])?;

// Deserialize accounts.
let mut accounts = #anchor_ident::try_accounts(
program_id,
&mut remaining_accounts,
ix_data,
)?;
let ctx = Context::new(program_id, &mut accounts, remaining_accounts);
let ctx = anchor_lang::context::Context::new(program_id, &mut accounts, remaining_accounts);

// Execute user defined function.
{
Expand Down Expand Up @@ -438,15 +438,15 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
if remaining_accounts.is_empty() {
return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into());
}
let mut state: anchor_lang::ProgramState<#state_ty> = anchor_lang::ProgramState::try_accounts(program_id, &mut remaining_accounts, &[])?;
let mut state: anchor_lang::accounts::state::ProgramState<#state_ty> = anchor_lang::accounts::state::ProgramState::try_accounts(program_id, &mut remaining_accounts, &[])?;

// Deserialize accounts.
let mut accounts = #anchor_ident::try_accounts(
program_id,
&mut remaining_accounts,
ix_data,
)?;
let ctx = Context::new(program_id, &mut accounts, remaining_accounts);
let ctx = anchor_lang::context::Context::new(program_id, &mut accounts, remaining_accounts);

// Execute user defined function.
state.#ix_method_name(
Expand Down Expand Up @@ -551,15 +551,15 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
if remaining_accounts.is_empty() {
return Err(anchor_lang::__private::ErrorCode::AccountNotEnoughKeys.into());
}
let mut state: anchor_lang::ProgramState<#state_ty> = anchor_lang::ProgramState::try_accounts(program_id, &mut remaining_accounts, &[])?;
let mut state: anchor_lang::accounts::state::ProgramState<#state_ty> = anchor_lang::accounts::state::ProgramState::try_accounts(program_id, &mut remaining_accounts, &[])?;

// Deserialize accounts.
let mut accounts = #anchor_ident::try_accounts(
program_id,
&mut remaining_accounts,
ix_data,
)?;
let ctx = Context::new(program_id, &mut accounts, remaining_accounts);
let ctx = anchor_lang::context::Context::new(program_id, &mut accounts, remaining_accounts);

// Execute user defined function.
state.#ix_method_name(
Expand Down Expand Up @@ -603,7 +603,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {

// Execute user defined function.
#state_name::#ix_method_name(
Context::new(program_id, &mut accounts, remaining_accounts),
anchor_lang::context::Context::new(program_id, &mut accounts, remaining_accounts),
#(#ix_arg_names),*
)?;

Expand Down Expand Up @@ -654,7 +654,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {

// Invoke user defined handler.
#program_name::#ix_method_name(
Context::new(program_id, &mut accounts, remaining_accounts),
anchor_lang::context::Context::new(program_id, &mut accounts, remaining_accounts),
#(#ix_arg_names),*
)?;

Expand Down

0 comments on commit a830644

Please sign in to comment.