Skip to main content

antegen_fiber_program/state/
fiber.rs

1use crate::constants::*;
2use crate::state::instruction::*;
3use anchor_lang::prelude::*;
4use anchor_lang::solana_program::instruction::Instruction;
5
6/// Trait for processing fiber instructions
7pub trait FiberInstructionProcessor {
8    /// Get the decompiled instruction from the fiber's compiled data,
9    /// replacing PAYER_PUBKEY with the provided executor
10    fn get_instruction(&self, executor: &Pubkey) -> Result<Instruction>;
11}
12
13/// Represents a single fiber (instruction) in a thread's execution sequence.
14/// Owned by the Fiber Program. Thread PDA is the universal signer/payer.
15#[account]
16#[derive(Debug, InitSpace)]
17pub struct FiberState {
18    /// The thread this fiber belongs to
19    pub thread: Pubkey,
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(
35            &[SEED_THREAD_FIBER, thread.as_ref(), &[fiber_index]],
36            &crate::ID,
37        )
38        .0
39    }
40}
41
42impl FiberInstructionProcessor for FiberState {
43    fn get_instruction(&self, executor: &Pubkey) -> Result<Instruction> {
44        // Deserialize the compiled instruction
45        let compiled = CompiledInstructionV0::try_from_slice(&self.compiled_instruction)?;
46        // Decompile the instruction
47        let mut instruction = decompile_instruction(&compiled)?;
48
49        // Replace PAYER_PUBKEY with the actual executor
50        for acc in instruction.accounts.iter_mut() {
51            if acc.pubkey.eq(&PAYER_PUBKEY) {
52                acc.pubkey = *executor;
53            }
54        }
55
56        Ok(instruction)
57    }
58}