如何避免我的锚程序抛出 "Access violation in stack frame"?

How do I avoid my anchor program throwing an "Access violation in stack frame"?

我的 Anchor 程序有一个如下所示的指令结构:

#[derive(Accounts)]
pub struct MyInstruction<'info> {
  pub my_account: Account<'info, MyAccount>,

  // ...
}

#[account]
pub struct MyAccount {
  // ... Many different fields
}

当我尝试 运行 一条使用该结构的指令时,我得到一个奇怪的堆栈错误,如下所示:

Program failed to complete: Access violation in stack frame 3 at address 0x200003fe0 of size 8 by instruction #28386

什么给了?

A​​nchor 默认将您的帐户放在堆栈上。但是,很可能是因为您的帐户很大,或者您有很多帐户,所以您在堆栈中是 space 中的 运行。

如果您查看上面的日志,您可能会遇到如下所示的错误:

Stack offset of -4128 exceeded max offset of -4096 by 32 bytes, please minimize large stack variables

要解决此问题,您可以尝试 Boxing 您的帐户结构,将它们移动到堆中:

#[derive(Accounts)]
pub struct MyInstruction<'info> {
  // Note the Box<>! 
  pub my_account: Box<Account<'info, MyAccount>>,
}