From 84fbeb5c83be7a9d6551fee2e174d19707f788a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Thu, 18 Feb 2021 19:29:08 +0100 Subject: [PATCH] Adds keyed_accounts parameter to MockInvokeContext constructor. --- programs/bpf/benches/bpf_loader.rs | 7 +- programs/bpf/tests/programs.rs | 2 +- programs/bpf_loader/src/lib.rs | 843 +++++++++++--------- programs/config/src/config_processor.rs | 32 +- programs/stake/src/stake_instruction.rs | 192 ++--- programs/stake/src/stake_state.rs | 19 +- programs/vote/src/vote_instruction.rs | 4 +- runtime/src/system_instruction_processor.rs | 2 +- sdk/src/process_instruction.rs | 6 +- 9 files changed, 578 insertions(+), 529 deletions(-) diff --git a/programs/bpf/benches/bpf_loader.rs b/programs/bpf/benches/bpf_loader.rs index 97bcbc9ea54ffd..eee04d771f5f17 100644 --- a/programs/bpf/benches/bpf_loader.rs +++ b/programs/bpf/benches/bpf_loader.rs @@ -91,7 +91,7 @@ fn bench_program_alu(bencher: &mut Bencher) { .unwrap(); inner_iter.write_u64::(0).unwrap(); let loader_id = bpf_loader::id(); - let mut invoke_context = MockInvokeContext::default(); + let mut invoke_context = MockInvokeContext::new(&[]); let elf = load_elf("bench_alu").unwrap(); let mut executable = @@ -194,8 +194,6 @@ fn bench_program_execute_noop(bencher: &mut Bencher) { fn bench_instruction_count_tuner(_bencher: &mut Bencher) { const BUDGET: u64 = 200_000; let loader_id = bpf_loader::id(); - let mut invoke_context = MockInvokeContext::default(); - invoke_context.compute_meter.remaining = BUDGET; let accounts = [RefCell::new(Account::new( 1, @@ -210,6 +208,9 @@ fn bench_instruction_count_tuner(_bencher: &mut Bencher) { .collect(); let instruction_data = vec![0u8]; + let mut invoke_context = MockInvokeContext::new(&keyed_accounts); + invoke_context.compute_meter.remaining = BUDGET; + // Serialize account data let mut serialized = serialize_parameters( &bpf_loader::id(), diff --git a/programs/bpf/tests/programs.rs b/programs/bpf/tests/programs.rs index 52ad175b22f402..bf0995d3ff7413 100644 --- a/programs/bpf/tests/programs.rs +++ b/programs/bpf/tests/programs.rs @@ -188,7 +188,7 @@ fn run_program( let mut data = vec![]; file.read_to_end(&mut data).unwrap(); let loader_id = bpf_loader::id(); - let mut invoke_context = MockInvokeContext::default(); + let mut invoke_context = MockInvokeContext::new(parameter_accounts); let parameter_bytes = serialize_parameters( &bpf_loader::id(), program_id, diff --git a/programs/bpf_loader/src/lib.rs b/programs/bpf_loader/src/lib.rs index 9eae2fe3e9a205..cc70e0abd09e1c 100644 --- a/programs/bpf_loader/src/lib.rs +++ b/programs/bpf_loader/src/lib.rs @@ -933,7 +933,7 @@ mod tests { &bpf_loader::id(), &[], &instruction_data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&[]) ) ); @@ -944,7 +944,7 @@ mod tests { &bpf_loader::id(), &keyed_accounts, &instruction_data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -958,7 +958,7 @@ mod tests { &bpf_loader::id(), &keyed_accounts, &instruction_data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); assert_eq!( @@ -976,7 +976,7 @@ mod tests { &bpf_loader::id(), &keyed_accounts, &instruction_data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -1001,7 +1001,7 @@ mod tests { &bpf_loader::id(), &[], &instruction_data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&[]) ) ); @@ -1012,7 +1012,7 @@ mod tests { &bpf_loader::id(), &keyed_accounts, &instruction_data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -1024,7 +1024,7 @@ mod tests { &bpf_loader::id(), &keyed_accounts, &instruction_data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); assert!(keyed_accounts[0].account.borrow().executable); @@ -1040,7 +1040,7 @@ mod tests { &bpf_loader::id(), &keyed_accounts, &instruction_data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -1060,25 +1060,33 @@ mod tests { let mut keyed_accounts = vec![KeyedAccount::new(&program_key, false, &program_account)]; - let mut invoke_context = MockInvokeContext::default(); - // Case: Empty keyed accounts assert_eq!( Err(InstructionError::NotEnoughAccountKeys), - process_instruction(&program_id, &[], &[], &mut invoke_context) + process_instruction(&program_id, &[], &[], &mut MockInvokeContext::new(&[])) ); // Case: Only a program account assert_eq!( Ok(()), - process_instruction(&program_key, &keyed_accounts, &[], &mut invoke_context) + process_instruction( + &program_key, + &keyed_accounts, + &[], + &mut MockInvokeContext::new(&keyed_accounts) + ) ); // Case: Account not a program keyed_accounts[0].account.borrow_mut().executable = false; assert_eq!( Err(InstructionError::InvalidInstructionData), - process_instruction(&program_id, &keyed_accounts, &[], &mut invoke_context) + process_instruction( + &program_id, + &keyed_accounts, + &[], + &mut MockInvokeContext::new(&keyed_accounts) + ) ); keyed_accounts[0].account.borrow_mut().executable = true; @@ -1087,7 +1095,12 @@ mod tests { keyed_accounts.push(KeyedAccount::new(&program_key, false, ¶meter_account)); assert_eq!( Ok(()), - process_instruction(&program_key, &keyed_accounts, &[], &mut invoke_context) + process_instruction( + &program_key, + &keyed_accounts, + &[], + &mut MockInvokeContext::new(&keyed_accounts) + ) ); // Case: limited budget @@ -1134,7 +1147,7 @@ mod tests { &program_key, &keyed_accounts, &[], - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -1162,7 +1175,7 @@ mod tests { &program_key, &keyed_accounts, &[], - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -1178,7 +1191,7 @@ mod tests { &program_key, &keyed_accounts, &[], - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -1206,7 +1219,7 @@ mod tests { &program_key, &keyed_accounts, &[], - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -1222,7 +1235,7 @@ mod tests { &program_key, &keyed_accounts, &[], - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -1245,16 +1258,17 @@ mod tests { ); // Case: Success + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&authority_address, false, &authority_account), + ]; assert_eq!( Ok(()), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&authority_address, false, &authority_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); let state: UpgradeableLoaderState = buffer_account.borrow().state().unwrap(); @@ -1266,16 +1280,17 @@ mod tests { ); // Case: Already initialized + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&authority_address, false, &authority_account), + ]; assert_eq!( Err(InstructionError::AccountAlreadyInitialized), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&authority_address, false, &authority_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); let state: UpgradeableLoaderState = buffer_account.borrow().state().unwrap(); @@ -1302,16 +1317,17 @@ mod tests { bytes: vec![42; 9], }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&buffer_address, true, &buffer_account), + ]; assert_eq!( Err(InstructionError::InvalidAccountData), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&buffer_address, true, &buffer_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -1327,16 +1343,17 @@ mod tests { authority_address: Some(buffer_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&buffer_address, true, &buffer_account), + ]; assert_eq!( Ok(()), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&buffer_address, true, &buffer_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); let state: UpgradeableLoaderState = buffer_account.borrow().state().unwrap(); @@ -1368,16 +1385,17 @@ mod tests { authority_address: Some(buffer_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&buffer_address, true, &buffer_account), + ]; assert_eq!( Ok(()), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&buffer_address, true, &buffer_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); let state: UpgradeableLoaderState = buffer_account.borrow().state().unwrap(); @@ -1404,16 +1422,17 @@ mod tests { authority_address: Some(buffer_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + ]; assert_eq!( Err(InstructionError::MissingRequiredSignature), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&buffer_address, false, &buffer_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -1429,16 +1448,17 @@ mod tests { authority_address: Some(buffer_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&buffer_address, true, &buffer_account), + ]; assert_eq!( Err(InstructionError::AccountDataTooSmall), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&buffer_address, true, &buffer_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -1454,16 +1474,17 @@ mod tests { authority_address: Some(buffer_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&buffer_address, true, &buffer_account), + ]; assert_eq!( Err(InstructionError::AccountDataTooSmall), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&buffer_address, true, &buffer_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -1480,16 +1501,17 @@ mod tests { authority_address: Some(buffer_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&authority_address, true, &buffer_account), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&authority_address, true, &buffer_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -1506,16 +1528,17 @@ mod tests { authority_address: None, }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&authority_address, true, &buffer_account), + ]; assert_eq!( Err(InstructionError::Immutable), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&authority_address, true, &buffer_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -2188,6 +2211,8 @@ mod tests { Pubkey::find_program_address(&[program_address.as_ref()], &id()); let spill_address = Pubkey::new_unique(); let upgrade_authority_account = Account::new_ref(1, 0, &Pubkey::new_unique()); + let rent_id = sysvar::rent::id(); + let clock_id = sysvar::clock::id(); #[allow(clippy::type_complexity)] fn get_accounts( @@ -2264,25 +2289,26 @@ mod tests { min_program_balance, min_programdata_balance, ); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Ok(()), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); assert_eq!(0, buffer_account.borrow().lamports); @@ -2326,25 +2352,26 @@ mod tests { upgrade_authority_address: None, }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::Immutable), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2359,25 +2386,27 @@ mod tests { min_program_balance, min_programdata_balance, ); + let invalid_upgrade_authority_address = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &invalid_upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &Pubkey::new_unique(), - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2392,25 +2421,26 @@ mod tests { min_program_balance, min_programdata_balance, ); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + false, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::MissingRequiredSignature), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - false, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2426,25 +2456,26 @@ mod tests { min_programdata_balance, ); program_account.borrow_mut().executable = false; + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::AccountNotExecutable), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2460,25 +2491,26 @@ mod tests { min_programdata_balance, ); program_account.borrow_mut().owner = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::IncorrectProgramId), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2493,25 +2525,26 @@ mod tests { min_program_balance, min_programdata_balance, ); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new_readonly(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::InvalidArgument), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new_readonly(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2530,25 +2563,26 @@ mod tests { .borrow_mut() .set_state(&UpgradeableLoaderState::Uninitialized) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::InvalidAccountData), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2563,25 +2597,27 @@ mod tests { min_program_balance, min_programdata_balance, ); + let invalid_programdata_address = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&invalid_programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::InvalidArgument), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&Pubkey::new_unique(), false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2600,25 +2636,26 @@ mod tests { .borrow_mut() .set_state(&UpgradeableLoaderState::Uninitialized) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::InvalidArgument), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2644,25 +2681,26 @@ mod tests { authority_address: Some(upgrade_authority_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::AccountDataTooSmall), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2684,25 +2722,26 @@ mod tests { }) .unwrap(); buffer_account.borrow_mut().data.truncate(5); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::InvalidAccountData), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2717,25 +2756,26 @@ mod tests { min_program_balance, min_programdata_balance, ); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2756,25 +2796,26 @@ mod tests { authority_address: None, }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2802,25 +2843,26 @@ mod tests { upgrade_authority_address: None, }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new(&program_address, false, &program_account), + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new(&spill_address, false, &spill_account), + KeyedAccount::new_readonly(&rent_id, false, &rent_account), + KeyedAccount::new_readonly(&clock_id, false, &clock_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new(&program_address, false, &program_account), - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new(&spill_address, false, &spill_account), - KeyedAccount::new_readonly(&sysvar::rent::id(), false, &rent_account), - KeyedAccount::new_readonly(&sysvar::clock::id(), false, &clock_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -2850,25 +2892,26 @@ mod tests { upgrade_authority_address: Some(upgrade_authority_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + KeyedAccount::new_readonly( + &new_upgrade_authority_address, + false, + &new_upgrade_authority_account, + ), + ]; assert_eq!( Ok(()), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ), - KeyedAccount::new_readonly( - &new_upgrade_authority_address, - false, - &new_upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); let state: UpgradeableLoaderState = programdata_account.borrow().state().unwrap(); @@ -2888,20 +2931,21 @@ mod tests { upgrade_authority_address: Some(upgrade_authority_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Ok(()), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - true, - &upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); let state: UpgradeableLoaderState = programdata_account.borrow().state().unwrap(); @@ -2921,20 +2965,21 @@ mod tests { upgrade_authority_address: Some(upgrade_authority_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new_readonly( + &upgrade_authority_address, + false, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::MissingRequiredSignature), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new_readonly( - &upgrade_authority_address, - false, - &upgrade_authority_account - ), - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2946,25 +2991,27 @@ mod tests { upgrade_authority_address: Some(upgrade_authority_address), }) .unwrap(); + let invalid_upgrade_authority_address = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new_readonly( + &invalid_upgrade_authority_address, + true, + &upgrade_authority_account, + ), + KeyedAccount::new_readonly( + &new_upgrade_authority_address, + false, + &new_upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new_readonly( - &Pubkey::new_unique(), - true, - &upgrade_authority_account - ), - KeyedAccount::new_readonly( - &new_upgrade_authority_address, - false, - &new_upgrade_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -2976,20 +3023,22 @@ mod tests { upgrade_authority_address: None, }) .unwrap(); + let invalid_upgrade_authority_address = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new_readonly( + &invalid_upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::Immutable), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new_readonly( - &Pubkey::new_unique(), - true, - &upgrade_authority_account - ), - ], + &keyed_accounts, &bincode::serialize(&UpgradeableLoaderInstruction::SetAuthority).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -3000,20 +3049,22 @@ mod tests { programdata_address: Pubkey::new_unique(), }) .unwrap(); + let invalid_upgrade_authority_address = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&programdata_address, false, &programdata_account), + KeyedAccount::new_readonly( + &invalid_upgrade_authority_address, + true, + &upgrade_authority_account, + ), + ]; assert_eq!( Err(InstructionError::InvalidAccountData), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&programdata_address, false, &programdata_account), - KeyedAccount::new_readonly( - &Pubkey::new_unique(), - true, - &upgrade_authority_account - ), - ], + &keyed_accounts, &bincode::serialize(&UpgradeableLoaderInstruction::SetAuthority).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -3039,21 +3090,18 @@ mod tests { authority_address: Some(authority_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new_readonly(&authority_address, true, &authority_account), + KeyedAccount::new_readonly(&new_authority_address, false, &new_authority_account), + ]; assert_eq!( Ok(()), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new_readonly(&authority_address, true, &authority_account), - KeyedAccount::new_readonly( - &new_authority_address, - false, - &new_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); let state: UpgradeableLoaderState = buffer_account.borrow().state().unwrap(); @@ -3071,16 +3119,17 @@ mod tests { authority_address: Some(authority_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new_readonly(&authority_address, true, &authority_account), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new_readonly(&authority_address, true, &authority_account) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); let state: UpgradeableLoaderState = buffer_account.borrow().state().unwrap(); @@ -3098,21 +3147,18 @@ mod tests { authority_address: Some(authority_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new_readonly(&authority_address, false, &authority_account), + KeyedAccount::new_readonly(&new_authority_address, false, &new_authority_account), + ]; assert_eq!( Err(InstructionError::MissingRequiredSignature), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new_readonly(&authority_address, false, &authority_account), - KeyedAccount::new_readonly( - &new_authority_address, - false, - &new_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -3123,21 +3169,19 @@ mod tests { authority_address: Some(authority_address), }) .unwrap(); + let invalid_authority_address = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new_readonly(&invalid_authority_address, true, &authority_account), + KeyedAccount::new_readonly(&new_authority_address, false, &new_authority_account), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new_readonly(&Pubkey::new_unique(), true, &authority_account), - KeyedAccount::new_readonly( - &new_authority_address, - false, - &new_authority_account - ) - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -3148,21 +3192,19 @@ mod tests { authority_address: None, }) .unwrap(); + let invalid_authority_address = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new_readonly(&invalid_authority_address, true, &authority_account), + KeyedAccount::new_readonly(&new_authority_address, false, &new_authority_account), + ]; assert_eq!( Err(InstructionError::Immutable), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new_readonly(&Pubkey::new_unique(), true, &authority_account), - KeyedAccount::new_readonly( - &new_authority_address, - false, - &new_authority_account - ) - ], + &keyed_accounts, &bincode::serialize(&UpgradeableLoaderInstruction::SetAuthority).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -3173,16 +3215,18 @@ mod tests { programdata_address: Pubkey::new_unique(), }) .unwrap(); + let invalid_authority_address = Pubkey::new_unique(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new_readonly(&invalid_authority_address, true, &authority_account), + ]; assert_eq!( Err(InstructionError::InvalidAccountData), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new_readonly(&Pubkey::new_unique(), true, &authority_account), - ], + &keyed_accounts, &bincode::serialize(&UpgradeableLoaderInstruction::SetAuthority).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); @@ -3193,16 +3237,17 @@ mod tests { authority_address: Some(authority_address), }) .unwrap(); + let keyed_accounts = [ + KeyedAccount::new(&buffer_address, false, &buffer_account), + KeyedAccount::new_readonly(&authority_address, true, &authority_account), + ]; assert_eq!( Err(InstructionError::IncorrectAuthority), process_instruction( &bpf_loader_upgradeable::id(), - &[ - KeyedAccount::new(&buffer_address, false, &buffer_account), - KeyedAccount::new_readonly(&authority_address, true, &authority_account), - ], + &keyed_accounts, &instruction, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ) ); } @@ -3263,7 +3308,7 @@ mod tests { &bpf_loader::id(), &keyed_accounts, &[], - &mut MockInvokeContext::default(), + &mut MockInvokeContext::new(&keyed_accounts), ); }, ); diff --git a/programs/config/src/config_processor.rs b/programs/config/src/config_processor.rs index c91e2c93d2d75f..beadd575f9f49c 100644 --- a/programs/config/src/config_processor.rs +++ b/programs/config/src/config_processor.rs @@ -171,7 +171,7 @@ mod tests { &id(), &keyed_accounts, &instructions[1].data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Ok(()) ); @@ -206,7 +206,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Ok(()) ); @@ -233,7 +233,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::InvalidInstructionData) ); @@ -256,7 +256,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::MissingRequiredSignature) ); @@ -291,7 +291,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Ok(()) ); @@ -322,7 +322,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::InvalidAccountData) ); @@ -353,7 +353,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::MissingRequiredSignature) ); @@ -369,7 +369,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::MissingRequiredSignature) ); @@ -406,7 +406,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Ok(()) ); @@ -426,7 +426,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Ok(()) ); @@ -451,7 +451,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::MissingRequiredSignature) ); @@ -474,7 +474,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::MissingRequiredSignature) ); @@ -512,7 +512,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Ok(()) ); @@ -531,7 +531,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Ok(()) ); @@ -552,7 +552,7 @@ mod tests { &id(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::MissingRequiredSignature) ); @@ -571,7 +571,7 @@ mod tests { &id(), &keyed_accounts, &instructions[1].data, - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::NotEnoughAccountKeys) ); diff --git a/programs/stake/src/stake_instruction.rs b/programs/stake/src/stake_instruction.rs index c85638c0feef6a..e3e94cb876462e 100644 --- a/programs/stake/src/stake_instruction.rs +++ b/programs/stake/src/stake_instruction.rs @@ -688,7 +688,7 @@ mod tests { &Pubkey::default(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default(), + &mut MockInvokeContext::new(&keyed_accounts), ) } } @@ -903,193 +903,197 @@ mod tests { Lockup::default() )) .unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&[]) ), Err(InstructionError::NotEnoughAccountKeys), ); // no account for rent + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let keyed_accounts = [KeyedAccount::new(&stake_address, false, &stake_account)]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[KeyedAccount::new( - &Pubkey::default(), - false, - &create_default_stake_account(), - )], + &keyed_accounts, &serialize(&StakeInstruction::Initialize( Authorized::default(), Lockup::default() )) .unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::NotEnoughAccountKeys), ); // rent fails to deserialize + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let rent_address = sysvar::rent::id(); + let rent_account = create_default_account(); + let keyed_accounts = [ + KeyedAccount::new(&stake_address, false, &stake_account), + KeyedAccount::new(&rent_address, false, &rent_account), + ]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[ - KeyedAccount::new(&Pubkey::default(), false, &create_default_stake_account()), - KeyedAccount::new(&sysvar::rent::id(), false, &create_default_account()) - ], + &keyed_accounts, &serialize(&StakeInstruction::Initialize( Authorized::default(), Lockup::default() )) .unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::InvalidArgument), ); // fails to deserialize stake state + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let rent_address = sysvar::rent::id(); + let rent_account = RefCell::new(account::create_account(&Rent::default(), 0)); + let keyed_accounts = [ + KeyedAccount::new(&stake_address, false, &stake_account), + KeyedAccount::new(&rent_address, false, &rent_account), + ]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[ - KeyedAccount::new(&Pubkey::default(), false, &create_default_stake_account()), - KeyedAccount::new( - &sysvar::rent::id(), - false, - &RefCell::new(account::create_account(&Rent::default(), 0)) - ) - ], + &keyed_accounts, &serialize(&StakeInstruction::Initialize( Authorized::default(), Lockup::default() )) .unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::InvalidAccountData), ); // gets the first check in delegate, wrong number of accounts + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let keyed_accounts = [KeyedAccount::new(&stake_address, false, &stake_account)]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[KeyedAccount::new( - &Pubkey::default(), - false, - &create_default_stake_account() - ),], + &keyed_accounts, &serialize(&StakeInstruction::DelegateStake).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::NotEnoughAccountKeys), ); // gets the sub-check for number of args + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let keyed_accounts = [KeyedAccount::new(&stake_address, false, &stake_account)]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[KeyedAccount::new( - &Pubkey::default(), - false, - &create_default_stake_account() - )], + &keyed_accounts, &serialize(&StakeInstruction::DelegateStake).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::NotEnoughAccountKeys), ); // gets the check non-deserialize-able account in delegate_stake + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let vote_address = Pubkey::default(); let mut bad_vote_account = create_default_account(); bad_vote_account.get_mut().owner = solana_vote_program::id(); + let clock_address = sysvar::clock::id(); + let clock_account = + RefCell::new(account::create_account(&sysvar::clock::Clock::default(), 1)); + let stake_history_address = sysvar::stake_history::id(); + let stake_history_account = RefCell::new(account::create_account( + &sysvar::stake_history::StakeHistory::default(), + 1, + )); + let config_address = config::id(); + let config_account = RefCell::new(config::create_account(0, &config::Config::default())); + let keyed_accounts = [ + KeyedAccount::new(&stake_address, true, &stake_account), + KeyedAccount::new(&vote_address, false, &bad_vote_account), + KeyedAccount::new(&clock_address, false, &clock_account), + KeyedAccount::new(&stake_history_address, false, &stake_history_account), + KeyedAccount::new(&config_address, false, &config_account), + ]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[ - KeyedAccount::new(&Pubkey::default(), true, &create_default_stake_account()), - KeyedAccount::new(&Pubkey::default(), false, &bad_vote_account), - KeyedAccount::new( - &sysvar::clock::id(), - false, - &RefCell::new(account::create_account(&sysvar::clock::Clock::default(), 1)) - ), - KeyedAccount::new( - &sysvar::stake_history::id(), - false, - &RefCell::new(account::create_account( - &sysvar::stake_history::StakeHistory::default(), - 1 - )) - ), - KeyedAccount::new( - &config::id(), - false, - &RefCell::new(config::create_account(0, &config::Config::default())) - ), - ], + &keyed_accounts, &serialize(&StakeInstruction::DelegateStake).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::InvalidAccountData), ); // Tests 3rd keyed account is of correct type (Clock instead of rewards) in withdraw + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let vote_address = Pubkey::default(); + let vote_account = create_default_account(); + let rewards_address = sysvar::rewards::id(); + let rewards_account = RefCell::new(account::create_account( + &sysvar::rewards::Rewards::new(0.0), + 1, + )); + let stake_history_address = sysvar::stake_history::id(); + let stake_history_account = + RefCell::new(account::create_account(&StakeHistory::default(), 1)); + let keyed_accounts = [ + KeyedAccount::new(&stake_address, false, &stake_account), + KeyedAccount::new(&vote_address, false, &vote_account), + KeyedAccount::new(&rewards_address, false, &rewards_account), + KeyedAccount::new(&stake_history_address, false, &stake_history_account), + ]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[ - KeyedAccount::new(&Pubkey::default(), false, &create_default_stake_account()), - KeyedAccount::new(&Pubkey::default(), false, &create_default_account()), - KeyedAccount::new( - &sysvar::rewards::id(), - false, - &RefCell::new(account::create_account( - &sysvar::rewards::Rewards::new(0.0), - 1 - )) - ), - KeyedAccount::new( - &sysvar::stake_history::id(), - false, - &RefCell::new(account::create_account(&StakeHistory::default(), 1,)) - ), - ], + &keyed_accounts, &serialize(&StakeInstruction::Withdraw(42)).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::InvalidArgument), ); // Tests correct number of accounts are provided in withdraw + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let keyed_accounts = [KeyedAccount::new(&stake_address, false, &stake_account)]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[KeyedAccount::new( - &Pubkey::default(), - false, - &create_default_stake_account() - )], + &keyed_accounts, &serialize(&StakeInstruction::Withdraw(42)).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::NotEnoughAccountKeys), ); // Tests 2nd keyed account is of correct type (Clock instead of rewards) in deactivate + let stake_address = Pubkey::default(); + let stake_account = create_default_stake_account(); + let rewards_address = sysvar::rewards::id(); + let rewards_account = RefCell::new(account::create_account( + &sysvar::rewards::Rewards::new(0.0), + 1, + )); + let keyed_accounts = [ + KeyedAccount::new(&stake_address, false, &stake_account), + KeyedAccount::new(&rewards_address, false, &rewards_account), + ]; assert_eq!( super::process_instruction( &Pubkey::default(), - &[ - KeyedAccount::new(&Pubkey::default(), false, &create_default_stake_account()), - KeyedAccount::new( - &sysvar::rewards::id(), - false, - &RefCell::new(account::create_account( - &sysvar::rewards::Rewards::new(0.0), - 1 - )) - ), - ], + &keyed_accounts, &serialize(&StakeInstruction::Deactivate).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&keyed_accounts) ), Err(InstructionError::InvalidArgument), ); @@ -1100,7 +1104,7 @@ mod tests { &Pubkey::default(), &[], &serialize(&StakeInstruction::Deactivate).unwrap(), - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&[]) ), Err(InstructionError::NotEnoughAccountKeys), ); diff --git a/programs/stake/src/stake_state.rs b/programs/stake/src/stake_state.rs index 5d69a7816d1e8a..9affd1588d41e3 100644 --- a/programs/stake/src/stake_state.rs +++ b/programs/stake/src/stake_state.rs @@ -5096,9 +5096,9 @@ mod tests { let source_stake_pubkey = solana_sdk::pubkey::new_rand(); let authorized_pubkey = solana_sdk::pubkey::new_rand(); let stake_lamports = 42; - let invoke_context = MockInvokeContext::default(); let signers = vec![authorized_pubkey].into_iter().collect(); + let invoke_context = MockInvokeContext::new(&[]); for state in &[ StakeState::Initialized(Meta::auto(&authorized_pubkey)), @@ -5206,7 +5206,7 @@ mod tests { #[test] fn test_merge_self_fails() { - let invoke_context = MockInvokeContext::default(); + let invoke_context = MockInvokeContext::new(&[]); let stake_address = Pubkey::new_unique(); let authority_pubkey = Pubkey::new_unique(); let signers = HashSet::from_iter(vec![authority_pubkey]); @@ -5250,7 +5250,6 @@ mod tests { #[test] fn test_merge_incorrect_authorized_staker() { - let invoke_context = MockInvokeContext::default(); let stake_pubkey = solana_sdk::pubkey::new_rand(); let source_stake_pubkey = solana_sdk::pubkey::new_rand(); let authorized_pubkey = solana_sdk::pubkey::new_rand(); @@ -5259,6 +5258,7 @@ mod tests { let signers = vec![authorized_pubkey].into_iter().collect(); let wrong_signers = vec![wrong_authorized_pubkey].into_iter().collect(); + let invoke_context = MockInvokeContext::new(&[]); for state in &[ StakeState::Initialized(Meta::auto(&authorized_pubkey)), @@ -5320,12 +5320,12 @@ mod tests { #[test] fn test_merge_invalid_account_data() { - let invoke_context = MockInvokeContext::default(); let stake_pubkey = solana_sdk::pubkey::new_rand(); let source_stake_pubkey = solana_sdk::pubkey::new_rand(); let authorized_pubkey = solana_sdk::pubkey::new_rand(); let stake_lamports = 42; let signers = vec![authorized_pubkey].into_iter().collect(); + let invoke_context = MockInvokeContext::new(&[]); for state in &[ StakeState::Uninitialized, @@ -5372,7 +5372,6 @@ mod tests { #[test] fn test_merge_fake_stake_source() { - let invoke_context = MockInvokeContext::default(); let stake_pubkey = solana_sdk::pubkey::new_rand(); let source_stake_pubkey = solana_sdk::pubkey::new_rand(); let authorized_pubkey = solana_sdk::pubkey::new_rand(); @@ -5404,6 +5403,7 @@ mod tests { .expect("source_stake_account"); let source_stake_keyed_account = KeyedAccount::new(&source_stake_pubkey, true, &source_stake_account); + let invoke_context = MockInvokeContext::new(&[]); assert_eq!( stake_keyed_account.merge( @@ -5419,7 +5419,6 @@ mod tests { #[test] fn test_merge_active_stake() { - let invoke_context = MockInvokeContext::default(); let base_lamports = 4242424242; let stake_address = Pubkey::new_unique(); let source_address = Pubkey::new_unique(); @@ -5473,6 +5472,7 @@ mod tests { let mut clock = Clock::default(); let mut stake_history = StakeHistory::default(); + let invoke_context = MockInvokeContext::new(&[]); clock.epoch = 0; let mut effective = base_lamports; @@ -6137,7 +6137,6 @@ mod tests { #[test] fn test_things_can_merge() { - let invoke_context = MockInvokeContext::default(); let good_stake = Stake { credits_observed: 4242, delegation: Delegation { @@ -6147,6 +6146,7 @@ mod tests { ..Delegation::default() }, }; + let invoke_context = MockInvokeContext::new(&[]); let identical = good_stake; assert!( @@ -6307,7 +6307,6 @@ mod tests { #[test] fn test_merge_kind_get_if_mergeable() { - let invoke_context = MockInvokeContext::default(); let authority_pubkey = Pubkey::new_unique(); let initial_lamports = 4242424242; let rent = Rent::default(); @@ -6326,9 +6325,9 @@ mod tests { ) .expect("stake_account"); let stake_keyed_account = KeyedAccount::new(&authority_pubkey, true, &stake_account); - let mut clock = Clock::default(); let mut stake_history = StakeHistory::default(); + let invoke_context = MockInvokeContext::new(&[]); // Uninitialized state fails assert_eq!( @@ -6540,7 +6539,6 @@ mod tests { #[test] fn test_merge_kind_merge() { - let invoke_context = MockInvokeContext::default(); let lamports = 424242; let meta = Meta { rent_exempt_reserve: 42, @@ -6556,6 +6554,7 @@ mod tests { let inactive = MergeKind::Inactive(Meta::default(), lamports); let activation_epoch = MergeKind::ActivationEpoch(meta, stake); let fully_active = MergeKind::FullyActive(meta, stake); + let invoke_context = MockInvokeContext::new(&[]); assert_eq!( inactive diff --git a/programs/vote/src/vote_instruction.rs b/programs/vote/src/vote_instruction.rs index 5cbff6bdf5d3bb..532b9248ade934 100644 --- a/programs/vote/src/vote_instruction.rs +++ b/programs/vote/src/vote_instruction.rs @@ -348,7 +348,7 @@ mod tests { &Pubkey::default(), &[], &[], - &mut MockInvokeContext::default() + &mut MockInvokeContext::new(&[]) ), Err(InstructionError::NotEnoughAccountKeys), ); @@ -386,7 +386,7 @@ mod tests { &Pubkey::default(), &keyed_accounts, &instruction.data, - &mut MockInvokeContext::default(), + &mut MockInvokeContext::new(&keyed_accounts), ) } } diff --git a/runtime/src/system_instruction_processor.rs b/runtime/src/system_instruction_processor.rs index 1441ef395c2b30..adc21adee28eee 100644 --- a/runtime/src/system_instruction_processor.rs +++ b/runtime/src/system_instruction_processor.rs @@ -394,7 +394,7 @@ mod tests { owner, keyed_accounts, instruction_data, - &mut MockInvokeContext::default(), + &mut MockInvokeContext::new(keyed_accounts), ) } diff --git a/sdk/src/process_instruction.rs b/sdk/src/process_instruction.rs index 8e6552c015c442..ddfaa8d85f36c9 100644 --- a/sdk/src/process_instruction.rs +++ b/sdk/src/process_instruction.rs @@ -314,8 +314,8 @@ pub struct MockInvokeContext<'a> { pub programs: Vec<(Pubkey, ProcessInstructionWithContext)>, pub invoke_depth: usize, } -impl<'a> Default for MockInvokeContext<'a> { - fn default() -> Self { +impl<'a> MockInvokeContext<'a> { + pub fn new(keyed_accounts: &'a [KeyedAccount<'a>]) -> Self { MockInvokeContext { key: Pubkey::default(), logger: MockLogger::default(), @@ -323,7 +323,7 @@ impl<'a> Default for MockInvokeContext<'a> { compute_meter: MockComputeMeter { remaining: std::i64::MAX as u64, }, - keyed_accounts: &[], // TODO [KeyedAccounts to InvokeContext refactoring] + keyed_accounts, programs: vec![], invoke_depth: 0, }