From 3dc22e7323fb97c8a36c57cc4c475ea589d0fa32 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Thu, 7 Mar 2019 13:42:01 -0700 Subject: [PATCH] Simulate auto-creation of system accounts --- programs/budget/src/budget_program.rs | 35 ++++++--------------------- runtime/src/runtime.rs | 5 +++- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/programs/budget/src/budget_program.rs b/programs/budget/src/budget_program.rs index 56a05f970a..cd5abaf92b 100644 --- a/programs/budget/src/budget_program.rs +++ b/programs/budget/src/budget_program.rs @@ -168,7 +168,7 @@ mod test { fn process_transaction( tx: &Transaction, - tx_accounts: &mut [Account], + tx_accounts: &mut Vec, ) -> Result<(), BudgetError> { runtime::process_transaction(tx, tx_accounts, process_instruction) } @@ -192,12 +192,7 @@ mod test { #[test] fn test_unsigned_witness_key() { - let mut accounts = vec![ - Account::new(1, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - ]; + let mut accounts = vec![Account::new(1, 0, system_program::id())]; // Initialize BudgetState let from = Keypair::new(); @@ -232,12 +227,7 @@ mod test { #[test] fn test_unsigned_timestamp() { - let mut accounts = vec![ - Account::new(1, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - ]; + let mut accounts = vec![Account::new(1, 0, system_program::id())]; // Initialize BudgetState let from = Keypair::new(); @@ -273,11 +263,7 @@ mod test { #[test] fn test_transfer_on_date() { - let mut accounts = vec![ - Account::new(1, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - ]; + let mut accounts = vec![Account::new(1, 0, system_program::id())]; let from_account = 0; let contract_account = 1; let to_account = 2; @@ -349,11 +335,7 @@ mod test { } #[test] fn test_cancel_transfer() { - let mut accounts = vec![ - Account::new(1, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - Account::new(0, 0, system_program::id()), - ]; + let mut accounts = vec![Account::new(1, 0, system_program::id())]; let from_account = 0; let contract_account = 1; let pay_account = 2; @@ -386,8 +368,7 @@ mod test { // nothing should be changed because apply witness didn't finalize a payment assert_eq!(accounts[from_account].lamports, 0); assert_eq!(accounts[contract_account].lamports, 1); - // this is the `to.pubkey()` account - assert_eq!(accounts[pay_account].lamports, 0); + assert_eq!(accounts.get(pay_account), None); // Now, cancel the transaction. from gets her funds back let tx = BudgetTransaction::new_signature( @@ -399,7 +380,7 @@ mod test { process_transaction(&tx, &mut accounts).unwrap(); assert_eq!(accounts[from_account].lamports, 1); assert_eq!(accounts[contract_account].lamports, 0); - assert_eq!(accounts[pay_account].lamports, 0); + assert_eq!(accounts.get(pay_account), None); // try to replay the cancel contract let tx = BudgetTransaction::new_signature( @@ -414,6 +395,6 @@ mod test { ); assert_eq!(accounts[from_account].lamports, 1); assert_eq!(accounts[contract_account].lamports, 0); - assert_eq!(accounts[pay_account].lamports, 0); + assert_eq!(accounts.get(pay_account), None); } } diff --git a/runtime/src/runtime.rs b/runtime/src/runtime.rs index 58ac9bab1a..fc9deecad3 100644 --- a/runtime/src/runtime.rs +++ b/runtime/src/runtime.rs @@ -192,12 +192,15 @@ pub fn execute_transaction( /// for easier usage and better stack traces. pub fn process_transaction( tx: &Transaction, - tx_accounts: &mut [Account], + tx_accounts: &mut Vec, process_instruction: F, ) -> Result<(), E> where F: Fn(&Pubkey, &mut [KeyedAccount], &[u8]) -> Result<(), E>, { + for _ in tx_accounts.len()..tx.account_keys.len() { + tx_accounts.push(Account::new(0, 0, system_program::id())); + } for (i, ix) in tx.instructions.iter().enumerate() { let mut ix_accounts = get_subset_unchecked_mut(tx_accounts, &ix.accounts); let mut keyed_accounts: Vec<_> = ix