From 454a445dabeeea7e76c593c6519997c5706a2876 Mon Sep 17 00:00:00 2001 From: "Jean Marchand (Exotic Markets)" Date: Wed, 9 Oct 2024 12:44:17 +0700 Subject: [PATCH] thread: Fix min-space calculation (#75) --- programs/thread/src/instructions/thread_create.rs | 2 +- programs/thread/src/state/thread.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/programs/thread/src/instructions/thread_create.rs b/programs/thread/src/instructions/thread_create.rs index 7f7d5b20..c279437f 100644 --- a/programs/thread/src/instructions/thread_create.rs +++ b/programs/thread/src/instructions/thread_create.rs @@ -30,7 +30,7 @@ pub struct ThreadCreate<'info> { domain.as_ref().unwrap_or(&Vec::new()).as_slice() ], bump, - payer= payer, + payer = payer, space = Thread::min_space(&instructions)? )] pub thread: Account<'info, Thread>, diff --git a/programs/thread/src/state/thread.rs b/programs/thread/src/state/thread.rs index d2e5905e..dc81ac64 100644 --- a/programs/thread/src/state/thread.rs +++ b/programs/thread/src/state/thread.rs @@ -71,8 +71,12 @@ pub trait ThreadAccount { impl Thread { pub fn min_space(instructions: &[SerializableInstruction]) -> Result { - let ins_number = instructions.len(); let ins_space = instructions.try_to_vec()?.len(); + let max_ins_size = instructions + .iter() + .map(|ins| ins.try_to_vec().map(|v| v.len()).unwrap_or(0)) + .max() + .unwrap_or(0); Ok( 8 @@ -84,7 +88,7 @@ impl Thread { + u64::MIN_SPACE // fee + (4 + 32) // id + (4 + ins_space) // instructions - + (1 + ins_space / ins_number) // next_instruction + + (1 + max_ins_size) // next_instruction + bool::MIN_SPACE // paused + u64::MIN_SPACE // rate_limit + Trigger::MIN_SPACE, // trigger