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: Fix using owner constraint with Boxed accounts #3087

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -25,6 +25,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Make stack frames slimmer on ATA creation ([#3065](https://github.com/coral-xyz/anchor/pull/3065)).
- lang: Remove `getrandom` dependency ([#3072](https://github.com/coral-xyz/anchor/pull/3072)).
- lang: Make `InitSpace` support unnamed & unit structs ([#3084](https://github.com/coral-xyz/anchor/pull/3084)).
- lang: Fix using `owner` constraint with `Box`ed accounts ([#3087](https://github.com/coral-xyz/anchor/pull/3087)).

### Breaking

Expand Down
10 changes: 9 additions & 1 deletion lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,24 @@ pub fn generate_constraint_raw(ident: &Ident, c: &ConstraintRaw) -> proc_macro2:

pub fn generate_constraint_owner(f: &Field, c: &ConstraintOwner) -> proc_macro2::TokenStream {
let ident = &f.ident;
let maybe_deref = match &f.ty {
Ty::Account(AccountTy { boxed, .. })
| Ty::InterfaceAccount(InterfaceAccountTy { boxed, .. }) => *boxed,
_ => false,
}
.then(|| quote!(*))
.unwrap_or_default();
let owner_address = &c.owner_address;
let error = generate_custom_error(
ident,
&c.error,
quote! { ConstraintOwner },
&Some(&(quote! { *my_owner }, quote! { owner_address })),
);

quote! {
{
let my_owner = AsRef::<AccountInfo>::as_ref(&#ident).owner;
let my_owner = AsRef::<AccountInfo>::as_ref(& #maybe_deref #ident).owner;
let owner_address = #owner_address;
if my_owner != &owner_address {
return #error;
Expand Down
9 changes: 9 additions & 0 deletions tests/misc/programs/misc/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::account::*;
use crate::program::Misc;
use anchor_lang::prelude::*;
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{Mint, Token, TokenAccount};
Expand Down Expand Up @@ -803,3 +804,11 @@ pub struct InitManyAssociatedTokenAccounts<'info> {
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
}

#[derive(Accounts)]
pub struct TestBoxedOwnerConstraint<'info> {
// Redundant check to test compilation
#[account(owner = program.key())]
pub my_account: Box<Account<'info, Data>>,
pub program: Program<'info, Misc>,
}
6 changes: 5 additions & 1 deletion tests/misc/programs/misc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,14 @@ pub mod misc {
Ok(())
}

#[allow(unused_variables)]
pub fn test_init_many_associated_token_accounts(
_ctx: Context<InitManyAssociatedTokenAccounts>,
) -> Result<()> {
Ok(())
}

/// Compilation test for https://github.com/coral-xyz/anchor/issues/3074
pub fn test_boxed_owner_constraint(_ctx: Context<TestBoxedOwnerConstraint>) -> Result<()> {
Ok(())
}
}
Loading