From 4c4b7d39b8e64b336085e3620e52bc8797d78aad Mon Sep 17 00:00:00 2001 From: Jack May Date: Fri, 6 Sep 2019 14:44:41 -0700 Subject: [PATCH] Cleanup program's ProcessInstruction (#5828) --- programs/bpf/rust/external_spend/src/lib.rs | 3 ++- programs/bpf/rust/noop/src/lib.rs | 5 ++-- programs/bpf/rust/tick_height/src/lib.rs | 3 ++- sdk/src/entrypoint.rs | 28 ++++++++++----------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/programs/bpf/rust/external_spend/src/lib.rs b/programs/bpf/rust/external_spend/src/lib.rs index 705f4c696..a50ecca43 100644 --- a/programs/bpf/rust/external_spend/src/lib.rs +++ b/programs/bpf/rust/external_spend/src/lib.rs @@ -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 diff --git a/programs/bpf/rust/noop/src/lib.rs b/programs/bpf/rust/noop/src/lib.rs index a09ed3bc3..1b31b415b 100644 --- a/programs/bpf/rust/noop/src/lib.rs +++ b/programs/bpf/rust/noop/src/lib.rs @@ -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 diff --git a/programs/bpf/rust/tick_height/src/lib.rs b/programs/bpf/rust/tick_height/src/lib.rs index 78d5908ff..888f67f7e 100644 --- a/programs/bpf/rust/tick_height/src/lib.rs +++ b/programs/bpf/rust/tick_height/src/lib.rs @@ -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); diff --git a/sdk/src/entrypoint.rs b/sdk/src/entrypoint.rs index 2e7388968..2deb04927 100644 --- a/sdk/src/entrypoint.rs +++ b/sdk/src/entrypoint.rs @@ -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>, SolClusterInfo<'a>, &'a [u8]), ()> { +) -> Result<(&'a Pubkey, Vec>, &'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)) }