Cleanup program's ProcessInstruction (#5828)

This commit is contained in:
Jack May
2019-09-06 14:44:41 -07:00
committed by GitHub
parent e8d88f3237
commit 4c4b7d39b8
4 changed files with 21 additions and 18 deletions

View File

@ -3,9 +3,10 @@
extern crate solana_sdk; extern crate solana_sdk;
use solana_sdk::entrypoint; use solana_sdk::entrypoint;
use solana_sdk::entrypoint::*; use solana_sdk::entrypoint::*;
use solana_sdk::pubkey::Pubkey;
entrypoint!(process_instruction); entrypoint!(process_instruction);
fn process_instruction(ka: &mut [SolKeyedAccount], _info: &SolClusterInfo, _data: &[u8]) -> bool { fn process_instruction(_program_id: &Pubkey, ka: &mut [SolKeyedAccount], _data: &[u8]) -> bool {
// account 0 is the mint and not owned by this program, any debit of its lamports // 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 // should result in a failed program execution. Test to ensure that this debit
// is seen by the runtime and fails as expected // is seen by the runtime and fails as expected

View File

@ -5,6 +5,7 @@
extern crate solana_sdk; extern crate solana_sdk;
use solana_sdk::entrypoint::*; use solana_sdk::entrypoint::*;
use solana_sdk::log::*; use solana_sdk::log::*;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::{entrypoint, info}; use solana_sdk::{entrypoint, info};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -20,9 +21,9 @@ fn return_sstruct() -> SStruct {
} }
entrypoint!(process_instruction); entrypoint!(process_instruction);
fn process_instruction(ka: &mut [SolKeyedAccount], info: &SolClusterInfo, data: &[u8]) -> bool { fn process_instruction(program_id: &Pubkey, ka: &mut [SolKeyedAccount], data: &[u8]) -> bool {
info!("Program identifier:"); info!("Program identifier:");
info.program_id.log(); program_id.log();
// Log the provided account keys and instruction input data. In the case of // Log the provided account keys and instruction input data. In the case of
// the no-op program, no account keys or input data are expected but real // the no-op program, no account keys or input data are expected but real

View File

@ -3,10 +3,11 @@
extern crate solana_sdk; extern crate solana_sdk;
use byteorder::{ByteOrder, LittleEndian}; use byteorder::{ByteOrder, LittleEndian};
use solana_sdk::entrypoint::*; use solana_sdk::entrypoint::*;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::{entrypoint, info}; use solana_sdk::{entrypoint, info};
entrypoint!(process_instruction); entrypoint!(process_instruction);
fn process_instruction(ka: &mut [SolKeyedAccount], _info: &SolClusterInfo, _data: &[u8]) -> bool { fn process_instruction(_program_id: &Pubkey, ka: &mut [SolKeyedAccount], _data: &[u8]) -> bool {
let tick_height = LittleEndian::read_u64(ka[2].data); let tick_height = LittleEndian::read_u64(ka[2].data);
assert_eq!(10u64, tick_height); assert_eq!(10u64, tick_height);

View File

@ -1,10 +1,10 @@
//! @brief Solana Rust-based BPF program entrypoint and its parameter types //! @brief Solana Rust-based BPF program entrypoint and its parameter types
extern crate alloc; extern crate alloc;
use crate::pubkey::Pubkey;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::mem::size_of; use core::mem::size_of;
use core::slice::{from_raw_parts, from_raw_parts_mut}; use core::slice::{from_raw_parts, from_raw_parts_mut};
use crate::pubkey::Pubkey;
/// Keyed Account /// Keyed Account
pub struct SolKeyedAccount<'a> { pub struct SolKeyedAccount<'a> {
@ -20,17 +20,18 @@ pub struct SolKeyedAccount<'a> {
pub owner: &'a Pubkey, pub owner: &'a Pubkey,
} }
/// Information about the state of the cluster immediately before the program /// User implemented program entrypoint
/// started executing the current instruction ///
pub struct SolClusterInfo<'a> { /// program_id: Program ID of the currently executing program
/// program_id of the currently executing program /// accounts: Accounts passed as part of the instruction
pub program_id: &'a Pubkey, /// data: Instruction data
} pub type ProcessInstruction =
fn(program_id: &Pubkey, accounts: &mut [SolKeyedAccount], data: &[u8]) -> bool;
/// Declare entrypoint of the program. /// Declare entrypoint of the program.
/// ///
/// Deserialize the program input parameters and call /// Deserialize the program input parameters and call
/// a user defined entrypoint. Users must call /// the user defined `ProcessInstruction`. Users must call
/// this function otherwise an entrypoint for /// this function otherwise an entrypoint for
/// their program will not be created. /// their program will not be created.
#[macro_export] #[macro_export]
@ -38,9 +39,9 @@ macro_rules! entrypoint {
($process_instruction:ident) => { ($process_instruction:ident) => {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> bool { pub unsafe extern "C" fn entrypoint(input: *mut u8) -> bool {
unsafe { unsafe {
if let Ok((mut kas, info, data)) = $crate::entrypoint::deserialize(input) { if let Ok((program_id, mut kas, data)) = $crate::entrypoint::deserialize(input) {
$process_instruction(&mut kas, &info, &data) $process_instruction(&program_id, &mut kas, &data)
} else { } else {
false false
} }
@ -53,7 +54,7 @@ macro_rules! entrypoint {
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub unsafe fn deserialize<'a>( pub unsafe fn deserialize<'a>(
input: *mut u8, input: *mut u8,
) -> Result<(Vec<SolKeyedAccount<'a>>, SolClusterInfo<'a>, &'a [u8]), ()> { ) -> Result<(&'a Pubkey, Vec<SolKeyedAccount<'a>>, &'a [u8]), ()> {
let mut offset: usize = 0; let mut offset: usize = 0;
// Number of KeyedAccounts present // Number of KeyedAccounts present
@ -111,7 +112,6 @@ pub unsafe fn deserialize<'a>(
// Program Id // Program Id
let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey); let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
let info = SolClusterInfo { program_id };
Ok((kas, info, data)) Ok((program_id, kas, data))
} }