lang: Log instruction names (#1057)

This commit is contained in:
Paul 2021-11-24 19:46:17 +01:00 committed by GitHub
parent af9d9d2aa7
commit 8a30d87064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 1 deletions

View File

@ -18,6 +18,7 @@ incremented for features.
### Features ### Features
* lang: Add `ErrorCode::AccountNotInitialized` error to separate the situation when the account has the wrong owner from when it does not exist (#[1024](https://github.com/project-serum/anchor/pull/1024)) * lang: Add `ErrorCode::AccountNotInitialized` error to separate the situation when the account has the wrong owner from when it does not exist (#[1024](https://github.com/project-serum/anchor/pull/1024))
* lang: Called instructions now log their name by default. This can be turned off with the `no-log-ix-name` flag ([#1057](https://github.com/project-serum/anchor/pull/1057))
## [0.18.2] - 2021-11-14 ## [0.18.2] - 2021-11-14

View File

@ -61,6 +61,7 @@ name = "{1}"
[features] [features]
no-entrypoint = [] no-entrypoint = []
no-idl = [] no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"] cpi = ["no-entrypoint"]
default = [] default = []

View File

@ -72,6 +72,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &mut anchor_lang::idl::IdlCreateAccounts, accounts: &mut anchor_lang::idl::IdlCreateAccounts,
data_len: u64, data_len: u64,
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!("Instruction: IdlCreateAccount");
if program_id != accounts.program.key { if program_id != accounts.program.key {
return Err(anchor_lang::__private::ErrorCode::IdlInstructionInvalidProgram.into()); return Err(anchor_lang::__private::ErrorCode::IdlInstructionInvalidProgram.into());
} }
@ -132,6 +135,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
program_id: &Pubkey, program_id: &Pubkey,
accounts: &mut anchor_lang::idl::IdlCreateBuffer, accounts: &mut anchor_lang::idl::IdlCreateBuffer,
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!("Instruction: IdlCreateBuffer");
let mut buffer = &mut accounts.buffer; let mut buffer = &mut accounts.buffer;
buffer.authority = *accounts.authority.key; buffer.authority = *accounts.authority.key;
Ok(()) Ok(())
@ -143,6 +149,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &mut anchor_lang::idl::IdlAccounts, accounts: &mut anchor_lang::idl::IdlAccounts,
idl_data: Vec<u8>, idl_data: Vec<u8>,
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!("Instruction: IdlWrite");
let mut idl = &mut accounts.idl; let mut idl = &mut accounts.idl;
idl.data.extend(idl_data); idl.data.extend(idl_data);
Ok(()) Ok(())
@ -154,6 +163,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &mut anchor_lang::idl::IdlAccounts, accounts: &mut anchor_lang::idl::IdlAccounts,
new_authority: Pubkey, new_authority: Pubkey,
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!("Instruction: IdlSetAuthority");
accounts.idl.authority = new_authority; accounts.idl.authority = new_authority;
Ok(()) Ok(())
} }
@ -163,6 +175,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
program_id: &Pubkey, program_id: &Pubkey,
accounts: &mut anchor_lang::idl::IdlSetBuffer, accounts: &mut anchor_lang::idl::IdlSetBuffer,
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!("Instruction: IdlSetBuffer");
accounts.idl.data = accounts.buffer.data.clone(); accounts.idl.data = accounts.buffer.data.clone();
Ok(()) Ok(())
} }
@ -180,12 +195,16 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let variant_arm = generate_ctor_variant(state); let variant_arm = generate_ctor_variant(state);
let ix_name: proc_macro2::TokenStream = let ix_name: proc_macro2::TokenStream =
generate_ctor_variant_name().parse().unwrap(); generate_ctor_variant_name().parse().unwrap();
let ix_name_log = format!("Instruction: {}", ix_name);
if state.is_zero_copy { if state.is_zero_copy {
quote! { quote! {
// One time state account initializer. Will faill on subsequent // One time state account initializer. Will faill on subsequent
// invocations. // invocations.
#[inline(never)] #[inline(never)]
pub fn __ctor(program_id: &Pubkey, accounts: &[AccountInfo], ix_data: &[u8]) -> ProgramResult { pub fn __ctor(program_id: &Pubkey, accounts: &[AccountInfo], ix_data: &[u8]) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!(#ix_name_log);
// Deserialize instruction data. // Deserialize instruction data.
let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..]) let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..])
.map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?;
@ -257,6 +276,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
// invocations. // invocations.
#[inline(never)] #[inline(never)]
pub fn __ctor(program_id: &Pubkey, accounts: &[AccountInfo], ix_data: &[u8]) -> ProgramResult { pub fn __ctor(program_id: &Pubkey, accounts: &[AccountInfo], ix_data: &[u8]) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!(#ix_name_log);
// Deserialize instruction data. // Deserialize instruction data.
let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..]) let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..])
.map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?;
@ -347,6 +369,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let variant_arm = let variant_arm =
generate_ix_variant(ix.raw_method.sig.ident.to_string(), &ix.args); generate_ix_variant(ix.raw_method.sig.ident.to_string(), &ix.args);
let ix_name = generate_ix_variant_name(ix.raw_method.sig.ident.to_string()); let ix_name = generate_ix_variant_name(ix.raw_method.sig.ident.to_string());
let ix_name_log = format!("Instruction: {}", ix_name);
if state.is_zero_copy { if state.is_zero_copy {
quote! { quote! {
@ -356,6 +379,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &[AccountInfo], accounts: &[AccountInfo],
ix_data: &[u8], ix_data: &[u8],
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!(#ix_name_log);
// Deserialize instruction. // Deserialize instruction.
let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..]) let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..])
.map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?;
@ -399,6 +425,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &[AccountInfo], accounts: &[AccountInfo],
ix_data: &[u8], ix_data: &[u8],
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!(#ix_name_log);
// Deserialize instruction. // Deserialize instruction.
let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..]) let ix = instruction::state::#ix_name::deserialize(&mut &ix_data[..])
.map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?;
@ -472,6 +501,8 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let ix_method_name = &ix.raw_method.sig.ident; let ix_method_name = &ix.raw_method.sig.ident;
let state_ty: proc_macro2::TokenStream = state.name.parse().unwrap(); let state_ty: proc_macro2::TokenStream = state.name.parse().unwrap();
let anchor_ident = &ix.anchor_ident; let anchor_ident = &ix.anchor_ident;
let ix_name = generate_ix_variant_name(ix.raw_method.sig.ident.to_string());
let ix_name_log = format!("Instruction: {}", ix_name);
let raw_args: Vec<&syn::PatType> = ix let raw_args: Vec<&syn::PatType> = ix
.args .args
@ -511,6 +542,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &[AccountInfo], accounts: &[AccountInfo],
ix_data: &[u8], ix_data: &[u8],
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!(#ix_name_log);
// Deserialize instruction. // Deserialize instruction.
#deserialize_instruction #deserialize_instruction
@ -555,6 +589,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &[AccountInfo], accounts: &[AccountInfo],
ix_data: &[u8], ix_data: &[u8],
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!(#ix_name_log);
// Deserialize instruction. // Deserialize instruction.
#deserialize_instruction #deserialize_instruction
@ -593,7 +630,7 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
let ix_method_name = &ix.raw_method.sig.ident; let ix_method_name = &ix.raw_method.sig.ident;
let anchor = &ix.anchor_ident; let anchor = &ix.anchor_ident;
let variant_arm = generate_ix_variant(ix.raw_method.sig.ident.to_string(), &ix.args); let variant_arm = generate_ix_variant(ix.raw_method.sig.ident.to_string(), &ix.args);
let ix_name_log = format!("Instruction: {}", ix_name);
quote! { quote! {
#[inline(never)] #[inline(never)]
pub fn #ix_method_name( pub fn #ix_method_name(
@ -601,6 +638,9 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &[AccountInfo], accounts: &[AccountInfo],
ix_data: &[u8], ix_data: &[u8],
) -> ProgramResult { ) -> ProgramResult {
#[cfg(not(feature = "no-log-ix-name"))]
anchor_lang::prelude::msg!(#ix_name_log);
// Deserialize data. // Deserialize data.
let ix = instruction::#ix_name::deserialize(&mut &ix_data[..]) let ix = instruction::#ix_name::deserialize(&mut &ix_data[..])
.map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?; .map_err(|_| anchor_lang::__private::ErrorCode::InstructionDidNotDeserialize)?;