Cleanup BPF SDK (#7965)

This commit is contained in:
Jack May
2020-01-24 13:41:14 -08:00
committed by GitHub
parent 34ed93d57c
commit 917067741a
6 changed files with 57 additions and 38 deletions

View File

@ -9,10 +9,12 @@ use crate::{
pubkey::Pubkey,
};
use alloc::vec::Vec;
use core::mem::size_of;
use core::slice::{from_raw_parts, from_raw_parts_mut};
use std::cell::RefCell;
use std::rc::Rc;
use std::{
cell::RefCell,
mem::size_of,
rc::Rc,
slice::{from_raw_parts, from_raw_parts_mut},
};
/// User implemented program entrypoint
///
@ -20,7 +22,7 @@ use std::rc::Rc;
/// accounts: Accounts passed as part of the instruction
/// data: Instruction data
pub type ProcessInstruction =
fn(program_id: &Pubkey, accounts: &mut [AccountInfo], data: &[u8]) -> u32;
fn(program_id: &Pubkey, accounts: &mut [AccountInfo], instruction_data: &[u8]) -> u32;
/// Programs indicate success with a return value of 0
pub const SUCCESS: u32 = 0;
@ -29,7 +31,7 @@ pub const SUCCESS: u32 = 0;
///
/// Deserialize the program input parameters and call
/// the user defined `ProcessInstruction`. Users must call
/// this function otherwise an entrypoint for
/// this macro otherwise an entrypoint for
/// their program will not be created.
#[macro_export]
macro_rules! entrypoint {
@ -38,8 +40,9 @@ macro_rules! entrypoint {
#[no_mangle]
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u32 {
unsafe {
let (program_id, mut accounts, data) = $crate::entrypoint::deserialize(input);
$process_instruction(&program_id, &mut accounts, &data)
let (program_id, mut accounts, instruction_data) =
$crate::entrypoint::deserialize(input);
$process_instruction(&program_id, &mut accounts, &instruction_data)
}
}
};
@ -67,8 +70,8 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
if dup_info == 0 {
let is_signer = {
#[allow(clippy::cast_ptr_alignment)]
let is_signer_val = *(input.add(offset) as *const u64);
(is_signer_val != 0)
let is_signer = *(input.add(offset) as *const u64);
(is_signer != 0)
};
offset += size_of::<u64>();
@ -80,11 +83,11 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
offset += size_of::<u64>();
#[allow(clippy::cast_ptr_alignment)]
let data_length = *(input.add(offset) as *const u64) as usize;
let data_len = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
let data = { from_raw_parts_mut(input.add(offset), data_length) };
offset += data_length;
let data = { from_raw_parts_mut(input.add(offset), data_len) };
offset += data_len;
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
offset += size_of::<Pubkey>();
@ -92,8 +95,8 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
let m = Rc::new(RefCell::new(AccountInfoMut { lamports, data }));
accounts.push(AccountInfo {
key,
is_signer,
key,
m,
owner,
});
@ -106,15 +109,15 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
// Instruction data
#[allow(clippy::cast_ptr_alignment)]
let data_length = *(input.add(offset) as *const u64) as usize;
let instruction_data_len = *(input.add(offset) as *const u64) as usize;
offset += size_of::<u64>();
let data = { from_raw_parts(input.add(offset), data_length) };
offset += data_length;
let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
offset += instruction_data_len;
// Program Id
let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
(program_id, accounts, data)
(program_id, accounts, instruction_data)
}

View File

@ -36,7 +36,7 @@ pub fn sol_log(message: &str) {
}
}
extern "C" {
fn sol_log_(message: *const u8, length: u64);
fn sol_log_(message: *const u8, len: u64);
}
/// Prints 64 bit values represented as hexadecimal to stdout
@ -59,7 +59,7 @@ extern "C" {
#[allow(dead_code)]
pub fn sol_log_slice(slice: &[u8]) {
for (i, s) in slice.iter().enumerate() {
sol_log_64(0, 0, 0, i as u64, u64::from(*s));
info!(0, 0, 0, i, *s);
}
}
@ -70,7 +70,7 @@ pub trait Log {
impl Log for Pubkey {
fn log(&self) {
for (i, k) in self.to_bytes().iter().enumerate() {
sol_log_64(0, 0, 0, i as u64, u64::from(*k));
info!(0, 0, 0, i, *k);
}
}
}
@ -82,19 +82,19 @@ impl Log for Pubkey {
#[allow(dead_code)]
pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
for (i, account) in accounts.iter().enumerate() {
sol_log("AccountInfo");
sol_log_64(0, 0, 0, 0, i as u64);
sol_log("- Is signer");
sol_log_64(0, 0, 0, 0, account.is_signer as u64);
sol_log("- Key");
info!("AccountInfo");
info!(0, 0, 0, 0, i);
info!("- Is signer");
info!(0, 0, 0, 0, account.is_signer);
info!("- Key");
account.key.log();
sol_log("- Lamports");
sol_log_64(0, 0, 0, 0, *account.m.borrow().lamports);
sol_log("- Account data length");
sol_log_64(0, 0, 0, 0, account.m.borrow().data.len() as u64);
sol_log("- Owner");
info!("- Lamports");
info!(0, 0, 0, 0, *account.m.borrow().lamports);
info!("- Account data length");
info!(0, 0, 0, 0, account.m.borrow().data.len());
info!("- Owner");
account.owner.log();
}
sol_log("Instruction data");
info!("Instruction data");
sol_log_slice(data);
}