2
Error: Function _ZN137_$LT$solana_randomness_consumer..InitiateWheel$u20$as$u20$anchor_lang..Accounts$LT$solana_randomness_consumer..InitiateWheelBumps$GT$$GT$12try_accounts17h8805a14f72c43977E Stack offset of 4376 exceeded max offset of 4096 by 280 bytes, please minimize large stack variables

I keep getting this error, and my calls to the smart contract fail because of it. I boxed every single possible account in the account, I try boxing other structs but it doesn't affect it at all.

I am not quite sure what I can do here to be honest. Since if I edit other structs it doesn't affect the amount of bytes that is exceeded. Only InitiateWheel matters.

3 Answers 3

1

An option is to add #[inline(never)] to any functions that InitiateWheel` might call into.

For example, the spl-stake-pool program was having issues in its main processor function because the compiler was inlining everything, causing the stack to be too big, so we added that directive to some functions, ie https://github.com/solana-labs/solana-program-library/blob/4f1668510adef2117ee3c043bd26b79789e67c8d/stake-pool/program/src/processor.rs#L712

0

If you have big structs you can use zero copy and move everything to the heap.

This struct for example would not fit on the stack, but with zero copy you can access it directly in memory:

const BOARD_SIZE_X: usize = 10;
const BOARD_SIZE_Y: usize = 10;

#[account(zero_copy(unsafe))]
#[repr(packed)]
#[derive(Default)]
pub struct GameDataAccount {
    board: [[Tile; BOARD_SIZE_X]; BOARD_SIZE_Y],
    action_id: u64,
}

#[zero_copy(unsafe)]
#[repr(packed)]
#[derive(Default)]
pub struct Tile {
    player: Pubkey,      // 32
    state: u8,           // 1
    health: u64,         // 8
    damage: u64,         // 8
    range: u16,          // 2
    collect_reward: u64, // 8
    avatar: Pubkey,      // 32 used in the client to display the avatar
    look_direction: u8,  // 1 (Up, right, down, left)
    ship_level: u16,     // 2
    start_health: u64,   // 8 (used to calculate the length of the health bar in the client)
}

Here is an example project https://github.com/solana-developers/anchor-zero-copy-example and a video walkthrough: https://www.youtube.com/watch?v=zs_yU0IuJxc&t=382s

0

Quote from Program Architecture https://solana.com/developers/courses/program-optimization/program-architecture:

Use Box to fix it

#[account]
pub struct SomeBigDataStruct {
    pub big_data: [u8; 5000],
}
 
#[derive(Accounts)]
pub struct SomeFunctionContext<'info> {
    pub some_big_data: Box<Account<'info, SomeBigDataStruct>>, // <- Box Added!
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.