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

fix: used identifiers in handler #2464

Merged
merged 7 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 3 additions & 3 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -754,7 +754,7 @@ fn generate_constraint_init_group(
let (owner, owner_optional_check) = match owner {
None => (
quote! {
program_id
__program_id
},
quote! {},
),
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 15 additions & 15 deletions lang/syn/src/codegen/accounts/try_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 {
Expand All @@ -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))?;
}
}
Expand Down Expand Up @@ -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)?;
}
}
Expand All @@ -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> {
Expand Down
26 changes: 13 additions & 13 deletions lang/syn/src/codegen/program/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -129,21 +129,21 @@ 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,
)?;

// 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),*
Expand All @@ -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)
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions lang/syn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions tests/misc/programs/misc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}
11 changes: 11 additions & 0 deletions tests/misc/programs/misc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
2 changes: 1 addition & 1 deletion tests/zero-copy/programs/zero-copy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
space = Bar::LEN + 8
)]
bar: AccountLoader<'info, Bar>,
Expand Down