Add BPF program entrypoint return type (#8111)

This commit is contained in:
Jack May 2020-02-04 12:25:42 -08:00 committed by GitHub
parent 78f6ddc5b7
commit b6d09f1901
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 19 deletions

View File

@ -2,7 +2,8 @@
extern crate solana_sdk; extern crate solana_sdk;
use solana_sdk::{ use solana_sdk::{
account_info::AccountInfo, entrypoint, info, program_error::ProgramError, pubkey::Pubkey, account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, info,
program_error::ProgramError, pubkey::Pubkey,
}; };
entrypoint!(process_instruction); entrypoint!(process_instruction);
@ -10,7 +11,7 @@ fn process_instruction(
_program_id: &Pubkey, _program_id: &Pubkey,
accounts: &[AccountInfo], accounts: &[AccountInfo],
instruction_data: &[u8], instruction_data: &[u8],
) -> Result<(), ProgramError> { ) -> ProgramResult {
match instruction_data[0] { match instruction_data[0] {
1 => { 1 => {
info!("modify first account data"); info!("modify first account data");

View File

@ -6,7 +6,9 @@ use num_traits::FromPrimitive;
use solana_sdk::{ use solana_sdk::{
account_info::AccountInfo, account_info::AccountInfo,
entrypoint, info, entrypoint,
entrypoint::ProgramResult,
info,
instruction_processor_utils::DecodeError, instruction_processor_utils::DecodeError,
program_error::{PrintProgramError, ProgramError}, program_error::{PrintProgramError, ProgramError},
pubkey::Pubkey, pubkey::Pubkey,
@ -48,9 +50,7 @@ fn process_instruction(
_program_id: &Pubkey, _program_id: &Pubkey,
accounts: &[AccountInfo], accounts: &[AccountInfo],
instruction_data: &[u8], instruction_data: &[u8],
) -> Result<(), ProgramError> { ) -> ProgramResult {
ProgramError::CustomError(42).print::<MyError>();
match instruction_data[0] { match instruction_data[0] {
1 => { 1 => {
info!("return success"); info!("return success");

View File

@ -2,7 +2,7 @@
extern crate solana_sdk; extern crate solana_sdk;
use solana_sdk::{ use solana_sdk::{
account_info::AccountInfo, entrypoint, program_error::ProgramError, pubkey::Pubkey, account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, pubkey::Pubkey,
}; };
entrypoint!(process_instruction); entrypoint!(process_instruction);
@ -10,7 +10,7 @@ fn process_instruction(
_program_id: &Pubkey, _program_id: &Pubkey,
accounts: &[AccountInfo], accounts: &[AccountInfo],
_instruction_data: &[u8], _instruction_data: &[u8],
) -> Result<(), ProgramError> { ) -> ProgramResult {
// 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,8 +5,7 @@
extern crate solana_sdk; extern crate solana_sdk;
use solana_sdk::{ use solana_sdk::{
account_info::AccountInfo, entrypoint, info, log::*, program_error::ProgramError, account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, info, log::*, pubkey::Pubkey,
pubkey::Pubkey,
}; };
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -26,7 +25,7 @@ fn process_instruction(
program_id: &Pubkey, program_id: &Pubkey,
accounts: &[AccountInfo], accounts: &[AccountInfo],
instruction_data: &[u8], instruction_data: &[u8],
) -> Result<(), ProgramError> { ) -> ProgramResult {
info!("Program identifier:"); info!("Program identifier:");
program_id.log(); program_id.log();

View File

@ -4,9 +4,10 @@ extern crate solana_sdk;
use solana_sdk::{ use solana_sdk::{
account_info::AccountInfo, account_info::AccountInfo,
clock::{get_segment_from_slot, DEFAULT_SLOTS_PER_EPOCH, DEFAULT_SLOTS_PER_SEGMENT}, clock::{get_segment_from_slot, DEFAULT_SLOTS_PER_EPOCH, DEFAULT_SLOTS_PER_SEGMENT},
entrypoint, info, entrypoint,
entrypoint::ProgramResult,
info,
log::Log, log::Log,
program_error::ProgramError,
pubkey::Pubkey, pubkey::Pubkey,
rent, rent,
sysvar::{ sysvar::{
@ -20,7 +21,7 @@ fn process_instruction(
_program_id: &Pubkey, _program_id: &Pubkey,
accounts: &[AccountInfo], accounts: &[AccountInfo],
_instruction_data: &[u8], _instruction_data: &[u8],
) -> Result<(), ProgramError> { ) -> ProgramResult {
// Clock // Clock
info!("Clock identifier:"); info!("Clock identifier:");
sysvar::clock::id().log(); sysvar::clock::id().log();

View File

@ -7,19 +7,20 @@ use std::{
cell::RefCell, cell::RefCell,
mem::size_of, mem::size_of,
rc::Rc, rc::Rc,
// Hide Result from bindgen gets confused about generics in non-generic type declarations
result::Result as ResultGeneric,
slice::{from_raw_parts, from_raw_parts_mut}, slice::{from_raw_parts, from_raw_parts_mut},
}; };
pub type ProgramResult = ResultGeneric<(), ProgramError>;
/// User implemented function to process an instruction /// User implemented function to process an instruction
/// ///
/// program_id: Program ID of the currently executing program /// program_id: Program ID of the currently executing program
/// accounts: Accounts passed as part of the instruction /// accounts: Accounts passed as part of the instruction
/// instruction_data: Instruction data /// instruction_data: Instruction data
pub type ProcessInstruction = fn( pub type ProcessInstruction =
program_id: &Pubkey, fn(program_id: &Pubkey, accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult;
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> Result<(), ProgramError>;
/// Programs indicate success with a return value of 0 /// Programs indicate success with a return value of 0
pub const SUCCESS: u64 = 0; pub const SUCCESS: u64 = 0;