From 318cdfbfc0c7012decfa40e59f50207c29ebe300 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 30 Dec 2021 00:00:00 +0100 Subject: [PATCH 01/14] lang: export accounts module instead of individual accounts --- lang/src/idl.rs | 2 ++ lang/src/lib.rs | 44 ++++++++++---------------------------------- lang/syn/src/lib.rs | 18 +++++++++--------- 3 files changed, 21 insertions(+), 43 deletions(-) diff --git a/lang/src/idl.rs b/lang/src/idl.rs index 3691616610..8960ec1604 100644 --- a/lang/src/idl.rs +++ b/lang/src/idl.rs @@ -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; diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 6903667701..bb5e30167f 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -32,7 +32,7 @@ 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; @@ -42,27 +42,6 @@ mod error; 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::*; @@ -244,18 +223,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, - 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, + 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, + declare_id, emit, error, event, interface, program, require, + solana_program::bpf_loader_upgradeable::UpgradeableLoaderState, state, zero_copy, + AccountDeserialize, AccountSerialize, Accounts, AccountsExit, AnchorDeserialize, + AnchorSerialize, Context, CpiContext, Id, Key, Owner, ProgramData, System, ToAccountInfo, + ToAccountInfos, ToAccountMetas, }; pub use borsh; diff --git a/lang/syn/src/lib.rs b/lang/syn/src/lib.rs index 38033705ab..62ac9fa692 100644 --- a/lang/syn/src/lib.rs +++ b/lang/syn/src/lib.rs @@ -279,24 +279,24 @@ impl Field { pub fn container_ty(&self) -> proc_macro2::TokenStream { match &self.ty { Ty::ProgramAccount(_) => quote! { - anchor_lang::ProgramAccount + anchor_lang::accounts::program_account::ProgramAccount }, Ty::Account(_) => quote! { - anchor_lang::Account + anchor_lang::accounts::account::Account }, Ty::AccountLoader(_) => quote! { - anchor_lang::AccountLoader + anchor_lang::accounts::loader_account::AccountLoader }, Ty::Loader(_) => quote! { - anchor_lang::Loader + anchor_lang::accounts::loader::Loader }, Ty::CpiAccount(_) => quote! { - anchor_lang::CpiAccount + anchor_lang::accounts::cpi_account::CpiAccount }, - Ty::Sysvar(_) => quote! { anchor_lang::Sysvar }, - Ty::CpiState(_) => quote! { anchor_lang::CpiState }, - Ty::ProgramState(_) => quote! { anchor_lang::ProgramState }, - Ty::Program(_) => quote! { anchor_lang::Program }, + Ty::Sysvar(_) => quote! { anchor_lang::accounts::sysvar::Sysvar }, + Ty::CpiState(_) => quote! { anchor_lang::accounts::cpi_state::CpiState }, + Ty::ProgramState(_) => quote! { anchor_lang::accounts::state::ProgramState }, + Ty::Program(_) => quote! { anchor_lang::accounts::program::Program }, Ty::AccountInfo => quote! {}, Ty::UncheckedAccount => quote! {}, Ty::Signer => quote! {}, From 98a9f04de591460f302e110d66cf40b0fb8c35d2 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 30 Dec 2021 00:30:05 +0100 Subject: [PATCH 02/14] lang: fix import paths --- CHANGELOG.md | 1 + lang/syn/src/codegen/accounts/constraints.rs | 2 +- lang/syn/src/codegen/program/handlers.rs | 10 +++++----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e14869d8fa..3b2ac2c019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ incremented for features. * client: Client::new and Client::new_with_options now accept `Rc` 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. ## [0.19.0] - 2021-12-08 diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index 70b80e13c8..e09c2c2ccf 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -728,7 +728,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.to_account_info().key != &anchor_lang::CpiState::<#account_ty>::address(#program_target.to_account_info().key) { + if #ident.to_account_info().key != &anchor_lang::accounts::cpi_state::CpiState::<#account_ty>::address(#program_target.to_account_info().key) { return Err(anchor_lang::__private::ErrorCode::ConstraintState.into()); } if #ident.to_account_info().owner != #program_target.to_account_info().key { diff --git a/lang/syn/src/codegen/program/handlers.rs b/lang/syn/src/codegen/program/handlers.rs index 3bfa47e1b3..4b8124e17e 100644 --- a/lang/syn/src/codegen/program/handlers.rs +++ b/lang/syn/src/codegen/program/handlers.rs @@ -246,7 +246,7 @@ 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 @@ -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)?; @@ -392,7 +392,7 @@ 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( @@ -438,7 +438,7 @@ 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( @@ -551,7 +551,7 @@ 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( From 6e8a33491dc5070ccecc133429456e82501cff1c Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 30 Dec 2021 00:32:10 +0100 Subject: [PATCH 03/14] docs: add PR to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b2ac2c019..532a5905e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ incremented for features. * 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 From a57dcce9b75bc2d28d0016982feb6c09b56777ab Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 30 Dec 2021 00:44:57 +0100 Subject: [PATCH 04/14] lang: fix import paths --- tests/chat/programs/chat/src/lib.rs | 1 + tests/misc/programs/misc/src/context.rs | 2 ++ tests/multisig/programs/multisig/src/lib.rs | 1 + 3 files changed, 4 insertions(+) diff --git a/tests/chat/programs/chat/src/lib.rs b/tests/chat/programs/chat/src/lib.rs index 9d04feabf2..bb6a897ef3 100644 --- a/tests/chat/programs/chat/src/lib.rs +++ b/tests/chat/programs/chat/src/lib.rs @@ -1,6 +1,7 @@ //! A simple chat program using a ring buffer to store messages. use anchor_lang::prelude::*; +use anchor_lang::accounts::loader::Loader; declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); diff --git a/tests/misc/programs/misc/src/context.rs b/tests/misc/programs/misc/src/context.rs index f0a34b431a..cee527451f 100644 --- a/tests/misc/programs/misc/src/context.rs +++ b/tests/misc/programs/misc/src/context.rs @@ -1,5 +1,7 @@ use crate::account::*; use anchor_lang::prelude::*; +use anchor_lang::accounts::loader::Loader; +use anchor_lang::accounts::cpi_state::CpiState; use anchor_spl::associated_token::AssociatedToken; use anchor_spl::token::{Mint, Token, TokenAccount}; use misc2::misc2::MyState as Misc2State; diff --git a/tests/multisig/programs/multisig/src/lib.rs b/tests/multisig/programs/multisig/src/lib.rs index 0ae09f258e..07811d6321 100644 --- a/tests/multisig/programs/multisig/src/lib.rs +++ b/tests/multisig/programs/multisig/src/lib.rs @@ -18,6 +18,7 @@ //! signed. use anchor_lang::prelude::*; +use anchor_lang::accounts::program_account::ProgramAccount; use anchor_lang::solana_program; use anchor_lang::solana_program::instruction::Instruction; use std::convert::Into; From 7142642cde278a73d456eaced3029a22f6c20d7c Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 30 Dec 2021 19:20:28 +0100 Subject: [PATCH 05/14] fix import paths and make context mod pub --- lang/attribute/access-control/src/lib.rs | 2 +- lang/attribute/interface/src/lib.rs | 2 +- lang/src/accounts/cpi_state.rs | 2 +- lang/src/accounts/loader.rs | 2 +- lang/src/accounts/loader_account.rs | 2 +- lang/src/context.rs | 1 + lang/src/lib.rs | 14 +++++--------- lang/syn/src/codegen/accounts/constraints.rs | 6 +++--- lang/syn/src/codegen/program/cpi.rs | 4 ++-- lang/syn/src/codegen/program/handlers.rs | 14 +++++++------- spl/src/associated_token.rs | 2 +- spl/src/dex.rs | 2 +- spl/src/shmem.rs | 2 +- spl/src/token.rs | 2 +- tests/lockup/programs/lockup/src/lib.rs | 1 + 15 files changed, 28 insertions(+), 30 deletions(-) diff --git a/lang/attribute/access-control/src/lib.rs b/lang/attribute/access-control/src/lib.rs index 4cb1699ccc..6b39b9b7f8 100644 --- a/lang/attribute/access-control/src/lib.rs +++ b/lang/attribute/access-control/src/lib.rs @@ -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 { diff --git a/lang/attribute/interface/src/lib.rs b/lang/attribute/interface/src/lib.rs index 68c314e4ae..e6dd5c6187 100644 --- a/lang/attribute/interface/src/lib.rs +++ b/lang/attribute/interface/src/lib.rs @@ -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 diff --git a/lang/src/accounts/cpi_state.rs b/lang/src/accounts/cpi_state.rs index 2378b9ae83..47c26914ed 100644 --- a/lang/src/accounts/cpi_state.rs +++ b/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, diff --git a/lang/src/accounts/loader.rs b/lang/src/accounts/loader.rs index 6a3cbb5227..bb5e380d58 100644 --- a/lang/src/accounts/loader.rs +++ b/lang/src/accounts/loader.rs @@ -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. diff --git a/lang/src/accounts/loader_account.rs b/lang/src/accounts/loader_account.rs index effb18c167..2d8edbe84c 100644 --- a/lang/src/accounts/loader_account.rs +++ b/lang/src/accounts/loader_account.rs @@ -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. diff --git a/lang/src/context.rs b/lang/src/context.rs index 01fa9a508f..9259810623 100644 --- a/lang/src/context.rs +++ b/lang/src/context.rs @@ -122,6 +122,7 @@ impl<'info, T: ToAccountInfos<'info> + ToAccountMetas> ToAccountMetas /// Context specifying non-argument inputs for cross-program-invocations /// targeted at program state instructions. +#[doc(hidden)] #[deprecated] pub struct CpiStateContext<'a, 'b, 'c, 'info, T: Accounts<'info>> { state: AccountInfo<'info>, diff --git a/lang/src/lib.rs b/lang/src/lib.rs index bb5e30167f..1bef91707d 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -35,7 +35,7 @@ mod account_meta; pub mod accounts; mod bpf_upgradeable_state; mod common; -mod context; +pub mod context; mod ctor; mod error; #[doc(hidden)] @@ -45,10 +45,6 @@ mod system_program; 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; @@ -227,11 +223,11 @@ pub mod prelude { accounts::loader_account::AccountLoader, accounts::program::Program, accounts::signer::Signer, accounts::system_account::SystemAccount, accounts::sysvar::Sysvar, accounts::unchecked_account::UncheckedAccount, constant, - declare_id, emit, error, event, interface, program, require, - solana_program::bpf_loader_upgradeable::UpgradeableLoaderState, state, zero_copy, + context::Context, context::CpiContext, declare_id, emit, error, event, interface, program, + require, solana_program::bpf_loader_upgradeable::UpgradeableLoaderState, state, zero_copy, AccountDeserialize, AccountSerialize, Accounts, AccountsExit, AnchorDeserialize, - AnchorSerialize, Context, CpiContext, Id, Key, Owner, ProgramData, System, ToAccountInfo, - ToAccountInfos, ToAccountMetas, + AnchorSerialize, Id, Key, Owner, ProgramData, System, ToAccountInfo, ToAccountInfos, + ToAccountMetas, }; pub use borsh; diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index e09c2c2ccf..640b1ce04e 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -444,7 +444,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)?; } @@ -477,7 +477,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; @@ -523,7 +523,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; diff --git a/lang/syn/src/codegen/program/cpi.rs b/lang/syn/src/codegen/program/cpi.rs index 8706df8b2e..df96d281d5 100644 --- a/lang/syn/src/codegen/program/cpi.rs +++ b/lang/syn/src/codegen/program/cpi.rs @@ -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 = { @@ -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 = { diff --git a/lang/syn/src/codegen/program/handlers.rs b/lang/syn/src/codegen/program/handlers.rs index 4b8124e17e..f622b6e13b 100644 --- a/lang/syn/src/codegen/program/handlers.rs +++ b/lang/syn/src/codegen/program/handlers.rs @@ -254,7 +254,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { { 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, @@ -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, @@ -400,7 +400,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { &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. { @@ -446,7 +446,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { &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( @@ -559,7 +559,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream { &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( @@ -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),* )?; @@ -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),* )?; diff --git a/spl/src/associated_token.rs b/spl/src/associated_token.rs index 179645ccaf..4effe4d457 100644 --- a/spl/src/associated_token.rs +++ b/spl/src/associated_token.rs @@ -1,7 +1,7 @@ use anchor_lang::solana_program::account_info::AccountInfo; use anchor_lang::solana_program::entrypoint::ProgramResult; use anchor_lang::solana_program::pubkey::Pubkey; -use anchor_lang::{Accounts, CpiContext}; +use anchor_lang::{context::CpiContext, Accounts}; pub use spl_associated_token_account::{get_associated_token_address, ID}; diff --git a/spl/src/dex.rs b/spl/src/dex.rs index 44953c57fb..da83b1ad11 100644 --- a/spl/src/dex.rs +++ b/spl/src/dex.rs @@ -2,7 +2,7 @@ use anchor_lang::solana_program::account_info::AccountInfo; use anchor_lang::solana_program::entrypoint::ProgramResult; use anchor_lang::solana_program::program_error::ProgramError; use anchor_lang::solana_program::pubkey::Pubkey; -use anchor_lang::{Accounts, CpiContext, ToAccountInfos}; +use anchor_lang::{context::CpiContext, Accounts, ToAccountInfos}; use serum_dex::instruction::SelfTradeBehavior; use serum_dex::matching::{OrderType, Side}; use std::io::Write; diff --git a/spl/src/shmem.rs b/spl/src/shmem.rs index d1fb5be08f..450c6806fb 100644 --- a/spl/src/shmem.rs +++ b/spl/src/shmem.rs @@ -2,7 +2,7 @@ //! [program](https://github.com/solana-labs/solana-program-library/tree/master/shared-memory). use anchor_lang::ToAccountInfo; -use anchor_lang::{Accounts, CpiContext}; +use anchor_lang::{context::CpiContext, Accounts}; use solana_program::account_info::AccountInfo; use solana_program::declare_id; use solana_program::entrypoint::ProgramResult; diff --git a/spl/src/token.rs b/spl/src/token.rs index 7a8e79b7cf..fcf20e077a 100644 --- a/spl/src/token.rs +++ b/spl/src/token.rs @@ -4,7 +4,7 @@ use anchor_lang::solana_program::entrypoint::ProgramResult; use anchor_lang::solana_program::program_error::ProgramError; use anchor_lang::solana_program::program_pack::Pack; use anchor_lang::solana_program::pubkey::Pubkey; -use anchor_lang::{Accounts, CpiContext}; +use anchor_lang::{context::CpiContext, Accounts}; use std::ops::Deref; pub use spl_token::ID; diff --git a/tests/lockup/programs/lockup/src/lib.rs b/tests/lockup/programs/lockup/src/lib.rs index b497919439..99caff542a 100644 --- a/tests/lockup/programs/lockup/src/lib.rs +++ b/tests/lockup/programs/lockup/src/lib.rs @@ -2,6 +2,7 @@ //! it's suggested to start with the other examples. use anchor_lang::prelude::*; +use anchor_lang::accounts::state::ProgramState; use anchor_lang::solana_program; use anchor_lang::solana_program::instruction::Instruction; use anchor_spl::token::{self, TokenAccount, Transfer}; From 542ecfcfa45bbc77bdeb2ce0a18b9024d8b97e81 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 30 Dec 2021 19:36:12 +0100 Subject: [PATCH 06/14] tests: fix import paths --- tests/lockup/programs/lockup/src/lib.rs | 2 +- tests/lockup/programs/registry/src/lib.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/lockup/programs/lockup/src/lib.rs b/tests/lockup/programs/lockup/src/lib.rs index 99caff542a..4bf4b7128c 100644 --- a/tests/lockup/programs/lockup/src/lib.rs +++ b/tests/lockup/programs/lockup/src/lib.rs @@ -1,8 +1,8 @@ //! A relatively advanced example of a lockup program. If you're new to Anchor, //! it's suggested to start with the other examples. -use anchor_lang::prelude::*; use anchor_lang::accounts::state::ProgramState; +use anchor_lang::prelude::*; use anchor_lang::solana_program; use anchor_lang::solana_program::instruction::Instruction; use anchor_spl::token::{self, TokenAccount, Transfer}; diff --git a/tests/lockup/programs/registry/src/lib.rs b/tests/lockup/programs/registry/src/lib.rs index 46a7b6ed4d..69e400d1dc 100644 --- a/tests/lockup/programs/registry/src/lib.rs +++ b/tests/lockup/programs/registry/src/lib.rs @@ -1,6 +1,7 @@ //! A relatively advanced example of a staking program. If you're new to Anchor, //! it's suggested to start with the other examples. +use anchor_lang::accounts::state::ProgramState; use anchor_lang::prelude::*; use anchor_lang::solana_program::account_info::next_account_info; use anchor_lang::solana_program::program_option::COption; From eb9eab58fec15b8f747c1283a1244949d6f10ae6 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Fri, 31 Dec 2021 12:29:39 +0100 Subject: [PATCH 07/14] docs --- lang/src/accounts/account.rs | 4 +++- lang/src/accounts/mod.rs | 2 ++ lang/src/context.rs | 2 ++ lang/src/lib.rs | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lang/src/accounts/account.rs b/lang/src/accounts/account.rs index c1f37a4033..1b58dd2b81 100644 --- a/lang/src/accounts/account.rs +++ b/lang/src/accounts/account.rs @@ -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; @@ -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 diff --git a/lang/src/accounts/mod.rs b/lang/src/accounts/mod.rs index 1202a17039..614dbadada 100644 --- a/lang/src/accounts/mod.rs +++ b/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; diff --git a/lang/src/context.rs b/lang/src/context.rs index 9259810623..512e46f1c9 100644 --- a/lang/src/context.rs +++ b/lang/src/context.rs @@ -1,3 +1,5 @@ +//! Data structures that are used to provide inputs to program endpoints + use crate::{Accounts, ToAccountInfos, ToAccountMetas}; use solana_program::account_info::AccountInfo; use solana_program::instruction::AccountMeta; diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 9c29eeb65f..42398c49a8 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -251,7 +251,7 @@ 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 From 825f0418d4544d4d881a4fb930a86d0461876fa4 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Fri, 31 Dec 2021 19:08:09 +0100 Subject: [PATCH 08/14] docs for CLOSED_ACCOUNT_DISCRIMINATOR --- lang/src/lib.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 42398c49a8..3aa30c0db9 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -257,6 +257,12 @@ pub mod __private { // 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)] @@ -279,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)] @@ -308,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. From 93b86c0ff02fa92d312af5ebea49e6b9d7a450bb Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Sun, 2 Jan 2022 19:36:56 +0100 Subject: [PATCH 09/14] tests: update submodule commit --- tests/cfo/deps/stake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cfo/deps/stake b/tests/cfo/deps/stake index 2f3e7e7975..bc73b51028 160000 --- a/tests/cfo/deps/stake +++ b/tests/cfo/deps/stake @@ -1 +1 @@ -Subproject commit 2f3e7e7975e8072ebf588cfddf9f8d25c1295cd5 +Subproject commit bc73b510285050f5a21da13c5bc3b8b4c959948f From f9aba9b51843d6db82987ec086f4361fae91a36c Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 6 Jan 2022 11:43:32 +0100 Subject: [PATCH 10/14] lang/docs: context docs --- lang/src/context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/src/context.rs b/lang/src/context.rs index 512e46f1c9..17ea086edb 100644 --- a/lang/src/context.rs +++ b/lang/src/context.rs @@ -1,4 +1,4 @@ -//! Data structures that are used to provide inputs to program endpoints +//! Data structures that are used to provide non-argument inputs to program endpoints use crate::{Accounts, ToAccountInfos, ToAccountMetas}; use solana_program::account_info::AccountInfo; From 08a93935399775e78fc5a3da8bafb94263bd8fbe Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 6 Jan 2022 15:25:47 +0100 Subject: [PATCH 11/14] ci: fix ci --- .github/actions/setup/action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/setup/action.yaml b/.github/actions/setup/action.yaml index c20d2a5e19..1ac983c81f 100644 --- a/.github/actions/setup/action.yaml +++ b/.github/actions/setup/action.yaml @@ -3,7 +3,7 @@ description: "Setup" runs: using: "composite" steps: - - run: sudo apt-get install -y pkg-config build-essential libudev-dev + - run: sudo apt-get update && sudo apt-get install -y pkg-config build-essential libudev-dev shell: bash - run: echo "ANCHOR_VERSION=$(cat ./VERSION)" >> $GITHUB_ENV shell: bash From 81e3fc0bb41abaa1a6684df0b10e50de588722c4 Mon Sep 17 00:00:00 2001 From: Paul Schaaf Date: Thu, 6 Jan 2022 17:05:45 +0100 Subject: [PATCH 12/14] lang: fix accidental reference changes --- lang/syn/src/codegen/accounts/constraints.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index 38155c8d80..a0e5661ef1 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -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::accounts::cpi_state::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() { From 0d67d4876ffe60064773f6e603fac00e98326890 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Thu, 6 Jan 2022 15:24:55 -0500 Subject: [PATCH 13/14] Update lang/src/accounts/mod.rs --- lang/src/accounts/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/src/accounts/mod.rs b/lang/src/accounts/mod.rs index 614dbadada..6bcd69f495 100644 --- a/lang/src/accounts/mod.rs +++ b/lang/src/accounts/mod.rs @@ -1,4 +1,4 @@ -//! Account types that can be used in the account validation struct +//! Account types that can be used in the account validation struct. pub mod account; pub mod account_info; From d84c5d34e2123943dd4288b4ee82fdeb80c00f8f Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Thu, 6 Jan 2022 15:24:59 -0500 Subject: [PATCH 14/14] Update lang/src/lib.rs --- lang/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/src/lib.rs b/lang/src/lib.rs index 3aa30c0db9..a5e15a6ce6 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -257,7 +257,7 @@ pub mod __private { // don't use #[doc(hidden)] on these pub use crate::error::ErrorCode; - /// The discriminator anchor uses to mark an account as closed + /// 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.