依然仍然居然没有入门
真的很烦
今天来看看spl里的一些示例代码
看看能不能有什么收获
先看第一个示例程序
cross-program-invocation
这是一个cpi的示例程序
我们看一下
process_instruction
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
// Create in iterator to safety reference accounts in the slice
let account_info_iter = &mut accounts.iter();
// Account info for the program being invoked
let system_program_info = next_account_info(account_info_iter)?;
// Account info to allocate
let allocated_info = next_account_info(account_info_iter)?;
let expected_allocated_key =
Pubkey::create_program_address(&[b"You pass butter", &[instruction_data[0]]], program_id)?;
if *allocated_info.key != expected_allocated_key {
// allocated key does not match the derived address
return Err(ProgramError::InvalidArgument);
}
// Invoke the system program to allocate account data
invoke_signed(
&system_instruction::allocate(allocated_info.key, SIZE as u64),
// Order doesn't matter and this slice could include all the accounts and be:
// `&accounts`
&[
system_program_info.clone(), // program being invoked also needs to be included
allocated_info.clone(),
],
&[&[b"You pass butter", &[instruction_data[0]]]],
)?;
Ok(())
}
遍历accounts
取出
system_program_info
allocated_info
create_program_address获取程序地址
然后调用invoke_signed()
1.instruction
2.account_infos
3.signers_seeds
然后看一下第二个示例程序
custom-heap
unsafe impl std::alloc::GlobalAlloc for BumpAllocator {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
const POS_PTR: *mut usize = HEAP_START_ADDRESS as usize as *mut usize;
const TOP_ADDRESS: usize = HEAP_START_ADDRESS as usize + HEAP_LENGTH;
const BOTTOM_ADDRESS: usize = HEAP_START_ADDRESS as usize + size_of::<*mut u8>();
let mut pos = *POS_PTR;
if pos == 0 {
// First time, set starting position
pos = TOP_ADDRESS;
}
pos = pos.saturating_sub(layout.size());
pos &= !(layout.align().saturating_sub(1));
if pos < BOTTOM_ADDRESS {
return null_mut();
}
*POS_PTR = pos;
pos as *mut u8
}
#[inline]
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {
// I'm a bump allocator, I don't free
}
}
这几个示例程序都好鸡肋啊
无语
哪位大神可以来帮帮我啊