Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: Fix cpi feature instructions not accounting for discriminator overrides #3376

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Fix `shell` command failing due to outdated program initialization ([#3351](https://github.com/coral-xyz/anchor/pull/3351)).
- idl: Fix detecting false-positives from doc comments during module path conversion ([#3359](https://github.com/coral-xyz/anchor/pull/3359)).
- cli: Remove passing the rent sysvar account to IDL instructions ([#3372](https://github.com/coral-xyz/anchor/pull/3372)).
- lang: Fix `cpi` feature instructions not accounting for discriminator overrides ([#3376](https://github.com/coral-xyz/anchor/pull/3376)).

### Breaking

Expand Down
12 changes: 7 additions & 5 deletions lang/syn/src/codegen/program/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ pub fn gen_discriminator(namespace: &str, name: impl ToString) -> proc_macro2::T
format!("&{:?}", discriminator).parse().unwrap()
}

pub fn generate_ix_variant(name: String, args: &[IxArg]) -> proc_macro2::TokenStream {
pub fn generate_ix_variant(name: &str, args: &[IxArg]) -> proc_macro2::TokenStream {
let ix_arg_names: Vec<&syn::Ident> = args.iter().map(|arg| &arg.name).collect();
let ix_name_camel: proc_macro2::TokenStream = {
let n = name.to_camel_case();
n.parse().unwrap()
};
let ix_name_camel = generate_ix_variant_name(name);

if args.is_empty() {
quote! {
Expand All @@ -42,3 +39,8 @@ pub fn generate_ix_variant(name: String, args: &[IxArg]) -> proc_macro2::TokenSt
}
}
}

pub fn generate_ix_variant_name(name: &str) -> proc_macro2::TokenStream {
let n = name.to_camel_case();
n.parse().unwrap()
}
12 changes: 7 additions & 5 deletions lang/syn/src/codegen/program/cpi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::codegen::program::common::{
gen_discriminator, generate_ix_variant, SIGHASH_GLOBAL_NAMESPACE,
};
use crate::codegen::program::common::{generate_ix_variant, generate_ix_variant_name};
use crate::Program;
use heck::SnakeCase;
use quote::{quote, ToTokens};
Expand All @@ -14,10 +12,14 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let accounts_ident: proc_macro2::TokenStream = format!("crate::cpi::accounts::{}", &ix.anchor_ident.to_string()).parse().unwrap();
let cpi_method = {
let name = &ix.raw_method.sig.ident;
let ix_variant = generate_ix_variant(name.to_string(), &ix.args);
let name_str = name.to_string();
let ix_variant = generate_ix_variant(&name_str, &ix.args);
let method_name = &ix.ident;
let args: Vec<&syn::PatType> = ix.args.iter().map(|arg| &arg.raw_arg).collect();
let discriminator = gen_discriminator(SIGHASH_GLOBAL_NAMESPACE, name);
let discriminator = {
let name = generate_ix_variant_name(&name_str);
quote! { <instruction::#name as anchor_lang::Discriminator>::DISCRIMINATOR }
};
let ret_type = &ix.returns.ty.to_token_stream();
let ix_cfgs = &ix.cfgs;
let (method_ret, maybe_return) = match ret_type.to_string().as_str() {
Expand Down
13 changes: 4 additions & 9 deletions lang/syn/src/codegen/program/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::codegen::program::common::*;
use crate::program_codegen::idl::idl_accounts_and_functions;
use crate::Program;
use heck::CamelCase;
use quote::{quote, ToTokens};

// Generate non-inlined wrappers for each instruction handler, since Solana's
Expand Down Expand Up @@ -98,11 +97,12 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
.iter()
.map(|ix| {
let ix_arg_names: Vec<&syn::Ident> = ix.args.iter().map(|arg| &arg.name).collect();
let ix_name = generate_ix_variant_name(ix.raw_method.sig.ident.to_string());
let ix_method_name = &ix.raw_method.sig.ident;
let anchor = &ix.anchor_ident;
let variant_arm = generate_ix_variant(ix.raw_method.sig.ident.to_string(), &ix.args);
let ix_method_name_str = ix_method_name.to_string();
let ix_name = generate_ix_variant_name(&ix_method_name_str);
let variant_arm = generate_ix_variant(&ix_method_name_str, &ix.args);
let ix_name_log = format!("Instruction: {ix_name}");
let anchor = &ix.anchor_ident;
let ret_type = &ix.returns.ty.to_token_stream();
let cfgs = &ix.cfgs;
let maybe_set_return_data = match ret_type.to_string().as_str() {
Expand Down Expand Up @@ -191,11 +191,6 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
}
}

fn generate_ix_variant_name(name: String) -> proc_macro2::TokenStream {
let n = name.to_camel_case();
n.parse().unwrap()
}

/// Generate the event module based on whether the `event-cpi` feature is enabled.
fn generate_event_cpi_mod() -> proc_macro2::TokenStream {
#[cfg(feature = "event-cpi")]
Expand Down
Loading