Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions pinocchio/interface/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use pinocchio::program_error::ProgramError;
use pinocchio::{
hint::{likely, unlikely},
program_error::ProgramError,
};

pub mod account;
pub mod account_state;
Expand Down Expand Up @@ -37,7 +40,7 @@ pub trait Initializable {
pub unsafe fn load<T: Initializable + Transmutable>(bytes: &[u8]) -> Result<&T, ProgramError> {
load_unchecked(bytes).and_then(|t: &T| {
// checks if the data is initialized
if t.is_initialized()? {
if likely(t.is_initialized()?) {
Ok(t)
} else {
Err(ProgramError::UninitializedAccount)
Expand All @@ -54,7 +57,7 @@ pub unsafe fn load<T: Initializable + Transmutable>(bytes: &[u8]) -> Result<&T,
/// The caller must ensure that `bytes` contains a valid representation of `T`.
#[inline(always)]
pub unsafe fn load_unchecked<T: Transmutable>(bytes: &[u8]) -> Result<&T, ProgramError> {
if bytes.len() != T::LEN {
if unlikely(bytes.len() != T::LEN) {
return Err(ProgramError::InvalidAccountData);
}
Ok(&*(bytes.as_ptr() as *const T))
Expand All @@ -71,7 +74,7 @@ pub unsafe fn load_mut<T: Initializable + Transmutable>(
) -> Result<&mut T, ProgramError> {
load_mut_unchecked(bytes).and_then(|t: &mut T| {
// checks if the data is initialized
if t.is_initialized()? {
if likely(t.is_initialized()?) {
Ok(t)
} else {
Err(ProgramError::UninitializedAccount)
Expand All @@ -90,7 +93,7 @@ pub unsafe fn load_mut<T: Initializable + Transmutable>(
pub unsafe fn load_mut_unchecked<T: Transmutable>(
bytes: &mut [u8],
) -> Result<&mut T, ProgramError> {
if bytes.len() != T::LEN {
if unlikely(bytes.len() != T::LEN) {
return Err(ProgramError::InvalidAccountData);
}
Ok(&mut *(bytes.as_mut_ptr() as *mut T))
Expand Down
4 changes: 2 additions & 2 deletions pinocchio/program/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn try_ui_amount_into_amount(ui_amount: &str, decimals: u8) -> Result<u64, Progr
#[inline(always)]
const fn unpack_amount(instruction_data: &[u8]) -> Result<u64, TokenError> {
// expected u64 (8)
if instruction_data.len() >= U64_BYTES {
if likely(instruction_data.len() >= U64_BYTES) {
// SAFETY: The minimum size of the instruction data is `U64_BYTES` bytes.
Ok(unsafe { u64::from_le_bytes(*(instruction_data.as_ptr() as *const [u8; U64_BYTES])) })
} else {
Expand All @@ -216,7 +216,7 @@ const fn unpack_amount(instruction_data: &[u8]) -> Result<u64, TokenError> {
#[inline(always)]
const fn unpack_amount_and_decimals(instruction_data: &[u8]) -> Result<(u64, u8), TokenError> {
// expected u64 (8) + u8 (1)
if instruction_data.len() >= 9 {
if likely(instruction_data.len() >= 9) {
let (amount, decimals) = instruction_data.split_at(U64_BYTES);
Ok((
// SAFETY: The size of `amount` is `U64_BYTES` bytes.
Expand Down
6 changes: 4 additions & 2 deletions pinocchio/program/src/processor/revoke.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use {
super::validate_owner,
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult},
pinocchio::{
account_info::AccountInfo, hint::unlikely, program_error::ProgramError, ProgramResult,
},
pinocchio_token_interface::{
error::TokenError,
state::{account::Account, load_mut},
Expand All @@ -24,7 +26,7 @@ pub fn process_revoke(accounts: &[AccountInfo]) -> ProgramResult {
return Err(ProgramError::NotEnoughAccountKeys);
};

if source_account.is_frozen()? {
if unlikely(source_account.is_frozen()?) {
return Err(TokenError::AccountFrozen.into());
}

Expand Down
16 changes: 10 additions & 6 deletions pinocchio/program/src/processor/shared/burn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use {
crate::processor::{check_account_owner, validate_owner},
pinocchio::{
account_info::AccountInfo, hint::likely, program_error::ProgramError, pubkey::pubkey_eq,
account_info::AccountInfo,
hint::{likely, unlikely},
program_error::ProgramError,
pubkey::pubkey_eq,
ProgramResult,
},
pinocchio_token_interface::{
Expand Down Expand Up @@ -31,10 +34,11 @@ pub fn process_burn(
// passed in, one of them will fail the `load_mut` check.
let mint = unsafe { load_mut::<Mint>(mint_info.borrow_mut_data_unchecked())? };

if source_account.is_frozen()? {
if unlikely(source_account.is_frozen()?) {
return Err(TokenError::AccountFrozen.into());
}
if source_account.is_native() {

if unlikely(source_account.is_native()) {
return Err(TokenError::NativeNotSupported.into());
}

Expand All @@ -45,12 +49,12 @@ pub fn process_burn(
.checked_sub(amount)
.ok_or(TokenError::InsufficientFunds)?;

if !pubkey_eq(mint_info.key(), &source_account.mint) {
if unlikely(!pubkey_eq(mint_info.key(), &source_account.mint)) {
return Err(TokenError::MintMismatch.into());
}

if let Some(expected_decimals) = expected_decimals {
if expected_decimals != mint.decimals {
if unlikely(expected_decimals != mint.decimals) {
return Err(TokenError::MintDecimalsMismatch.into());
}
}
Expand Down Expand Up @@ -80,7 +84,7 @@ pub fn process_burn(

// Updates the source account and mint supply.

if amount == 0 {
if unlikely(amount == 0) {
check_account_owner(source_account_info)?;
check_account_owner(mint_info)?;
} else {
Expand Down
9 changes: 5 additions & 4 deletions pinocchio/program/src/processor/shared/mint_to.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use {
crate::processor::{check_account_owner, validate_owner},
pinocchio::{
account_info::AccountInfo, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult,
account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::pubkey_eq,
ProgramResult,
},
pinocchio_token_interface::{
error::TokenError,
Expand All @@ -27,15 +28,15 @@ pub fn process_mint_to(
let destination_account =
unsafe { load_mut::<Account>(destination_account_info.borrow_mut_data_unchecked())? };

if destination_account.is_frozen()? {
if unlikely(destination_account.is_frozen()?) {
return Err(TokenError::AccountFrozen.into());
}

if destination_account.is_native() {
if unlikely(destination_account.is_native()) {
return Err(TokenError::NativeNotSupported.into());
}

if !pubkey_eq(mint_info.key(), &destination_account.mint) {
if unlikely(!pubkey_eq(mint_info.key(), &destination_account.mint)) {
return Err(TokenError::MintMismatch.into());
}

Expand Down