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

Return ProgramError from programs #8280

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use solana_rbpf::{memory_region::MemoryRegion, EbpfVm};
use solana_sdk::{
account::KeyedAccount,
entrypoint::SUCCESS,
instruction::InstructionError,
loader_instruction::LoaderInstruction,
program_utils::DecodeError,
program_error::ProgramError,
program_utils::{is_executable, limited_deserialize, next_keyed_account},
pubkey::Pubkey,
sysvar::rent,
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn serialize_parameters(
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
data: &[u8],
) -> Result<Vec<u8>, InstructionError> {
) -> Result<Vec<u8>, ProgramError> {
assert_eq!(32, mem::size_of::<Pubkey>());

let mut v: Vec<u8> = Vec::new();
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn serialize_parameters(
pub fn deserialize_parameters(
keyed_accounts: &[KeyedAccount],
buffer: &[u8],
) -> Result<(), InstructionError> {
) -> Result<(), ProgramError> {
assert_eq!(32, mem::size_of::<Pubkey>());

let mut start = mem::size_of::<u64>(); // number of accounts
Expand Down Expand Up @@ -139,12 +139,12 @@ pub fn process_instruction(
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
) -> Result<(), InstructionError> {
) -> Result<(), ProgramError> {
solana_logger::setup_with_default("solana=info");

if keyed_accounts.is_empty() {
warn!("No account keys");
return Err(InstructionError::NotEnoughAccountKeys);
return Err(ProgramError::NotEnoughAccountKeys);
}

if is_executable(keyed_accounts)? {
Expand All @@ -166,7 +166,7 @@ pub fn process_instruction(
match vm.execute_program(parameter_bytes.as_slice(), &[], &[heap_region]) {
Ok(status) => {
if status != SUCCESS {
let error: InstructionError = status.into();
let error: ProgramError = status.into();
warn!("BPF program failed: {:?}", error);
return Err(error);
}
Expand All @@ -185,14 +185,14 @@ pub fn process_instruction(
let program = next_keyed_account(&mut keyed_accounts_iter)?;
if program.signer_key().is_none() {
warn!("key[0] did not sign the transaction");
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}
let offset = offset as usize;
let len = bytes.len();
trace!("Write: offset={} length={}", offset, len);
if program.data_len()? < offset + len {
warn!("Write overflow: {} < {}", program.data_len()?, offset + len);
return Err(InstructionError::AccountDataTooSmall);
return Err(ProgramError::AccountDataTooSmall);
}
program.try_account_ref_mut()?.data[offset..offset + len].copy_from_slice(&bytes);
}
Expand All @@ -203,12 +203,12 @@ pub fn process_instruction(

if program.signer_key().is_none() {
warn!("key[0] did not sign the transaction");
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}

if let Err(e) = check_elf(&program.try_account_ref()?.data) {
warn!("Invalid ELF: {}", e);
return Err(InstructionError::InvalidAccountData);
return Err(ProgramError::InvalidAccountData);
}

rent::verify_rent_exemption(&program, &rent)?;
Expand Down Expand Up @@ -259,13 +259,13 @@ mod tests {

// Case: Empty keyed accounts
assert_eq!(
Err(InstructionError::NotEnoughAccountKeys),
Err(ProgramError::NotEnoughAccountKeys),
process_instruction(&program_id, &vec![], &instruction_data)
);

// Case: Not signed
assert_eq!(
Err(InstructionError::MissingRequiredSignature),
Err(ProgramError::MissingRequiredSignature),
process_instruction(&program_id, &keyed_accounts, &instruction_data)
);

Expand All @@ -285,7 +285,7 @@ mod tests {
let mut keyed_accounts = vec![KeyedAccount::new(&program_key, true, &program_account)];
keyed_accounts[0].account.borrow_mut().data = vec![0; 5];
assert_eq!(
Err(InstructionError::AccountDataTooSmall),
Err(ProgramError::AccountDataTooSmall),
process_instruction(&program_id, &keyed_accounts, &instruction_data)
);
}
Expand All @@ -306,7 +306,7 @@ mod tests {

// Case: Empty keyed accounts
assert_eq!(
Err(InstructionError::NotEnoughAccountKeys),
Err(ProgramError::NotEnoughAccountKeys),
process_instruction(&program_id, &vec![], &instruction_data)
);

Expand All @@ -315,7 +315,7 @@ mod tests {

// Case: Not signed
assert_eq!(
Err(InstructionError::MissingRequiredSignature),
Err(ProgramError::MissingRequiredSignature),
process_instruction(&program_id, &keyed_accounts, &instruction_data)
);

Expand All @@ -339,7 +339,7 @@ mod tests {
KeyedAccount::new(&rent_key, false, &rent_account),
];
assert_eq!(
Err(InstructionError::InvalidAccountData),
Err(ProgramError::InvalidAccountData),
process_instruction(&program_id, &keyed_accounts, &instruction_data)
);
}
Expand All @@ -363,7 +363,7 @@ mod tests {

// Case: Empty keyed accounts
assert_eq!(
Err(InstructionError::NotEnoughAccountKeys),
Err(ProgramError::NotEnoughAccountKeys),
process_instruction(&program_id, &vec![], &vec![])
);

Expand All @@ -376,7 +376,7 @@ mod tests {
// Case: Account not executable
keyed_accounts[0].account.borrow_mut().executable = false;
assert_eq!(
Err(InstructionError::InvalidInstructionData),
Err(ProgramError::InvalidInstructionData),
process_instruction(&program_id, &keyed_accounts, &vec![])
);
keyed_accounts[0].account.borrow_mut().executable = true;
Expand Down
40 changes: 20 additions & 20 deletions programs/config/src/config_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use crate::ConfigKeys;
use bincode::deserialize;
use log::*;
use solana_sdk::account::KeyedAccount;
use solana_sdk::instruction::InstructionError;
use solana_sdk::program_error::ProgramError;
use solana_sdk::program_utils::{limited_deserialize, next_keyed_account};
use solana_sdk::pubkey::Pubkey;

pub fn process_instruction(
_program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
data: &[u8],
) -> Result<(), InstructionError> {
) -> Result<(), ProgramError> {
let key_list: ConfigKeys = limited_deserialize(data)?;
let keyed_accounts_iter = &mut keyed_accounts.iter();
let config_keyed_account = &mut next_keyed_account(keyed_accounts_iter)?;
Expand All @@ -25,7 +25,7 @@ pub fn process_instruction(
config_account.data.len(),
err
);
InstructionError::InvalidAccountData
ProgramError::InvalidAccountData
})?
};
let current_signer_keys: Vec<Pubkey> = current_data
Expand All @@ -40,7 +40,7 @@ pub fn process_instruction(
// or when no signers specified in Config data
if config_keyed_account.signer_key().is_none() {
error!("account[0].signer_key().is_none()");
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}
}

Expand All @@ -51,19 +51,19 @@ pub fn process_instruction(
let signer_account = keyed_accounts_iter.next();
if signer_account.is_none() {
error!("account {:?} is not in account list", signer);
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}
let signer_key = signer_account.unwrap().signer_key();
if signer_key.is_none() {
error!("account {:?} signer_key().is_none()", signer);
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}
if signer_key.unwrap() != signer {
error!(
"account[{:?}].signer_key() does not match Config data)",
counter + 1
);
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}
// If Config account is already initialized, update signatures must match Config data
if !current_data.keys.is_empty()
Expand All @@ -73,11 +73,11 @@ pub fn process_instruction(
.is_none()
{
error!("account {:?} is not in stored signer list", signer);
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}
} else if config_keyed_account.signer_key().is_none() {
error!("account[0].signer_key().is_none()");
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}
}

Expand All @@ -88,12 +88,12 @@ pub fn process_instruction(
counter,
current_signer_keys.len()
);
return Err(InstructionError::MissingRequiredSignature);
return Err(ProgramError::MissingRequiredSignature);
}

if config_keyed_account.data_len()? < data.len() {
error!("instruction data too large");
return Err(InstructionError::InvalidInstructionData);
return Err(ProgramError::InvalidInstructionData);
}

config_keyed_account.try_account_ref_mut()?.data[..data.len()].copy_from_slice(&data);
Expand Down Expand Up @@ -215,7 +215,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instruction.data),
Err(InstructionError::InvalidInstructionData)
Err(ProgramError::InvalidInstructionData)
);
}

Expand All @@ -233,7 +233,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instruction.data),
Err(InstructionError::MissingRequiredSignature)
Err(ProgramError::MissingRequiredSignature)
);
}

Expand Down Expand Up @@ -290,7 +290,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instruction.data),
Err(InstructionError::InvalidAccountData)
Err(ProgramError::InvalidAccountData)
);
}

Expand All @@ -316,7 +316,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instruction.data),
Err(InstructionError::MissingRequiredSignature)
Err(ProgramError::MissingRequiredSignature)
);

// Config-data pubkey not a signer
Expand All @@ -327,7 +327,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instruction.data),
Err(InstructionError::MissingRequiredSignature)
Err(ProgramError::MissingRequiredSignature)
);
}

Expand Down Expand Up @@ -395,7 +395,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instruction.data),
Err(InstructionError::MissingRequiredSignature)
Err(ProgramError::MissingRequiredSignature)
);

// Attempt update with incorrect signatures
Expand All @@ -414,7 +414,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instruction.data),
Err(InstructionError::MissingRequiredSignature)
Err(ProgramError::MissingRequiredSignature)
);
}

Expand Down Expand Up @@ -477,7 +477,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instruction.data),
Err(InstructionError::MissingRequiredSignature)
Err(ProgramError::MissingRequiredSignature)
);
}

Expand All @@ -491,7 +491,7 @@ mod tests {
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
assert_eq!(
process_instruction(&id(), &keyed_accounts, &instructions[1].data),
Err(InstructionError::NotEnoughAccountKeys)
Err(ProgramError::NotEnoughAccountKeys)
);
}
}
10 changes: 5 additions & 5 deletions programs/stake/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use solana_config_program::{create_config_account, get_config_data, ConfigState}
use solana_sdk::{
account::{Account, KeyedAccount},
genesis_config::GenesisConfig,
instruction::InstructionError,
program_error::ProgramError,
};

// stake config ID
Expand Down Expand Up @@ -63,11 +63,11 @@ pub fn create_account(lamports: u64, config: &Config) -> Account {
create_config_account(vec![], config, lamports)
}

pub fn from_keyed_account(account: &KeyedAccount) -> Result<Config, InstructionError> {
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Config, ProgramError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
return Err(ProgramError::InvalidArgument);
}
Config::from(&*account.try_account_ref()?).ok_or(InstructionError::InvalidArgument)
Config::from(&*account.try_account_ref()?).ok_or(ProgramError::InvalidArgument)
}

#[cfg(test)]
Expand All @@ -82,7 +82,7 @@ mod tests {
assert_eq!(Config::from(&account.borrow()), Some(Config::default()));
assert_eq!(
from_keyed_account(&KeyedAccount::new(&Pubkey::default(), false, &mut account)),
Err(InstructionError::InvalidArgument)
Err(ProgramError::InvalidArgument)
);
}
}
Loading