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

Reason for stack violation warnings inside anchor macro generation #63

Open
sunguru98 opened this issue Apr 16, 2024 · 2 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@sunguru98
Copy link
Contributor

sunguru98 commented Apr 16, 2024

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
Copy link
Contributor Author

sunguru98 commented Apr 16, 2024

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
Copy link
Contributor

resolved by

coral-xyz/anchor#2913

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants