Skip to content

Commit 85e5cbb

Browse files
committed
fix: resolve pinocchio feature compile issues
1 parent 9df1ca5 commit 85e5cbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+558
-853
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use core::ops::{Deref, DerefMut};
2+
3+
use crate::error::AccountError;
4+
5+
/// Trait to abstract over different AccountInfo implementations (pinocchio vs solana)
6+
pub trait AccountInfoTrait {
7+
type DataRef<'a>: Deref<Target = [u8]>
8+
where
9+
Self: 'a;
10+
type DataRefMut<'a>: DerefMut<Target = [u8]>
11+
where
12+
Self: 'a;
13+
14+
/// Return raw byte array for maximum compatibility
15+
fn key(&self) -> [u8; 32];
16+
fn is_writable(&self) -> bool;
17+
fn is_signer(&self) -> bool;
18+
fn executable(&self) -> bool;
19+
fn lamports(&self) -> u64;
20+
fn data_len(&self) -> usize;
21+
22+
/// Unified data access interface
23+
fn try_borrow_data(&self) -> Result<Self::DataRef<'_>, AccountError>;
24+
fn try_borrow_mut_data(&self) -> Result<Self::DataRefMut<'_>, AccountError>;
25+
26+
/// Check ownership safely - each implementation handles this without exposing owner
27+
fn is_owned_by(&self, program: &[u8; 32]) -> bool;
28+
29+
/// PDA functions - each implementation uses its own backend
30+
fn find_program_address(seeds: &[&[u8]], program_id: &[u8; 32]) -> ([u8; 32], u8);
31+
fn create_program_address(
32+
seeds: &[&[u8]],
33+
program_id: &[u8; 32],
34+
) -> Result<[u8; 32], AccountError>;
35+
36+
/// Get minimum rent balance for a given size
37+
fn get_min_rent_balance(size: usize) -> Result<u64, AccountError>;
38+
39+
fn data_is_empty(&self) -> bool {
40+
self.data_len() == 0
41+
}
42+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub mod account_info_trait;
2+
#[cfg(feature = "pinocchio")]
3+
pub mod pinocchio;
4+
#[cfg(feature = "solana")]
5+
pub mod solana;
6+
pub mod test_account_info;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use super::account_info_trait::AccountInfoTrait;
2+
use crate::error::AccountError;
3+
4+
/// Implement trait for pinocchio AccountInfo
5+
impl AccountInfoTrait for pinocchio::account_info::AccountInfo {
6+
type DataRef<'a> = pinocchio::account_info::Ref<'a, [u8]>;
7+
type DataRefMut<'a> = pinocchio::account_info::RefMut<'a, [u8]>;
8+
9+
fn key(&self) -> [u8; 32] {
10+
*self.key()
11+
}
12+
13+
fn is_writable(&self) -> bool {
14+
self.is_writable()
15+
}
16+
17+
fn is_signer(&self) -> bool {
18+
self.is_signer()
19+
}
20+
21+
fn executable(&self) -> bool {
22+
self.executable()
23+
}
24+
25+
fn lamports(&self) -> u64 {
26+
self.lamports()
27+
}
28+
29+
fn data_len(&self) -> usize {
30+
self.data_len()
31+
}
32+
33+
fn try_borrow_data(&self) -> Result<Self::DataRef<'_>, AccountError> {
34+
self.try_borrow_data().map_err(Into::into)
35+
}
36+
37+
fn try_borrow_mut_data(&self) -> Result<Self::DataRefMut<'_>, AccountError> {
38+
self.try_borrow_mut_data().map_err(Into::into)
39+
}
40+
41+
fn is_owned_by(&self, program: &[u8; 32]) -> bool {
42+
pinocchio::account_info::AccountInfo::is_owned_by(self, program)
43+
}
44+
45+
fn find_program_address(_seeds: &[&[u8]], _program_id: &[u8; 32]) -> ([u8; 32], u8) {
46+
#[cfg(target_os = "solana")]
47+
{
48+
let program_pubkey = pinocchio::pubkey::Pubkey::from(*_program_id);
49+
let (pubkey, bump) = pinocchio::pubkey::find_program_address(_seeds, &program_pubkey);
50+
(pubkey, bump)
51+
}
52+
// Pinocchio does not support find_program_address outside of target_os solana.
53+
// That is annoying for rust unit tests.
54+
#[cfg(all(not(target_os = "solana"), feature = "solana"))]
55+
{
56+
let program_pubkey = solana_pubkey::Pubkey::from(*_program_id);
57+
let (pubkey, bump) =
58+
solana_pubkey::Pubkey::find_program_address(_seeds, &program_pubkey);
59+
(pubkey.to_bytes(), bump)
60+
}
61+
#[cfg(all(not(target_os = "solana"), not(feature = "solana")))]
62+
{
63+
panic!("find_program_address not supported with pinocchio outside target_os = solana without solana feature");
64+
}
65+
}
66+
67+
fn create_program_address(
68+
_seeds: &[&[u8]],
69+
_program_id: &[u8; 32],
70+
) -> Result<[u8; 32], AccountError> {
71+
#[cfg(target_os = "solana")]
72+
{
73+
let program_pubkey = pinocchio::pubkey::Pubkey::from(*_program_id);
74+
pinocchio::pubkey::create_program_address(_seeds, &program_pubkey)
75+
.map_err(|_| AccountError::InvalidSeeds)
76+
}
77+
// Pinocchio does not support find_program_address outside of target_os solana.
78+
// That is annoying for rust unit tests.
79+
#[cfg(all(not(target_os = "solana"), feature = "solana"))]
80+
{
81+
let program_pubkey = solana_pubkey::Pubkey::from(*_program_id);
82+
let pubkey = solana_pubkey::Pubkey::create_program_address(_seeds, &program_pubkey)
83+
.map_err(|_| AccountError::InvalidSeeds)?;
84+
Ok(pubkey.to_bytes())
85+
}
86+
#[cfg(all(not(target_os = "solana"), not(feature = "solana")))]
87+
{
88+
Err(AccountError::InvalidSeeds)
89+
}
90+
}
91+
92+
fn get_min_rent_balance(_size: usize) -> Result<u64, AccountError> {
93+
#[cfg(target_os = "solana")]
94+
{
95+
use pinocchio::sysvars::Sysvar;
96+
pinocchio::sysvars::rent::Rent::get()
97+
.map(|rent| rent.minimum_balance(_size))
98+
.map_err(|_| AccountError::FailedBorrowRentSysvar)
99+
}
100+
#[cfg(all(not(target_os = "solana"), feature = "solana"))]
101+
{
102+
use solana_sysvar::Sysvar;
103+
104+
solana_sysvar::rent::Rent::get()
105+
.map(|rent| rent.minimum_balance(_size))
106+
.map_err(|_| AccountError::FailedBorrowRentSysvar)
107+
}
108+
#[cfg(all(not(target_os = "solana"), not(feature = "solana")))]
109+
{
110+
Err(AccountError::FailedBorrowRentSysvar)
111+
}
112+
}
113+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use super::account_info_trait::AccountInfoTrait;
2+
use crate::error::AccountError;
3+
4+
/// Implement trait for solana AccountInfo
5+
impl AccountInfoTrait for solana_account_info::AccountInfo<'_> {
6+
type DataRef<'a>
7+
= std::cell::Ref<'a, [u8]>
8+
where
9+
Self: 'a;
10+
type DataRefMut<'a>
11+
= std::cell::RefMut<'a, [u8]>
12+
where
13+
Self: 'a;
14+
15+
fn key(&self) -> [u8; 32] {
16+
self.key.to_bytes()
17+
}
18+
19+
fn is_writable(&self) -> bool {
20+
self.is_writable
21+
}
22+
23+
fn is_signer(&self) -> bool {
24+
self.is_signer
25+
}
26+
27+
fn executable(&self) -> bool {
28+
self.executable
29+
}
30+
31+
fn lamports(&self) -> u64 {
32+
**self.lamports.borrow()
33+
}
34+
35+
fn data_len(&self) -> usize {
36+
self.data.borrow().len()
37+
}
38+
39+
fn try_borrow_data(&self) -> Result<Self::DataRef<'_>, AccountError> {
40+
self.data
41+
.try_borrow()
42+
.map(|r| std::cell::Ref::map(r, |data| &**data))
43+
.map_err(Into::into)
44+
}
45+
46+
fn try_borrow_mut_data(&self) -> Result<Self::DataRefMut<'_>, AccountError> {
47+
self.data
48+
.try_borrow_mut()
49+
.map(|r| std::cell::RefMut::map(r, |data| &mut **data))
50+
.map_err(Into::into)
51+
}
52+
53+
fn is_owned_by(&self, program: &[u8; 32]) -> bool {
54+
self.owner.as_ref() == program
55+
}
56+
57+
fn find_program_address(seeds: &[&[u8]], program_id: &[u8; 32]) -> ([u8; 32], u8) {
58+
let program_pubkey = solana_pubkey::Pubkey::from(*program_id);
59+
let (pubkey, bump) = solana_pubkey::Pubkey::find_program_address(seeds, &program_pubkey);
60+
(pubkey.to_bytes(), bump)
61+
}
62+
63+
fn create_program_address(
64+
seeds: &[&[u8]],
65+
program_id: &[u8; 32],
66+
) -> Result<[u8; 32], AccountError> {
67+
let program_pubkey = solana_pubkey::Pubkey::from(*program_id);
68+
solana_pubkey::Pubkey::create_program_address(seeds, &program_pubkey)
69+
.map(|pubkey| pubkey.to_bytes())
70+
.map_err(|_| AccountError::InvalidSeeds)
71+
}
72+
73+
fn get_min_rent_balance(size: usize) -> Result<u64, AccountError> {
74+
use solana_sysvar::Sysvar;
75+
solana_sysvar::rent::Rent::get()
76+
.map(|rent| rent.minimum_balance(size))
77+
.map_err(|_| AccountError::FailedBorrowRentSysvar)
78+
}
79+
}

0 commit comments

Comments
 (0)