antegen_thread_program/state/
fiber.rs

1use crate::*;
2use anchor_lang::prelude::*;
3use anchor_lang::solana_program::instruction::Instruction;
4
5/// Trait for processing fiber instructions
6pub trait FiberInstructionProcessor {
7    /// Get the decompiled instruction from the fiber's compiled data,
8    /// replacing PAYER_PUBKEY with the provided executor
9    fn get_instruction(&self, executor: &Pubkey) -> Result<Instruction>;
10}
11
12/// Represents a single fiber (instruction) in a thread's execution sequence.
13#[account]
14#[derive(Debug, InitSpace)]
15pub struct FiberState {
16    /// The thread this fiber belongs to
17    pub thread: Pubkey,
18    /// The index of this fiber in the thread's execution sequence
19    pub fiber_index: u8,
20    /// The compiled instruction data
21    #[max_len(1024)]
22    pub compiled_instruction: Vec<u8>,
23    /// When this fiber was last executed
24    pub last_executed: i64,
25    /// Total number of executions
26    pub exec_count: u64,
27    /// Priority fee in microlamports for compute unit price (0 = no priority fee)
28    pub priority_fee: u64,
29}
30
31impl FiberState {
32    /// Derive the pubkey of a fiber account.
33    pub fn pubkey(thread: Pubkey, fiber_index: u8) -> Pubkey {
34        Pubkey::find_program_address(&[SEED_THREAD_FIBER, thread.as_ref(), &[fiber_index]], &crate::ID).0
35    }
36}
37
38impl FiberInstructionProcessor for FiberState {
39    fn get_instruction(&self, executor: &Pubkey) -> Result<Instruction> {
40        // Deserialize the compiled instruction
41        let compiled = CompiledInstructionV0::try_from_slice(&self.compiled_instruction)?;
42        // Decompile the instruction
43        let mut instruction = decompile_instruction(&compiled)?;
44
45        // Replace PAYER_PUBKEY with the actual executor
46        for acc in instruction.accounts.iter_mut() {
47            if acc.pubkey.eq(&PAYER_PUBKEY) {
48                acc.pubkey = *executor;
49            }
50        }
51
52        Ok(instruction)
53    }
54}