solana/sdk/program/src/program.rs

61 lines
2.0 KiB
Rust
Raw Normal View History

use crate::{account_info::AccountInfo, entrypoint::ProgramResult, instruction::Instruction};
2020-04-28 14:33:56 -07:00
/// Invoke a cross-program instruction
pub fn invoke(instruction: &Instruction, account_infos: &[AccountInfo]) -> ProgramResult {
invoke_signed(instruction, account_infos, &[])
}
/// Invoke a cross-program instruction with program signatures
pub fn invoke_signed(
instruction: &Instruction,
account_infos: &[AccountInfo],
signers_seeds: &[&[&[u8]]],
2020-04-28 14:33:56 -07:00
) -> 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_lamports()?;
let _ = account_info.try_borrow_mut_data()?;
} else {
let _ = account_info.try_borrow_lamports()?;
let _ = account_info.try_borrow_data()?;
}
break;
}
}
}
#[cfg(target_arch = "bpf")]
{
let result = unsafe {
sol_invoke_signed_rust(
instruction as *const _ as *const u8,
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 {
crate::entrypoint::SUCCESS => Ok(()),
_ => Err(result.into()),
}
2020-04-28 14:33:56 -07:00
}
#[cfg(not(target_arch = "bpf"))]
crate::program_stubs::sol_invoke_signed(instruction, account_infos, signers_seeds)
2020-04-28 14:33:56 -07:00
}
#[cfg(target_arch = "bpf")]
2020-04-28 14:33:56 -07:00
extern "C" {
fn sol_invoke_signed_rust(
instruction_addr: *const u8,
account_infos_addr: *const u8,
account_infos_len: u64,
signers_seeds_addr: *const u8,
signers_seeds_len: u64,
) -> u64;
}