Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 1c1819f

Browse files
authored
token-2022: [L-05] Use Pubkey's == instead of sol_memcmp (#6859)
* Update instruction counts * Use Pubkey `==` instead of `sol_memcmp`
1 parent bba4651 commit 1c1819f

File tree

3 files changed

+38
-53
lines changed

3 files changed

+38
-53
lines changed

token/program-2022/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ mod entrypoint;
2525
// Export current sdk types for downstream users building with a different sdk
2626
// version
2727
use solana_program::{
28-
entrypoint::ProgramResult,
29-
program_error::ProgramError,
30-
program_memory::sol_memcmp,
31-
pubkey::{Pubkey, PUBKEY_BYTES},
32-
system_program,
28+
entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, system_program,
3329
};
3430
pub use {solana_program, solana_zk_token_sdk};
3531

@@ -132,9 +128,3 @@ pub fn check_system_program_account(system_program_id: &Pubkey) -> ProgramResult
132128
}
133129
Ok(())
134130
}
135-
136-
/// Checks two pubkeys for equality in a computationally cheap way using
137-
/// `sol_memcmp`
138-
pub fn cmp_pubkeys(a: &Pubkey, b: &Pubkey) -> bool {
139-
sol_memcmp(a.as_ref(), b.as_ref(), PUBKEY_BYTES) == 0
140-
}

token/program-2022/src/processor.rs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use {
44
crate::{
5-
check_program_account, cmp_pubkeys,
5+
check_program_account,
66
error::TokenError,
77
extension::{
88
confidential_transfer::{self, ConfidentialTransferAccount, ConfidentialTransferMint},
@@ -192,7 +192,7 @@ impl Processor {
192192
account.base.delegate = PodCOption::none();
193193
account.base.delegated_amount = 0.into();
194194
account.base.state = starting_state.into();
195-
if cmp_pubkeys(mint_info.key, &native_mint::id()) {
195+
if mint_info.key == &native_mint::id() {
196196
let rent_exempt_reserve = rent.minimum_balance(new_account_info_data_len);
197197
account.base.is_native = PodCOption::some(rent_exempt_reserve.into());
198198
account.base.amount = new_account_info
@@ -324,7 +324,7 @@ impl Processor {
324324
}
325325
let (fee, maybe_permanent_delegate, maybe_transfer_hook_program_id) =
326326
if let Some((mint_info, expected_decimals)) = expected_mint_info {
327-
if !cmp_pubkeys(&source_account.base.mint, mint_info.key) {
327+
if &source_account.base.mint != mint_info.key {
328328
return Err(TokenError::MintMismatch.into());
329329
}
330330

@@ -380,24 +380,22 @@ impl Processor {
380380
}
381381
}
382382

383-
let self_transfer = cmp_pubkeys(source_account_info.key, destination_account_info.key);
383+
let self_transfer = source_account_info.key == destination_account_info.key;
384384
match (source_account.base.delegate, maybe_permanent_delegate) {
385-
(_, Some(ref delegate)) if cmp_pubkeys(authority_info.key, delegate) => {
386-
Self::validate_owner(
387-
program_id,
388-
delegate,
389-
authority_info,
390-
authority_info_data_len,
391-
account_info_iter.as_slice(),
392-
)?
393-
}
385+
(_, Some(ref delegate)) if authority_info.key == delegate => Self::validate_owner(
386+
program_id,
387+
delegate,
388+
authority_info,
389+
authority_info_data_len,
390+
account_info_iter.as_slice(),
391+
)?,
394392
(
395393
PodCOption {
396394
option: PodCOption::<Pubkey>::SOME,
397395
value: delegate,
398396
},
399397
_,
400-
) if cmp_pubkeys(authority_info.key, &delegate) => {
398+
) if authority_info.key == &delegate => {
401399
Self::validate_owner(
402400
program_id,
403401
&delegate,
@@ -465,7 +463,7 @@ impl Processor {
465463
if destination_account.base.is_frozen() {
466464
return Err(TokenError::AccountFrozen.into());
467465
}
468-
if !cmp_pubkeys(&source_account.base.mint, &destination_account.base.mint) {
466+
if source_account.base.mint != destination_account.base.mint {
469467
return Err(TokenError::MintMismatch.into());
470468
}
471469

@@ -573,7 +571,7 @@ impl Processor {
573571
}
574572

575573
if let Some((mint_info, expected_decimals)) = expected_mint_info {
576-
if !cmp_pubkeys(&source_account.base.mint, mint_info.key) {
574+
if &source_account.base.mint != mint_info.key {
577575
return Err(TokenError::MintMismatch.into());
578576
}
579577

@@ -624,7 +622,7 @@ impl Processor {
624622
PodCOption {
625623
option: PodCOption::<Pubkey>::SOME,
626624
value: delegate,
627-
} if cmp_pubkeys(authority_info.key, delegate) => delegate,
625+
} if authority_info.key == delegate => delegate,
628626
_ => &source_account.base.owner,
629627
},
630628
authority_info,
@@ -939,7 +937,7 @@ impl Processor {
939937
if destination_account.base.is_native() {
940938
return Err(TokenError::NativeNotSupported.into());
941939
}
942-
if !cmp_pubkeys(mint_info.key, &destination_account.base.mint) {
940+
if mint_info.key != &destination_account.base.mint {
943941
return Err(TokenError::MintMismatch.into());
944942
}
945943

@@ -1040,22 +1038,20 @@ impl Processor {
10401038
.is_owned_by_system_program_or_incinerator()
10411039
{
10421040
match (&source_account.base.delegate, maybe_permanent_delegate) {
1043-
(_, Some(ref delegate)) if cmp_pubkeys(authority_info.key, delegate) => {
1044-
Self::validate_owner(
1045-
program_id,
1046-
delegate,
1047-
authority_info,
1048-
authority_info_data_len,
1049-
account_info_iter.as_slice(),
1050-
)?
1051-
}
1041+
(_, Some(ref delegate)) if authority_info.key == delegate => Self::validate_owner(
1042+
program_id,
1043+
delegate,
1044+
authority_info,
1045+
authority_info_data_len,
1046+
account_info_iter.as_slice(),
1047+
)?,
10521048
(
10531049
PodCOption {
10541050
option: PodCOption::<Pubkey>::SOME,
10551051
value: delegate,
10561052
},
10571053
_,
1058-
) if cmp_pubkeys(authority_info.key, delegate) => {
1054+
) if authority_info.key == delegate => {
10591055
Self::validate_owner(
10601056
program_id,
10611057
delegate,
@@ -1120,7 +1116,7 @@ impl Processor {
11201116
let authority_info = next_account_info(account_info_iter)?;
11211117
let authority_info_data_len = authority_info.data_len();
11221118

1123-
if cmp_pubkeys(source_account_info.key, destination_account_info.key) {
1119+
if source_account_info.key == destination_account_info.key {
11241120
return Err(ProgramError::InvalidAccountData);
11251121
}
11261122

@@ -1144,7 +1140,7 @@ impl Processor {
11441140
if let Ok(cpi_guard) = source_account.get_extension::<CpiGuard>() {
11451141
if cpi_guard.lock_cpi.into()
11461142
&& in_cpi()
1147-
&& !cmp_pubkeys(destination_account_info.key, &source_account.base.owner)
1143+
&& destination_account_info.key != &source_account.base.owner
11481144
{
11491145
return Err(TokenError::CpiGuardCloseAccountBlocked.into());
11501146
}
@@ -1230,7 +1226,7 @@ impl Processor {
12301226
if source_account.base.is_native() {
12311227
return Err(TokenError::NativeNotSupported.into());
12321228
}
1233-
if !cmp_pubkeys(mint_info.key, &source_account.base.mint) {
1229+
if mint_info.key != &source_account.base.mint {
12341230
return Err(TokenError::MintMismatch.into());
12351231
}
12361232

@@ -1808,20 +1804,19 @@ impl Processor {
18081804
owner_account_data_len: usize,
18091805
signers: &[AccountInfo],
18101806
) -> ProgramResult {
1811-
if !cmp_pubkeys(expected_owner, owner_account_info.key) {
1807+
if expected_owner != owner_account_info.key {
18121808
return Err(TokenError::OwnerMismatch.into());
18131809
}
18141810

1815-
if cmp_pubkeys(program_id, owner_account_info.owner)
1816-
&& owner_account_data_len == PodMultisig::SIZE_OF
1811+
if program_id == owner_account_info.owner && owner_account_data_len == PodMultisig::SIZE_OF
18171812
{
18181813
let multisig_data = &owner_account_info.data.borrow();
18191814
let multisig = pod_from_bytes::<PodMultisig>(multisig_data)?;
18201815
let mut num_signers = 0;
18211816
let mut matched = [false; MAX_SIGNERS];
18221817
for signer in signers.iter() {
18231818
for (position, key) in multisig.signers[0..multisig.n as usize].iter().enumerate() {
1824-
if cmp_pubkeys(key, signer.key) && !matched[position] {
1819+
if key == signer.key && !matched[position] {
18251820
if !signer.is_signer {
18261821
return Err(ProgramError::MissingRequiredSignature);
18271822
}

token/program-2022/tests/assert_instruction_count.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const TRANSFER_AMOUNT: u64 = 1_000_000_000_000_000;
2222
#[tokio::test]
2323
async fn initialize_mint() {
2424
let mut pt = ProgramTest::new("spl_token_2022", id(), processor!(Processor::process));
25-
pt.set_compute_max_units(5_000); // last known 1378
25+
pt.set_compute_max_units(5_000); // last known 1401
2626
let (mut banks_client, payer, recent_blockhash) = pt.start().await;
2727

2828
let owner_key = Pubkey::new_unique();
@@ -60,7 +60,7 @@ async fn initialize_mint() {
6060
#[tokio::test]
6161
async fn initialize_account() {
6262
let mut pt = ProgramTest::new("spl_token_2022", id(), processor!(Processor::process));
63-
pt.set_compute_max_units(8_000); // last known 1783
63+
pt.set_compute_max_units(8_000); // last known 1620
6464
let (mut banks_client, payer, recent_blockhash) = pt.start().await;
6565

6666
let owner = Keypair::new();
@@ -112,7 +112,7 @@ async fn initialize_account() {
112112
#[tokio::test]
113113
async fn mint_to() {
114114
let mut pt = ProgramTest::new("spl_token_2022", id(), processor!(Processor::process));
115-
pt.set_compute_max_units(8_000); // last known 1115
115+
pt.set_compute_max_units(8_000); // last known 967
116116
let (mut banks_client, payer, recent_blockhash) = pt.start().await;
117117

118118
let owner = Keypair::new();
@@ -161,7 +161,7 @@ async fn mint_to() {
161161
#[tokio::test]
162162
async fn transfer() {
163163
let mut pt = ProgramTest::new("spl_token_2022", id(), processor!(Processor::process));
164-
pt.set_compute_max_units(8_000); // last known 1502
164+
pt.set_compute_max_units(8_000); // last known 1222
165165
let (mut banks_client, payer, recent_blockhash) = pt.start().await;
166166

167167
let owner = Keypair::new();
@@ -229,7 +229,7 @@ async fn transfer() {
229229
#[tokio::test]
230230
async fn transfer_checked() {
231231
let mut pt = ProgramTest::new("spl_token_2022", id(), processor!(Processor::process));
232-
pt.set_compute_max_units(8_000); // last known 1849
232+
pt.set_compute_max_units(8_000); // last known 1516
233233
let (mut banks_client, payer, recent_blockhash) = pt.start().await;
234234

235235
let owner = Keypair::new();
@@ -299,7 +299,7 @@ async fn transfer_checked() {
299299
#[tokio::test]
300300
async fn burn() {
301301
let mut pt = ProgramTest::new("spl_token_2022", id(), processor!(Processor::process));
302-
pt.set_compute_max_units(8_000); // last known 1226
302+
pt.set_compute_max_units(8_000); // last known 1070
303303
let (mut banks_client, payer, recent_blockhash) = pt.start().await;
304304

305305
let owner = Keypair::new();
@@ -356,7 +356,7 @@ async fn burn() {
356356
#[tokio::test]
357357
async fn close_account() {
358358
let mut pt = ProgramTest::new("spl_token_2022", id(), processor!(Processor::process));
359-
pt.set_compute_max_units(8_000); // last known 1315
359+
pt.set_compute_max_units(8_000); // last known 1111
360360
let (mut banks_client, payer, recent_blockhash) = pt.start().await;
361361

362362
let owner = Keypair::new();

0 commit comments

Comments
 (0)