wen-community / wen-program-library

Apache License 2.0
90 stars 20 forks source link

Reason for stack violation warnings inside anchor macro generation #63

Open sunguru98 opened 4 months ago

sunguru98 commented 4 months ago

The anchor token extensions macro inside the Accounts struct was triggering the token extensions stack violations warnings after fiddling with the expanded code using cargo expand

The following code creates the "too many large-sized variables" warning

for e in extensions {
      match e {
            ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::GroupPointer => {
                let cpi_program = token_program.to_account_info();
                let accounts = ::anchor_spl::token_interface::GroupPointerInitialize {
                    token_program_id: token_program.to_account_info(),
                    mint: mint.to_account_info(),
                };
                let cpi_ctx = anchor_lang::context::CpiContext::new(
                    cpi_program,
                    accounts,
                );
                ::anchor_spl::token_interface::group_pointer_initialize(
                    cpi_ctx,
                    Option::<anchor_lang::prelude::Pubkey>::None,
                    Option::<anchor_lang::prelude::Pubkey>::None,
                )?;
            }
            ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::GroupMemberPointer => {
                let cpi_program = token_program.to_account_info();
                let accounts = ::anchor_spl::token_interface::GroupMemberPointerInitialize {
                    token_program_id: token_program.to_account_info(),
                    mint: mint.to_account_info(),
                };
                let cpi_ctx = anchor_lang::context::CpiContext::new(
                    cpi_program,
                    accounts,
                );
                ::anchor_spl::token_interface::group_member_pointer_initialize(
                    cpi_ctx,
                    Option::<anchor_lang::prelude::Pubkey>::Some(manager.key()),
                    Option::<anchor_lang::prelude::Pubkey>::None,
                )?;
            }
            ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::MetadataPointer => {
                let cpi_program = token_program.to_account_info();
                let accounts = ::anchor_spl::token_interface::MetadataPointerInitialize {
                    token_program_id: token_program.to_account_info(),
                    mint: mint.to_account_info(),
                };
                let cpi_ctx = anchor_lang::context::CpiContext::new(
                    cpi_program,
                    accounts,
                );
                ::anchor_spl::token_interface::metadata_pointer_initialize(
                    cpi_ctx,
                    Option::<
                        anchor_lang::prelude::Pubkey,
                    >::Some(authority.key()),
                    Option::<anchor_lang::prelude::Pubkey>::Some(mint.key()),
                )?;
            }
            ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::MintCloseAuthority => {
                let cpi_program = token_program.to_account_info();
                let accounts = ::anchor_spl::token_interface::MintCloseAuthorityInitialize {
                    token_program_id: token_program.to_account_info(),
                    mint: mint.to_account_info(),
                };
                let cpi_ctx = anchor_lang::context::CpiContext::new(
                    cpi_program,
                    accounts,
                );
                ::anchor_spl::token_interface::mint_close_authority_initialize(
                    cpi_ctx,
                    Option::<
                        &anchor_lang::prelude::Pubkey,
                    >::Some(&manager.key()),
                )?;
            }
            ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::TransferHook => {
                let cpi_program = token_program.to_account_info();
                let accounts = ::anchor_spl::token_interface::TransferHookInitialize {
                    token_program_id: token_program.to_account_info(),
                    mint: mint.to_account_info(),
                };
                let cpi_ctx = anchor_lang::context::CpiContext::new(
                    cpi_program,
                    accounts,
                );
                ::anchor_spl::token_interface::transfer_hook_initialize(
                    cpi_ctx,
                    Option::<
                        anchor_lang::prelude::Pubkey,
                    >::Some(authority.key()),
                    Option::<anchor_lang::prelude::Pubkey>::None,
                )?;
            }
            ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::NonTransferable => {
                let cpi_program = token_program.to_account_info();
                let accounts = ::anchor_spl::token_interface::NonTransferableMintInitialize {
                    token_program_id: token_program.to_account_info(),
                    mint: mint.to_account_info(),
                };
                let cpi_ctx = anchor_lang::context::CpiContext::new(
                    cpi_program,
                    accounts,
                );
                ::anchor_spl::token_interface::non_transferable_mint_initialize(
                    cpi_ctx,
                )?;
            }
            ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::PermanentDelegate => {
                let cpi_program = token_program.to_account_info();
                let accounts = ::anchor_spl::token_interface::PermanentDelegateInitialize {
                    token_program_id: token_program.to_account_info(),
                    mint: mint.to_account_info(),
                };
                let cpi_ctx = anchor_lang::context::CpiContext::new(
                    cpi_program,
                    accounts,
                );
                ::anchor_spl::token_interface::permanent_delegate_initialize(
                    cpi_ctx,
                    Option::<
                        &anchor_lang::prelude::Pubkey,
                    >::Some(
                            &args
                                .permanent_delegate
                                .unwrap_or_else(|| manager.key())
                                .key(),
                        )
                        .unwrap(),
                )?;
            }
            _ => {}
     }
}
sunguru98 commented 4 months ago

The match arms's variables were causing overload when too many extensions were iterated through. This is why, when fewer extensions were passed the warning wasn't occurring. The following minimalistic change immediately fixed that warning

for e in extensions {
      match e {
          ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::GroupPointer => {
              ::anchor_spl::token_interface::group_pointer_initialize(
                  anchor_lang::context::CpiContext::new(
                  token_program.to_account_info(),
                  ::anchor_spl::token_interface::GroupPointerInitialize {
                  token_program_id: token_program.to_account_info(),
                  mint: mint.to_account_info(),
              },
              ),
                  Option::<anchor_lang::prelude::Pubkey>::None,
                  Option::<anchor_lang::prelude::Pubkey>::None,
              )?;
          }
          ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::GroupMemberPointer => {
              ::anchor_spl::token_interface::group_member_pointer_initialize(
                  anchor_lang::context::CpiContext::new(
                  token_program.to_account_info(),
                  ::anchor_spl::token_interface::GroupMemberPointerInitialize {
                  token_program_id: token_program.to_account_info(),
                  mint: mint.to_account_info(),
              },
              ),
                  Option::<anchor_lang::prelude::Pubkey>::Some(manager.key()),
                  Option::<anchor_lang::prelude::Pubkey>::None,
              )?;
          }
          ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::MetadataPointer => {
              ::anchor_spl::token_interface::metadata_pointer_initialize(
                  anchor_lang::context::CpiContext::new(
                  token_program.to_account_info(),
                  ::anchor_spl::token_interface::MetadataPointerInitialize {
                  token_program_id: token_program.to_account_info(),
                  mint: mint.to_account_info(),
              },
              ),
                  Option::<
                      anchor_lang::prelude::Pubkey,
                  >::Some(authority.key()),
                  Option::<anchor_lang::prelude::Pubkey>::Some(mint.key()),
              )?;
          }
          ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::MintCloseAuthority => {
              ::anchor_spl::token_interface::mint_close_authority_initialize(
                  anchor_lang::context::CpiContext::new(
                  token_program.to_account_info(),
                  ::anchor_spl::token_interface::MintCloseAuthorityInitialize {
                  token_program_id: token_program.to_account_info(),
                  mint: mint.to_account_info(),
              },
              ),
                  Option::<
                      &anchor_lang::prelude::Pubkey,
                  >::Some(&manager.key()),
              )?;
          }
          ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::TransferHook => {
              ::anchor_spl::token_interface::transfer_hook_initialize(
                  anchor_lang::context::CpiContext::new(
                  token_program.to_account_info(),
                  ::anchor_spl::token_interface::TransferHookInitialize {
                  token_program_id: token_program.to_account_info(),
                  mint: mint.to_account_info(),
              },
              ),
                  Option::<
                      anchor_lang::prelude::Pubkey,
                  >::Some(authority.key()),
                  Option::<anchor_lang::prelude::Pubkey>::None,
              )?;
          }
          ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::NonTransferable => {
              ::anchor_spl::token_interface::non_transferable_mint_initialize(
                  anchor_lang::context::CpiContext::new(
                  token_program.to_account_info(),
                  ::anchor_spl::token_interface::NonTransferableMintInitialize {
                  token_program_id: token_program.to_account_info(),
                  mint: mint.to_account_info(),
              },
              ),
              )?;
          }
          ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::PermanentDelegate => {
              ::anchor_spl::token_interface::permanent_delegate_initialize(
                  anchor_lang::context::CpiContext::new(
                  token_program.to_account_info(),
                  ::anchor_spl::token_interface::PermanentDelegateInitialize {
                  token_program_id: token_program.to_account_info(),
                  mint: mint.to_account_info(),
              },
              ),
                  Option::<
                      &anchor_lang::prelude::Pubkey,
                  >::Some(
                          &args
                              .permanent_delegate
                              .unwrap_or_else(|| manager.key())
                              .key(),
                      )
                      .unwrap(),
              )?;
          }
          _ => {}
      }
  }

We just need to find the macro generation part in anchor-spl to update towards the latest fix and that should probably remove the warning.

kespinola commented 4 months ago

resolved by

https://github.com/coral-xyz/anchor/pull/2913

We are still waiting for the next release of anchor to make use of it in WNS.