Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpi #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions sdk/src/cpi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Reference: https://github.com/anza-xyz/agave/blob/e207c6e0eaf8e1657fbfaff07da05ca6a7928349/programs/sbf/rust/ro_modify/src/lib.rs#L105-L129

use crate::{
account_info::AccountInfo, instruction::Instruction, program_error::ProgramError,
syscalls::sol_invoke_signed_c,
};

/// The struct expected to be pointed to by `sol_invoke_signed_c()`'s first arg.
///
/// The u64 fields are raw pointers.
///
/// DO NOT EXPOSE THIS STRUCT -
/// to ensure pointers are valid upon use, the scope of this struct should
/// only be limited to the stack where sol_invoke_signed_c happens and then
/// discarded immediately after
#[derive(Debug)]
#[repr(C)]
struct SolInstruction {
program_id_addr: u64,
accounts_addr: u64,
accounts_len: usize,
data_addr: u64,
data_len: usize,
}

// Reference: https://github.com/anza-xyz/agave/blob/c8685ce0e1bb9b26014f1024de2cd2b8c308cbde/programs/sbf/rust/ro_modify/src/lib.rs#L122-L132
impl<'p, 'a, 'd> From<Instruction<'p, 'a, 'd>> for SolInstruction {
#[inline]
fn from(
Instruction {
program_id,
accounts,
data,
}: Instruction,
) -> Self {
Self {
program_id_addr: program_id.as_ptr() as u64,
accounts_addr: accounts.as_ptr() as u64,
accounts_len: accounts.len(),
data_addr: data.as_ptr() as u64,
data_len: data.len(),
}
}
}

/// The array elem of `sol_invoke_signed_c()`'s `account_infos_addr` arg.
///
/// The u64 fields are raw pointers.
///
/// DO NOT EXPOSE THIS STRUCT -
/// to ensure pointers are valid upon use, the scope of this struct should
/// only be limited to the stack where sol_invoke_signed_c happens and then
/// discarded immediately after
#[derive(Debug, Clone, Copy)]
#[repr(C)]
struct SolAccountInfo {
key_addr: u64,
lamports_addr: u64,
data_len: u64,
data_addr: u64,
owner_addr: u64,
rent_epoch: u64,
is_signer: bool,
is_writable: bool,
executable: bool,
}

impl SolAccountInfo {
const fn null() -> Self {
Self {
key_addr: 0,
lamports_addr: 0,
data_len: 0,
data_addr: 0,
owner_addr: 0,
rent_epoch: 0,
is_signer: false,
is_writable: false,
executable: false,
}
}
}

impl From<&AccountInfo> for SolAccountInfo {
#[inline]
fn from(value: &AccountInfo) -> Self {
let (data_len, data_addr) = {
let a = unsafe { value.unchecked_borrow_data() };
let len = a.len();
(len, a.as_ptr())
};
let lamports_addr = unsafe { value.unchecked_borrow_lamports() } as *const _;
Self {
key_addr: value.key().as_ptr() as u64,
lamports_addr: lamports_addr as u64,
data_len: data_len as u64,
data_addr: data_addr as u64,
owner_addr: value.owner().as_ptr() as u64,
is_signer: value.is_signer(),
is_writable: value.is_writable(),
executable: value.executable(),
rent_epoch: 0, // dont care
}
}
}

pub fn invoke_signed<'a, const MAX_ACCOUNTS_INCL_PROGRAM: usize>(
ix: Instruction,
program_account_info: &'a AccountInfo,
account_infos: impl IntoIterator<Item = &'a AccountInfo>,
signers: &[&[&[u8]]],
) -> Result<(), ProgramError> {
let (sol_account_infos, sol_account_infos_len) = core::iter::once(program_account_info)
.chain(account_infos)
.enumerate()
.try_fold(
([SolAccountInfo::null(); MAX_ACCOUNTS_INCL_PROGRAM], 0),
|(mut arr, len), (i, curr)| {
if i >= MAX_ACCOUNTS_INCL_PROGRAM {
return Err(ProgramError::InvalidArgument);
}
arr[i] = SolAccountInfo::from(curr);
Ok((arr, len + 1))
},
)?;
let sol_ix = SolInstruction::from(ix);
let result = unsafe {
sol_invoke_signed_c(
&sol_ix as *const _ as *const _,
&sol_account_infos as *const _ as *const _,
sol_account_infos_len,
signers as *const _ as *const _,
signers.len() as u64,
)
};
match result {
crate::entrypoint::SUCCESS => Ok(()),
_ => Err(result.into()),
}
}
7 changes: 7 additions & 0 deletions sdk/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ pub struct ProcessedSiblingInstruction {
/// Number of AccountMeta structures
pub accounts_len: u64,
}

#[derive(Clone, Copy, Debug)]
pub struct Instruction<'p, 'a, 'd> {
pub program_id: &'p [u8; 32],
pub accounts: &'a [AccountMeta],
pub data: &'d [u8],
}
1 change: 1 addition & 0 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]

pub mod account_info;
pub mod cpi;
pub mod entrypoint;
pub mod instruction;
pub mod log;
Expand Down
18 changes: 18 additions & 0 deletions sdk/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ pub fn log_pubkey(pubkey: &Pubkey) {
#[cfg(not(target_os = "solana"))]
core::hint::black_box(pubkey);
}

pub fn find_program_address(seeds: &[&[u8]], program_id: &Pubkey) -> (Pubkey, u8) {
let mut bytes = [0; 32];
let mut bump_seed = u8::MAX;
let result = unsafe {
crate::syscalls::sol_try_find_program_address(
seeds as *const _ as *const u8,
seeds.len() as u64,
program_id as *const _ as *const u8,
&mut bytes as *mut _ as *mut u8,
&mut bump_seed as *mut _,
)
};
match result {
crate::entrypoint::SUCCESS => (bytes, bump_seed),
_ => panic!("PDA cannot be found"),
}
}