Merge InstructionError and ProgramError
From the user's perspective, it's just an instruction error. For program-specific errors, we still have InstructionError::CustomError.
This commit is contained in:
@ -7,8 +7,9 @@ use libloading::os::windows::*;
|
||||
use log::*;
|
||||
use solana_sdk::account::KeyedAccount;
|
||||
use solana_sdk::loader_instruction::LoaderInstruction;
|
||||
use solana_sdk::native_program::{self, ProgramError};
|
||||
use solana_sdk::native_program;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::transaction::InstructionError;
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::str;
|
||||
@ -51,7 +52,7 @@ pub fn entrypoint(
|
||||
keyed_accounts: &mut [KeyedAccount],
|
||||
ix_data: &[u8],
|
||||
tick_height: u64,
|
||||
) -> Result<(), ProgramError> {
|
||||
) -> Result<(), InstructionError> {
|
||||
if keyed_accounts[0].account.executable {
|
||||
// dispatch it
|
||||
let (names, params) = keyed_accounts.split_at_mut(1);
|
||||
@ -60,7 +61,7 @@ pub fn entrypoint(
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
warn!("Invalid UTF-8 sequence: {}", e);
|
||||
return Err(ProgramError::GenericError);
|
||||
return Err(InstructionError::GenericError);
|
||||
}
|
||||
};
|
||||
trace!("Call native {:?}", name);
|
||||
@ -77,20 +78,20 @@ pub fn entrypoint(
|
||||
e,
|
||||
native_program::ENTRYPOINT
|
||||
);
|
||||
return Err(ProgramError::GenericError);
|
||||
return Err(InstructionError::GenericError);
|
||||
}
|
||||
};
|
||||
return entrypoint(program_id, params, ix_data, tick_height);
|
||||
},
|
||||
Err(e) => {
|
||||
warn!("Unable to load: {:?}", e);
|
||||
return Err(ProgramError::GenericError);
|
||||
return Err(InstructionError::GenericError);
|
||||
}
|
||||
}
|
||||
} else if let Ok(instruction) = deserialize(ix_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 } => {
|
||||
@ -102,7 +103,7 @@ pub fn entrypoint(
|
||||
keyed_accounts[0].account.data.len(),
|
||||
offset + bytes.len()
|
||||
);
|
||||
return Err(ProgramError::GenericError);
|
||||
return Err(InstructionError::GenericError);
|
||||
}
|
||||
// native loader takes a name and we assume it all comes in at once
|
||||
keyed_accounts[0].account.data = bytes;
|
||||
@ -118,7 +119,7 @@ pub fn entrypoint(
|
||||
}
|
||||
} else {
|
||||
warn!("Invalid data in instruction: {:?}", ix_data);
|
||||
return Err(ProgramError::GenericError);
|
||||
return Err(InstructionError::GenericError);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
use crate::native_loader;
|
||||
use solana_sdk::account::{create_keyed_accounts, Account, KeyedAccount};
|
||||
use solana_sdk::native_program::ProgramError;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::system_program;
|
||||
use solana_sdk::transaction::{InstructionError, Transaction, TransactionError};
|
||||
@ -70,18 +69,18 @@ fn verify_instruction(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_error(err: ProgramError) -> ProgramError {
|
||||
fn verify_error(err: InstructionError) -> InstructionError {
|
||||
match err {
|
||||
ProgramError::CustomError(mut error) => {
|
||||
InstructionError::CustomError(mut error) => {
|
||||
error.truncate(32);
|
||||
ProgramError::CustomError(error)
|
||||
InstructionError::CustomError(error)
|
||||
}
|
||||
e => e,
|
||||
}
|
||||
}
|
||||
|
||||
pub type ProcessInstruction =
|
||||
fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), ProgramError>;
|
||||
fn(&Pubkey, &mut [KeyedAccount], &[u8], u64) -> Result<(), InstructionError>;
|
||||
|
||||
pub struct Runtime {
|
||||
instruction_processors: Vec<(Pubkey, ProcessInstruction)>,
|
||||
@ -118,7 +117,7 @@ impl Runtime {
|
||||
executable_accounts: &mut [(Pubkey, Account)],
|
||||
program_accounts: &mut [&mut Account],
|
||||
tick_height: u64,
|
||||
) -> Result<(), ProgramError> {
|
||||
) -> Result<(), InstructionError> {
|
||||
let program_id = tx.program_id(instruction_index);
|
||||
|
||||
let mut keyed_accounts = create_keyed_accounts(executable_accounts);
|
||||
@ -182,8 +181,7 @@ impl Runtime {
|
||||
program_accounts,
|
||||
tick_height,
|
||||
)
|
||||
.map_err(verify_error)
|
||||
.map_err(InstructionError::ProgramError)?;
|
||||
.map_err(verify_error)?;
|
||||
|
||||
// Verify the instruction
|
||||
for ((pre_program_id, pre_lamports, pre_data), post_account) in
|
||||
@ -322,15 +320,15 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_verify_error() {
|
||||
let short_error = ProgramError::CustomError(vec![1, 2, 3]);
|
||||
let short_error = InstructionError::CustomError(vec![1, 2, 3]);
|
||||
let expected_short_error = short_error.clone(); // short CustomError errors should be untouched
|
||||
assert_eq!(verify_error(short_error), expected_short_error);
|
||||
|
||||
let long_error = ProgramError::CustomError(vec![8; 40]);
|
||||
let expected_long_error = ProgramError::CustomError(vec![8; 32]); // long CustomError errors should be truncated
|
||||
let long_error = InstructionError::CustomError(vec![8; 40]);
|
||||
let expected_long_error = InstructionError::CustomError(vec![8; 32]); // long CustomError errors should be truncated
|
||||
assert_eq!(verify_error(long_error), expected_long_error);
|
||||
|
||||
let other_error = ProgramError::GenericError;
|
||||
let other_error = InstructionError::GenericError;
|
||||
let expected_other_error = other_error.clone(); // non-CustomError errors should be untouched
|
||||
assert_eq!(verify_error(other_error), expected_other_error);
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
use bincode::serialize;
|
||||
use log::*;
|
||||
use solana_sdk::account::KeyedAccount;
|
||||
use solana_sdk::native_program::ProgramError;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::system_instruction::{SystemError, SystemInstruction};
|
||||
use solana_sdk::system_program;
|
||||
use solana_sdk::transaction::InstructionError;
|
||||
|
||||
const FROM_ACCOUNT_INDEX: usize = 0;
|
||||
const TO_ACCOUNT_INDEX: usize = 1;
|
||||
@ -69,7 +69,7 @@ pub fn entrypoint(
|
||||
keyed_accounts: &mut [KeyedAccount],
|
||||
data: &[u8],
|
||||
_tick_height: u64,
|
||||
) -> Result<(), ProgramError> {
|
||||
) -> Result<(), InstructionError> {
|
||||
if let Ok(instruction) = bincode::deserialize(data) {
|
||||
trace!("process_instruction: {:?}", instruction);
|
||||
trace!("keyed_accounts: {:?}", keyed_accounts);
|
||||
@ -77,7 +77,7 @@ pub fn entrypoint(
|
||||
// All system instructions require that accounts_keys[0] be a signer
|
||||
if keyed_accounts[FROM_ACCOUNT_INDEX].signer_key().is_none() {
|
||||
info!("account[from] is unsigned");
|
||||
Err(ProgramError::MissingRequiredSignature)?;
|
||||
Err(InstructionError::MissingRequiredSignature)?;
|
||||
}
|
||||
|
||||
match instruction {
|
||||
@ -88,16 +88,16 @@ pub fn entrypoint(
|
||||
} => create_system_account(keyed_accounts, lamports, space, &program_id),
|
||||
SystemInstruction::Assign { program_id } => {
|
||||
if !system_program::check_id(&keyed_accounts[FROM_ACCOUNT_INDEX].account.owner) {
|
||||
Err(ProgramError::IncorrectProgramId)?;
|
||||
Err(InstructionError::IncorrectProgramId)?;
|
||||
}
|
||||
assign_account_to_program(keyed_accounts, &program_id)
|
||||
}
|
||||
SystemInstruction::Move { lamports } => move_lamports(keyed_accounts, lamports),
|
||||
}
|
||||
.map_err(|e| ProgramError::CustomError(serialize(&e).unwrap()))
|
||||
.map_err(|e| InstructionError::CustomError(serialize(&e).unwrap()))
|
||||
} else {
|
||||
info!("Invalid instruction data: {:?}", data);
|
||||
Err(ProgramError::InvalidInstructionData)
|
||||
Err(InstructionError::InvalidInstructionData)
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,7 +108,6 @@ mod tests {
|
||||
use crate::bank_client::BankClient;
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::genesis_block::GenesisBlock;
|
||||
use solana_sdk::native_program::ProgramError;
|
||||
use solana_sdk::script::Script;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction::SystemInstruction;
|
||||
@ -245,7 +244,7 @@ mod tests {
|
||||
};
|
||||
let data = serialize(&instruction).unwrap();
|
||||
let result = entrypoint(&system_program::id(), &mut keyed_accounts, &data, 0);
|
||||
assert_eq!(result, Err(ProgramError::IncorrectProgramId));
|
||||
assert_eq!(result, Err(InstructionError::IncorrectProgramId));
|
||||
assert_eq!(from_account.owner, new_program_owner);
|
||||
}
|
||||
|
||||
@ -301,7 +300,7 @@ mod tests {
|
||||
mallory_client.process_script(malicious_script),
|
||||
Err(TransactionError::InstructionError(
|
||||
0,
|
||||
InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
|
||||
InstructionError::MissingRequiredSignature
|
||||
))
|
||||
);
|
||||
assert_eq!(bank.get_balance(&alice_pubkey), 50);
|
||||
|
Reference in New Issue
Block a user