rialo-s-invoke 0.2.0

A drop-in replacement for `solana_program::program::invoke*` with better compute and heap efficiency.
Documentation
#![allow(unsafe_code)]
#![allow(clippy::ptr_as_ptr)]
#![allow(unexpected_cfgs)]

use rialo_s_account_info::AccountInfo;
use rialo_s_instruction::Instruction;
use rialo_s_program_entrypoint::ProgramResult;

#[cfg(all(target_os = "solana", not(target_arch = "riscv64")))]
mod stable_instruction_borrowed;

pub fn invoke(instruction: &Instruction, account_infos: &[AccountInfo<'_>]) -> ProgramResult {
    invoke_signed(instruction, account_infos, &[])
}

pub fn invoke_unchecked(
    instruction: &Instruction,
    account_infos: &[AccountInfo<'_>],
) -> ProgramResult {
    invoke_signed_unchecked(instruction, account_infos, &[])
}

pub fn invoke_signed(
    instruction: &Instruction,
    account_infos: &[AccountInfo<'_>],
    signers_seeds: &[&[&[u8]]],
) -> ProgramResult {
    // Check that the account RefCells are consistent with the request
    for account_meta in instruction.accounts.iter() {
        for account_info in account_infos.iter() {
            if account_meta.pubkey == *account_info.key {
                if account_meta.is_writable {
                    let _ = account_info.try_borrow_mut_kelvins()?;
                    let _ = account_info.try_borrow_mut_data()?;
                } else {
                    let _ = account_info.try_borrow_kelvins()?;
                    let _ = account_info.try_borrow_data()?;
                }
                break;
            }
        }
    }

    invoke_signed_unchecked(instruction, account_infos, signers_seeds)
}

#[cfg(target_os = "solana")]
use rialo_s_define_syscall::definitions::rlo_invoke_signed_rust;

#[cfg(not(target_os = "solana"))]
#[allow(dead_code)]
unsafe fn rlo_invoke_signed_rust(_: *const u8, _: *const u8, _: u64, _: *const u8, _: u64) -> u64 {
    unimplemented!("only supported with `target_os = \"solana\"")
}

#[allow(unused_variables)]
pub fn invoke_signed_unchecked(
    instruction: &Instruction,
    account_infos: &[AccountInfo<'_>],
    signers_seeds: &[&[&[u8]]],
) -> ProgramResult {
    #[cfg(all(target_os = "solana", not(target_arch = "riscv64")))]
    {
        // BPF: Use StableInstruction (existing behavior)
        use stable_instruction_borrowed::StableInstructionBorrowed;
        let stable = StableInstructionBorrowed::new(instruction);
        let instruction_addr = stable.instruction_addr();

        let result = unsafe {
            rlo_invoke_signed_rust(
                instruction_addr,
                account_infos as *const _ as *const u8,
                account_infos.len() as u64,
                signers_seeds as *const _ as *const u8,
                signers_seeds.len() as u64,
            )
        };

        match result {
            rialo_s_program_entrypoint::SUCCESS => Ok(()),
            _ => Err(result.into()),
        }
    }

    #[cfg(all(target_os = "solana", target_arch = "riscv64"))]
    {
        // RISC-V: Use HostInstruction (correct layout for loader)
        use rialo_s_cpi::{marshal_accounts, marshal_instruction};

        let host_instruction = marshal_instruction(instruction);
        let host_accounts = marshal_accounts(account_infos);

        let result = unsafe {
            rlo_invoke_signed_rust(
                &host_instruction as *const _ as *const u8,
                host_accounts.as_ptr() as *const u8,
                host_accounts.len() as u64,
                signers_seeds as *const _ as *const u8,
                signers_seeds.len() as u64,
            )
        };

        match result {
            rialo_s_program_entrypoint::SUCCESS => Ok(()),
            _ => Err(result.into()),
        }
    }

    #[cfg(not(target_os = "solana"))]
    Ok(())
}