Skip to content

Commit

Permalink
thread: Fix min-space calculation (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen authored Oct 9, 2024
1 parent edf8b37 commit 454a445
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion programs/thread/src/instructions/thread_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
8 changes: 6 additions & 2 deletions programs/thread/src/state/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ pub trait ThreadAccount {

impl Thread {
pub fn min_space(instructions: &[SerializableInstruction]) -> Result<usize> {
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
Expand All @@ -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
Expand Down

0 comments on commit 454a445

Please sign in to comment.