Skip to content

Commit d93fa90

Browse files
authored
fix(spl): refactor get_rent function and replace slice_eq with Pubkey eq (#143)
This PR makes the following specs passed: - test_process_initialize_mint_freeze - test_process_initialize_mint_no_freeze - test_process_initialize_mint2_freeze - test_process_initialize_mint2_no_freeze
1 parent 73db3ab commit d93fa90

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

program/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ assumptions = []
1717

1818
[dependencies]
1919
arrayref = "0.3.9"
20+
bincode = "1.3"
2021
bytemuck = "1.20.0"
2122
num-derive = "0.4"
2223
num-traits = { workspace = true }

program/src/entrypoint-runtime-verification.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ fn get_mint(account_info: &AccountInfo) -> MintWrapper {
7878
MintWrapper(Mint::unpack_unchecked(&account_info.data.borrow()))
7979
}
8080

81+
macro_rules! assert_pubkey_from_slice {
82+
($actual:expr, $slice:expr) => {{
83+
let expected_pubkey = Pubkey::new_from_array($slice.try_into().unwrap());
84+
assert_eq!($actual, expected_pubkey);
85+
}};
86+
}
87+
8188
/// A wrapper struct as middleware so that the same functions called
8289
/// on the p-token Account are called on the spl Account. However,
8390
/// this means that fields have to be accessed through functions.
@@ -232,8 +239,9 @@ fn get_multisig(account_info: &AccountInfo) -> MultisigWrapper {
232239
MultisigWrapper(Multisig::unpack_unchecked(&account_info.data.borrow()))
233240
}
234241

235-
fn get_rent(_account_info: &AccountInfo) -> solana_rent::Rent {
236-
solana_rent::Rent::get().unwrap()
242+
fn get_rent(account_info: &AccountInfo) -> solana_rent::Rent {
243+
// Directly deserialize from account data without key check
244+
bincode::deserialize(&account_info.data.borrow()).unwrap()
237245
}
238246

239247
#[inline(never)]
@@ -899,12 +907,13 @@ fn test_process_initialize_mint_freeze(
899907
} else {
900908
assert!(result.is_ok());
901909

902-
assert!(get_mint(&accounts[0]).is_initialized().unwrap());
903-
assert_eq!(get_mint(&accounts[0]).mint_authority().unwrap().as_ref(), &instruction_data[1..33]);
904-
assert_eq!(get_mint(&accounts[0]).decimals(), instruction_data[0]);
910+
let mint_new = get_mint(&accounts[0]);
911+
assert!(mint_new.is_initialized().unwrap());
912+
assert_pubkey_from_slice!(*mint_new.mint_authority().unwrap(), instruction_data[1..33]);
913+
assert_eq!(mint_new.decimals(), instruction_data[0]);
905914

906915
if instruction_data[33] == 1 {
907-
assert_eq!(get_mint(&accounts[0]).freeze_authority().unwrap().as_ref(), &instruction_data[34..66]);
916+
assert_pubkey_from_slice!(*mint_new.freeze_authority().unwrap(), instruction_data[34..66]);
908917
}
909918
}
910919

@@ -968,11 +977,11 @@ fn test_process_initialize_mint_no_freeze(
968977
assert!(result.is_ok());
969978

970979
assert!(get_mint(&accounts[0]).is_initialized().unwrap());
971-
assert_eq!(get_mint(&accounts[0]).mint_authority().unwrap().as_ref(), &instruction_data[1..33]);
980+
assert_pubkey_from_slice!(*get_mint(&accounts[0]).mint_authority().unwrap(), instruction_data[1..33]);
972981
assert_eq!(get_mint(&accounts[0]).decimals(), instruction_data[0]);
973982

974983
if instruction_data[33] == 1 {
975-
assert_eq!(get_mint(&accounts[0]).freeze_authority().unwrap().as_ref(), &instruction_data[34..66]);
984+
assert_pubkey_from_slice!(*get_mint(&accounts[0]).freeze_authority().unwrap(), instruction_data[34..66]);
976985
}
977986
}
978987

@@ -2333,7 +2342,7 @@ fn test_process_set_authority_account(
23332342
return result;
23342343
}
23352344

2336-
assert_eq!(get_account(&accounts[0]).owner().as_ref(), &instruction_data[2..34]);
2345+
assert_pubkey_from_slice!(get_account(&accounts[0]).owner(), instruction_data[2..34]);
23372346
assert_eq!(get_account(&accounts[0]).delegate(), None);
23382347
assert_eq!(get_account(&accounts[0]).delegated_amount(), 0);
23392348
if get_account(&accounts[0]).is_native() {
@@ -2352,7 +2361,7 @@ fn test_process_set_authority_account(
23522361
)?;
23532362

23542363
if instruction_data[1] == 1 { // 1 ==> 34 <= instruction_data.len()
2355-
assert_eq!(get_account(&accounts[0]).close_authority().unwrap().as_ref(), &instruction_data[2..34]);
2364+
assert_pubkey_from_slice!(*get_account(&accounts[0]).close_authority().unwrap(), instruction_data[2..34]);
23562365
} else {
23572366
assert_eq!(get_account(&accounts[0]).close_authority(), None);
23582367
}
@@ -2458,7 +2467,7 @@ fn test_process_set_authority_account_multisig(
24582467
return result;
24592468
}
24602469

2461-
assert_eq!(get_account(&accounts[0]).owner().as_ref(), &instruction_data[2..34]);
2470+
assert_pubkey_from_slice!(get_account(&accounts[0]).owner(), instruction_data[2..34]);
24622471
assert_eq!(get_account(&accounts[0]).delegate(), None);
24632472
assert_eq!(get_account(&accounts[0]).delegated_amount(), 0);
24642473
if get_account(&accounts[0]).is_native() {
@@ -2477,7 +2486,7 @@ fn test_process_set_authority_account_multisig(
24772486
)?;
24782487

24792488
if instruction_data[1] == 1 { // 1 ==> 34 <= instruction_data.len()
2480-
assert_eq!(get_account(&accounts[0]).close_authority().unwrap().as_ref(), &instruction_data[2..34]);
2489+
assert_pubkey_from_slice!(*get_account(&accounts[0]).close_authority().unwrap(), instruction_data[2..34]);
24812490
} else {
24822491
assert_eq!(get_account(&accounts[0]).close_authority(), None);
24832492
}
@@ -2573,7 +2582,7 @@ fn test_process_set_authority_mint(
25732582
)?;
25742583

25752584
if instruction_data[1] == 1 { // 1 ==> 34 <= instruction_data.len()
2576-
assert_eq!(get_mint(&accounts[0]).mint_authority().unwrap().as_ref(), &instruction_data[2..34]);
2585+
assert_pubkey_from_slice!(*get_mint(&accounts[0]).mint_authority().unwrap(), instruction_data[2..34]);
25772586
} else {
25782587
assert_eq!(get_mint(&accounts[0]).mint_authority(), None);
25792588
}
@@ -2593,7 +2602,7 @@ fn test_process_set_authority_mint(
25932602
)?;
25942603

25952604
if instruction_data[1] == 1 { // 1 ==> 34 <= instruction_data.len()
2596-
assert_eq!(get_mint(&accounts[0]).freeze_authority().unwrap().as_ref(), &instruction_data[2..34]);
2605+
assert_pubkey_from_slice!(*get_mint(&accounts[0]).freeze_authority().unwrap(), instruction_data[2..34]);
25972606
} else {
25982607
assert_eq!(get_mint(&accounts[0]).freeze_authority(), None);
25992608
}
@@ -2689,7 +2698,7 @@ fn test_process_set_authority_mint_multisig(
26892698
)?;
26902699

26912700
if instruction_data[1] == 1 { // 1 ==> 34 <= instruction_data.len()
2692-
assert_eq!(get_mint(&accounts[0]).mint_authority().unwrap().as_ref(), &instruction_data[2..34]);
2701+
assert_pubkey_from_slice!(*get_mint(&accounts[0]).mint_authority().unwrap(), instruction_data[2..34]);
26932702
} else {
26942703
assert_eq!(get_mint(&accounts[0]).mint_authority(), None);
26952704
}
@@ -2709,7 +2718,7 @@ fn test_process_set_authority_mint_multisig(
27092718
)?;
27102719

27112720
if instruction_data[1] == 1 { // 1 ==> 34 <= instruction_data.len()
2712-
assert_eq!(get_mint(&accounts[0]).freeze_authority().unwrap().as_ref(), &instruction_data[2..34]);
2721+
assert_pubkey_from_slice!(*get_mint(&accounts[0]).freeze_authority().unwrap(), instruction_data[2..34]);
27132722
} else {
27142723
assert_eq!(get_mint(&accounts[0]).freeze_authority(), None);
27152724
}
@@ -4614,12 +4623,13 @@ fn test_process_initialize_mint2_freeze(
46144623
} else {
46154624
assert!(result.is_ok());
46164625

4617-
assert!(get_mint(&accounts[0]).is_initialized().unwrap());
4618-
assert_eq!(get_mint(&accounts[0]).mint_authority().unwrap().as_ref(), &instruction_data[1..33]);
4619-
assert_eq!(get_mint(&accounts[0]).decimals(), instruction_data[0]);
4626+
let mint_new = get_mint(&accounts[0]);
4627+
assert!(mint_new.is_initialized().unwrap());
4628+
assert_pubkey_from_slice!(*mint_new.mint_authority().unwrap(), instruction_data[1..33]);
4629+
assert_eq!(mint_new.decimals(), instruction_data[0]);
46204630

46214631
if instruction_data[33] == 1 {
4622-
assert_eq!(get_mint(&accounts[0]).freeze_authority().unwrap().as_ref(), &instruction_data[34..66]);
4632+
assert_pubkey_from_slice!(*mint_new.freeze_authority().unwrap(), instruction_data[34..66]);
46234633
}
46244634
}
46254635

@@ -4680,12 +4690,13 @@ fn test_process_initialize_mint2_no_freeze(
46804690
} else {
46814691
assert!(result.is_ok());
46824692

4683-
assert!(get_mint(&accounts[0]).is_initialized().unwrap());
4684-
assert_eq!(get_mint(&accounts[0]).mint_authority().unwrap().as_ref(), &instruction_data[1..33]);
4685-
assert_eq!(get_mint(&accounts[0]).decimals(), instruction_data[0]);
4693+
let mint_new = get_mint(&accounts[0]);
4694+
assert!(mint_new.is_initialized().unwrap());
4695+
assert_pubkey_from_slice!(*mint_new.mint_authority().unwrap(), instruction_data[1..33]);
4696+
assert_eq!(mint_new.decimals(), instruction_data[0]);
46864697

46874698
if instruction_data[33] == 1 {
4688-
assert_eq!(get_mint(&accounts[0]).freeze_authority().unwrap().as_ref(), &instruction_data[34..66]);
4699+
assert_pubkey_from_slice!(*mint_new.freeze_authority().unwrap(), instruction_data[34..66]);
46894700
}
46904701
}
46914702

0 commit comments

Comments
 (0)