Cleanup BPF SDK (#7965)
This commit is contained in:
@ -6,10 +6,14 @@ use solana_sdk::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
entrypoint!(process_instruction);
|
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;
|
const FAILURE: u32 = 1;
|
||||||
|
|
||||||
match data[0] {
|
match instruction_data[0] {
|
||||||
1 => {
|
1 => {
|
||||||
info!("modify first account data");
|
info!("modify first account data");
|
||||||
accounts[2].m.borrow_mut().data[0] = 1;
|
accounts[2].m.borrow_mut().data[0] = 1;
|
||||||
|
@ -4,7 +4,11 @@ extern crate solana_sdk;
|
|||||||
use solana_sdk::{account_info::AccountInfo, entrypoint, entrypoint::SUCCESS, pubkey::Pubkey};
|
use solana_sdk::{account_info::AccountInfo, entrypoint, entrypoint::SUCCESS, pubkey::Pubkey};
|
||||||
|
|
||||||
entrypoint!(process_instruction);
|
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
|
// 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
|
||||||
|
@ -20,7 +20,11 @@ fn return_sstruct() -> SStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
entrypoint!(process_instruction);
|
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:");
|
info!("Program identifier:");
|
||||||
program_id.log();
|
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
|
// 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.
|
// programs will have specific requirements so they can do their work.
|
||||||
info!("Account keys and instruction input data:");
|
info!("Account keys and instruction input data:");
|
||||||
sol_log_params(accounts, data);
|
sol_log_params(accounts, instruction_data);
|
||||||
|
|
||||||
{
|
{
|
||||||
// Test - use std methods, unwrap
|
// Test - use std methods, unwrap
|
||||||
|
@ -17,7 +17,11 @@ use solana_sdk::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
entrypoint!(process_instruction);
|
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
|
// Clock
|
||||||
info!("Clock identifier:");
|
info!("Clock identifier:");
|
||||||
sysvar::clock::id().log();
|
sysvar::clock::id().log();
|
||||||
|
@ -9,10 +9,12 @@ use crate::{
|
|||||||
pubkey::Pubkey,
|
pubkey::Pubkey,
|
||||||
};
|
};
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use core::mem::size_of;
|
use std::{
|
||||||
use core::slice::{from_raw_parts, from_raw_parts_mut};
|
cell::RefCell,
|
||||||
use std::cell::RefCell;
|
mem::size_of,
|
||||||
use std::rc::Rc;
|
rc::Rc,
|
||||||
|
slice::{from_raw_parts, from_raw_parts_mut},
|
||||||
|
};
|
||||||
|
|
||||||
/// User implemented program entrypoint
|
/// User implemented program entrypoint
|
||||||
///
|
///
|
||||||
@ -20,7 +22,7 @@ use std::rc::Rc;
|
|||||||
/// accounts: Accounts passed as part of the instruction
|
/// accounts: Accounts passed as part of the instruction
|
||||||
/// data: Instruction data
|
/// data: Instruction data
|
||||||
pub type ProcessInstruction =
|
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
|
/// Programs indicate success with a return value of 0
|
||||||
pub const SUCCESS: u32 = 0;
|
pub const SUCCESS: u32 = 0;
|
||||||
@ -29,7 +31,7 @@ pub const SUCCESS: u32 = 0;
|
|||||||
///
|
///
|
||||||
/// Deserialize the program input parameters and call
|
/// Deserialize the program input parameters and call
|
||||||
/// the user defined `ProcessInstruction`. Users must 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.
|
/// their program will not be created.
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! entrypoint {
|
macro_rules! entrypoint {
|
||||||
@ -38,8 +40,9 @@ macro_rules! entrypoint {
|
|||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u32 {
|
pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u32 {
|
||||||
unsafe {
|
unsafe {
|
||||||
let (program_id, mut accounts, data) = $crate::entrypoint::deserialize(input);
|
let (program_id, mut accounts, instruction_data) =
|
||||||
$process_instruction(&program_id, &mut accounts, &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 {
|
if dup_info == 0 {
|
||||||
let is_signer = {
|
let is_signer = {
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
let is_signer_val = *(input.add(offset) as *const u64);
|
let is_signer = *(input.add(offset) as *const u64);
|
||||||
(is_signer_val != 0)
|
(is_signer != 0)
|
||||||
};
|
};
|
||||||
offset += size_of::<u64>();
|
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>();
|
offset += size_of::<u64>();
|
||||||
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
#[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>();
|
offset += size_of::<u64>();
|
||||||
|
|
||||||
let data = { from_raw_parts_mut(input.add(offset), data_length) };
|
let data = { from_raw_parts_mut(input.add(offset), data_len) };
|
||||||
offset += data_length;
|
offset += data_len;
|
||||||
|
|
||||||
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
let owner: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
||||||
offset += size_of::<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 }));
|
let m = Rc::new(RefCell::new(AccountInfoMut { lamports, data }));
|
||||||
|
|
||||||
accounts.push(AccountInfo {
|
accounts.push(AccountInfo {
|
||||||
key,
|
|
||||||
is_signer,
|
is_signer,
|
||||||
|
key,
|
||||||
m,
|
m,
|
||||||
owner,
|
owner,
|
||||||
});
|
});
|
||||||
@ -106,15 +109,15 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
|
|||||||
// Instruction data
|
// Instruction data
|
||||||
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
#[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>();
|
offset += size_of::<u64>();
|
||||||
|
|
||||||
let data = { from_raw_parts(input.add(offset), data_length) };
|
let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };
|
||||||
offset += data_length;
|
offset += instruction_data_len;
|
||||||
|
|
||||||
// Program Id
|
// Program Id
|
||||||
|
|
||||||
let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
let program_id: &Pubkey = &*(input.add(offset) as *const Pubkey);
|
||||||
|
|
||||||
(program_id, accounts, data)
|
(program_id, accounts, instruction_data)
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ pub fn sol_log(message: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
extern "C" {
|
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
|
/// Prints 64 bit values represented as hexadecimal to stdout
|
||||||
@ -59,7 +59,7 @@ extern "C" {
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn sol_log_slice(slice: &[u8]) {
|
pub fn sol_log_slice(slice: &[u8]) {
|
||||||
for (i, s) in slice.iter().enumerate() {
|
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 {
|
impl Log for Pubkey {
|
||||||
fn log(&self) {
|
fn log(&self) {
|
||||||
for (i, k) in self.to_bytes().iter().enumerate() {
|
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)]
|
#[allow(dead_code)]
|
||||||
pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
|
pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
|
||||||
for (i, account) in accounts.iter().enumerate() {
|
for (i, account) in accounts.iter().enumerate() {
|
||||||
sol_log("AccountInfo");
|
info!("AccountInfo");
|
||||||
sol_log_64(0, 0, 0, 0, i as u64);
|
info!(0, 0, 0, 0, i);
|
||||||
sol_log("- Is signer");
|
info!("- Is signer");
|
||||||
sol_log_64(0, 0, 0, 0, account.is_signer as u64);
|
info!(0, 0, 0, 0, account.is_signer);
|
||||||
sol_log("- Key");
|
info!("- Key");
|
||||||
account.key.log();
|
account.key.log();
|
||||||
sol_log("- Lamports");
|
info!("- Lamports");
|
||||||
sol_log_64(0, 0, 0, 0, *account.m.borrow().lamports);
|
info!(0, 0, 0, 0, *account.m.borrow().lamports);
|
||||||
sol_log("- Account data length");
|
info!("- Account data length");
|
||||||
sol_log_64(0, 0, 0, 0, account.m.borrow().data.len() as u64);
|
info!(0, 0, 0, 0, account.m.borrow().data.len());
|
||||||
sol_log("- Owner");
|
info!("- Owner");
|
||||||
account.owner.log();
|
account.owner.log();
|
||||||
}
|
}
|
||||||
sol_log("Instruction data");
|
info!("Instruction data");
|
||||||
sol_log_slice(data);
|
sol_log_slice(data);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user