From 8ad391e0267ac031b4b1cadc48f4ae97e72fb339 Mon Sep 17 00:00:00 2001 From: febo Date: Fri, 5 Sep 2025 01:16:52 +0100 Subject: [PATCH 1/7] Make amount optional --- p-token/src/processor/unwrap_lamports.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/p-token/src/processor/unwrap_lamports.rs b/p-token/src/processor/unwrap_lamports.rs index 3bf91659..884de006 100644 --- a/p-token/src/processor/unwrap_lamports.rs +++ b/p-token/src/processor/unwrap_lamports.rs @@ -6,6 +6,7 @@ use { }, pinocchio_token_interface::{ error::TokenError, + likely, state::{account::Account, load_mut}, }, }; From 0ae47062b86d8bf35329de79b82e1be5c52fad25 Mon Sep 17 00:00:00 2001 From: febo Date: Sat, 13 Sep 2025 15:24:05 +0100 Subject: [PATCH 2/7] Update import --- p-token/src/processor/unwrap_lamports.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/p-token/src/processor/unwrap_lamports.rs b/p-token/src/processor/unwrap_lamports.rs index 884de006..3bf91659 100644 --- a/p-token/src/processor/unwrap_lamports.rs +++ b/p-token/src/processor/unwrap_lamports.rs @@ -6,7 +6,6 @@ use { }, pinocchio_token_interface::{ error::TokenError, - likely, state::{account::Account, load_mut}, }, }; From 0bccd71b7cd09490d87bcfadbbfe3b591909586a Mon Sep 17 00:00:00 2001 From: febo Date: Thu, 11 Sep 2025 11:11:14 +0100 Subject: [PATCH 3/7] Use compiler hints --- Cargo.lock | 6 ++---- p-interface/Cargo.toml | 2 +- p-interface/src/native_mint.rs | 1 + p-interface/src/state/account.rs | 8 ++++++-- p-token/Cargo.toml | 2 +- p-token/src/processor/mod.rs | 15 ++++++++++----- p-token/src/processor/set_authority.rs | 7 +++++-- p-token/src/processor/shared/approve.rs | 6 ++++-- p-token/src/processor/shared/burn.rs | 11 +++++++---- p-token/src/processor/shared/initialize_mint.rs | 5 ++++- p-token/src/processor/shared/mint_to.rs | 6 ++++-- .../src/processor/shared/toggle_account_state.rs | 6 ++++-- p-token/src/processor/shared/transfer.rs | 8 +++++--- 13 files changed, 54 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db8e89d4..1dec3fc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3044,14 +3044,12 @@ checksum = "5b971851087bc3699b001954ad02389d50c41405ece3548cbcafc88b3e20017a" [[package]] name = "pinocchio-log" version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f266eb7ddac75ef32c42025d5cc571da4f8c9e6d5c3d70820c204d5fee72e57a" +source = "git+https://github.com/anza-xyz/pinocchio.git?branch=febo%2Fpubkey-eq#e80ef09d41e3dd994a5e6279fec777bca6db5dd9" [[package]] name = "pinocchio-pubkey" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb0225638cadcbebae8932cb7f49cb5da7c15c21beb19f048f05a5ca7d93f065" +source = "git+https://github.com/anza-xyz/pinocchio.git?branch=febo%2Fpubkey-eq#e80ef09d41e3dd994a5e6279fec777bca6db5dd9" dependencies = [ "five8_const", "pinocchio", diff --git a/p-interface/Cargo.toml b/p-interface/Cargo.toml index 20a49ed3..b442b4c2 100644 --- a/p-interface/Cargo.toml +++ b/p-interface/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["rlib"] [dependencies] pinocchio = { workspace = true } -pinocchio-pubkey = "0.3" +pinocchio-pubkey = { version = "0.3", git = "https://github.com/anza-xyz/pinocchio.git", branch = "febo/pubkey-eq" } [dev-dependencies] strum = "0.27" diff --git a/p-interface/src/native_mint.rs b/p-interface/src/native_mint.rs index f0a0d736..0d7f1bba 100644 --- a/p-interface/src/native_mint.rs +++ b/p-interface/src/native_mint.rs @@ -10,5 +10,6 @@ pub const ID: Pubkey = pinocchio_pubkey::pubkey!("So1111111111111111111111111111 #[inline(always)] pub fn is_native_mint(mint: &Pubkey) -> bool { + // Avoid using `pubkey_eq` since it increased CU consumption. mint == &ID } diff --git a/p-interface/src/state/account.rs b/p-interface/src/state/account.rs index 6647d25c..401e1c2d 100644 --- a/p-interface/src/state/account.rs +++ b/p-interface/src/state/account.rs @@ -1,6 +1,10 @@ use { super::{account_state::AccountState, COption, Initializable, Transmutable}, - pinocchio::{hint::likely, program_error::ProgramError, pubkey::Pubkey}, + pinocchio::{ + hint::likely, + program_error::ProgramError, + pubkey::{pubkey_eq, Pubkey}, + }, }; /// Incinerator address. @@ -147,7 +151,7 @@ impl Account { #[inline(always)] pub fn is_owned_by_system_program_or_incinerator(&self) -> bool { - SYSTEM_PROGRAM_ID == self.owner || INCINERATOR_ID == self.owner + pubkey_eq(&SYSTEM_PROGRAM_ID, &self.owner) || pubkey_eq(&INCINERATOR_ID, &self.owner) } } diff --git a/p-token/Cargo.toml b/p-token/Cargo.toml index bab4a551..4c8dd5d5 100644 --- a/p-token/Cargo.toml +++ b/p-token/Cargo.toml @@ -16,7 +16,7 @@ logging = [] [dependencies] pinocchio = { workspace = true } -pinocchio-log = { version = "0.5", default-features = false } +pinocchio-log = { version = "0.5", default-features = false, git = "https://github.com/anza-xyz/pinocchio.git", branch = "febo/pubkey-eq" } pinocchio-token-interface = { version = "^0", path = "../p-interface" } [dev-dependencies] diff --git a/p-token/src/processor/mod.rs b/p-token/src/processor/mod.rs index b71d1d63..227b3336 100644 --- a/p-token/src/processor/mod.rs +++ b/p-token/src/processor/mod.rs @@ -1,8 +1,13 @@ use { core::{slice::from_raw_parts, str::from_utf8_unchecked}, pinocchio::{ - account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::Pubkey, - syscalls::sol_memcpy_, ProgramResult, + account_info::AccountInfo, + hint::likely, + hint::unlikely, + program_error::ProgramError, + pubkey::{pubkey_eq, Pubkey}, + syscalls::sol_memcpy_, + ProgramResult, }, pinocchio_token_interface::{ error::TokenError, @@ -79,7 +84,7 @@ const MAX_FORMATTED_DIGITS: usize = u8::MAX as usize + 2; /// Checks that the account is owned by the expected program. #[inline(always)] fn check_account_owner(account_info: &AccountInfo) -> ProgramResult { - if account_info.is_owned_by(&TOKEN_PROGRAM_ID) { + if likely(account_info.is_owned_by(&TOKEN_PROGRAM_ID)) { Ok(()) } else { Err(ProgramError::IncorrectProgramId) @@ -101,7 +106,7 @@ unsafe fn validate_owner( owner_account_info: &AccountInfo, signers: &[AccountInfo], ) -> ProgramResult { - if expected_owner != owner_account_info.key() { + if unlikely(!pubkey_eq(expected_owner, owner_account_info.key())) { return Err(TokenError::OwnerMismatch.into()); } @@ -121,7 +126,7 @@ unsafe fn validate_owner( for signer in signers.iter() { for (position, key) in multisig.signers[0..multisig.n as usize].iter().enumerate() { - if key == signer.key() && !matched[position] { + if pubkey_eq(key, signer.key()) && !matched[position] { if !signer.is_signer() { return Err(ProgramError::MissingRequiredSignature); } diff --git a/p-token/src/processor/set_authority.rs b/p-token/src/processor/set_authority.rs index ade89ac0..681e0983 100644 --- a/p-token/src/processor/set_authority.rs +++ b/p-token/src/processor/set_authority.rs @@ -1,7 +1,8 @@ use { super::validate_owner, pinocchio::{ - account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, ProgramResult, + account_info::AccountInfo, hint::likely, program_error::ProgramError, pubkey::Pubkey, + ProgramResult, }, pinocchio_token_interface::{ error::TokenError, @@ -22,7 +23,9 @@ pub fn process_set_authority(accounts: &[AccountInfo], instruction_data: &[u8]) let authority_type = AuthorityType::try_from(*instruction_data.get_unchecked(0))?; let new_authority = if *instruction_data.get_unchecked(1) == 0 { None - } else if *instruction_data.get_unchecked(1) == 1 && instruction_data.len() >= 34 { + } else if likely(*instruction_data.get_unchecked(1) == 1) + && instruction_data.len() >= 34 + { Some(&*(instruction_data.as_ptr().add(2) as *const Pubkey)) } else { return Err(TokenError::InvalidInstruction.into()); diff --git a/p-token/src/processor/shared/approve.rs b/p-token/src/processor/shared/approve.rs index 06129528..b439ad98 100644 --- a/p-token/src/processor/shared/approve.rs +++ b/p-token/src/processor/shared/approve.rs @@ -1,6 +1,8 @@ use { crate::processor::validate_owner, - pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult}, + pinocchio::{ + account_info::AccountInfo, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult, + }, pinocchio_token_interface::{ error::TokenError, state::{account::Account, load, load_mut, mint::Mint}, @@ -56,7 +58,7 @@ pub fn process_approve( } if let Some((mint_info, expected_decimals)) = expected_mint_info { - if mint_info.key() != &source_account.mint { + if !pubkey_eq(mint_info.key(), &source_account.mint) { return Err(TokenError::MintMismatch.into()); } diff --git a/p-token/src/processor/shared/burn.rs b/p-token/src/processor/shared/burn.rs index bf25f2ba..e3b2d02c 100644 --- a/p-token/src/processor/shared/burn.rs +++ b/p-token/src/processor/shared/burn.rs @@ -1,6 +1,9 @@ use { crate::processor::{check_account_owner, validate_owner}, - pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult}, + pinocchio::{ + account_info::AccountInfo, hint::likely, program_error::ProgramError, pubkey::pubkey_eq, + ProgramResult, + }, pinocchio_token_interface::{ error::TokenError, state::{account::Account, load_mut, mint::Mint}, @@ -42,7 +45,7 @@ pub fn process_burn( .checked_sub(amount) .ok_or(TokenError::InsufficientFunds)?; - if mint_info.key() != &source_account.mint { + if !pubkey_eq(mint_info.key(), &source_account.mint) { return Err(TokenError::MintMismatch.into()); } @@ -52,9 +55,9 @@ pub fn process_burn( } } - if !source_account.is_owned_by_system_program_or_incinerator() { + if likely(!source_account.is_owned_by_system_program_or_incinerator()) { match source_account.delegate() { - Some(delegate) if authority_info.key() == delegate => { + Some(delegate) if pubkey_eq(authority_info.key(), delegate) => { // SAFETY: `authority_info` is not currently borrowed. unsafe { validate_owner(delegate, authority_info, remaining)? }; diff --git a/p-token/src/processor/shared/initialize_mint.rs b/p-token/src/processor/shared/initialize_mint.rs index 3dff60b0..750d2521 100644 --- a/p-token/src/processor/shared/initialize_mint.rs +++ b/p-token/src/processor/shared/initialize_mint.rs @@ -1,6 +1,7 @@ use { pinocchio::{ account_info::AccountInfo, + hint::likely, program_error::ProgramError, pubkey::Pubkey, sysvars::{rent::Rent, Sysvar}, @@ -30,7 +31,9 @@ pub fn process_initialize_mint( let mint_authority = &*(instruction_data.as_ptr().add(1) as *const Pubkey); let freeze_authority = if *instruction_data.get_unchecked(33) == 0 { None - } else if *instruction_data.get_unchecked(33) == 1 && instruction_data.len() >= 66 { + } else if likely(*instruction_data.get_unchecked(33) == 1) + && instruction_data.len() >= 66 + { Some(&*(instruction_data.as_ptr().add(34) as *const Pubkey)) } else { return Err(TokenError::InvalidInstruction.into()); diff --git a/p-token/src/processor/shared/mint_to.rs b/p-token/src/processor/shared/mint_to.rs index 7f205c0e..d8c3373d 100644 --- a/p-token/src/processor/shared/mint_to.rs +++ b/p-token/src/processor/shared/mint_to.rs @@ -1,6 +1,8 @@ use { crate::processor::{check_account_owner, validate_owner}, - pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult}, + pinocchio::{ + account_info::AccountInfo, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult, + }, pinocchio_token_interface::{ error::TokenError, state::{account::Account, load_mut, mint::Mint}, @@ -33,7 +35,7 @@ pub fn process_mint_to( return Err(TokenError::NativeNotSupported.into()); } - if mint_info.key() != &destination_account.mint { + if !pubkey_eq(mint_info.key(), &destination_account.mint) { return Err(TokenError::MintMismatch.into()); } diff --git a/p-token/src/processor/shared/toggle_account_state.rs b/p-token/src/processor/shared/toggle_account_state.rs index e09c7118..32cb4470 100644 --- a/p-token/src/processor/shared/toggle_account_state.rs +++ b/p-token/src/processor/shared/toggle_account_state.rs @@ -1,6 +1,8 @@ use { crate::processor::validate_owner, - pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult}, + pinocchio::{ + account_info::AccountInfo, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult, + }, pinocchio_token_interface::{ error::TokenError, state::{account::Account, account_state::AccountState, load, load_mut, mint::Mint}, @@ -24,7 +26,7 @@ pub fn process_toggle_account_state(accounts: &[AccountInfo], freeze: bool) -> P if source_account.is_native() { return Err(TokenError::NativeNotSupported.into()); } - if mint_info.key() != &source_account.mint { + if !pubkey_eq(mint_info.key(), &source_account.mint) { return Err(TokenError::MintMismatch.into()); } diff --git a/p-token/src/processor/shared/transfer.rs b/p-token/src/processor/shared/transfer.rs index c637989a..a61e12fa 100644 --- a/p-token/src/processor/shared/transfer.rs +++ b/p-token/src/processor/shared/transfer.rs @@ -1,7 +1,9 @@ use { crate::processor::{check_account_owner, validate_owner}, pinocchio::{ - account_info::AccountInfo, hint::unlikely, program_error::ProgramError, ProgramResult, + + account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult, + , }, pinocchio_token_interface::{ error::TokenError, @@ -103,7 +105,7 @@ pub fn process_transfer( .checked_sub(amount) .ok_or(TokenError::InsufficientFunds)?; - if source_account.mint != destination_account.mint { + if !pubkey_eq(&source_account.mint, &destination_account.mint) { return Err(TokenError::MintMismatch.into()); } @@ -113,7 +115,7 @@ pub fn process_transfer( // Validates the mint information. if let Some((mint_info, decimals)) = expected_mint_info { - if mint_info.key() != &source_account.mint { + if !pubkey_eq(mint_info.key(), &source_account.mint) { return Err(TokenError::MintMismatch.into()); } From 5c94816808871ae533826da89a1ad73559f48faa Mon Sep 17 00:00:00 2001 From: febo Date: Thu, 11 Sep 2025 18:34:35 +0100 Subject: [PATCH 4/7] More hints --- p-token/src/processor/shared/approve.rs | 7 ++++--- p-token/src/processor/unwrap_lamports.rs | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/p-token/src/processor/shared/approve.rs b/p-token/src/processor/shared/approve.rs index b439ad98..d1c1b981 100644 --- a/p-token/src/processor/shared/approve.rs +++ b/p-token/src/processor/shared/approve.rs @@ -1,7 +1,8 @@ use { crate::processor::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, @@ -58,7 +59,7 @@ pub fn process_approve( } if let Some((mint_info, expected_decimals)) = expected_mint_info { - if !pubkey_eq(mint_info.key(), &source_account.mint) { + if unlikely(!pubkey_eq(mint_info.key(), &source_account.mint)) { return Err(TokenError::MintMismatch.into()); } @@ -66,7 +67,7 @@ pub fn process_approve( // `load` validates that the mint is initialized. let mint = unsafe { load::(mint_info.borrow_data_unchecked())? }; - if expected_decimals != mint.decimals { + if unlikely(expected_decimals != mint.decimals) { return Err(TokenError::MintDecimalsMismatch.into()); } } diff --git a/p-token/src/processor/unwrap_lamports.rs b/p-token/src/processor/unwrap_lamports.rs index 3bf91659..7e3d68d5 100644 --- a/p-token/src/processor/unwrap_lamports.rs +++ b/p-token/src/processor/unwrap_lamports.rs @@ -7,6 +7,7 @@ use { pinocchio_token_interface::{ error::TokenError, state::{account::Account, load_mut}, + unlikely, }, }; @@ -62,7 +63,7 @@ pub fn process_unwrap_lamports(accounts: &[AccountInfo], instruction_data: &[u8] // raw pointer. let self_transfer = source_account_info == destination_account_info; - if self_transfer || amount == 0 { + if unlikely(self_transfer || amount == 0) { // Validates the token account owner since we are not writing // to the account. check_account_owner(source_account_info) From 71e1addde13204069d83b6a31b7683aadae30f4d Mon Sep 17 00:00:00 2001 From: febo Date: Thu, 11 Sep 2025 18:37:13 +0100 Subject: [PATCH 5/7] Update hint import --- p-token/src/processor/mod.rs | 3 +-- p-token/src/processor/shared/transfer.rs | 5 ++--- p-token/src/processor/unwrap_lamports.rs | 6 ++++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/p-token/src/processor/mod.rs b/p-token/src/processor/mod.rs index 227b3336..07cb8985 100644 --- a/p-token/src/processor/mod.rs +++ b/p-token/src/processor/mod.rs @@ -2,8 +2,7 @@ use { core::{slice::from_raw_parts, str::from_utf8_unchecked}, pinocchio::{ account_info::AccountInfo, - hint::likely, - hint::unlikely, + hint::{likely, unlikely}, program_error::ProgramError, pubkey::{pubkey_eq, Pubkey}, syscalls::sol_memcpy_, diff --git a/p-token/src/processor/shared/transfer.rs b/p-token/src/processor/shared/transfer.rs index a61e12fa..07c5fad8 100644 --- a/p-token/src/processor/shared/transfer.rs +++ b/p-token/src/processor/shared/transfer.rs @@ -1,9 +1,8 @@ use { crate::processor::{check_account_owner, validate_owner}, pinocchio::{ - - account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::pubkey_eq, ProgramResult, - , + account_info::AccountInfo, hint::unlikely, program_error::ProgramError, pubkey::pubkey_eq, + ProgramResult, }, pinocchio_token_interface::{ error::TokenError, diff --git a/p-token/src/processor/unwrap_lamports.rs b/p-token/src/processor/unwrap_lamports.rs index 7e3d68d5..30e0f5d2 100644 --- a/p-token/src/processor/unwrap_lamports.rs +++ b/p-token/src/processor/unwrap_lamports.rs @@ -2,12 +2,14 @@ use { super::validate_owner, crate::processor::{check_account_owner, unpack_amount}, pinocchio::{ - account_info::AccountInfo, hint::likely, program_error::ProgramError, ProgramResult, + account_info::AccountInfo, + hint::{likely, unlikely}, + program_error::ProgramError, + ProgramResult, }, pinocchio_token_interface::{ error::TokenError, state::{account::Account, load_mut}, - unlikely, }, }; From 7002efd6ff13875ac5646e853f415bf646a23a49 Mon Sep 17 00:00:00 2001 From: febo Date: Sat, 13 Sep 2025 15:36:13 +0100 Subject: [PATCH 6/7] Revert dependencies --- Cargo.lock | 6 ++++-- p-interface/Cargo.toml | 2 +- p-token/Cargo.toml | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1dec3fc5..db8e89d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3044,12 +3044,14 @@ checksum = "5b971851087bc3699b001954ad02389d50c41405ece3548cbcafc88b3e20017a" [[package]] name = "pinocchio-log" version = "0.5.0" -source = "git+https://github.com/anza-xyz/pinocchio.git?branch=febo%2Fpubkey-eq#e80ef09d41e3dd994a5e6279fec777bca6db5dd9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f266eb7ddac75ef32c42025d5cc571da4f8c9e6d5c3d70820c204d5fee72e57a" [[package]] name = "pinocchio-pubkey" version = "0.3.0" -source = "git+https://github.com/anza-xyz/pinocchio.git?branch=febo%2Fpubkey-eq#e80ef09d41e3dd994a5e6279fec777bca6db5dd9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0225638cadcbebae8932cb7f49cb5da7c15c21beb19f048f05a5ca7d93f065" dependencies = [ "five8_const", "pinocchio", diff --git a/p-interface/Cargo.toml b/p-interface/Cargo.toml index b442b4c2..1c2d1eac 100644 --- a/p-interface/Cargo.toml +++ b/p-interface/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["rlib"] [dependencies] pinocchio = { workspace = true } -pinocchio-pubkey = { version = "0.3", git = "https://github.com/anza-xyz/pinocchio.git", branch = "febo/pubkey-eq" } +pinocchio-pubkey = { version = "0.3" } [dev-dependencies] strum = "0.27" diff --git a/p-token/Cargo.toml b/p-token/Cargo.toml index 4c8dd5d5..bab4a551 100644 --- a/p-token/Cargo.toml +++ b/p-token/Cargo.toml @@ -16,7 +16,7 @@ logging = [] [dependencies] pinocchio = { workspace = true } -pinocchio-log = { version = "0.5", default-features = false, git = "https://github.com/anza-xyz/pinocchio.git", branch = "febo/pubkey-eq" } +pinocchio-log = { version = "0.5", default-features = false } pinocchio-token-interface = { version = "^0", path = "../p-interface" } [dev-dependencies] From f7dfe4aa0fcd6eb6576420e00ae5702f4a75cdb0 Mon Sep 17 00:00:00 2001 From: febo Date: Mon, 15 Sep 2025 15:03:08 +0100 Subject: [PATCH 7/7] Clean up --- p-interface/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p-interface/Cargo.toml b/p-interface/Cargo.toml index 1c2d1eac..20a49ed3 100644 --- a/p-interface/Cargo.toml +++ b/p-interface/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["rlib"] [dependencies] pinocchio = { workspace = true } -pinocchio-pubkey = { version = "0.3" } +pinocchio-pubkey = "0.3" [dev-dependencies] strum = "0.27"