From 871dd470190012e211cb8ebe757e5ceadb1b6161 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Fri, 23 Nov 2018 16:11:53 -0700 Subject: [PATCH] Extract the part of execute_instruction that should only return a ProgramError TODO: hoist load_executable_accounts() and then change process_instruction() to return ProgramError. --- src/bank.rs | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/bank.rs b/src/bank.rs index c685c15bff..05e8739606 100644 --- a/src/bank.rs +++ b/src/bank.rs @@ -790,24 +790,15 @@ impl Bank { Ok(accounts) } - /// Execute an instruction - /// This method calls the instruction's program entry pont method and verifies that the result of - /// the call does not violate the bank's accounting rules. - /// The accounts are committed back to the bank only if this function returns Ok(_). - fn execute_instruction( + /// Process an instruction + /// This method calls the instruction's program entry pont method + fn process_instruction( &self, tx: &Transaction, instruction_index: usize, program_accounts: &mut [&mut Account], ) -> Result<()> { let program_id = tx.program_id(instruction_index); - // TODO: the runtime should be checking read/write access to memory - // we are trusting the hard coded contracts not to clobber or allocate - let pre_total: u64 = program_accounts.iter().map(|a| a.tokens).sum(); - let pre_data: Vec<_> = program_accounts - .iter_mut() - .map(|a| (a.owner, a.tokens)) - .collect(); // Call the contract method // It's up to the contract to implement its own rules on moving funds @@ -865,6 +856,29 @@ impl Bank { return Err(BankError::ProgramError(instruction_index as u8, err)); } } + Ok(()) + } + + /// Execute an instruction + /// This method calls the instruction's program entry pont method and verifies that the result of + /// the call does not violate the bank's accounting rules. + /// The accounts are committed back to the bank only if this function returns Ok(_). + fn execute_instruction( + &self, + tx: &Transaction, + instruction_index: usize, + program_accounts: &mut [&mut Account], + ) -> Result<()> { + let program_id = tx.program_id(instruction_index); + // TODO: the runtime should be checking read/write access to memory + // we are trusting the hard coded contracts not to clobber or allocate + let pre_total: u64 = program_accounts.iter().map(|a| a.tokens).sum(); + let pre_data: Vec<_> = program_accounts + .iter_mut() + .map(|a| (a.owner, a.tokens)) + .collect(); + + self.process_instruction(tx, instruction_index, program_accounts)?; // Verify the instruction for ((pre_program_id, pre_tokens), post_account) in