From e4aaf76b0b1af87f7801d9865f0c711a8e7f04d9 Mon Sep 17 00:00:00 2001 From: Nikhil B N Date: Sat, 12 Feb 2022 00:45:41 +0530 Subject: [PATCH 1/2] Remove literal constraint Signed-off-by: Nikhil B N --- lang/syn/src/codegen/accounts/constraints.rs | 23 -------------------- lang/syn/src/lib.rs | 8 ------- lang/syn/src/parser/accounts/constraints.rs | 15 ++----------- 3 files changed, 2 insertions(+), 44 deletions(-) diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index 1754b88ee2..663340b3da 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -28,7 +28,6 @@ pub fn generate_composite(f: &CompositeField) -> proc_macro2::TokenStream { .iter() .filter_map(|c| match c { Constraint::Raw(_) => Some(c), - Constraint::Literal(_) => Some(c), _ => panic!("Invariant violation: composite constraints can only be raw or literals"), }) .map(|c| generate_constraint_composite(f, c)) @@ -47,7 +46,6 @@ pub fn linearize(c_group: &ConstraintGroup) -> Vec { mutable, signer, has_one, - literal, raw, owner, rent_exempt, @@ -80,7 +78,6 @@ pub fn linearize(c_group: &ConstraintGroup) -> Vec { constraints.push(Constraint::Signer(c)); } constraints.append(&mut has_one.into_iter().map(Constraint::HasOne).collect()); - constraints.append(&mut literal.into_iter().map(Constraint::Literal).collect()); constraints.append(&mut raw.into_iter().map(Constraint::Raw).collect()); if let Some(c) = owner { constraints.push(Constraint::Owner(c)); @@ -110,7 +107,6 @@ fn generate_constraint(f: &Field, c: &Constraint) -> proc_macro2::TokenStream { Constraint::Mut(c) => generate_constraint_mut(f, c), Constraint::HasOne(c) => generate_constraint_has_one(f, c), Constraint::Signer(c) => generate_constraint_signer(f, c), - Constraint::Literal(c) => generate_constraint_literal(c), Constraint::Raw(c) => generate_constraint_raw(c), Constraint::Owner(c) => generate_constraint_owner(f, c), Constraint::RentExempt(c) => generate_constraint_rent_exempt(f, c), @@ -126,7 +122,6 @@ fn generate_constraint(f: &Field, c: &Constraint) -> proc_macro2::TokenStream { fn generate_constraint_composite(_f: &CompositeField, c: &Constraint) -> proc_macro2::TokenStream { match c { Constraint::Raw(c) => generate_constraint_raw(c), - Constraint::Literal(c) => generate_constraint_literal(c), _ => panic!("Invariant violation"), } } @@ -219,24 +214,6 @@ pub fn generate_constraint_signer(f: &Field, c: &ConstraintSigner) -> proc_macro } } -pub fn generate_constraint_literal(c: &ConstraintLiteral) -> proc_macro2::TokenStream { - let lit: proc_macro2::TokenStream = { - let lit = &c.lit; - let constraint = lit.value().replace('\"', ""); - let message = format!( - "Deprecated. Should be used with constraint: #[account(constraint = {})]", - constraint, - ); - lit.span().warning(message).emit_as_item_tokens(); - constraint.parse().unwrap() - }; - quote! { - if !(#lit) { - return Err(anchor_lang::__private::ErrorCode::Deprecated.into()); - } - } -} - pub fn generate_constraint_raw(c: &ConstraintRaw) -> proc_macro2::TokenStream { let raw = &c.raw; let error = generate_custom_error(&c.error, quote! { ConstraintRaw }); diff --git a/lang/syn/src/lib.rs b/lang/syn/src/lib.rs index 7d29cc51c6..e5663dc08a 100644 --- a/lang/syn/src/lib.rs +++ b/lang/syn/src/lib.rs @@ -572,7 +572,6 @@ pub struct ConstraintGroup { executable: Option, state: Option, has_one: Vec, - literal: Vec, raw: Vec, close: Option, address: Option, @@ -608,7 +607,6 @@ pub enum Constraint { Mut(ConstraintMut), Signer(ConstraintSigner), HasOne(ConstraintHasOne), - Literal(ConstraintLiteral), Raw(ConstraintRaw), Owner(ConstraintOwner), RentExempt(ConstraintRentExempt), @@ -629,7 +627,6 @@ pub enum ConstraintToken { Mut(Context), Signer(Context), HasOne(Context), - Literal(Context), Raw(Context), Owner(Context), RentExempt(Context), @@ -684,11 +681,6 @@ pub struct ConstraintHasOne { pub error: Option, } -#[derive(Debug, Clone)] -pub struct ConstraintLiteral { - pub lit: LitStr, -} - #[derive(Debug, Clone)] pub struct ConstraintRaw { pub raw: Expr, diff --git a/lang/syn/src/parser/accounts/constraints.rs b/lang/syn/src/parser/accounts/constraints.rs index 533d5053fc..f9eff02bfe 100644 --- a/lang/syn/src/parser/accounts/constraints.rs +++ b/lang/syn/src/parser/accounts/constraints.rs @@ -50,10 +50,9 @@ pub fn is_instruction(attr: &&syn::Attribute) -> bool { // Parses a single constraint from a parse stream for `#[account()]`. pub fn parse_token(stream: ParseStream) -> ParseResult { let is_lit = stream.peek(LitStr); + // Using a literal constraint is deprecated. if is_lit { - let lit: LitStr = stream.parse()?; - let c = ConstraintToken::Literal(Context::new(lit.span(), ConstraintLiteral { lit })); - return Ok(c); + return Err(anchor_lang::__private::ErrorCode::Deprecated.into()); } let ident = stream.call(Ident::parse_any)?; @@ -316,7 +315,6 @@ pub struct ConstraintGroupBuilder<'ty> { pub mutable: Option>, pub signer: Option>, pub has_one: Vec>, - pub literal: Vec>, pub raw: Vec>, pub owner: Option>, pub rent_exempt: Option>, @@ -347,7 +345,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> { mutable: None, signer: None, has_one: Vec::new(), - literal: Vec::new(), raw: Vec::new(), owner: None, rent_exempt: None, @@ -525,7 +522,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> { mutable, signer, has_one, - literal, raw, owner, rent_exempt, @@ -633,7 +629,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> { mutable: into_inner!(mutable), signer: into_inner!(signer), has_one: into_inner_vec!(has_one), - literal: into_inner_vec!(literal), raw: into_inner_vec!(raw), owner: into_inner!(owner), rent_exempt: into_inner!(rent_exempt), @@ -653,7 +648,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> { ConstraintToken::Mut(c) => self.add_mut(c), ConstraintToken::Signer(c) => self.add_signer(c), ConstraintToken::HasOne(c) => self.add_has_one(c), - ConstraintToken::Literal(c) => self.add_literal(c), ConstraintToken::Raw(c) => self.add_raw(c), ConstraintToken::Owner(c) => self.add_owner(c), ConstraintToken::RentExempt(c) => self.add_rent_exempt(c), @@ -920,11 +914,6 @@ impl<'ty> ConstraintGroupBuilder<'ty> { Ok(()) } - fn add_literal(&mut self, c: Context) -> ParseResult<()> { - self.literal.push(c); - Ok(()) - } - fn add_raw(&mut self, c: Context) -> ParseResult<()> { self.raw.push(c); Ok(()) From db2f4987197cb5d588f1cab3e22b0536192e0b4e Mon Sep 17 00:00:00 2001 From: Nikhil B N Date: Tue, 15 Feb 2022 00:14:22 +0530 Subject: [PATCH 2/2] Replace `#[account("...")]` with `#[account(constraint = "...")]` Signed-off-by: Nikhil B N --- lang/src/idl.rs | 2 +- tests/lockup/programs/lockup/src/lib.rs | 2 +- tests/lockup/programs/registry/src/lib.rs | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lang/src/idl.rs b/lang/src/idl.rs index 8960ec1604..818658cc88 100644 --- a/lang/src/idl.rs +++ b/lang/src/idl.rs @@ -86,7 +86,7 @@ pub struct IdlSetBuffer<'info> { // // Note: we use the same account for the "write buffer", similar to the // bpf upgradeable loader's mechanism. -#[account("internal")] +#[account(constraint = internal)] #[derive(Debug)] pub struct IdlAccount { // Address that can modify the IDL. diff --git a/tests/lockup/programs/lockup/src/lib.rs b/tests/lockup/programs/lockup/src/lib.rs index d7013d861a..6b0d90b32c 100644 --- a/tests/lockup/programs/lockup/src/lib.rs +++ b/tests/lockup/programs/lockup/src/lib.rs @@ -293,7 +293,7 @@ pub struct WhitelistTransfer<'info> { bump = vesting.nonce, )] vesting_signer: AccountInfo<'info>, - #[account("token_program.key == &token::ID")] + #[account(constraint = token_program.key == &token::ID)] token_program: AccountInfo<'info>, #[account(mut)] whitelisted_program_vault: AccountInfo<'info>, diff --git a/tests/lockup/programs/registry/src/lib.rs b/tests/lockup/programs/registry/src/lib.rs index d0577b3411..80cd5c77d6 100644 --- a/tests/lockup/programs/registry/src/lib.rs +++ b/tests/lockup/programs/registry/src/lib.rs @@ -570,7 +570,7 @@ pub struct Initialize<'info> { registrar: Account<'info, Registrar>, #[account(zero)] reward_event_q: Account<'info, RewardQueue>, - #[account("pool_mint.decimals == 0")] + #[account(constraint = pool_mint.decimals == 0)] pool_mint: Account<'info, Mint>, } @@ -608,20 +608,20 @@ pub struct CreateMember<'info> { member: Box>, beneficiary: Signer<'info>, #[account( - "&balances.spt.owner == member_signer.key", - "balances.spt.mint == registrar.pool_mint", - "balances.vault.mint == registrar.mint" + constraint = &balances.spt.owner == member_signer.key, + constraint = balances.spt.mint == registrar.pool_mint, + constraint = balances.vault.mint == registrar.mint )] balances: BalanceSandboxAccounts<'info>, #[account( - "&balances_locked.spt.owner == member_signer.key", - "balances_locked.spt.mint == registrar.pool_mint", - "balances_locked.vault.mint == registrar.mint" + constraint = &balances_locked.spt.owner == member_signer.key, + constraint = balances_locked.spt.mint == registrar.pool_mint, + constraint = balances_locked.vault.mint == registrar.mint )] balances_locked: BalanceSandboxAccounts<'info>, member_signer: AccountInfo<'info>, // Misc. - #[account("token_program.key == &token::ID")] + #[account(constraint = token_program.key == &token::ID)] token_program: AccountInfo<'info>, } @@ -952,7 +952,7 @@ pub struct ClaimReward<'info> { pub struct ClaimRewardLocked<'info> { cmn: ClaimRewardCommon<'info>, registry: ProgramState<'info, Registry>, - #[account("lockup_program.key == ®istry.lockup_program")] + #[account(constraint = lockup_program.key == ®istry.lockup_program)] lockup_program: AccountInfo<'info>, }