Generalize error codes

This commit is contained in:
Greg Fitzgerald
2019-03-13 12:48:11 -06:00
parent 1de5ae1ef0
commit 296415945a
3 changed files with 9 additions and 6 deletions

View File

@ -85,7 +85,7 @@ pub fn entrypoint(
// All system instructions require that accounts_keys[0] be a signer // All system instructions require that accounts_keys[0] be a signer
if keyed_accounts[FROM_ACCOUNT_INDEX].signer_key().is_none() { if keyed_accounts[FROM_ACCOUNT_INDEX].signer_key().is_none() {
info!("account[from] is unsigned"); info!("account[from] is unsigned");
Err(ProgramError::InvalidArgument)?; Err(ProgramError::MissingRequiredSignature)?;
} }
match instruction { match instruction {
@ -96,7 +96,7 @@ pub fn entrypoint(
} => create_system_account(keyed_accounts, lamports, space, &program_id), } => create_system_account(keyed_accounts, lamports, space, &program_id),
SystemInstruction::Assign { program_id } => { SystemInstruction::Assign { program_id } => {
if !system_program::check_id(&keyed_accounts[FROM_ACCOUNT_INDEX].account.owner) { if !system_program::check_id(&keyed_accounts[FROM_ACCOUNT_INDEX].account.owner) {
Err(ProgramError::AssignOfUnownedAccount)?; Err(ProgramError::IncorrectProgramId)?;
} }
assign_account_to_program(keyed_accounts, &program_id) assign_account_to_program(keyed_accounts, &program_id)
} }
@ -245,7 +245,7 @@ mod tests {
}; };
let data = serialize(&instruction).unwrap(); let data = serialize(&instruction).unwrap();
let result = entrypoint(&system_program::id(), &mut keyed_accounts, &data, 0); let result = entrypoint(&system_program::id(), &mut keyed_accounts, &data, 0);
assert_eq!(result, Err(ProgramError::AssignOfUnownedAccount)); assert_eq!(result, Err(ProgramError::IncorrectProgramId));
assert_eq!(from_account.owner, new_program_owner); assert_eq!(from_account.owner, new_program_owner);
} }

View File

@ -49,7 +49,7 @@ fn test_system_unsigned_transaction() {
system_bank.bank.process_transaction(&tx), system_bank.bank.process_transaction(&tx),
Err(BankError::InstructionError( Err(BankError::InstructionError(
0, 0,
InstructionError::ProgramError(ProgramError::InvalidArgument) InstructionError::ProgramError(ProgramError::MissingRequiredSignature)
)) ))
); );
assert_eq!(system_bank.bank.get_balance(&from_keypair.pubkey()), 50); assert_eq!(system_bank.bank.get_balance(&from_keypair.pubkey()), 50);

View File

@ -17,8 +17,11 @@ pub enum ProgramError {
/// An account's userdata was too small /// An account's userdata was too small
UserdataTooSmall, UserdataTooSmall,
/// SystemInstruction::Assign was attempted on an account unowned by the system program /// The account did not have the expected program id
AssignOfUnownedAccount, IncorrectProgramId,
/// A signature was required but not found
MissingRequiredSignature,
/// CustomError allows on-chain programs to implement program-specific error types and see /// CustomError allows on-chain programs to implement program-specific error types and see
/// them returned by the Solana runtime. A CustomError may be any type that is serialized /// them returned by the Solana runtime. A CustomError may be any type that is serialized