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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 38 deletions

View File

@ -6,10 +6,14 @@ use solana_sdk::{
};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, accounts: &mut [AccountInfo], data: &[u8]) -> u32 {
fn process_instruction(
_program_id: &Pubkey,
accounts: &mut [AccountInfo],
instruction_data: &[u8],
) -> u32 {
const FAILURE: u32 = 1;
match data[0] {
match instruction_data[0] {
1 => {
info!("modify first account data");
accounts[2].m.borrow_mut().data[0] = 1;

View File

@ -4,7 +4,11 @@ extern crate solana_sdk;
use solana_sdk::{account_info::AccountInfo, entrypoint, entrypoint::SUCCESS, pubkey::Pubkey};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, accounts: &mut [AccountInfo], _data: &[u8]) -> u32 {
fn process_instruction(
_program_id: &Pubkey,
accounts: &mut [AccountInfo],
_instruction_data: &[u8],
) -> u32 {
// account 0 is the mint and not owned by this program, any debit of its lamports
// should result in a failed program execution. Test to ensure that this debit
// is seen by the runtime and fails as expected

View File

@ -20,7 +20,11 @@ fn return_sstruct() -> SStruct {
}
entrypoint!(process_instruction);
fn process_instruction(program_id: &Pubkey, accounts: &mut [AccountInfo], data: &[u8]) -> u32 {
fn process_instruction(
program_id: &Pubkey,
accounts: &mut [AccountInfo],
instruction_data: &[u8],
) -> u32 {
info!("Program identifier:");
program_id.log();
@ -28,7 +32,7 @@ fn process_instruction(program_id: &Pubkey, accounts: &mut [AccountInfo], data:
// the no-op program, no account keys or input data are expected but real
// programs will have specific requirements so they can do their work.
info!("Account keys and instruction input data:");
sol_log_params(accounts, data);
sol_log_params(accounts, instruction_data);
{
// Test - use std methods, unwrap

View File

@ -17,7 +17,11 @@ use solana_sdk::{
};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, accounts: &mut [AccountInfo], _data: &[u8]) -> u32 {
fn process_instruction(
_program_id: &Pubkey,
accounts: &mut [AccountInfo],
_instruction_data: &[u8],
) -> u32 {
// Clock
info!("Clock identifier:");
sysvar::clock::id().log();

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);
}