Skip to content

Commit 15cdd80

Browse files
committed
reduced diff
1 parent 5d181a7 commit 15cdd80

File tree

20 files changed

+161
-133
lines changed

20 files changed

+161
-133
lines changed

program-libs/account-checks/src/account_info/account_info_trait.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ use core::ops::{Deref, DerefMut};
22

33
/// Trait to abstract over different AccountInfo implementations (pinocchio vs solana)
44
pub trait AccountInfoTrait {
5-
type DataRef<'a>: Deref<Target = [u8]> where Self: 'a;
6-
type DataRefMut<'a>: DerefMut<Target = [u8]> where Self: 'a;
5+
type DataRef<'a>: Deref<Target = [u8]>
6+
where
7+
Self: 'a;
8+
type DataRefMut<'a>: DerefMut<Target = [u8]>
9+
where
10+
Self: 'a;
711
type BorrowError: core::fmt::Debug + Into<crate::error::AccountError>;
812

913
/// Return raw byte array for maximum compatibility
@@ -13,22 +17,25 @@ pub trait AccountInfoTrait {
1317
fn executable(&self) -> bool;
1418
fn lamports(&self) -> u64;
1519
fn data_len(&self) -> usize;
16-
20+
1721
/// Unified data access interface
1822
fn try_borrow_data(&self) -> Result<Self::DataRef<'_>, Self::BorrowError>;
1923
fn try_borrow_mut_data(&self) -> Result<Self::DataRefMut<'_>, Self::BorrowError>;
20-
24+
2125
/// Check ownership safely - each implementation handles this without exposing owner
2226
fn is_owned_by(&self, program: &[u8; 32]) -> bool;
23-
27+
2428
/// PDA functions - each implementation uses its own backend
2529
fn find_program_address(seeds: &[&[u8]], program_id: &[u8; 32]) -> ([u8; 32], u8);
26-
fn create_program_address(seeds: &[&[u8]], program_id: &[u8; 32]) -> Result<[u8; 32], crate::error::AccountError>;
27-
30+
fn create_program_address(
31+
seeds: &[&[u8]],
32+
program_id: &[u8; 32],
33+
) -> Result<[u8; 32], crate::error::AccountError>;
34+
2835
/// Get minimum rent balance for a given size
2936
fn get_min_rent_balance(size: usize) -> Result<u64, crate::error::AccountError>;
30-
37+
3138
fn data_is_empty(&self) -> bool {
3239
self.data_len() == 0
3340
}
34-
}
41+
}

program-libs/account-checks/src/account_info/pinocchio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ impl AccountInfoTrait for pinocchio::account_info::AccountInfo {
8989
}
9090
}
9191

92-
fn get_min_rent_balance(size: usize) -> Result<u64, crate::error::AccountError> {
92+
fn get_min_rent_balance(_size: usize) -> Result<u64, crate::error::AccountError> {
9393
#[cfg(target_os = "solana")]
9494
{
9595
use pinocchio::sysvars::Sysvar;
9696
pinocchio::sysvars::rent::Rent::get()
97-
.map(|rent| rent.minimum_balance(size))
97+
.map(|rent| rent.minimum_balance(_size))
9898
.map_err(|_| crate::error::AccountError::FailedBorrowRentSysvar)
9999
}
100100
#[cfg(all(not(target_os = "solana"), feature = "solana"))]
101101
{
102102
use solana_sysvar::Sysvar;
103103
solana_sysvar::rent::Rent::get()
104-
.map(|rent| rent.minimum_balance(size))
104+
.map(|rent| rent.minimum_balance(_size))
105105
.map_err(|_| crate::error::AccountError::FailedBorrowRentSysvar)
106106
}
107107
#[cfg(all(not(target_os = "solana"), not(feature = "solana")))]

program-libs/account-checks/tests/account_info.rs

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
/// Comprehensive tests for AccountInfoTrait implementations:
2-
/// - Solana implementation (solana_account_info::AccountInfo)
3-
/// - Pinocchio implementation (pinocchio::account_info::AccountInfo)
4-
///
5-
/// Tests cover all trait methods with both functional and failing test cases:
6-
/// 1. key() - Returns account public key
7-
/// 2. is_writable() - Check if account is writable
8-
/// 3. is_signer() - Check if account is a signer
9-
/// 4. executable() - Check if account is executable
10-
/// 5. lamports() - Get account lamport balance
11-
/// 6. data_len() - Get account data length
12-
/// 7. try_borrow_data() - Borrow account data immutably
13-
/// 8. try_borrow_mut_data() - Borrow account data mutably
14-
/// 9. is_owned_by() - Check account ownership
15-
/// 10. find_program_address() - Find PDA (static method)
16-
/// 11. create_program_address() - Create PDA (static method)
17-
/// 12. data_is_empty() - Check if account data is empty
18-
/// 13. get_min_rent_balance() - Get minimum rent balance for size
19-
20-
use light_account_checks::{error::AccountError, AccountInfoTrait};
1+
#![cfg(all(feature = "solana", feature = "pinocchio"))]
2+
// Comprehensive tests for AccountInfoTrait implementations:
3+
// - Solana implementation (solana_account_info::AccountInfo)
4+
// - Pinocchio implementation (pinocchio::account_info::AccountInfo)
5+
//
6+
// Tests cover all trait methods with both functional and failing test cases:
7+
// 1. key() - Returns account public key
8+
// 2. is_writable() - Check if account is writable
9+
// 3. is_signer() - Check if account is a signer
10+
// 4. executable() - Check if account is executable
11+
// 5. lamports() - Get account lamport balance
12+
// 6. data_len() - Get account data length
13+
// 7. try_borrow_data() - Borrow account data immutably
14+
// 8. try_borrow_mut_data() - Borrow account data mutably
15+
// 9. is_owned_by() - Check account ownership
16+
// 10. find_program_address() - Find PDA (static method)
17+
// 11. create_program_address() - Create PDA (static method)
18+
// 12. data_is_empty() - Check if account data is empty
19+
// 13. get_min_rent_balance() - Get minimum rent balance for size
20+
21+
use light_account_checks::AccountInfoTrait;
2122

2223
// Test helper functions
2324
#[cfg(feature = "solana")]
@@ -32,7 +33,9 @@ fn create_test_account_solana(
3233
) -> light_account_checks::account_info::test_account_info::solana_program::TestAccount {
3334
let mut account =
3435
light_account_checks::account_info::test_account_info::solana_program::TestAccount::new(
35-
key, owner, data.len(),
36+
key,
37+
owner,
38+
data.len(),
3639
);
3740
account.data = data;
3841
account.lamports = lamports;
@@ -109,22 +112,29 @@ fn test_solana_account_info_trait() {
109112

110113
// Test try_borrow_mut_data() - functional (writable account)
111114
{
112-
let mut borrowed_mut_data = AccountInfoTrait::try_borrow_mut_data(&account_info).unwrap();
115+
let mut borrowed_mut_data =
116+
AccountInfoTrait::try_borrow_mut_data(&account_info).unwrap();
113117
borrowed_mut_data[0] = 99;
114118
} // Drop mutable borrow
115-
119+
116120
// Verify mutation worked
117121
{
118122
let borrowed_data = AccountInfoTrait::try_borrow_data(&account_info).unwrap();
119123
assert_eq!(borrowed_data[0], 99);
120124
}
121125

122126
// Test is_owned_by() - functional (correct owner)
123-
assert!(AccountInfoTrait::is_owned_by(&account_info, &owner.to_bytes()));
127+
assert!(AccountInfoTrait::is_owned_by(
128+
&account_info,
129+
&owner.to_bytes()
130+
));
124131

125132
// Test is_owned_by() - failing (wrong owner)
126133
let wrong_owner = create_pubkey();
127-
assert!(!AccountInfoTrait::is_owned_by(&account_info, &wrong_owner.to_bytes()));
134+
assert!(!AccountInfoTrait::is_owned_by(
135+
&account_info,
136+
&wrong_owner.to_bytes()
137+
));
128138

129139
// Test data_is_empty() - failing (has data)
130140
assert!(!AccountInfoTrait::data_is_empty(&account_info));
@@ -191,23 +201,25 @@ fn test_solana_account_info_trait() {
191201

192202
// Test static methods (find_program_address and create_program_address)
193203
{
204+
use light_account_checks::error::AccountError;
194205
use solana_account_info::AccountInfo;
195206

196207
let program_id = create_pubkey();
197208
let seeds = &[b"test_seed".as_ref(), &[1, 2, 3]];
198209

199210
// Test find_program_address() - functional
200211
let (pda, bump) = AccountInfo::find_program_address(seeds, &program_id.to_bytes());
201-
212+
202213
// Verify the PDA is valid by using Solana's function
203-
let (expected_pda, expected_bump) =
214+
let (expected_pda, expected_bump) =
204215
solana_pubkey::Pubkey::find_program_address(seeds, &program_id);
205216
assert_eq!(pda, expected_pda.to_bytes());
206217
assert_eq!(bump, expected_bump);
207218

208219
// Test create_program_address() - functional
209220
let seeds_with_bump = &[b"test_seed".as_ref(), &[1, 2, 3], &[bump]];
210-
let created_pda = AccountInfo::create_program_address(seeds_with_bump, &program_id.to_bytes()).unwrap();
221+
let created_pda =
222+
AccountInfo::create_program_address(seeds_with_bump, &program_id.to_bytes()).unwrap();
211223
assert_eq!(created_pda, pda);
212224

213225
// Test create_program_address() - failing (invalid bump)
@@ -293,7 +305,7 @@ fn test_pinocchio_account_info_trait() {
293305
let mut borrowed_mut_data = AccountInfoTrait::try_borrow_mut_data(&account).unwrap();
294306
borrowed_mut_data[0] = 99;
295307
} // Drop mutable borrow
296-
308+
297309
// Verify mutation worked
298310
{
299311
let borrowed_data = AccountInfoTrait::try_borrow_data(&account).unwrap();
@@ -335,8 +347,8 @@ fn test_pinocchio_account_info_trait() {
335347
key,
336348
owner,
337349
data.clone(),
338-
true, // writable
339-
true, // signer
350+
true, // writable
351+
true, // signer
340352
false, // executable
341353
);
342354

@@ -381,26 +393,38 @@ fn test_pinocchio_account_info_trait() {
381393
// Note: Pinocchio implementation falls back to Solana when solana feature is enabled
382394
#[cfg(feature = "solana")]
383395
{
396+
use light_account_checks::{error::AccountError, AccountInfoTrait};
397+
384398
let program_id = [4u8; 32];
385399
let seeds = &[b"test_seed".as_ref(), &[1, 2, 3]];
386400

387401
// Test find_program_address() - functional
388-
let (pda, bump) = pinocchio::account_info::AccountInfo::find_program_address(seeds, &program_id);
389-
402+
let (pda, bump) =
403+
pinocchio::account_info::AccountInfo::find_program_address(seeds, &program_id);
404+
390405
// Verify the PDA is valid by using Solana's function
391-
let (expected_pda, expected_bump) =
392-
solana_pubkey::Pubkey::find_program_address(seeds, &solana_pubkey::Pubkey::from(program_id));
406+
let (expected_pda, expected_bump) = solana_pubkey::Pubkey::find_program_address(
407+
seeds,
408+
&solana_pubkey::Pubkey::from(program_id),
409+
);
393410
assert_eq!(pda, expected_pda.to_bytes());
394411
assert_eq!(bump, expected_bump);
395412

396413
// Test create_program_address() - functional
397414
let seeds_with_bump = &[b"test_seed".as_ref(), &[1, 2, 3], &[bump]];
398-
let created_pda = pinocchio::account_info::AccountInfo::create_program_address(seeds_with_bump, &program_id).unwrap();
415+
let created_pda = pinocchio::account_info::AccountInfo::create_program_address(
416+
seeds_with_bump,
417+
&program_id,
418+
)
419+
.unwrap();
399420
assert_eq!(created_pda, pda);
400421

401422
// Test create_program_address() - failing (invalid bump)
402423
let invalid_seeds = &[b"test_seed".as_ref(), &[1, 2, 3], &[255u8]]; // Invalid bump
403-
let result = pinocchio::account_info::AccountInfo::create_program_address(invalid_seeds, &program_id);
424+
let result = pinocchio::account_info::AccountInfo::create_program_address(
425+
invalid_seeds,
426+
&program_id,
427+
);
404428
assert!(result.is_err());
405429
assert_eq!(result.unwrap_err(), AccountError::InvalidSeeds);
406430
}
@@ -454,16 +478,16 @@ fn test_pinocchio_account_info_trait() {
454478
// Zero size should still have base rent (when solana feature enabled) or 0 (when not)
455479
let zero_rent = AccountInfo::get_min_rent_balance(0);
456480
assert!(zero_rent.is_ok());
457-
481+
458482
#[cfg(feature = "solana")]
459483
{
460484
assert_eq!(zero_rent.unwrap(), 890880);
461485
}
462-
486+
463487
#[cfg(not(feature = "solana"))]
464488
{
465489
assert_eq!(zero_rent.unwrap(), 0);
466490
}
467491
}
468492
*/
469-
}
493+
}

0 commit comments

Comments
 (0)