From 3601add9474137df0fc13ed992908a1c7d701824 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Wed, 26 Jan 2022 14:21:08 -0500 Subject: [PATCH] update --- lang/src/accounts/account.rs | 7 ++ lang/src/accounts/account_loader.rs | 12 +-- lang/src/accounts/loader.rs | 11 +- lang/src/accounts/program_account.rs | 12 ++- lang/syn/src/codegen/accounts/constraints.rs | 100 +++++++++---------- lang/syn/src/idl/mod.rs | 1 + 6 files changed, 75 insertions(+), 68 deletions(-) diff --git a/lang/src/accounts/account.rs b/lang/src/accounts/account.rs index 8707f299..c4c3b2d0 100644 --- a/lang/src/accounts/account.rs +++ b/lang/src/accounts/account.rs @@ -406,3 +406,10 @@ impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> DerefMut for &mut self.account } } + +#[cfg(not(feature = "deprecated-layout"))] +impl<'a, T: AccountSerialize + AccountDeserialize + Owner + Clone> Bump for Account<'a, T> { + fn seed(&self) -> u8 { + self.info.data.borrow()[1] + } +} diff --git a/lang/src/accounts/account_loader.rs b/lang/src/accounts/account_loader.rs index 2aef9935..718c01cd 100644 --- a/lang/src/accounts/account_loader.rs +++ b/lang/src/accounts/account_loader.rs @@ -1,10 +1,7 @@ //! Type facilitating on demand zero copy deserialization. use crate::error::ErrorCode; -use crate::{ - Accounts, AccountsClose, AccountsExit, Bump, Owner, ToAccountInfo, ToAccountInfos, - ToAccountMetas, ZeroCopy, -}; +use crate::*; use arrayref::array_ref; use solana_program::account_info::AccountInfo; use solana_program::entrypoint::ProgramResult; @@ -245,11 +242,8 @@ impl<'info, T: ZeroCopy + Owner> ToAccountInfos<'info> for AccountLoader<'info, } #[cfg(not(feature = "deprecated-layout"))] -impl<'info, T> Bump for T -where - T: AsRef>, -{ +impl<'a, T: ZeroCopy + Owner> Bump for AccountLoader<'a, T> { fn seed(&self) -> u8 { - self.as_ref().data.borrow()[1] + self.acc_info.data.borrow()[1] } } diff --git a/lang/src/accounts/loader.rs b/lang/src/accounts/loader.rs index 293722b4..4e01df65 100644 --- a/lang/src/accounts/loader.rs +++ b/lang/src/accounts/loader.rs @@ -1,7 +1,5 @@ use crate::error::ErrorCode; -use crate::{ - Accounts, AccountsClose, AccountsExit, ToAccountInfo, ToAccountInfos, ToAccountMetas, ZeroCopy, -}; +use crate::*; use arrayref::array_ref; use solana_program::account_info::AccountInfo; use solana_program::entrypoint::ProgramResult; @@ -189,3 +187,10 @@ impl<'info, T: ZeroCopy> ToAccountInfos<'info> for Loader<'info, T> { vec![self.acc_info.clone()] } } + +#[cfg(not(feature = "deprecated-layout"))] +impl<'a, T: ZeroCopy> Bump for Loader<'a, T> { + fn seed(&self) -> u8 { + self.acc_info.data.borrow()[1] + } +} diff --git a/lang/src/accounts/program_account.rs b/lang/src/accounts/program_account.rs index f865ef38..9f0d3d62 100644 --- a/lang/src/accounts/program_account.rs +++ b/lang/src/accounts/program_account.rs @@ -1,10 +1,7 @@ #[allow(deprecated)] use crate::accounts::cpi_account::CpiAccount; use crate::error::ErrorCode; -use crate::{ - AccountDeserialize, AccountSerialize, Accounts, AccountsClose, AccountsExit, ToAccountInfo, - ToAccountInfos, ToAccountMetas, -}; +use crate::*; use solana_program::account_info::AccountInfo; use solana_program::entrypoint::ProgramResult; use solana_program::instruction::AccountMeta; @@ -182,3 +179,10 @@ where Self::new(a.to_account_info(), Deref::deref(&a).clone()) } } + +#[cfg(not(feature = "deprecated-layout"))] +impl<'a, T: AccountSerialize + AccountDeserialize + Clone> Bump for ProgramAccount<'a, T> { + fn seed(&self) -> u8 { + self.inner.info.data.borrow()[1] + } +} diff --git a/lang/syn/src/codegen/accounts/constraints.rs b/lang/syn/src/codegen/accounts/constraints.rs index 8391b8e6..9475a3fe 100644 --- a/lang/syn/src/codegen/accounts/constraints.rs +++ b/lang/syn/src/codegen/accounts/constraints.rs @@ -142,10 +142,6 @@ fn generate_constraint_address(f: &Field, c: &ConstraintAddress) -> proc_macro2: } } -pub fn generate_constraint_init(f: &Field, c: &ConstraintInitGroup) -> proc_macro2::TokenStream { - generate_constraint_init_group(f, c) -} - pub fn generate_constraint_zeroed(f: &Field, _c: &ConstraintZeroed) -> proc_macro2::TokenStream { let field = &f.ident; let ty_decl = f.ty_decl(); @@ -304,54 +300,6 @@ pub fn generate_constraint_rent_exempt( } } -fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_macro2::TokenStream { - let payer = { - let p = &c.payer; - quote! { - let payer = #p.to_account_info(); - } - }; - - let seeds_with_nonce = match &c.seeds { - None => quote! {}, - Some(c) => { - let s = &mut c.seeds.clone(); - // If the seeds came with a trailing comma, we need to chop it off - // before we interpolate them below. - if let Some(pair) = s.pop() { - s.push_value(pair.into_value()); - } - let maybe_seeds_plus_comma = (!s.is_empty()).then(|| { - quote! { #s, } - }); - let inner = match c.bump.as_ref() { - // Bump target not given. Use the canonical bump. - None => { - quote! { - [ - #maybe_seeds_plus_comma - &[ - Pubkey::find_program_address( - &[#s], - program_id, - ).1 - ][..] - ] - } - } - // Bump target given. Use it. - Some(b) => quote! { - [#maybe_seeds_plus_comma &[#b][..]] - }, - }; - quote! { - &#inner[..] - } - } - }; - generate_init(f, c.if_needed, seeds_with_nonce, payer, &c.space, &c.kind) -} - fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2::TokenStream { let name = &f.ident; let s = &mut c.seeds.clone(); @@ -442,6 +390,54 @@ fn generate_constraint_associated_token( } } +fn generate_constraint_init(f: &Field, c: &ConstraintInitGroup) -> proc_macro2::TokenStream { + let payer = { + let p = &c.payer; + quote! { + let payer = #p.to_account_info(); + } + }; + + let seeds_with_nonce = match &c.seeds { + None => quote! {}, + Some(c) => { + let s = &mut c.seeds.clone(); + // If the seeds came with a trailing comma, we need to chop it off + // before we interpolate them below. + if let Some(pair) = s.pop() { + s.push_value(pair.into_value()); + } + let maybe_seeds_plus_comma = (!s.is_empty()).then(|| { + quote! { #s, } + }); + let inner = match c.bump.as_ref() { + // Bump target not given. Use the canonical bump. + None => { + quote! { + [ + #maybe_seeds_plus_comma + &[ + Pubkey::find_program_address( + &[#s], + program_id, + ).1 + ][..] + ] + } + } + // Bump target given. Use it. + Some(b) => quote! { + [#maybe_seeds_plus_comma &[#b][..]] + }, + }; + quote! { + &#inner[..] + } + } + }; + generate_init(f, c.if_needed, seeds_with_nonce, payer, &c.space, &c.kind) +} + // `if_needed` is set if account allocation and initialization is optional. pub fn generate_init( f: &Field, diff --git a/lang/syn/src/idl/mod.rs b/lang/syn/src/idl/mod.rs index f77ac97c..01407a79 100644 --- a/lang/syn/src/idl/mod.rs +++ b/lang/syn/src/idl/mod.rs @@ -5,6 +5,7 @@ pub mod file; pub mod pda; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(rename_all = "camelCase")] pub struct Idl { // Version of the idl protocol. pub layout_version: String,