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

View File

@ -3,9 +3,10 @@
extern crate solana_sdk;
use solana_sdk::entrypoint;
use solana_sdk::entrypoint::*;
use solana_sdk::pubkey::Pubkey;
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
// should result in a failed program execution. Test to ensure that this debit
// is seen by the runtime and fails as expected

View File

@ -5,6 +5,7 @@
extern crate solana_sdk;
use solana_sdk::entrypoint::*;
use solana_sdk::log::*;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::{entrypoint, info};
#[derive(Debug, PartialEq)]
@ -20,9 +21,9 @@ fn return_sstruct() -> SStruct {
}
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_id.log();
program_id.log();
// 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

View File

@ -3,10 +3,11 @@
extern crate solana_sdk;
use byteorder::{ByteOrder, LittleEndian};
use solana_sdk::entrypoint::*;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::{entrypoint, info};
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);
assert_eq!(10u64, tick_height);

View File

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