Skip to content

Commit

Permalink
lang: Remove the fallback function shortcut in try_entry function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Jul 24, 2024
1 parent ccac42d commit c06ec44
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Add non-8-byte discriminator support in `declare_program!` ([#3103](https://github.com/coral-xyz/anchor/pull/3103)).
- client: Make `ThreadSafeSigner` trait public ([#3107](https://github.com/coral-xyz/anchor/pull/3107)).
- lang: Update `dispatch` function to support dynamic discriminators ([#3104](https://github.com/coral-xyz/anchor/pull/3104)).
- lang: Remove the fallback function shortcut in `try_entry` function ([#3109](https://github.com/coral-xyz/anchor/pull/3109)).

### Fixes

Expand Down
65 changes: 32 additions & 33 deletions lang/syn/src/codegen/program/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,39 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
}
});

let fallback_fn = gen_fallback(program).unwrap_or(quote! {
Err(anchor_lang::error::ErrorCode::InstructionFallbackNotFound.into())
});
// Generate the event-cpi instruction handler based on whether the `event-cpi` feature is enabled.
let event_cpi_handler = {
#[cfg(feature = "event-cpi")]
quote! {
// `event-cpi` feature is enabled, dispatch self-cpi instruction
__private::__events::__event_dispatch(
program_id,
accounts,
&data[anchor_lang::event::EVENT_IX_TAG_LE.len()..]
)
}
#[cfg(not(feature = "event-cpi"))]
quote! {
// `event-cpi` feature is not enabled
Err(anchor_lang::error::ErrorCode::EventInstructionStub.into())
}
};

let event_cpi_handler = generate_event_cpi_handler();
let fallback_fn = program
.fallback_fn
.as_ref()
.map(|fallback_fn| {
let program_name = &program.name;
let fn_name = &fallback_fn.raw_method.sig.ident;
quote! {
#program_name::#fn_name(program_id, accounts, data)
}
})
.unwrap_or_else(|| {
quote! {
Err(anchor_lang::error::ErrorCode::InstructionFallbackNotFound.into())
}
});

quote! {
/// Performs method dispatch.
Expand Down Expand Up @@ -77,32 +105,3 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
}
}
}

pub fn gen_fallback(program: &Program) -> Option<proc_macro2::TokenStream> {
program.fallback_fn.as_ref().map(|fallback_fn| {
let program_name = &program.name;
let method = &fallback_fn.raw_method;
let fn_name = &method.sig.ident;
quote! {
#program_name::#fn_name(program_id, accounts, data)
}
})
}

/// Generate the event-cpi instruction handler based on whether the `event-cpi` feature is enabled.
pub fn generate_event_cpi_handler() -> proc_macro2::TokenStream {
#[cfg(feature = "event-cpi")]
quote! {
// `event-cpi` feature is enabled, dispatch self-cpi instruction
__private::__events::__event_dispatch(
program_id,
accounts,
&data[anchor_lang::event::EVENT_IX_TAG_LE.len()..]
)
}
#[cfg(not(feature = "event-cpi"))]
quote! {
// `event-cpi` feature is not enabled
Err(anchor_lang::error::ErrorCode::EventInstructionStub.into())
}
}
7 changes: 0 additions & 7 deletions lang/syn/src/codegen/program/entry.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use crate::program_codegen::dispatch;
use crate::Program;
use heck::CamelCase;
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::error::ErrorCode::InstructionMissing.into())
});
quote! {
#[cfg(not(feature = "no-entrypoint"))]
anchor_lang::solana_program::entrypoint!(entry);
Expand Down Expand Up @@ -62,9 +58,6 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
if *program_id != ID {
return Err(anchor_lang::error::ErrorCode::DeclaredProgramIdMismatch.into());
}
if data.len() < 8 {
return #fallback_maybe;
}

dispatch(program_id, accounts, data)
}
Expand Down

0 comments on commit c06ec44

Please sign in to comment.