Handle deserialize failure with error

This commit is contained in:
Stephen Akridge 2018-09-26 13:50:30 -07:00 committed by sakridge
parent ac8d738045
commit d546614936
2 changed files with 19 additions and 7 deletions

View File

@ -423,7 +423,9 @@ impl Bank {
return Err(BankError::ProgramRuntimeError); return Err(BankError::ProgramRuntimeError);
} }
} else if StorageProgram::check_id(&tx.program_id) { } else if StorageProgram::check_id(&tx.program_id) {
StorageProgram::process_transaction(&tx, accounts) if StorageProgram::process_transaction(&tx, accounts).is_err() {
return Err(BankError::ProgramRuntimeError);
}
} else if TicTacToeProgram::check_id(&tx.program_id) { } else if TicTacToeProgram::check_id(&tx.program_id) {
if TicTacToeProgram::process_transaction(&tx, accounts).is_err() { if TicTacToeProgram::process_transaction(&tx, accounts).is_err() {
return Err(BankError::ProgramRuntimeError); return Err(BankError::ProgramRuntimeError);

View File

@ -12,6 +12,10 @@ pub enum StorageProgram {
SubmitMiningProof { sha_state: [u8; 32] }, SubmitMiningProof { sha_state: [u8; 32] },
} }
pub enum StorageError {
InvalidUserData,
}
pub const STORAGE_PROGRAM_ID: [u8; 32] = [1u8; 32]; pub const STORAGE_PROGRAM_ID: [u8; 32] = [1u8; 32];
impl StorageProgram { impl StorageProgram {
@ -27,13 +31,19 @@ impl StorageProgram {
account.tokens account.tokens
} }
pub fn process_transaction(tx: &Transaction, _accounts: &mut [Account]) { pub fn process_transaction(
let syscall: StorageProgram = deserialize(&tx.userdata).unwrap(); tx: &Transaction,
match syscall { _accounts: &mut [Account],
StorageProgram::SubmitMiningProof { sha_state } => { ) -> Result<(), StorageError> {
info!("Mining proof submitted with state {}", sha_state[0]); if let Ok(syscall) = deserialize(&tx.userdata) {
return; match syscall {
StorageProgram::SubmitMiningProof { sha_state } => {
info!("Mining proof submitted with state {}", sha_state[0]);
return Ok(());
}
} }
} else {
return Err(StorageError::InvalidUserData);
} }
} }
} }