Explorar o código

lang: `#[account(owner)]` uses pubkey instead of account target (#691)

Armani Ferrante %!s(int64=4) %!d(string=hai) anos
pai
achega
526de235d1

+ 4 - 0
CHANGELOG.md

@@ -11,6 +11,10 @@ incremented for features.
 
 ## [Unreleased]
 
+### Breaking Changes
+
+* lang: `#[account(owner = <pubkey>)]` now requires a `Pubkey` instead of an account ([#691](https://github.com/project-serum/anchor/pull/691)).
+
 ## [0.15.0] - 2021-09-07
 
 ### Features

+ 3 - 2
examples/tutorial/basic-3/programs/puppet-master/src/lib.rs

@@ -1,6 +1,6 @@
 // #region core
 use anchor_lang::prelude::*;
-use puppet::{Puppet, SetData};
+use puppet::{self, Puppet, SetData};
 
 declare_id!("HmbTLCmaGvZhKnn1Zfa1JVnp7vkMV4DYVxPLWBVoN65L");
 
@@ -19,8 +19,9 @@ mod puppet_master {
 
 #[derive(Accounts)]
 pub struct PullStrings<'info> {
-    #[account(mut, owner = puppet_program)]
+    #[account(mut)]
     pub puppet: Account<'info, Puppet>,
+    #[account(address = puppet::ID)]
     pub puppet_program: AccountInfo<'info>,
 }
 // #endregion core

+ 2 - 2
lang/syn/src/codegen/accounts/constraints.rs

@@ -244,9 +244,9 @@ pub fn generate_constraint_raw(c: &ConstraintRaw) -> proc_macro2::TokenStream {
 
 pub fn generate_constraint_owner(f: &Field, c: &ConstraintOwner) -> proc_macro2::TokenStream {
     let ident = &f.ident;
-    let owner_target = c.owner_target.clone();
+    let owner_address = &c.owner_address;
     quote! {
-        if #ident.to_account_info().owner != #owner_target.to_account_info().key {
+        if #ident.to_account_info().owner != &#owner_address {
             return Err(anchor_lang::__private::ErrorCode::ConstraintOwner.into());
         }
     }

+ 1 - 1
lang/syn/src/lib.rs

@@ -561,7 +561,7 @@ pub struct ConstraintRaw {
 
 #[derive(Debug, Clone)]
 pub struct ConstraintOwner {
-    pub owner_target: Expr,
+    pub owner_address: Expr,
 }
 
 #[derive(Debug, Clone)]

+ 2 - 10
lang/syn/src/parser/accounts/constraints.rs

@@ -156,7 +156,7 @@ pub fn parse_token(stream: ParseStream) -> ParseResult<ConstraintToken> {
                 "owner" => ConstraintToken::Owner(Context::new(
                     span,
                     ConstraintOwner {
-                        owner_target: stream.parse()?,
+                        owner_address: stream.parse()?,
                     },
                 )),
                 "rent_exempt" => ConstraintToken::RentExempt(Context::new(
@@ -447,14 +447,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
             };
         }
 
-        let (owner, pda_owner) = {
-            if seeds.is_some() {
-                (None, owner.map(|o| o.owner_target.clone()))
-            } else {
-                (owner, None)
-            }
-        };
-
         let seeds = seeds.map(|c| ConstraintSeedsGroup {
             is_init: init.is_some(),
             seeds: c.seeds.clone(),
@@ -491,7 +483,7 @@ impl<'ty> ConstraintGroupBuilder<'ty> {
                     }
                 } else {
                     InitKind::Program {
-                        owner: pda_owner.clone(),
+                        owner: owner.as_ref().map(|o| o.owner_address.clone()),
                     }
                 },
             })).transpose()?,

+ 8 - 11
tests/cfo/programs/cfo/src/lib.rs

@@ -364,7 +364,7 @@ pub struct CreateOfficerToken<'info> {
         payer = payer,
     )]
     token: Account<'info, TokenAccount>,
-    #[account(owner = token_program)]
+    #[account(owner = spl_token::ID)]
     mint: AccountInfo<'info>,
     #[account(mut, signer)]
     payer: AccountInfo<'info>,
@@ -392,7 +392,7 @@ pub struct SweepFees<'info> {
     officer: Account<'info, Officer>,
     #[account(
         mut,
-        owner = dex.token_program,
+        owner = spl_token::ID,
         seeds = [officer.key().as_ref(), mint.key().as_ref()],
         bump,
     )]
@@ -423,12 +423,12 @@ pub struct SwapToUsdc<'info> {
     officer: Account<'info, Officer>,
     market: DexMarketAccounts<'info>,
     #[account(
-        owner = token_program,
+        owner = spl_token::ID,
         constraint = &officer.treasury != from_vault.key,
         constraint = &officer.stake != from_vault.key,
     )]
     from_vault: AccountInfo<'info>,
-    #[account(owner = token_program)]
+    #[account(owner = spl_token::ID)]
     quote_vault: AccountInfo<'info>,
     #[account(seeds = [officer.key().as_ref(), mint::USDC.as_ref()], bump)]
     usdc_vault: AccountInfo<'info>,
@@ -452,12 +452,12 @@ pub struct SwapToSrm<'info> {
     officer: Account<'info, Officer>,
     market: DexMarketAccounts<'info>,
     #[account(
-        owner = token_program,
+        owner = spl_token::ID,
         constraint = &officer.treasury != from_vault.key,
         constraint = &officer.stake != from_vault.key,
     )]
     from_vault: AccountInfo<'info>,
-    #[account(owner = token_program)]
+    #[account(owner = spl_token::ID)]
     quote_vault: AccountInfo<'info>,
     #[account(
         seeds = [officer.key().as_ref(), mint::SRM.as_ref()],
@@ -518,10 +518,7 @@ pub struct Distribute<'info> {
     officer: Account<'info, Officer>,
     treasury: AccountInfo<'info>,
     stake: AccountInfo<'info>,
-    #[account(
-        owner = token_program,
-        constraint = srm_vault.mint == mint::SRM,
-    )]
+    #[account(constraint = srm_vault.mint == mint::SRM)]
     srm_vault: Account<'info, TokenAccount>,
     #[account(address = mint::SRM)]
     mint: AccountInfo<'info>,
@@ -551,7 +548,7 @@ pub struct DropStakeReward<'info> {
     mint: AccountInfo<'info>,
     srm: DropStakeRewardPool<'info>,
     msrm: DropStakeRewardPool<'info>,
-    #[account(owner = registry_program)]
+    #[account(owner = *registry_program.key)]
     msrm_registrar: Box<Account<'info, Registrar>>,
     #[account(address = token::ID)]
     token_program: AccountInfo<'info>,

+ 1 - 1
tests/misc/programs/misc/src/context.rs

@@ -96,7 +96,7 @@ pub struct Initialize<'info> {
 
 #[derive(Accounts)]
 pub struct TestOwner<'info> {
-    #[account(owner = misc)]
+    #[account(owner = *misc.key)]
     pub data: AccountInfo<'info>,
     pub misc: AccountInfo<'info>,
 }