Skip to content

Commit

Permalink
lang: Fix using owner constraint with Boxed accounts (#3087)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Jul 15, 2024
1 parent a2bd50b commit 79d1cec
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
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(())
}
}

0 comments on commit 79d1cec

Please sign in to comment.