Refactoring: Move KeyedAccounts to InvokeContext (#15410)

Collects all parametric occurrences and the construction of keyed_accounts and puts them into InvokeContext.
This commit is contained in:
Alexander Meißner
2021-04-19 18:48:48 +02:00
committed by GitHub
parent 015bc034a5
commit 9dfcb921cf
27 changed files with 1508 additions and 1408 deletions

View File

@ -14,7 +14,7 @@ use solana_sdk::{
feature_set,
hash::Hash,
instruction::{AccountMeta, Instruction, InstructionError},
keyed_account::{from_keyed_account, get_signers, next_keyed_account, KeyedAccount},
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index, KeyedAccount},
process_instruction::InvokeContext,
program_utils::limited_deserialize,
pubkey::Pubkey,
@ -277,17 +277,17 @@ fn verify_rent_exemption(
pub fn process_instruction(
_program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
trace!("process_instruction: {:?}", data);
trace!("keyed_accounts: {:?}", keyed_accounts);
let signers: HashSet<Pubkey> = get_signers(keyed_accounts);
let keyed_accounts = &mut keyed_accounts.iter();
let me = &mut next_keyed_account(keyed_accounts)?;
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
if invoke_context.is_feature_active(&feature_set::check_program_owner::id())
&& me.owner()? != id()
@ -297,12 +297,12 @@ pub fn process_instruction(
match limited_deserialize(data)? {
VoteInstruction::InitializeAccount(vote_init) => {
verify_rent_exemption(me, next_keyed_account(keyed_accounts)?)?;
verify_rent_exemption(me, keyed_account_at_index(keyed_accounts, 1)?)?;
vote_state::initialize_account(
me,
&vote_init,
&signers,
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
invoke_context.is_feature_active(&feature_set::check_init_vote_data::id()),
)
}
@ -311,11 +311,11 @@ pub fn process_instruction(
&voter_pubkey,
vote_authorize,
&signers,
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 1)?)?,
),
VoteInstruction::UpdateValidatorIdentity => vote_state::update_validator_identity(
me,
next_keyed_account(keyed_accounts)?.unsigned_key(),
keyed_account_at_index(keyed_accounts, 1)?.unsigned_key(),
&signers,
),
VoteInstruction::UpdateCommission(commission) => {
@ -325,14 +325,14 @@ pub fn process_instruction(
inc_new_counter_info!("vote-native", 1);
vote_state::process_vote(
me,
&from_keyed_account::<SlotHashes>(next_keyed_account(keyed_accounts)?)?,
&from_keyed_account::<Clock>(next_keyed_account(keyed_accounts)?)?,
&from_keyed_account::<SlotHashes>(keyed_account_at_index(keyed_accounts, 1)?)?,
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
&vote,
&signers,
)
}
VoteInstruction::Withdraw(lamports) => {
let to = next_keyed_account(keyed_accounts)?;
let to = keyed_account_at_index(keyed_accounts, 1)?;
vote_state::withdraw(me, lamports, to, &signers)
}
}
@ -356,8 +356,7 @@ mod tests {
super::process_instruction(
&Pubkey::default(),
&[],
&[],
&mut MockInvokeContext::default()
&mut MockInvokeContext::new(vec![])
),
Err(InstructionError::NotEnoughAccountKeys),
);
@ -401,9 +400,8 @@ mod tests {
.collect();
super::process_instruction(
&Pubkey::default(),
&keyed_accounts,
&instruction.data,
&mut MockInvokeContext::default(),
&mut MockInvokeContext::new(keyed_accounts),
)
}
}

View File

@ -788,7 +788,7 @@ mod tests {
account::AccountSharedData,
account_utils::StateMut,
hash::hash,
keyed_account::{get_signers, next_keyed_account},
keyed_account::{get_signers, keyed_account_at_index},
};
use std::cell::RefCell;
@ -1715,9 +1715,8 @@ mod tests {
KeyedAccount::new(&authorized_withdrawer_pubkey, true, &withdrawer_account),
];
let signers: HashSet<Pubkey> = get_signers(keyed_accounts);
let keyed_accounts = &mut keyed_accounts.iter();
let vote_keyed_account = next_keyed_account(keyed_accounts).unwrap();
let withdrawer_keyed_account = next_keyed_account(keyed_accounts).unwrap();
let vote_keyed_account = keyed_account_at_index(keyed_accounts, 0).unwrap();
let withdrawer_keyed_account = keyed_account_at_index(keyed_accounts, 1).unwrap();
let res = withdraw(
vote_keyed_account,
lamports,