Skip to content

Commit 0ca65f9

Browse files
committed
fix: preserve error information in pinocchio ProgramError conversion
1 parent 6a0b7c1 commit 0ca65f9

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl AccountInfoTrait for pinocchio::account_info::AccountInfo {
7373
pinocchio::pubkey::create_program_address(_seeds, &program_pubkey)
7474
.map_err(|_| AccountError::InvalidSeeds)
7575
}
76-
// Pinocchio does not support find_program_address outside of target_os solana.
76+
// Pinocchio does not support create_program_address outside of target_os solana.
7777
#[cfg(all(not(target_os = "solana"), feature = "solana"))]
7878
{
7979
let program_pubkey = solana_pubkey::Pubkey::from(*_program_id);

program-libs/account-checks/src/error.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub enum AccountError {
3030
ProgramNotExecutable,
3131
#[error("Account not zeroed.")]
3232
AccountNotZeroed,
33+
#[error("Pinocchio program error with code: {0}")]
34+
PinocchioProgramError(u32),
3335
}
3436

3537
// TODO: reconfigure error codes
@@ -50,6 +52,7 @@ impl From<AccountError> for u32 {
5052
AccountError::InvalidProgramId => 12017,
5153
AccountError::ProgramNotExecutable => 12018,
5254
AccountError::AccountNotZeroed => 12019,
55+
AccountError::PinocchioProgramError(code) => code,
5356
}
5457
}
5558
}
@@ -70,8 +73,30 @@ impl From<AccountError> for solana_program_error::ProgramError {
7073

7174
#[cfg(feature = "pinocchio")]
7275
impl From<pinocchio::program_error::ProgramError> for AccountError {
73-
fn from(_: pinocchio::program_error::ProgramError) -> Self {
74-
AccountError::BorrowAccountDataFailed
76+
fn from(error: pinocchio::program_error::ProgramError) -> Self {
77+
match error {
78+
pinocchio::program_error::ProgramError::Custom(code) => {
79+
AccountError::PinocchioProgramError(code)
80+
}
81+
_ => {
82+
// Convert other ProgramError variants to error codes
83+
let error_code = match error {
84+
pinocchio::program_error::ProgramError::InvalidArgument => 1,
85+
pinocchio::program_error::ProgramError::InvalidInstructionData => 2,
86+
pinocchio::program_error::ProgramError::InvalidAccountData => 3,
87+
pinocchio::program_error::ProgramError::AccountDataTooSmall => 4,
88+
pinocchio::program_error::ProgramError::InsufficientFunds => 5,
89+
pinocchio::program_error::ProgramError::IncorrectProgramId => 6,
90+
pinocchio::program_error::ProgramError::MissingRequiredSignature => 7,
91+
pinocchio::program_error::ProgramError::AccountAlreadyInitialized => 8,
92+
pinocchio::program_error::ProgramError::UninitializedAccount => 9,
93+
pinocchio::program_error::ProgramError::NotEnoughAccountKeys => 10,
94+
pinocchio::program_error::ProgramError::AccountBorrowFailed => 11,
95+
_ => 0, // Unknown error
96+
};
97+
AccountError::PinocchioProgramError(error_code)
98+
}
99+
}
75100
}
76101
}
77102

program-tests/account-compression-test/tests/merkle_tree_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ async fn insert_into_nullifier_queues<R: Rpc>(
14071407
.iter()
14081408
.map(|(pubkey, index)| (*pubkey, *index))
14091409
.collect::<Vec<(Pubkey, u8)>>();
1410-
remaining_accounts.sort_by(|a, b| a.1.cmp(&b.1));
1410+
remaining_accounts.sort_by_key(|(_, idx)| *idx);
14111411
let remaining_accounts = remaining_accounts
14121412
.iter()
14131413
.map(|(pubkey, _)| AccountMeta::new(*pubkey, false))

program-tests/utils/src/e2e_test_env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3399,7 +3399,7 @@ pub fn to_account_metas_light(
33993399
})
34003400
.collect::<Vec<(AccountMeta, usize)>>();
34013401
// hash maps are not sorted so we need to sort manually and collect into a vector again
3402-
remaining_accounts.sort_by(|a, b| a.1.cmp(&b.1));
3402+
remaining_accounts.sort_by_key(|(_, idx)| *idx);
34033403
let remaining_accounts = remaining_accounts
34043404
.iter()
34053405
.map(|(k, _)| k.clone())

program-tests/utils/src/system_program.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ pub fn create_invoke_instruction_data_and_remaining_accounts(
665665
.map(|(k, i)| (AccountMeta::new(*k, false), *i))
666666
.collect::<Vec<(AccountMeta, usize)>>();
667667
// hash maps are not sorted so we need to sort manually and collect into a vector again
668-
remaining_accounts.sort_by(|a, b| a.1.cmp(&b.1));
668+
remaining_accounts.sort_by_key(|(_, idx)| *idx);
669669
let remaining_accounts = remaining_accounts
670670
.iter()
671671
.map(|(k, _)| k.clone())

programs/compressed-token/src/process_transfer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ pub mod transfer_sdk {
10981098
})
10991099
.collect::<Vec<(AccountMeta, usize)>>();
11001100
// hash maps are not sorted so we need to sort manually and collect into a vector again
1101-
remaining_accounts.sort_by(|a, b| a.1.cmp(&b.1));
1101+
remaining_accounts.sort_by_key(|(_, idx)| *idx);
11021102
let remaining_accounts = remaining_accounts
11031103
.iter()
11041104
.map(|(k, _)| k.clone())

0 commit comments

Comments
 (0)