From d54661493659e3cccdaae137a8dba0ab9dbd744c Mon Sep 17 00:00:00 2001 From: Stephen Akridge Date: Wed, 26 Sep 2018 13:50:30 -0700 Subject: [PATCH] Handle deserialize failure with error --- src/bank.rs | 4 +++- src/storage_program.rs | 22 ++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/bank.rs b/src/bank.rs index 781567cef5..267326f225 100644 --- a/src/bank.rs +++ b/src/bank.rs @@ -423,7 +423,9 @@ impl Bank { return Err(BankError::ProgramRuntimeError); } } 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) { if TicTacToeProgram::process_transaction(&tx, accounts).is_err() { return Err(BankError::ProgramRuntimeError); diff --git a/src/storage_program.rs b/src/storage_program.rs index 3923bfa456..9636f070f9 100644 --- a/src/storage_program.rs +++ b/src/storage_program.rs @@ -12,6 +12,10 @@ pub enum StorageProgram { SubmitMiningProof { sha_state: [u8; 32] }, } +pub enum StorageError { + InvalidUserData, +} + pub const STORAGE_PROGRAM_ID: [u8; 32] = [1u8; 32]; impl StorageProgram { @@ -27,13 +31,19 @@ impl StorageProgram { account.tokens } - pub fn process_transaction(tx: &Transaction, _accounts: &mut [Account]) { - let syscall: StorageProgram = deserialize(&tx.userdata).unwrap(); - match syscall { - StorageProgram::SubmitMiningProof { sha_state } => { - info!("Mining proof submitted with state {}", sha_state[0]); - return; + pub fn process_transaction( + tx: &Transaction, + _accounts: &mut [Account], + ) -> Result<(), StorageError> { + if let Ok(syscall) = deserialize(&tx.userdata) { + match syscall { + StorageProgram::SubmitMiningProof { sha_state } => { + info!("Mining proof submitted with state {}", sha_state[0]); + return Ok(()); + } } + } else { + return Err(StorageError::InvalidUserData); } } }