Refactor: Remove program_id from process_instruction() (#20540)

* Replaces usage of program_id parameter by invoke_context.get_caller()?.

* Removes "pubkey: &Pubkey" parameter from "process_instruction()".
This commit is contained in:
Alexander Meißner
2021-10-10 22:29:18 +02:00
committed by GitHub
parent c16510152e
commit f30f3bddbb
23 changed files with 110 additions and 175 deletions

View File

@ -7,7 +7,6 @@ use {
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index},
process_instruction::{get_sysvar, InvokeContext},
program_utils::limited_deserialize,
pubkey::Pubkey,
stake::{
instruction::StakeInstruction,
program::id,
@ -24,7 +23,6 @@ use {
pub use solana_sdk::stake::instruction::*;
pub fn process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -333,6 +331,7 @@ mod tests {
instruction::{AccountMeta, Instruction},
keyed_account::create_keyed_accounts_unified,
process_instruction::{mock_set_sysvar, MockInvokeContext},
pubkey::Pubkey,
rent::Rent,
stake::{
config as stake_config,
@ -379,10 +378,9 @@ mod tests {
let mut keyed_accounts = keyed_accounts.to_vec();
keyed_accounts.insert(0, (false, false, owner, &processor_account));
super::process_instruction(
owner,
1,
instruction_data,
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts)),
&mut MockInvokeContext::new(owner, create_keyed_accounts_unified(&keyed_accounts)),
)
}
@ -440,20 +438,17 @@ mod tests {
.collect();
let processor_id = id();
keyed_accounts.insert(0, (false, false, &processor_id, &processor_account));
let mut invoke_context =
MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
let mut invoke_context = MockInvokeContext::new(
&processor_id,
create_keyed_accounts_unified(&keyed_accounts),
);
mock_set_sysvar(
&mut invoke_context,
sysvar::clock::id(),
sysvar::clock::Clock::default(),
)
.unwrap();
super::process_instruction(
&Pubkey::default(),
1,
&instruction.data,
&mut invoke_context,
)
super::process_instruction(1, &instruction.data, &mut invoke_context)
}
}
@ -1104,7 +1099,7 @@ mod tests {
(true, false, &custodian, &custodian_account),
];
let mut invoke_context =
MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
MockInvokeContext::new(&id(), create_keyed_accounts_unified(&keyed_accounts));
let clock = Clock::default();
let mut data = vec![];
bincode::serialize_into(&mut data, &clock).unwrap();
@ -1114,7 +1109,6 @@ mod tests {
assert_eq!(
super::process_instruction(
&Pubkey::default(),
1,
&serialize(&StakeInstruction::SetLockupChecked(LockupCheckedArgs {
unix_timestamp: None,

View File

@ -5075,7 +5075,7 @@ mod tests {
let stake_lamports = 42;
let signers = vec![authorized_pubkey].into_iter().collect();
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
for state in &[
StakeState::Initialized(Meta::auto(&authorized_pubkey)),
@ -5179,7 +5179,7 @@ mod tests {
#[test]
fn test_merge_self_fails() {
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let stake_address = Pubkey::new_unique();
let authority_pubkey = Pubkey::new_unique();
let signers = HashSet::from_iter(vec![authority_pubkey]);
@ -5232,7 +5232,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(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
for state in &[
StakeState::Initialized(Meta::auto(&authorized_pubkey)),
@ -5298,7 +5298,7 @@ mod tests {
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(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
for state in &[
StakeState::Uninitialized,
@ -5368,7 +5368,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(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
assert_eq!(
stake_keyed_account.merge(
@ -5438,7 +5438,7 @@ mod tests {
let mut clock = Clock::default();
let mut stake_history = StakeHistory::default();
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
clock.epoch = 0;
let mut effective = base_lamports;
@ -6016,7 +6016,7 @@ mod tests {
..Delegation::default()
},
};
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let identical = good_stake;
assert!(
@ -6105,7 +6105,7 @@ mod tests {
#[test]
fn test_metas_can_merge_pre_v4() {
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// Identical Metas can merge
assert!(MergeKind::metas_can_merge(
&invoke_context,
@ -6191,7 +6191,7 @@ mod tests {
#[test]
fn test_metas_can_merge_v4() {
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// Identical Metas can merge
assert!(MergeKind::metas_can_merge(
&invoke_context,
@ -6357,7 +6357,7 @@ mod tests {
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(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// Uninitialized state fails
assert_eq!(
@ -6584,7 +6584,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(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
assert_eq!(
inactive
@ -6670,7 +6670,7 @@ mod tests {
credits_observed: credits_a,
};
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// activating stake merge, match credits observed
let activation_epoch_a = MergeKind::ActivationEpoch(meta, stake_a);