fix: used identifiers in handler (#2464)

This commit is contained in:
CanardMandarin 2023-04-22 21:26:22 +02:00 committed by GitHub
parent 670b4f5005
commit 2e89b79c51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 90 additions and 34 deletions

View File

@ -18,8 +18,10 @@ The minor version will be incremented upon a breaking change and the patch versi
### Fixes
- ts: Narrowed `AccountClient` type to it's appropriate account type ([#2440](https://github.com/coral-xyz/anchor/pull/2440))
- lang: Fix inability to use identifiers `program_id`, `accounts`, `ix_data`, `remaining_accounts` in instruction arguments ([#2464](https://github.com/coral-xyz/anchor/pull/2464))
### Breaking
- 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

View File

@ -480,7 +480,7 @@ fn generate_constraint_init_group(
quote! {
let (__pda_address, __bump) = Pubkey::find_program_address(
&[#maybe_seeds_plus_comma],
program_id,
__program_id,
);
__bumps.insert(#name_str.to_string(), __bump);
#validate_pda
@ -754,7 +754,7 @@ fn generate_constraint_init_group(
let (owner, owner_optional_check) = match owner {
None => (
quote! {
program_id
__program_id
},
quote! {},
),
@ -862,7 +862,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2
// If they specified a seeds::program to use when deriving the PDA, use it.
.map(|program_id| quote! { #program_id.key() })
// Otherwise fall back to the current program's program_id.
.unwrap_or(quote! { program_id });
.unwrap_or(quote! { __program_id });
// If the seeds came with a trailing comma, we need to chop it off
// before we interpolate them below.

View File

@ -25,7 +25,7 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
quote! {
#[cfg(feature = "anchor-debug")]
::solana_program::log::sol_log(stringify!(#name));
let #name: #ty = anchor_lang::Accounts::try_accounts(program_id, accounts, ix_data, __bumps, __reallocs)?;
let #name: #ty = anchor_lang::Accounts::try_accounts(__program_id, __accounts, __ix_data, __bumps, __reallocs)?;
}
}
AccountField::Field(f) => {
@ -45,24 +45,24 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
quote!{ return Err(anchor_lang::error::ErrorCode::AccountNotEnoughKeys.into()); }
};
quote! {
let #name = if accounts.is_empty() {
let #name = if __accounts.is_empty() {
#empty_behavior
} else if accounts[0].key == program_id {
*accounts = &accounts[1..];
} else if __accounts[0].key == __program_id {
*__accounts = &__accounts[1..];
None
} else {
let account = &accounts[0];
*accounts = &accounts[1..];
let account = &__accounts[0];
*__accounts = &__accounts[1..];
Some(account)
};
}
} else {
quote!{
if accounts.is_empty() {
if __accounts.is_empty() {
return Err(anchor_lang::error::ErrorCode::AccountNotEnoughKeys.into());
}
let #name = &accounts[0];
*accounts = &accounts[1..];
let #name = &__accounts[0];
*__accounts = &__accounts[1..];
}
}
} else {
@ -71,7 +71,7 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
quote! {
#[cfg(feature = "anchor-debug")]
::solana_program::log::sol_log(stringify!(#typed_name));
let #typed_name = anchor_lang::Accounts::try_accounts(program_id, accounts, ix_data, __bumps, __reallocs)
let #typed_name = anchor_lang::Accounts::try_accounts(__program_id, __accounts, __ix_data, __bumps, __reallocs)
.map_err(|e| e.with_account_name(#name))?;
}
}
@ -100,14 +100,14 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
})
.collect();
quote! {
let mut ix_data = ix_data;
let mut __ix_data = __ix_data;
#[derive(anchor_lang::AnchorSerialize, anchor_lang::AnchorDeserialize)]
struct __Args {
#strct_inner
}
let __Args {
#(#field_names),*
} = __Args::deserialize(&mut ix_data)
} = __Args::deserialize(&mut __ix_data)
.map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?;
}
}
@ -118,9 +118,9 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
impl<#combined_generics> anchor_lang::Accounts<#trait_generics> for #name<#struct_generics> #where_clause {
#[inline(never)]
fn try_accounts(
program_id: &anchor_lang::solana_program::pubkey::Pubkey,
accounts: &mut &[anchor_lang::solana_program::account_info::AccountInfo<'info>],
ix_data: &[u8],
__program_id: &anchor_lang::solana_program::pubkey::Pubkey,
__accounts: &mut &[anchor_lang::solana_program::account_info::AccountInfo<'info>],
__ix_data: &[u8],
__bumps: &mut std::collections::BTreeMap<String, u8>,
__reallocs: &mut std::collections::BTreeSet<anchor_lang::solana_program::pubkey::Pubkey>,
) -> anchor_lang::Result<Self> {

View File

@ -111,15 +111,15 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
quote! {
#[inline(never)]
pub fn #ix_method_name(
program_id: &Pubkey,
accounts: &[AccountInfo],
ix_data: &[u8],
__program_id: &Pubkey,
__accounts: &[AccountInfo],
__ix_data: &[u8],
) -> anchor_lang::Result<()> {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!(#ix_name_log);
// Deserialize data.
let ix = instruction::#ix_name::deserialize(&mut &ix_data[..])
let ix = instruction::#ix_name::deserialize(&mut &__ix_data[..])
.map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?;
let instruction::#variant_arm = ix;
@ -129,11 +129,11 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let mut __reallocs = std::collections::BTreeSet::new();
// Deserialize accounts.
let mut remaining_accounts: &[AccountInfo] = accounts;
let mut accounts = #anchor::try_accounts(
program_id,
&mut remaining_accounts,
ix_data,
let mut __remaining_accounts: &[AccountInfo] = __accounts;
let mut __accounts = #anchor::try_accounts(
__program_id,
&mut __remaining_accounts,
__ix_data,
&mut __bumps,
&mut __reallocs,
)?;
@ -141,9 +141,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
// Invoke user defined handler.
let result = #program_name::#ix_method_name(
anchor_lang::context::Context::new(
program_id,
&mut accounts,
remaining_accounts,
__program_id,
&mut __accounts,
__remaining_accounts,
__bumps,
),
#(#ix_arg_names),*
@ -153,7 +153,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
#maybe_set_return_data
// Exit routine.
accounts.exit(program_id)
__accounts.exit(__program_id)
}
}
})

View File

@ -309,9 +309,9 @@ impl Field {
let field_str = field.to_string();
let container_ty = self.container_ty();
let owner_addr = match &kind {
None => quote! { program_id },
None => quote! { __program_id },
Some(InitKind::Program { .. }) => quote! {
program_id
__program_id
},
_ => quote! {
&anchor_spl::token::ID

View File

@ -725,3 +725,46 @@ pub struct TestAssociatedTokenWithTokenProgramConstraint<'info> {
/// CHECK: ignore
pub associated_token_token_program: AccountInfo<'info>,
}
#[derive(Accounts)]
#[instruction(
program_id: u8,
accounts: u8,
ix_data: u8,
remaining_accounts: u8
)]
pub struct TestUsedIdentifiers<'info> {
#[account(
constraint = 1 == program_id,
constraint = 2 == accounts,
constraint = 3 == ix_data,
constraint = 4 == remaining_accounts,
)]
/// CHECK: ignore
pub test: AccountInfo<'info>,
#[account(
seeds = [&[program_id], &[accounts], &[ix_data], &[remaining_accounts]],
bump = program_id,
)]
/// CHECK: ignore
pub test1: AccountInfo<'info>,
#[account(
seeds = [&[program_id], &[accounts], &[ix_data], &[remaining_accounts]],
bump = accounts,
)]
/// CHECK: ignore
pub test2: AccountInfo<'info>,
#[account(
seeds = [&[program_id], &[accounts], &[ix_data], &[remaining_accounts]],
bump = ix_data,
)]
/// CHECK: ignore
pub test3: AccountInfo<'info>,
#[account(
seeds = [&[program_id], &[accounts], &[ix_data], &[remaining_accounts]],
bump = remaining_accounts,
)]
/// CHECK: ignore
pub test4: AccountInfo<'info>,
}

View File

@ -392,4 +392,15 @@ pub mod misc {
pub fn test_associated_token_with_token_program_constraint(_ctx: Context<TestAssociatedTokenWithTokenProgramConstraint>) -> Result<()> {
Ok(())
}
#[allow(unused_variables)]
pub fn test_used_identifiers(
_ctx: Context<TestUsedIdentifiers>,
program_id: u8,
accounts: u8,
ix_data: u8,
remaining_accounts: u8
) -> Result<()> {
Ok(())
}
}

View File

@ -97,7 +97,7 @@ pub struct CreateBar<'info> {
init,
seeds = [authority.key().as_ref(), foo.key().as_ref()],
bump,
payer = authority, owner = *program_id,
payer = authority, owner = ID,
space = Bar::LEN + 8
)]
bar: AccountLoader<'info, Bar>,