Refactor: process_instruction() (#20448)
* Adds first_instruction_account parameter to process_instruction(). * Removes InvokeContext::remove_first_keyed_account() from all BPF loaders. * Removes InvokeContext::remove_first_keyed_account() from all builtin programs. * Removes InvokeContext::remove_first_keyed_account() from all mock ups. * Deprecates InvokeContext::remove_first_keyed_account(). * Documents index base of keyed_account_at_index(). * Adds dynamic offset to call sites of "keyed_account_at_index()".
This commit is contained in:
committed by
GitHub
parent
a6a4cfda89
commit
4e65487d2f
@@ -309,6 +309,7 @@ fn verify_rent_exemption(
|
||||
|
||||
pub fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut dyn InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
@@ -317,22 +318,26 @@ pub fn process_instruction(
|
||||
trace!("process_instruction: {:?}", data);
|
||||
trace!("keyed_accounts: {:?}", keyed_accounts);
|
||||
|
||||
let signers: HashSet<Pubkey> = get_signers(keyed_accounts);
|
||||
|
||||
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
|
||||
|
||||
let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
|
||||
if me.owner()? != id() {
|
||||
return Err(InstructionError::InvalidAccountOwner);
|
||||
}
|
||||
|
||||
let signers: HashSet<Pubkey> = get_signers(&keyed_accounts[1..]);
|
||||
match limited_deserialize(data)? {
|
||||
VoteInstruction::InitializeAccount(vote_init) => {
|
||||
verify_rent_exemption(me, keyed_account_at_index(keyed_accounts, 1)?)?;
|
||||
verify_rent_exemption(
|
||||
me,
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
|
||||
)?;
|
||||
vote_state::initialize_account(
|
||||
me,
|
||||
&vote_init,
|
||||
&signers,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 2,
|
||||
)?)?,
|
||||
invoke_context.is_feature_active(&feature_set::check_init_vote_data::id()),
|
||||
)
|
||||
}
|
||||
@@ -341,11 +346,14 @@ pub fn process_instruction(
|
||||
&voter_pubkey,
|
||||
vote_authorize,
|
||||
&signers,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 1)?)?,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 1,
|
||||
)?)?,
|
||||
),
|
||||
VoteInstruction::UpdateValidatorIdentity => vote_state::update_validator_identity(
|
||||
me,
|
||||
keyed_account_at_index(keyed_accounts, 1)?.unsigned_key(),
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?.unsigned_key(),
|
||||
&signers,
|
||||
),
|
||||
VoteInstruction::UpdateCommission(commission) => {
|
||||
@@ -355,28 +363,38 @@ pub fn process_instruction(
|
||||
inc_new_counter_info!("vote-native", 1);
|
||||
vote_state::process_vote(
|
||||
me,
|
||||
&from_keyed_account::<SlotHashes>(keyed_account_at_index(keyed_accounts, 1)?)?,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
|
||||
&from_keyed_account::<SlotHashes>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 1,
|
||||
)?)?,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 2,
|
||||
)?)?,
|
||||
&vote,
|
||||
&signers,
|
||||
)
|
||||
}
|
||||
VoteInstruction::Withdraw(lamports) => {
|
||||
let to = keyed_account_at_index(keyed_accounts, 1)?;
|
||||
let to = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
||||
vote_state::withdraw(me, lamports, to, &signers)
|
||||
}
|
||||
VoteInstruction::AuthorizeChecked(vote_authorize) => {
|
||||
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
||||
{
|
||||
let voter_pubkey = &keyed_account_at_index(keyed_accounts, 3)?
|
||||
.signer_key()
|
||||
.ok_or(InstructionError::MissingRequiredSignature)?;
|
||||
let voter_pubkey =
|
||||
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
|
||||
.signer_key()
|
||||
.ok_or(InstructionError::MissingRequiredSignature)?;
|
||||
vote_state::authorize(
|
||||
me,
|
||||
voter_pubkey,
|
||||
vote_authorize,
|
||||
&signers,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 1)?)?,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 1,
|
||||
)?)?,
|
||||
)
|
||||
} else {
|
||||
Err(InstructionError::InvalidInstructionData)
|
||||
@@ -391,6 +409,7 @@ mod tests {
|
||||
use bincode::serialize;
|
||||
use solana_sdk::{
|
||||
account::{self, Account, AccountSharedData},
|
||||
keyed_account::create_keyed_accounts_unified,
|
||||
process_instruction::MockInvokeContext,
|
||||
rent::Rent,
|
||||
};
|
||||
@@ -401,21 +420,24 @@ mod tests {
|
||||
RefCell::new(AccountSharedData::default())
|
||||
}
|
||||
|
||||
// these are for 100% coverage in this file
|
||||
#[test]
|
||||
fn test_vote_process_instruction_decode_bail() {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&[],
|
||||
&mut MockInvokeContext::new(vec![])
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
fn process_instruction(
|
||||
owner: &Pubkey,
|
||||
instruction_data: &[u8],
|
||||
keyed_accounts: &[(bool, bool, &Pubkey, &RefCell<AccountSharedData>)],
|
||||
) -> Result<(), InstructionError> {
|
||||
let processor_account = AccountSharedData::new_ref(0, 0, &solana_sdk::native_loader::id());
|
||||
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)),
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::same_item_push)]
|
||||
fn process_instruction(instruction: &Instruction) -> Result<(), InstructionError> {
|
||||
fn process_instruction_as_one_arg(instruction: &Instruction) -> Result<(), InstructionError> {
|
||||
let mut accounts: Vec<_> = instruction
|
||||
.accounts
|
||||
.iter()
|
||||
@@ -448,13 +470,9 @@ mod tests {
|
||||
.accounts
|
||||
.iter()
|
||||
.zip(accounts.iter())
|
||||
.map(|(meta, account)| KeyedAccount::new(&meta.pubkey, meta.is_signer, account))
|
||||
.map(|(meta, account)| (meta.is_signer, false, &meta.pubkey, account))
|
||||
.collect();
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts),
|
||||
)
|
||||
process_instruction(&Pubkey::default(), &instruction.data, &keyed_accounts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -462,10 +480,19 @@ mod tests {
|
||||
Pubkey::from_str("BadVote111111111111111111111111111111111111").unwrap()
|
||||
}
|
||||
|
||||
// these are for 100% coverage in this file
|
||||
#[test]
|
||||
fn test_vote_process_instruction_decode_bail() {
|
||||
assert_eq!(
|
||||
process_instruction(&Pubkey::default(), &[], &[]),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spoofed_vote() {
|
||||
assert_eq!(
|
||||
process_instruction(&vote(
|
||||
process_instruction_as_one_arg(&vote(
|
||||
&invalid_vote_state_pubkey(),
|
||||
&Pubkey::default(),
|
||||
Vote::default(),
|
||||
@@ -484,11 +511,11 @@ mod tests {
|
||||
100,
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instructions[1]),
|
||||
process_instruction_as_one_arg(&instructions[1]),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&vote(
|
||||
process_instruction_as_one_arg(&vote(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
Vote::default(),
|
||||
@@ -496,7 +523,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&vote_switch(
|
||||
process_instruction_as_one_arg(&vote_switch(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
Vote::default(),
|
||||
@@ -505,7 +532,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&authorize(
|
||||
process_instruction_as_one_arg(&authorize(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
@@ -514,7 +541,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&update_validator_identity(
|
||||
process_instruction_as_one_arg(&update_validator_identity(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
@@ -522,7 +549,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&update_commission(
|
||||
process_instruction_as_one_arg(&update_commission(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
0,
|
||||
@@ -531,7 +558,7 @@ mod tests {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
process_instruction(&withdraw(
|
||||
process_instruction_as_one_arg(&withdraw(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
0,
|
||||
@@ -556,7 +583,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts = instruction.accounts[0..2].to_vec();
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
|
||||
@@ -568,7 +595,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts = instruction.accounts[0..2].to_vec();
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
|
||||
@@ -581,7 +608,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(new_authorized_pubkey, false);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@@ -593,7 +620,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(new_authorized_pubkey, false);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@@ -606,35 +633,29 @@ mod tests {
|
||||
let default_authorized_pubkey = Pubkey::default();
|
||||
let authorized_account = create_default_account();
|
||||
let new_authorized_account = create_default_account();
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&vote_pubkey, false, &vote_account),
|
||||
KeyedAccount::new(&clock_address, false, &clock_account),
|
||||
KeyedAccount::new(&default_authorized_pubkey, true, &authorized_account),
|
||||
KeyedAccount::new(&new_authorized_pubkey, true, &new_authorized_account),
|
||||
let keyed_accounts: [(bool, bool, &Pubkey, &RefCell<AccountSharedData>); 4] = [
|
||||
(false, false, &vote_pubkey, &vote_account),
|
||||
(false, false, &clock_address, &clock_account),
|
||||
(true, false, &default_authorized_pubkey, &authorized_account),
|
||||
(true, false, &new_authorized_pubkey, &new_authorized_account),
|
||||
];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&VoteInstruction::AuthorizeChecked(VoteAuthorize::Voter)).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(())
|
||||
);
|
||||
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&vote_pubkey, false, &vote_account),
|
||||
KeyedAccount::new(&clock_address, false, &clock_account),
|
||||
KeyedAccount::new(&default_authorized_pubkey, true, &authorized_account),
|
||||
KeyedAccount::new(&new_authorized_pubkey, true, &new_authorized_account),
|
||||
];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&VoteInstruction::AuthorizeChecked(
|
||||
VoteAuthorize::Withdrawer
|
||||
))
|
||||
.unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(())
|
||||
);
|
||||
|
Reference in New Issue
Block a user