Skip to content

Commit

Permalink
Merge branch 'master' of github.com:project-serum/anchor into remove-…
Browse files Browse the repository at this point in the history
…depricated

Signed-off-by: Nikhil B N <[email protected]>
  • Loading branch information
NBNARADHYA committed Feb 14, 2022
2 parents e4aaf76 + d5e7e2a commit 07eb693
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 70 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ incremented for features.

## [Unreleased]

### Fixes

* ts: Allow nullable types for `Option<T>` mapped types ([#1428](https://github.com/project-serum/anchor/pull/1428)).

### Breaking

* lang: Enforce that the payer for an init-ed account be marked `mut` ([#1271](https://github.com/project-serum/anchor/pull/1271)).
* lang: All error-related code is now in the error module ([#1426](https://github.com/project-serum/anchor/pull/1426)).

## [0.21.0] - 2022-02-07

Expand Down
14 changes: 7 additions & 7 deletions lang/attribute/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ pub fn account(
impl #impl_gen anchor_lang::AccountDeserialize for #account_name #type_gen #where_clause {
fn try_deserialize(buf: &mut &[u8]) -> std::result::Result<Self, ProgramError> {
if buf.len() < #discriminator.len() {
return Err(anchor_lang::__private::ErrorCode::AccountDiscriminatorNotFound.into());
return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorNotFound.into());
}
let given_disc = &buf[..8];
if &#discriminator != given_disc {
return Err(anchor_lang::__private::ErrorCode::AccountDiscriminatorMismatch.into());
return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch.into());
}
Self::try_deserialize_unchecked(buf)
}
Expand All @@ -176,12 +176,12 @@ pub fn account(
#[automatically_derived]
impl #impl_gen anchor_lang::AccountSerialize for #account_name #type_gen #where_clause {
fn try_serialize<W: std::io::Write>(&self, writer: &mut W) -> std::result::Result<(), ProgramError> {
writer.write_all(&#discriminator).map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?;
writer.write_all(&#discriminator).map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?;
AnchorSerialize::serialize(
self,
writer
)
.map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?;
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?;
Ok(())
}
}
Expand All @@ -190,19 +190,19 @@ pub fn account(
impl #impl_gen anchor_lang::AccountDeserialize for #account_name #type_gen #where_clause {
fn try_deserialize(buf: &mut &[u8]) -> std::result::Result<Self, ProgramError> {
if buf.len() < #discriminator.len() {
return Err(anchor_lang::__private::ErrorCode::AccountDiscriminatorNotFound.into());
return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorNotFound.into());
}
let given_disc = &buf[..8];
if &#discriminator != given_disc {
return Err(anchor_lang::__private::ErrorCode::AccountDiscriminatorMismatch.into());
return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch.into());
}
Self::try_deserialize_unchecked(buf)
}

fn try_deserialize_unchecked(buf: &mut &[u8]) -> std::result::Result<Self, ProgramError> {
let mut data: &[u8] = &buf[8..];
AnchorDeserialize::deserialize(&mut data)
.map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotDeserialize.into())
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub fn interface(
#(#args_no_tys),*
};
let mut ix_data = anchor_lang::AnchorSerialize::try_to_vec(&ix)
.map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotSerialize)?;
.map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotSerialize)?;
let mut data = #sighash_tts.to_vec();
data.append(&mut ix_data);
let accounts = ctx.to_account_metas(None);
Expand Down
2 changes: 1 addition & 1 deletion lang/attribute/state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn state(
fn size(&self) -> std::result::Result<u64, anchor_lang::solana_program::program_error::ProgramError> {
Ok(8 + self
.try_to_vec()
.map_err(|_| anchor_lang::__private::ErrorCode::AccountDidNotSerialize)?
.map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotSerialize)?
.len() as u64)
}
}
Expand Down
5 changes: 4 additions & 1 deletion lang/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::error;

/// The starting point for user defined error codes.
pub const ERROR_CODE_OFFSET: u32 = 6000;

/// Error codes that can be returned by internal framework code.
///
/// - &gt;= 100 Instruction error codes
Expand All @@ -10,7 +13,7 @@ use crate::error;
/// - = 5000 deprecated error code
///
/// The starting point for user-defined errors is defined
/// by the [ERROR_CODE_OFFSET](crate::__private::ERROR_CODE_OFFSET).
/// by the [ERROR_CODE_OFFSET](crate::error::ERROR_CODE_OFFSET).
#[error(offset = 0)]
pub enum ErrorCode {
// Instructions
Expand Down
9 changes: 0 additions & 9 deletions lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,20 +271,11 @@ pub mod prelude {
/// Internal module used by macros and unstable apis.
#[doc(hidden)]
pub mod __private {
// Modules with useful information for users
// don't use #[doc(hidden)] on these
pub use crate::error::ErrorCode;

/// The discriminator anchor uses to mark an account as closed.
pub const CLOSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [255, 255, 255, 255, 255, 255, 255, 255];

/// The starting point for user defined error codes.
pub const ERROR_CODE_OFFSET: u32 = 6000;

pub use crate::ctor::Ctor;

pub use crate::error::Error;

pub use anchor_attribute_account::ZeroCopyAccessor;

pub use anchor_attribute_event::EventIndex;
Expand Down
53 changes: 26 additions & 27 deletions lang/syn/src/codegen/accounts/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::*;
use proc_macro2_diagnostics::SpanDiagnosticExt;
use quote::quote;
use syn::Expr;

Expand Down Expand Up @@ -28,7 +27,7 @@ pub fn generate_composite(f: &CompositeField) -> proc_macro2::TokenStream {
.iter()
.filter_map(|c| match c {
Constraint::Raw(_) => Some(c),
_ => panic!("Invariant violation: composite constraints can only be raw or literals"),
_ => panic!("Invariant violation: composite constraints can only be raw"),
})
.map(|c| generate_constraint_composite(f, c))
.collect();
Expand Down Expand Up @@ -152,7 +151,7 @@ pub fn generate_constraint_zeroed(f: &Field, _c: &ConstraintZeroed) -> proc_macr
__disc_bytes.copy_from_slice(&__data[..8]);
let __discriminator = u64::from_le_bytes(__disc_bytes);
if __discriminator != 0 {
return Err(anchor_lang::__private::ErrorCode::ConstraintZero.into());
return Err(anchor_lang::error::ErrorCode::ConstraintZero.into());
}
#from_account_info
};
Expand All @@ -164,7 +163,7 @@ pub fn generate_constraint_close(f: &Field, c: &ConstraintClose) -> proc_macro2:
let target = &c.sol_dest;
quote! {
if #field.key() == #target.key() {
return Err(anchor_lang::__private::ErrorCode::ConstraintClose.into());
return Err(anchor_lang::error::ErrorCode::ConstraintClose.into());
}
}
}
Expand Down Expand Up @@ -247,7 +246,7 @@ pub fn generate_constraint_rent_exempt(
ConstraintRentExempt::Skip => quote! {},
ConstraintRentExempt::Enforce => quote! {
if !__anchor_rent.is_exempt(#info.lamports(), #info.try_data_len()?) {
return Err(anchor_lang::__private::ErrorCode::ConstraintRentExempt.into());
return Err(anchor_lang::error::ErrorCode::ConstraintRentExempt.into());
}
},
}
Expand Down Expand Up @@ -344,10 +343,10 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma
let pa: #ty_decl = #from_account_info;
if #if_needed {
if pa.mint != #mint.key() {
return Err(anchor_lang::__private::ErrorCode::ConstraintTokenMint.into());
return Err(anchor_lang::error::ErrorCode::ConstraintTokenMint.into());
}
if pa.owner != #owner.key() {
return Err(anchor_lang::__private::ErrorCode::ConstraintTokenOwner.into());
return Err(anchor_lang::error::ErrorCode::ConstraintTokenOwner.into());
}
}
pa
Expand Down Expand Up @@ -379,14 +378,14 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma
let pa: #ty_decl = #from_account_info;
if #if_needed {
if pa.mint != #mint.key() {
return Err(anchor_lang::__private::ErrorCode::ConstraintTokenMint.into());
return Err(anchor_lang::error::ErrorCode::ConstraintTokenMint.into());
}
if pa.owner != #owner.key() {
return Err(anchor_lang::__private::ErrorCode::ConstraintTokenOwner.into());
return Err(anchor_lang::error::ErrorCode::ConstraintTokenOwner.into());
}

if pa.key() != anchor_spl::associated_token::get_associated_token_address(&#owner.key(), &#mint.key()) {
return Err(anchor_lang::__private::ErrorCode::AccountNotAssociatedTokenAccount.into());
return Err(anchor_lang::error::ErrorCode::AccountNotAssociatedTokenAccount.into());
}
}
pa
Expand Down Expand Up @@ -432,16 +431,16 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma
let pa: #ty_decl = #from_account_info;
if #if_needed {
if pa.mint_authority != anchor_lang::solana_program::program_option::COption::Some(#owner.key()) {
return Err(anchor_lang::__private::ErrorCode::ConstraintMintMintAuthority.into());
return Err(anchor_lang::error::ErrorCode::ConstraintMintMintAuthority.into());
}
if pa.freeze_authority
.as_ref()
.map(|fa| #freeze_authority.as_ref().map(|expected_fa| fa != *expected_fa).unwrap_or(true))
.unwrap_or(#freeze_authority.is_some()) {
return Err(anchor_lang::__private::ErrorCode::ConstraintMintFreezeAuthority.into());
return Err(anchor_lang::error::ErrorCode::ConstraintMintFreezeAuthority.into());
}
if pa.decimals != #decimals {
return Err(anchor_lang::__private::ErrorCode::ConstraintMintDecimals.into());
return Err(anchor_lang::error::ErrorCode::ConstraintMintDecimals.into());
}
}
pa
Expand Down Expand Up @@ -517,17 +516,17 @@ fn generate_constraint_init_group(f: &Field, c: &ConstraintInitGroup) -> proc_ma
// Assert the account was created correctly.
if #if_needed {
if space != actual_field.data_len() {
return Err(anchor_lang::__private::ErrorCode::ConstraintSpace.into());
return Err(anchor_lang::error::ErrorCode::ConstraintSpace.into());
}

if actual_owner != #owner {
return Err(anchor_lang::__private::ErrorCode::ConstraintOwner.into());
return Err(anchor_lang::error::ErrorCode::ConstraintOwner.into());
}

{
let required_lamports = __anchor_rent.minimum_balance(space);
if pa.to_account_info().lamports() < required_lamports {
return Err(anchor_lang::__private::ErrorCode::ConstraintRentExempt.into());
return Err(anchor_lang::error::ErrorCode::ConstraintRentExempt.into());
}
}
}
Expand Down Expand Up @@ -569,10 +568,10 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2
let b = c.bump.as_ref().unwrap();
quote! {
if #name.key() != __pda_address {
return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into());
return Err(anchor_lang::error::ErrorCode::ConstraintSeeds.into());
}
if __bump != #b {
return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into());
return Err(anchor_lang::error::ErrorCode::ConstraintSeeds.into());
}
}
}
Expand All @@ -584,7 +583,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2
else if c.is_init {
quote! {
if #name.key() != __pda_address {
return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into());
return Err(anchor_lang::error::ErrorCode::ConstraintSeeds.into());
}
}
}
Expand All @@ -607,7 +606,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2
let __pda_address = Pubkey::create_program_address(
&[#maybe_seeds_plus_comma &[#b][..]],
&#deriving_program_id,
).map_err(|_| anchor_lang::__private::ErrorCode::ConstraintSeeds)?;
).map_err(|_| anchor_lang::error::ErrorCode::ConstraintSeeds)?;
},
};
quote! {
Expand All @@ -616,7 +615,7 @@ fn generate_constraint_seeds(f: &Field, c: &ConstraintSeedsGroup) -> proc_macro2

// Check it.
if #name.key() != __pda_address {
return Err(anchor_lang::__private::ErrorCode::ConstraintSeeds.into());
return Err(anchor_lang::error::ErrorCode::ConstraintSeeds.into());
}
}
}
Expand All @@ -631,11 +630,11 @@ fn generate_constraint_associated_token(
let spl_token_mint_address = &c.mint;
quote! {
if #name.owner != #wallet_address.key() {
return Err(anchor_lang::__private::ErrorCode::ConstraintTokenOwner.into());
return Err(anchor_lang::error::ErrorCode::ConstraintTokenOwner.into());
}
let __associated_token_address = anchor_spl::associated_token::get_associated_token_address(&#wallet_address.key(), &#spl_token_mint_address.key());
if #name.key() != __associated_token_address {
return Err(anchor_lang::__private::ErrorCode::ConstraintAssociated.into());
return Err(anchor_lang::error::ErrorCode::ConstraintAssociated.into());
}
}
}
Expand Down Expand Up @@ -730,7 +729,7 @@ pub fn generate_constraint_executable(
let name = &f.ident;
quote! {
if !#name.to_account_info().executable {
return Err(anchor_lang::__private::ErrorCode::ConstraintExecutable.into());
return Err(anchor_lang::error::ErrorCode::ConstraintExecutable.into());
}
}
}
Expand All @@ -746,10 +745,10 @@ pub fn generate_constraint_state(f: &Field, c: &ConstraintState) -> proc_macro2:
// Checks the given state account is the canonical state account for
// the target program.
if #ident.key() != anchor_lang::accounts::cpi_state::CpiState::<#account_ty>::address(&#program_target.key()) {
return Err(anchor_lang::__private::ErrorCode::ConstraintState.into());
return Err(anchor_lang::error::ErrorCode::ConstraintState.into());
}
if #ident.as_ref().owner != &#program_target.key() {
return Err(anchor_lang::__private::ErrorCode::ConstraintState.into());
return Err(anchor_lang::error::ErrorCode::ConstraintState.into());
}
}
}
Expand All @@ -760,6 +759,6 @@ fn generate_custom_error(
) -> proc_macro2::TokenStream {
match custom_error {
Some(error) => quote! { #error.into() },
None => quote! { anchor_lang::__private::ErrorCode::#error.into() },
None => quote! { anchor_lang::error::ErrorCode::#error.into() },
}
}
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/accounts/try_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
let __Args {
#(#field_names),*
} = __Args::deserialize(&mut ix_data)
.map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?;
.map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotDeserialize)?;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn generate(error: Error) -> proc_macro2::TokenStream {
.collect();

let offset = match error.args {
None => quote! { anchor_lang::__private::ERROR_CODE_OFFSET},
None => quote! { anchor_lang::error::ERROR_CODE_OFFSET},
Some(args) => {
let offset = &args.offset;
quote! { #offset }
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/program/cpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let ix = {
let ix = instruction::#ix_variant;
let mut ix_data = AnchorSerialize::try_to_vec(&ix)
.map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotSerialize)?;
.map_err(|_| anchor_lang::error::ErrorCode::InstructionDidNotSerialize)?;
let mut data = #sighash_tts.to_vec();
data.append(&mut ix_data);
let accounts = ctx.to_account_metas(None);
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/program/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
})
.collect();
let fallback_fn = gen_fallback(program).unwrap_or(quote! {
Err(anchor_lang::__private::ErrorCode::InstructionFallbackNotFound.into())
Err(anchor_lang::error::ErrorCode::InstructionFallbackNotFound.into())
});
quote! {
/// Performs method dispatch.
Expand Down
2 changes: 1 addition & 1 deletion lang/syn/src/codegen/program/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use quote::quote;
pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let name: proc_macro2::TokenStream = program.name.to_string().to_camel_case().parse().unwrap();
let fallback_maybe = dispatch::gen_fallback(program).unwrap_or(quote! {
Err(anchor_lang::__private::ErrorCode::InstructionMissing.into());
Err(anchor_lang::error::ErrorCode::InstructionMissing.into());
});
quote! {
#[cfg(not(feature = "no-entrypoint"))]
Expand Down
Loading

0 comments on commit 07eb693

Please sign in to comment.