Skip to content

Commit

Permalink
Merge InstructionError and ProgramError
Browse files Browse the repository at this point in the history
From the user's perspective, it's just an instruction error.
For program-specific errors, we still have
InstructionError::CustomError.
  • Loading branch information
garious committed Mar 18, 2019
1 parent 607b368 commit 8d032ab
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 187 deletions.
2 changes: 1 addition & 1 deletion core/src/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ mod tests {
let (_, entries) = entry_receiver.recv().unwrap();
assert_eq!(entries[0].0.transactions.len(), transactions.len());

// ProgramErrors should still be recorded
// InstructionErrors should still be recorded
results[0] = Err(TransactionError::InstructionError(
1,
InstructionError::new_result_with_negative_lamports(),
Expand Down
2 changes: 1 addition & 1 deletion core/src/blocktree_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ mod tests {
entries.push(entry);

// Add a second Transaction that will produce a
// ProgramError<0, ResultWithNegativeLamports> error when processed
// InstructionError<0, ResultWithNegativeLamports> error when processed
let keypair2 = Keypair::new();
let tx = SystemTransaction::new_account(&keypair, &keypair2.pubkey(), 42, blockhash, 0);
let entry = Entry::new(&last_entry_hash, 1, vec![tx]);
Expand Down
16 changes: 8 additions & 8 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use log::*;
use solana_rbpf::{EbpfVmRaw, MemoryRegion};
use solana_sdk::account::KeyedAccount;
use solana_sdk::loader_instruction::LoaderInstruction;
use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::solana_entrypoint;
use solana_sdk::transaction::InstructionError;
use std::ffi::CStr;
use std::io::prelude::*;
use std::io::{Error, ErrorKind};
Expand Down Expand Up @@ -185,7 +185,7 @@ fn entrypoint(
keyed_accounts: &mut [KeyedAccount],
tx_data: &[u8],
tick_height: u64,
) -> Result<(), ProgramError> {
) -> Result<(), InstructionError> {
solana_logger::setup();

if keyed_accounts[0].account.executable {
Expand All @@ -197,20 +197,20 @@ fn entrypoint(
Ok(vm) => vm,
Err(e) => {
warn!("Failed to create BPF VM: {}", e);
return Err(ProgramError::GenericError);
return Err(InstructionError::GenericError);
}
};
let mut v = serialize_parameters(program_id, params, &tx_data, tick_height);
match vm.execute_program(v.as_mut_slice()) {
Ok(status) => {
if 0 == status {
warn!("BPF program failed: {}", status);
return Err(ProgramError::GenericError);
return Err(InstructionError::GenericError);
}
}
Err(e) => {
warn!("BPF VM failed to run program: {}", e);
return Err(ProgramError::GenericError);
return Err(InstructionError::GenericError);
}
}
deserialize_parameters(params, &v);
Expand All @@ -221,7 +221,7 @@ fn entrypoint(
} else if let Ok(instruction) = bincode::deserialize(tx_data) {
if keyed_accounts[0].signer_key().is_none() {
warn!("key[0] did not sign the transaction");
return Err(ProgramError::GenericError);
return Err(InstructionError::GenericError);
}
match instruction {
LoaderInstruction::Write { offset, bytes } => {
Expand All @@ -234,7 +234,7 @@ fn entrypoint(
keyed_accounts[0].account.data.len(),
offset + len
);
return Err(ProgramError::GenericError);
return Err(InstructionError::GenericError);
}
keyed_accounts[0].account.data[offset..offset + len].copy_from_slice(&bytes);
}
Expand All @@ -248,7 +248,7 @@ fn entrypoint(
}
} else {
warn!("Invalid program transaction: {:?}", tx_data);
return Err(ProgramError::GenericError);
return Err(InstructionError::GenericError);
}
Ok(())
}
Expand Down
28 changes: 13 additions & 15 deletions programs/budget/src/budget_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use solana_budget_api::budget_instruction::BudgetInstruction;
use solana_budget_api::budget_state::{BudgetError, BudgetState};
use solana_budget_api::payment_plan::Witness;
use solana_sdk::account::KeyedAccount;
use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::transaction::InstructionError;

/// Process a Witness Signature. Any payment plans waiting on this signature
/// will progress one step.
Expand Down Expand Up @@ -75,10 +75,10 @@ pub fn process_instruction(
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
_tick_height: u64,
) -> Result<(), ProgramError> {
) -> Result<(), InstructionError> {
let instruction = deserialize(data).map_err(|err| {
info!("Invalid transaction data: {:?} {:?}", data, err);
ProgramError::InvalidInstructionData
InstructionError::InvalidInstructionData
})?;

trace!("process_instruction: {:?}", instruction);
Expand All @@ -94,7 +94,7 @@ pub fn process_instruction(
let existing = BudgetState::deserialize(&keyed_accounts[0].account.data).ok();
if Some(true) == existing.map(|x| x.initialized) {
trace!("contract already exists");
return Err(ProgramError::AccountAlreadyInitialized);
return Err(InstructionError::AccountAlreadyInitialized);
}
let mut budget_state = BudgetState::default();
budget_state.pending_budget = Some(expr);
Expand All @@ -108,14 +108,14 @@ pub fn process_instruction(
}
if !budget_state.initialized {
trace!("contract is uninitialized");
return Err(ProgramError::UninitializedAccount);
return Err(InstructionError::UninitializedAccount);
}
if keyed_accounts[0].signer_key().is_none() {
return Err(ProgramError::MissingRequiredSignature);
return Err(InstructionError::MissingRequiredSignature);
}
trace!("apply timestamp");
apply_timestamp(&mut budget_state, keyed_accounts, dt)
.map_err(|e| ProgramError::CustomError(serialize(&e).unwrap()))?;
.map_err(|e| InstructionError::CustomError(serialize(&e).unwrap()))?;
trace!("apply timestamp committed");
budget_state.serialize(&mut keyed_accounts[1].account.data)
}
Expand All @@ -126,14 +126,14 @@ pub fn process_instruction(
}
if !budget_state.initialized {
trace!("contract is uninitialized");
return Err(ProgramError::UninitializedAccount);
return Err(InstructionError::UninitializedAccount);
}
if keyed_accounts[0].signer_key().is_none() {
return Err(ProgramError::MissingRequiredSignature);
return Err(InstructionError::MissingRequiredSignature);
}
trace!("apply signature");
apply_signature(&mut budget_state, keyed_accounts)
.map_err(|e| ProgramError::CustomError(serialize(&e).unwrap()))?;
.map_err(|e| InstructionError::CustomError(serialize(&e).unwrap()))?;
trace!("apply signature committed");
budget_state.serialize(&mut keyed_accounts[1].account.data)
}
Expand Down Expand Up @@ -207,7 +207,7 @@ mod test {
mallory_client.process_transaction(transaction),
Err(TransactionError::InstructionError(
0,
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
InstructionError::MissingRequiredSignature
))
);
}
Expand Down Expand Up @@ -254,7 +254,7 @@ mod test {
mallory_client.process_transaction(transaction),
Err(TransactionError::InstructionError(
0,
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
InstructionError::MissingRequiredSignature
))
);
}
Expand Down Expand Up @@ -296,9 +296,7 @@ mod test {
alice_client.process_instruction(instruction).unwrap_err(),
TransactionError::InstructionError(
0,
InstructionError::ProgramError(ProgramError::CustomError(
serialize(&BudgetError::DestinationMissing).unwrap()
))
InstructionError::CustomError(serialize(&BudgetError::DestinationMissing).unwrap())
)
);
assert_eq!(bank.get_balance(&alice_pubkey), 1);
Expand Down
4 changes: 2 additions & 2 deletions programs/budget/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ mod budget_processor;
use crate::budget_processor::process_instruction;
use log::*;
use solana_sdk::account::KeyedAccount;
use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::solana_entrypoint;
use solana_sdk::transaction::InstructionError;

solana_entrypoint!(entrypoint);
fn entrypoint(
program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
tick_height: u64,
) -> Result<(), ProgramError> {
) -> Result<(), InstructionError> {
solana_logger::setup();

trace!("process_instruction: {:?}", data);
Expand Down
12 changes: 6 additions & 6 deletions programs/budget_api/src/budget_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::budget_expr::BudgetExpr;
use bincode::{self, deserialize, serialize_into};
use serde_derive::{Deserialize, Serialize};
use solana_sdk::native_program::ProgramError;
use solana_sdk::transaction::InstructionError;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum BudgetError {
Expand All @@ -27,12 +27,12 @@ impl BudgetState {
self.pending_budget.is_some()
}

pub fn serialize(&self, output: &mut [u8]) -> Result<(), ProgramError> {
serialize_into(output, self).map_err(|_| ProgramError::AccountDataTooSmall)
pub fn serialize(&self, output: &mut [u8]) -> Result<(), InstructionError> {
serialize_into(output, self).map_err(|_| InstructionError::AccountDataTooSmall)
}

pub fn deserialize(input: &[u8]) -> Result<Self, ProgramError> {
deserialize(input).map_err(|_| ProgramError::InvalidAccountData)
pub fn deserialize(input: &[u8]) -> Result<Self, InstructionError> {
deserialize(input).map_err(|_| InstructionError::InvalidAccountData)
}
}

Expand All @@ -57,7 +57,7 @@ mod test {
let b = BudgetState::default();
assert_eq!(
b.serialize(&mut a.data),
Err(ProgramError::AccountDataTooSmall)
Err(InstructionError::AccountDataTooSmall)
);
}
}
14 changes: 7 additions & 7 deletions programs/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
use log::*;
use solana_config_api::check_id;
use solana_sdk::account::KeyedAccount;
use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::solana_entrypoint;
use solana_sdk::transaction::InstructionError;

fn process_instruction(
_program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
_tick_height: u64,
) -> Result<(), ProgramError> {
) -> Result<(), InstructionError> {
if !check_id(&keyed_accounts[0].account.owner) {
error!("account[0] is not assigned to the config program");
Err(ProgramError::IncorrectProgramId)?;
Err(InstructionError::IncorrectProgramId)?;
}

if keyed_accounts[0].signer_key().is_none() {
error!("account[0] should sign the transaction");
Err(ProgramError::MissingRequiredSignature)?;
Err(InstructionError::MissingRequiredSignature)?;
}

if keyed_accounts[0].account.data.len() < data.len() {
error!("instruction data too large");
Err(ProgramError::InvalidInstructionData)?;
Err(InstructionError::InvalidInstructionData)?;
}

keyed_accounts[0].account.data[0..data.len()].copy_from_slice(data);
Expand All @@ -38,7 +38,7 @@ fn entrypoint(
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
tick_height: u64,
) -> Result<(), ProgramError> {
) -> Result<(), InstructionError> {
solana_logger::setup();

trace!("process_instruction: {:?}", data);
Expand Down Expand Up @@ -163,7 +163,7 @@ mod tests {
config_client.process_instruction(instruction),
Err(TransactionError::InstructionError(
0,
InstructionError::ProgramError(ProgramError::IncorrectProgramId)
InstructionError::IncorrectProgramId
))
);
}
Expand Down
6 changes: 3 additions & 3 deletions programs/failure/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use solana_sdk::account::KeyedAccount;
use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::solana_entrypoint;
use solana_sdk::transaction::InstructionError;

solana_entrypoint!(entrypoint);
fn entrypoint(
_program_id: &Pubkey,
_keyed_accounts: &mut [KeyedAccount],
_data: &[u8],
_tick_height: u64,
) -> Result<(), ProgramError> {
Err(ProgramError::GenericError)
) -> Result<(), InstructionError> {
Err(InstructionError::GenericError)
}
3 changes: 1 addition & 2 deletions programs/failure/tests/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use solana_runtime::bank::Bank;
use solana_runtime::loader_utils::load_program;
use solana_sdk::genesis_block::GenesisBlock;
use solana_sdk::native_loader;
use solana_sdk::native_program::ProgramError;
use solana_sdk::transaction::{InstructionError, Transaction, TransactionError};

#[test]
Expand All @@ -26,7 +25,7 @@ fn test_program_native_failure() {
bank.process_transaction(&tx),
Err(TransactionError::InstructionError(
0,
InstructionError::ProgramError(ProgramError::GenericError)
InstructionError::GenericError
))
);
}
4 changes: 2 additions & 2 deletions programs/noop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use log::*;
use solana_sdk::account::KeyedAccount;
use solana_sdk::native_program::ProgramError;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::solana_entrypoint;
use solana_sdk::transaction::InstructionError;

solana_entrypoint!(entrypoint);
fn entrypoint(
program_id: &Pubkey,
keyed_accounts: &mut [KeyedAccount],
data: &[u8],
tick_height: u64,
) -> Result<(), ProgramError> {
) -> Result<(), InstructionError> {
solana_logger::setup();
info!("noop: program_id: {:?}", program_id);
info!("noop: keyed_accounts: {:#?}", keyed_accounts);
Expand Down
Loading

0 comments on commit 8d032ab

Please sign in to comment.