Remove custom Error enum, just use ProgramError

This commit is contained in:
Michael Vines 2018-12-03 22:23:29 -08:00
parent f184d69c7a
commit e6fa74fe69
2 changed files with 14 additions and 47 deletions

View File

@ -14,11 +14,6 @@ pub enum StorageProgram {
SubmitMiningProof { sha_state: Hash },
}
pub enum StorageError {
InvalidArgument,
InvalidUserData,
}
const STORAGE_PROGRAM_ID: [u8; 32] = [
130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
@ -36,14 +31,14 @@ pub fn get_balance(account: &Account) -> u64 {
account.tokens
}
pub fn process_instruction(
pub fn process(
tx: &Transaction,
pix: usize,
_accounts: &mut [&mut Account],
) -> Result<(), StorageError> {
) -> Result<(), ProgramError> {
// accounts_keys[0] must be signed
if tx.signer_key(pix, 0).is_none() {
Err(StorageError::InvalidArgument)?;
Err(ProgramError::InvalidArgument)?;
}
if let Ok(syscall) = deserialize(tx.userdata(pix)) {
@ -54,18 +49,10 @@ pub fn process_instruction(
}
}
} else {
return Err(StorageError::InvalidUserData);
return Err(ProgramError::InvalidUserdata);
}
}
pub fn process(
tx: &Transaction,
instruction_index: usize,
accounts: &mut [&mut Account],
) -> std::result::Result<(), ProgramError> {
process_instruction(&tx, instruction_index, accounts).map_err(|_| ProgramError::GenericError)
}
#[cfg(test)]
mod test {
use super::*;
@ -75,6 +62,6 @@ mod test {
fn test_storage_tx() {
let keypair = Keypair::new();
let tx = Transaction::new(&keypair, &[], id(), &(), Default::default(), 0);
assert!(process_instruction(&tx, 0, &mut []).is_err());
assert!(process(&tx, 0, &mut []).is_err());
}
}

View File

@ -14,19 +14,7 @@ use std::mem;
// Maximum number of votes to keep around
const MAX_VOTE_HISTORY: usize = 32;
#[derive(Debug, PartialEq)]
pub enum Error {
UserdataDeserializeFailure,
InvalidArguments,
InvalidUserdata,
UserdataTooSmall,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "error")
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub type Result<T> = std::result::Result<T, ProgramError>;
#[derive(Serialize, Default, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Vote {
@ -65,21 +53,21 @@ pub fn id() -> Pubkey {
Pubkey::new(&VOTE_PROGRAM_ID)
}
pub fn process_instruction(
pub fn process(
tx: &Transaction,
instruction_index: usize,
accounts: &mut [&mut Account],
) -> Result<()> {
// all vote instructions require that accounts_keys[0] be a signer
if tx.signer_key(instruction_index, 0).is_none() {
Err(Error::InvalidArguments)?;
Err(ProgramError::InvalidArgument)?;
}
match deserialize(tx.userdata(instruction_index)) {
Ok(VoteInstruction::RegisterAccount) => {
if !check_id(&accounts[1].owner) {
error!("accounts[1] is not assigned to the VOTE_PROGRAM");
Err(Error::InvalidArguments)?;
Err(ProgramError::InvalidArgument)?;
}
// TODO: a single validator could register multiple "vote accounts"
@ -96,7 +84,7 @@ pub fn process_instruction(
Ok(VoteInstruction::NewVote(vote)) => {
if !check_id(&accounts[0].owner) {
error!("accounts[0] is not assigned to the VOTE_PROGRAM");
Err(Error::InvalidArguments)?;
Err(ProgramError::InvalidArgument)?;
}
let mut vote_state = VoteProgram::deserialize(&accounts[0].userdata)?;
@ -120,19 +108,11 @@ pub fn process_instruction(
"Invalid vote transaction userdata: {:?}",
tx.userdata(instruction_index)
);
Err(Error::UserdataDeserializeFailure)
Err(ProgramError::InvalidUserdata)
}
}
}
pub fn process(
tx: &Transaction,
instruction_index: usize,
accounts: &mut [&mut Account],
) -> std::result::Result<(), ProgramError> {
process_instruction(&tx, instruction_index, accounts).map_err(|_| ProgramError::GenericError)
}
pub fn get_max_size() -> usize {
// Upper limit on the size of the Vote State. Equal to
// sizeof(VoteProgram) + MAX_VOTE_HISTORY * sizeof(Vote) +
@ -148,11 +128,11 @@ impl VoteProgram {
let len = LittleEndian::read_u16(&input[0..2]) as usize;
if len == 0 || input.len() < len + 2 {
Err(Error::InvalidUserdata)
Err(ProgramError::InvalidUserdata)
} else {
deserialize(&input[2..=len + 1]).map_err(|err| {
error!("Unable to deserialize vote state: {:?}", err);
Error::InvalidUserdata
ProgramError::InvalidUserdata
})
}
}
@ -166,7 +146,7 @@ impl VoteProgram {
self_serialized.len(),
output.len() + 2,
);
return Err(Error::UserdataTooSmall);
return Err(ProgramError::UserdataTooSmall);
}
let serialized_len = self_serialized.len() as u16;