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
File diff suppressed because it is too large
Load Diff
@ -4,6 +4,7 @@ use solana_sdk::{
|
||||
|
||||
pub fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut dyn InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
|
@ -15,13 +15,15 @@ use std::collections::BTreeSet;
|
||||
|
||||
pub fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut dyn InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
let key_list: ConfigKeys = limited_deserialize(data)?;
|
||||
let config_keyed_account = &mut keyed_account_at_index(keyed_accounts, 0)?;
|
||||
let config_keyed_account =
|
||||
&mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
|
||||
let current_data: ConfigKeys = {
|
||||
let config_account = config_keyed_account.try_account_ref_mut()?;
|
||||
if config_account.owner() != &crate::id() {
|
||||
@ -53,7 +55,7 @@ pub fn process_instruction(
|
||||
}
|
||||
|
||||
let mut counter = 0;
|
||||
let mut keyed_accounts_iter = keyed_accounts.iter().skip(1);
|
||||
let mut keyed_accounts_iter = keyed_accounts.iter().skip(2);
|
||||
for (signer, _) in key_list.keys.iter().filter(|(_, is_signer)| *is_signer) {
|
||||
counter += 1;
|
||||
if signer != config_keyed_account.unsigned_key() {
|
||||
@ -147,6 +149,22 @@ mod tests {
|
||||
};
|
||||
use std::cell::RefCell;
|
||||
|
||||
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)),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||
struct MyConfig {
|
||||
pub item: u64,
|
||||
@ -193,14 +211,9 @@ mod tests {
|
||||
owner: id(),
|
||||
..Account::default()
|
||||
}));
|
||||
let accounts = vec![(true, false, &config_pubkey, &config_account)];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
let keyed_accounts = [(true, false, &config_pubkey, &config_account)];
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instructions[1].data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instructions[1].data, &keyed_accounts),
|
||||
Ok(())
|
||||
);
|
||||
|
||||
@ -210,8 +223,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_process_create_ok() {
|
||||
solana_logger::setup();
|
||||
let keys = vec![];
|
||||
let (_, config_account) = create_config_account(keys);
|
||||
let (_, config_account) = create_config_account(vec![]);
|
||||
assert_eq!(
|
||||
Some(MyConfig::default()),
|
||||
deserialize(get_config_data(config_account.borrow().data()).unwrap()).ok()
|
||||
@ -227,14 +239,9 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let accounts = vec![(true, false, &config_pubkey, &config_account)];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
let keyed_accounts = [(true, false, &config_pubkey, &config_account)];
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
);
|
||||
assert_eq!(
|
||||
@ -253,14 +260,9 @@ mod tests {
|
||||
|
||||
let mut instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
instruction.data = vec![0; 123]; // <-- Replace data with a vector that's too large
|
||||
let accounts = vec![(true, false, &config_pubkey, &config_account)];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
let keyed_accounts = [(true, false, &config_pubkey, &config_account)];
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidInstructionData)
|
||||
);
|
||||
}
|
||||
@ -275,14 +277,9 @@ mod tests {
|
||||
|
||||
let mut instruction = config_instruction::store(&config_pubkey, true, vec![], &my_config);
|
||||
instruction.accounts[0].is_signer = false; // <----- not a signer
|
||||
let accounts = vec![(false, false, &config_pubkey, &config_account)];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
let keyed_accounts = [(false, false, &config_pubkey, &config_account)];
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
);
|
||||
}
|
||||
@ -305,18 +302,13 @@ mod tests {
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let signer0_account = RefCell::new(AccountSharedData::default());
|
||||
let signer1_account = RefCell::new(AccountSharedData::default());
|
||||
let accounts = vec![
|
||||
let keyed_accounts = [
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
(true, false, &signer1_pubkey, &signer1_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
@ -342,14 +334,9 @@ mod tests {
|
||||
owner: id(),
|
||||
..Account::default()
|
||||
}));
|
||||
let accounts = vec![(true, false, &signer0_pubkey, &signer0_account)];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
let keyed_accounts = [(true, false, &signer0_pubkey, &signer0_account)];
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidAccountData)
|
||||
);
|
||||
}
|
||||
@ -369,32 +356,19 @@ mod tests {
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
|
||||
// Config-data pubkey doesn't match signer
|
||||
let accounts = vec![
|
||||
let mut keyed_accounts = [
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer1_pubkey, &signer1_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
);
|
||||
|
||||
// Config-data pubkey not a signer
|
||||
let accounts = vec![
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(false, false, &signer0_pubkey, &signer0_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
keyed_accounts[1] = (false, false, &signer0_pubkey, &signer0_account);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
);
|
||||
}
|
||||
@ -419,18 +393,13 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let accounts = vec![
|
||||
let mut keyed_accounts = [
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
(true, false, &signer1_pubkey, &signer1_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
);
|
||||
|
||||
@ -438,18 +407,9 @@ mod tests {
|
||||
let new_config = MyConfig::new(84);
|
||||
let instruction =
|
||||
config_instruction::store(&config_pubkey, false, keys.clone(), &new_config);
|
||||
let accounts = vec![
|
||||
(false, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
(true, false, &signer1_pubkey, &signer1_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
keyed_accounts[0].0 = false;
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
@ -463,18 +423,9 @@ mod tests {
|
||||
// Attempt update with incomplete signatures
|
||||
let keys = vec![(pubkey, false), (signer0_pubkey, true)];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
|
||||
let accounts = vec![
|
||||
(false, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
(false, false, &signer1_pubkey, &signer1_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
keyed_accounts[2].0 = false;
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
);
|
||||
|
||||
@ -485,18 +436,9 @@ mod tests {
|
||||
(signer2_pubkey, true),
|
||||
];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
|
||||
let accounts = vec![
|
||||
(false, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
(true, false, &signer2_pubkey, &signer2_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
keyed_accounts[2] = (true, false, &signer2_pubkey, &signer2_account);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
);
|
||||
}
|
||||
@ -518,18 +460,13 @@ mod tests {
|
||||
|
||||
// Attempt initialization with duplicate signer inputs
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let accounts = vec![
|
||||
let keyed_accounts = [
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
@ -552,18 +489,13 @@ mod tests {
|
||||
let my_config = MyConfig::new(42);
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let accounts = vec![
|
||||
let mut keyed_accounts = [
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
(true, false, &signer1_pubkey, &signer1_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
@ -575,18 +507,9 @@ mod tests {
|
||||
(signer0_pubkey, true),
|
||||
];
|
||||
let instruction = config_instruction::store(&config_pubkey, false, dupe_keys, &new_config);
|
||||
let accounts = vec![
|
||||
(false, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
keyed_accounts[2] = keyed_accounts[1];
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
}
|
||||
@ -613,17 +536,12 @@ mod tests {
|
||||
];
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
|
||||
let accounts = vec![
|
||||
let keyed_accounts = [
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
);
|
||||
|
||||
@ -631,17 +549,8 @@ mod tests {
|
||||
let new_config = MyConfig::new(84);
|
||||
let instruction =
|
||||
config_instruction::store(&config_pubkey, true, keys.clone(), &new_config);
|
||||
let accounts = vec![
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Ok(())
|
||||
);
|
||||
let meta_data: ConfigKeys = deserialize(config_account.borrow().data()).unwrap();
|
||||
@ -655,14 +564,8 @@ mod tests {
|
||||
// Attempt update with incomplete signatures
|
||||
let keys = vec![(pubkey, false), (config_keypair.pubkey(), true)];
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
|
||||
let accounts = vec![(true, false, &config_pubkey, &config_account)];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts[0..1]),
|
||||
Err(InstructionError::MissingRequiredSignature)
|
||||
);
|
||||
}
|
||||
@ -671,16 +574,11 @@ mod tests {
|
||||
fn test_config_initialize_no_panic() {
|
||||
let from_pubkey = solana_sdk::pubkey::new_rand();
|
||||
let config_pubkey = solana_sdk::pubkey::new_rand();
|
||||
let (_, _config_account) = create_config_account(vec![]);
|
||||
let instructions =
|
||||
config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, vec![]);
|
||||
let accounts = vec![];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instructions[1].data,
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
),
|
||||
process_instruction(&id(), &instructions[1].data, &[]),
|
||||
Err(InstructionError::NotEnoughAccountKeys)
|
||||
);
|
||||
}
|
||||
@ -693,6 +591,7 @@ mod tests {
|
||||
let signer0_pubkey = solana_sdk::pubkey::new_rand();
|
||||
let signer0_account = RefCell::new(AccountSharedData::default());
|
||||
let config_account = RefCell::new(AccountSharedData::default());
|
||||
let (_, _config_account) = create_config_account(vec![]);
|
||||
let keys = vec![
|
||||
(from_pubkey, false),
|
||||
(signer0_pubkey, true),
|
||||
@ -700,17 +599,12 @@ mod tests {
|
||||
];
|
||||
|
||||
let instruction = config_instruction::store(&config_pubkey, true, keys, &new_config);
|
||||
let accounts = vec![
|
||||
let keyed_accounts = [
|
||||
(true, false, &config_pubkey, &config_account),
|
||||
(true, false, &signer0_pubkey, &signer0_account),
|
||||
];
|
||||
let keyed_accounts = create_keyed_accounts_unified(&accounts);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
&id(),
|
||||
&instruction.data,
|
||||
&mut MockInvokeContext::new(keyed_accounts),
|
||||
),
|
||||
process_instruction(&id(), &instruction.data, &keyed_accounts),
|
||||
Err(InstructionError::InvalidAccountOwner)
|
||||
);
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ pub use solana_sdk::stake::instruction::*;
|
||||
|
||||
pub fn process_instruction(
|
||||
_program_id: &Pubkey,
|
||||
first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut dyn InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
@ -33,19 +34,20 @@ pub fn process_instruction(
|
||||
trace!("process_instruction: {:?}", data);
|
||||
trace!("keyed_accounts: {:?}", keyed_accounts);
|
||||
|
||||
let signers = get_signers(keyed_accounts);
|
||||
|
||||
let me = &keyed_account_at_index(keyed_accounts, 0)?;
|
||||
|
||||
let me = &keyed_account_at_index(keyed_accounts, first_instruction_account)?;
|
||||
if me.owner()? != id() {
|
||||
return Err(InstructionError::InvalidAccountOwner);
|
||||
}
|
||||
|
||||
let signers = get_signers(&keyed_accounts[1..]);
|
||||
match limited_deserialize(data)? {
|
||||
StakeInstruction::Initialize(authorized, lockup) => me.initialize(
|
||||
&authorized,
|
||||
&lockup,
|
||||
&from_keyed_account::<Rent>(keyed_account_at_index(keyed_accounts, 1)?)?,
|
||||
&from_keyed_account::<Rent>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 1,
|
||||
)?)?,
|
||||
),
|
||||
StakeInstruction::Authorize(authorized_pubkey, stake_authorize) => {
|
||||
let require_custodian_for_locked_stake_authorize = invoke_context.is_feature_active(
|
||||
@ -53,12 +55,16 @@ pub fn process_instruction(
|
||||
);
|
||||
|
||||
if require_custodian_for_locked_stake_authorize {
|
||||
let clock =
|
||||
from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 1)?)?;
|
||||
let _current_authority = keyed_account_at_index(keyed_accounts, 2)?;
|
||||
let custodian = keyed_account_at_index(keyed_accounts, 3)
|
||||
.ok()
|
||||
.map(|ka| ka.unsigned_key());
|
||||
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 1,
|
||||
)?)?;
|
||||
let _current_authority =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
|
||||
let custodian =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)
|
||||
.ok()
|
||||
.map(|ka| ka.unsigned_key());
|
||||
|
||||
me.authorize(
|
||||
&signers,
|
||||
@ -80,17 +86,21 @@ pub fn process_instruction(
|
||||
}
|
||||
}
|
||||
StakeInstruction::AuthorizeWithSeed(args) => {
|
||||
let authority_base = keyed_account_at_index(keyed_accounts, 1)?;
|
||||
let authority_base =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
||||
let require_custodian_for_locked_stake_authorize = invoke_context.is_feature_active(
|
||||
&feature_set::require_custodian_for_locked_stake_authorize::id(),
|
||||
);
|
||||
|
||||
if require_custodian_for_locked_stake_authorize {
|
||||
let clock =
|
||||
from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?;
|
||||
let custodian = keyed_account_at_index(keyed_accounts, 3)
|
||||
.ok()
|
||||
.map(|ka| ka.unsigned_key());
|
||||
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 2,
|
||||
)?)?;
|
||||
let custodian =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)
|
||||
.ok()
|
||||
.map(|ka| ka.unsigned_key());
|
||||
|
||||
me.authorize_with_seed(
|
||||
authority_base,
|
||||
@ -118,48 +128,74 @@ pub fn process_instruction(
|
||||
StakeInstruction::DelegateStake => {
|
||||
let can_reverse_deactivation =
|
||||
invoke_context.is_feature_active(&feature_set::stake_program_v4::id());
|
||||
let vote = keyed_account_at_index(keyed_accounts, 1)?;
|
||||
let vote = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
||||
|
||||
me.delegate(
|
||||
vote,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
|
||||
&from_keyed_account::<StakeHistory>(keyed_account_at_index(keyed_accounts, 3)?)?,
|
||||
&config::from_keyed_account(keyed_account_at_index(keyed_accounts, 4)?)?,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 2,
|
||||
)?)?,
|
||||
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 3,
|
||||
)?)?,
|
||||
&config::from_keyed_account(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 4,
|
||||
)?)?,
|
||||
&signers,
|
||||
can_reverse_deactivation,
|
||||
)
|
||||
}
|
||||
StakeInstruction::Split(lamports) => {
|
||||
let split_stake = &keyed_account_at_index(keyed_accounts, 1)?;
|
||||
let split_stake =
|
||||
&keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
||||
me.split(lamports, split_stake, &signers)
|
||||
}
|
||||
StakeInstruction::Merge => {
|
||||
let source_stake = &keyed_account_at_index(keyed_accounts, 1)?;
|
||||
let source_stake =
|
||||
&keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
||||
let can_merge_expired_lockups =
|
||||
invoke_context.is_feature_active(&feature_set::stake_program_v4::id());
|
||||
me.merge(
|
||||
invoke_context,
|
||||
source_stake,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
|
||||
&from_keyed_account::<StakeHistory>(keyed_account_at_index(keyed_accounts, 3)?)?,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 2,
|
||||
)?)?,
|
||||
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 3,
|
||||
)?)?,
|
||||
&signers,
|
||||
can_merge_expired_lockups,
|
||||
)
|
||||
}
|
||||
StakeInstruction::Withdraw(lamports) => {
|
||||
let to = &keyed_account_at_index(keyed_accounts, 1)?;
|
||||
let to = &keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
||||
me.withdraw(
|
||||
lamports,
|
||||
to,
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?,
|
||||
&from_keyed_account::<StakeHistory>(keyed_account_at_index(keyed_accounts, 3)?)?,
|
||||
keyed_account_at_index(keyed_accounts, 4)?,
|
||||
keyed_account_at_index(keyed_accounts, 5).ok(),
|
||||
&from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 2,
|
||||
)?)?,
|
||||
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 3,
|
||||
)?)?,
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?,
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 5).ok(),
|
||||
invoke_context.is_feature_active(&feature_set::stake_program_v4::id()),
|
||||
)
|
||||
}
|
||||
StakeInstruction::Deactivate => me.deactivate(
|
||||
&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,
|
||||
)?)?,
|
||||
&signers,
|
||||
),
|
||||
StakeInstruction::SetLockup(lockup) => {
|
||||
@ -174,16 +210,23 @@ pub fn process_instruction(
|
||||
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
||||
{
|
||||
let authorized = Authorized {
|
||||
staker: *keyed_account_at_index(keyed_accounts, 2)?.unsigned_key(),
|
||||
withdrawer: *keyed_account_at_index(keyed_accounts, 3)?
|
||||
.signer_key()
|
||||
.ok_or(InstructionError::MissingRequiredSignature)?,
|
||||
staker: *keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?
|
||||
.unsigned_key(),
|
||||
withdrawer: *keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 3,
|
||||
)?
|
||||
.signer_key()
|
||||
.ok_or(InstructionError::MissingRequiredSignature)?,
|
||||
};
|
||||
|
||||
me.initialize(
|
||||
&authorized,
|
||||
&Lockup::default(),
|
||||
&from_keyed_account::<Rent>(keyed_account_at_index(keyed_accounts, 1)?)?,
|
||||
&from_keyed_account::<Rent>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 1,
|
||||
)?)?,
|
||||
)
|
||||
} else {
|
||||
Err(InstructionError::InvalidInstructionData)
|
||||
@ -192,15 +235,20 @@ pub fn process_instruction(
|
||||
StakeInstruction::AuthorizeChecked(stake_authorize) => {
|
||||
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
||||
{
|
||||
let clock =
|
||||
from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 1)?)?;
|
||||
let _current_authority = keyed_account_at_index(keyed_accounts, 2)?;
|
||||
let authorized_pubkey = &keyed_account_at_index(keyed_accounts, 3)?
|
||||
.signer_key()
|
||||
.ok_or(InstructionError::MissingRequiredSignature)?;
|
||||
let custodian = keyed_account_at_index(keyed_accounts, 4)
|
||||
.ok()
|
||||
.map(|ka| ka.unsigned_key());
|
||||
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 1,
|
||||
)?)?;
|
||||
let _current_authority =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
|
||||
let authorized_pubkey =
|
||||
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
|
||||
.signer_key()
|
||||
.ok_or(InstructionError::MissingRequiredSignature)?;
|
||||
let custodian =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)
|
||||
.ok()
|
||||
.map(|ka| ka.unsigned_key());
|
||||
|
||||
me.authorize(
|
||||
&signers,
|
||||
@ -217,15 +265,20 @@ pub fn process_instruction(
|
||||
StakeInstruction::AuthorizeCheckedWithSeed(args) => {
|
||||
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
||||
{
|
||||
let authority_base = keyed_account_at_index(keyed_accounts, 1)?;
|
||||
let clock =
|
||||
from_keyed_account::<Clock>(keyed_account_at_index(keyed_accounts, 2)?)?;
|
||||
let authorized_pubkey = &keyed_account_at_index(keyed_accounts, 3)?
|
||||
.signer_key()
|
||||
.ok_or(InstructionError::MissingRequiredSignature)?;
|
||||
let custodian = keyed_account_at_index(keyed_accounts, 4)
|
||||
.ok()
|
||||
.map(|ka| ka.unsigned_key());
|
||||
let authority_base =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
|
||||
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
|
||||
keyed_accounts,
|
||||
first_instruction_account + 2,
|
||||
)?)?;
|
||||
let authorized_pubkey =
|
||||
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
|
||||
.signer_key()
|
||||
.ok_or(InstructionError::MissingRequiredSignature)?;
|
||||
let custodian =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)
|
||||
.ok()
|
||||
.map(|ka| ka.unsigned_key());
|
||||
|
||||
me.authorize_with_seed(
|
||||
authority_base,
|
||||
@ -244,7 +297,9 @@ pub fn process_instruction(
|
||||
StakeInstruction::SetLockupChecked(lockup_checked) => {
|
||||
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
|
||||
{
|
||||
let custodian = if let Ok(custodian) = keyed_account_at_index(keyed_accounts, 2) {
|
||||
let custodian = if let Ok(custodian) =
|
||||
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)
|
||||
{
|
||||
Some(
|
||||
*custodian
|
||||
.signer_key()
|
||||
@ -276,7 +331,7 @@ mod tests {
|
||||
use solana_sdk::{
|
||||
account::{self, Account, AccountSharedData, WritableAccount},
|
||||
instruction::{AccountMeta, Instruction},
|
||||
keyed_account::KeyedAccount,
|
||||
keyed_account::create_keyed_accounts_unified,
|
||||
process_instruction::{mock_set_sysvar, MockInvokeContext},
|
||||
rent::Rent,
|
||||
stake::{
|
||||
@ -315,7 +370,27 @@ mod tests {
|
||||
Pubkey::from_str("Spoofed111111111111111111111111111111111111").unwrap()
|
||||
}
|
||||
|
||||
fn process_instruction(instruction: &Instruction) -> Result<(), InstructionError> {
|
||||
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)),
|
||||
)
|
||||
}
|
||||
|
||||
fn process_instruction_as_one_arg(instruction: &Instruction) -> Result<(), InstructionError> {
|
||||
let processor_account = RefCell::new(AccountSharedData::from(Account {
|
||||
owner: solana_sdk::native_loader::id(),
|
||||
..Account::default()
|
||||
}));
|
||||
let accounts: Vec<_> = instruction
|
||||
.accounts
|
||||
.iter()
|
||||
@ -357,28 +432,35 @@ mod tests {
|
||||
.collect();
|
||||
|
||||
{
|
||||
let keyed_accounts: Vec<_> = instruction
|
||||
let mut keyed_accounts: Vec<_> = instruction
|
||||
.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();
|
||||
|
||||
let mut invoke_context = MockInvokeContext::new(keyed_accounts);
|
||||
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));
|
||||
mock_set_sysvar(
|
||||
&mut invoke_context,
|
||||
sysvar::clock::id(),
|
||||
sysvar::clock::Clock::default(),
|
||||
)
|
||||
.unwrap();
|
||||
super::process_instruction(&Pubkey::default(), &instruction.data, &mut invoke_context)
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
1,
|
||||
&instruction.data,
|
||||
&mut invoke_context,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_stake_process_instruction() {
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::initialize(
|
||||
process_instruction_as_one_arg(&instruction::initialize(
|
||||
&Pubkey::default(),
|
||||
&Authorized::default(),
|
||||
&Lockup::default()
|
||||
@ -386,7 +468,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::authorize(
|
||||
process_instruction_as_one_arg(&instruction::authorize(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
@ -396,7 +478,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
process_instruction_as_one_arg(
|
||||
&instruction::split(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
@ -407,7 +489,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
process_instruction_as_one_arg(
|
||||
&instruction::merge(
|
||||
&Pubkey::default(),
|
||||
&invalid_stake_state_pubkey(),
|
||||
@ -417,7 +499,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
process_instruction_as_one_arg(
|
||||
&instruction::split_with_seed(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
@ -430,7 +512,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::delegate_stake(
|
||||
process_instruction_as_one_arg(&instruction::delegate_stake(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&invalid_vote_state_pubkey(),
|
||||
@ -438,7 +520,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::withdraw(
|
||||
process_instruction_as_one_arg(&instruction::withdraw(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
@ -448,14 +530,14 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::deactivate_stake(
|
||||
process_instruction_as_one_arg(&instruction::deactivate_stake(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default()
|
||||
)),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::set_lockup(
|
||||
process_instruction_as_one_arg(&instruction::set_lockup(
|
||||
&Pubkey::default(),
|
||||
&LockupArgs::default(),
|
||||
&Pubkey::default()
|
||||
@ -467,7 +549,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_spoofed_stake_accounts() {
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::initialize(
|
||||
process_instruction_as_one_arg(&instruction::initialize(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&Authorized::default(),
|
||||
&Lockup::default()
|
||||
@ -475,7 +557,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::authorize(
|
||||
process_instruction_as_one_arg(&instruction::authorize(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
@ -485,7 +567,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
process_instruction_as_one_arg(
|
||||
&instruction::split(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&Pubkey::default(),
|
||||
@ -496,7 +578,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
process_instruction_as_one_arg(
|
||||
&instruction::split(
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
@ -507,7 +589,7 @@ mod tests {
|
||||
Err(InstructionError::IncorrectProgramId),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
process_instruction_as_one_arg(
|
||||
&instruction::merge(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&Pubkey::default(),
|
||||
@ -517,7 +599,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
process_instruction_as_one_arg(
|
||||
&instruction::merge(
|
||||
&Pubkey::default(),
|
||||
&spoofed_stake_state_pubkey(),
|
||||
@ -527,7 +609,7 @@ mod tests {
|
||||
Err(InstructionError::IncorrectProgramId),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(
|
||||
process_instruction_as_one_arg(
|
||||
&instruction::split_with_seed(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&Pubkey::default(),
|
||||
@ -540,7 +622,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::delegate_stake(
|
||||
process_instruction_as_one_arg(&instruction::delegate_stake(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&Pubkey::default(),
|
||||
&Pubkey::default(),
|
||||
@ -548,7 +630,7 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::withdraw(
|
||||
process_instruction_as_one_arg(&instruction::withdraw(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&Pubkey::default(),
|
||||
&solana_sdk::pubkey::new_rand(),
|
||||
@ -558,14 +640,14 @@ mod tests {
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::deactivate_stake(
|
||||
process_instruction_as_one_arg(&instruction::deactivate_stake(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&Pubkey::default()
|
||||
)),
|
||||
Err(InstructionError::InvalidAccountOwner),
|
||||
);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction::set_lockup(
|
||||
process_instruction_as_one_arg(&instruction::set_lockup(
|
||||
&spoofed_stake_state_pubkey(),
|
||||
&LockupArgs::default(),
|
||||
&Pubkey::default()
|
||||
@ -580,14 +662,14 @@ mod tests {
|
||||
|
||||
// gets the "is_empty()" check
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::Initialize(
|
||||
Authorized::default(),
|
||||
Lockup::default()
|
||||
))
|
||||
.unwrap(),
|
||||
&mut MockInvokeContext::new(vec![])
|
||||
&[],
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
@ -595,16 +677,16 @@ mod tests {
|
||||
// no account for rent
|
||||
let stake_address = Pubkey::default();
|
||||
let stake_account = create_default_stake_account();
|
||||
let keyed_accounts = vec![KeyedAccount::new(&stake_address, false, &stake_account)];
|
||||
let keyed_accounts = [(false, false, &stake_address, &stake_account)];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::Initialize(
|
||||
Authorized::default(),
|
||||
Lockup::default()
|
||||
))
|
||||
.unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
@ -614,19 +696,19 @@ mod tests {
|
||||
let stake_account = create_default_stake_account();
|
||||
let rent_address = sysvar::rent::id();
|
||||
let rent_account = create_default_account();
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&stake_address, false, &stake_account),
|
||||
KeyedAccount::new(&rent_address, false, &rent_account),
|
||||
let keyed_accounts = [
|
||||
(false, false, &stake_address, &stake_account),
|
||||
(false, false, &rent_address, &rent_account),
|
||||
];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::Initialize(
|
||||
Authorized::default(),
|
||||
Lockup::default()
|
||||
))
|
||||
.unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
@ -638,19 +720,19 @@ mod tests {
|
||||
let rent_account = RefCell::new(account::create_account_shared_data_for_test(
|
||||
&Rent::default(),
|
||||
));
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&stake_address, false, &stake_account),
|
||||
KeyedAccount::new(&rent_address, false, &rent_account),
|
||||
let keyed_accounts = [
|
||||
(false, false, &stake_address, &stake_account),
|
||||
(false, false, &rent_address, &rent_account),
|
||||
];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::Initialize(
|
||||
Authorized::default(),
|
||||
Lockup::default()
|
||||
))
|
||||
.unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
@ -658,12 +740,12 @@ mod tests {
|
||||
// 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 = vec![KeyedAccount::new(&stake_address, false, &stake_account)];
|
||||
let keyed_accounts = [(false, false, &stake_address, &stake_account)];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
@ -671,12 +753,12 @@ mod tests {
|
||||
// gets the sub-check for number of args
|
||||
let stake_address = Pubkey::default();
|
||||
let stake_account = create_default_stake_account();
|
||||
let keyed_accounts = vec![KeyedAccount::new(&stake_address, false, &stake_account)];
|
||||
let keyed_accounts = [(false, false, &stake_address, &stake_account)];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
@ -700,18 +782,18 @@ mod tests {
|
||||
let config_address = stake_config::id();
|
||||
let config_account =
|
||||
RefCell::new(config::create_account(0, &stake_config::Config::default()));
|
||||
let keyed_accounts = vec![
|
||||
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),
|
||||
let keyed_accounts = [
|
||||
(true, false, &stake_address, &stake_account),
|
||||
(false, false, &vote_address, &bad_vote_account),
|
||||
(false, false, &clock_address, &clock_account),
|
||||
(false, false, &stake_history_address, &stake_history_account),
|
||||
(false, false, &config_address, &config_account),
|
||||
];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::DelegateStake).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::InvalidAccountData),
|
||||
);
|
||||
@ -729,17 +811,17 @@ mod tests {
|
||||
let stake_history_account = RefCell::new(account::create_account_shared_data_for_test(
|
||||
&StakeHistory::default(),
|
||||
));
|
||||
let keyed_accounts = vec![
|
||||
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),
|
||||
let keyed_accounts = [
|
||||
(false, false, &stake_address, &stake_account),
|
||||
(false, false, &vote_address, &vote_account),
|
||||
(false, false, &rewards_address, &rewards_account),
|
||||
(false, false, &stake_history_address, &stake_history_account),
|
||||
];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
@ -747,12 +829,12 @@ mod tests {
|
||||
// Tests correct number of accounts are provided in withdraw
|
||||
let stake_address = Pubkey::default();
|
||||
let stake_account = create_default_stake_account();
|
||||
let keyed_accounts = vec![KeyedAccount::new(&stake_address, false, &stake_account)];
|
||||
let keyed_accounts = [(false, false, &stake_address, &stake_account)];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::Withdraw(42)).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
@ -764,25 +846,25 @@ mod tests {
|
||||
let rewards_account = RefCell::new(account::create_account_shared_data_for_test(
|
||||
&sysvar::rewards::Rewards::new(0.0),
|
||||
));
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&stake_address, false, &stake_account),
|
||||
KeyedAccount::new(&rewards_address, false, &rewards_account),
|
||||
let keyed_accounts = [
|
||||
(false, false, &stake_address, &stake_account),
|
||||
(false, false, &rewards_address, &rewards_account),
|
||||
];
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Err(InstructionError::InvalidArgument),
|
||||
);
|
||||
|
||||
// Tests correct number of accounts are provided in deactivate
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::Deactivate).unwrap(),
|
||||
&mut MockInvokeContext::new(vec![])
|
||||
&[],
|
||||
),
|
||||
Err(InstructionError::NotEnoughAccountKeys),
|
||||
);
|
||||
@ -799,7 +881,7 @@ mod tests {
|
||||
initialize_checked(&stake_address, &Authorized { staker, withdrawer });
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@ -816,18 +898,17 @@ mod tests {
|
||||
let staker_account = create_default_account();
|
||||
let withdrawer_account = create_default_account();
|
||||
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&stake_address, false, &stake_account),
|
||||
KeyedAccount::new(&rent_address, false, &rent_account),
|
||||
KeyedAccount::new(&staker, false, &staker_account),
|
||||
KeyedAccount::new(&withdrawer, true, &withdrawer_account),
|
||||
let keyed_accounts: [(bool, bool, &Pubkey, &RefCell<AccountSharedData>); 4] = [
|
||||
(false, false, &stake_address, &stake_account),
|
||||
(false, false, &rent_address, &rent_account),
|
||||
(false, false, &staker, &staker_account),
|
||||
(true, false, &withdrawer, &withdrawer_account),
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::InitializeChecked).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
@ -843,7 +924,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@ -856,7 +937,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(withdrawer, false);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@ -875,37 +956,30 @@ mod tests {
|
||||
let authorized_account = create_default_account();
|
||||
let new_authorized_account = create_default_account();
|
||||
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&stake_address, false, &stake_account),
|
||||
KeyedAccount::new(&clock_address, false, &clock_account),
|
||||
KeyedAccount::new(&authorized_address, true, &authorized_account),
|
||||
KeyedAccount::new(&staker, true, &new_authorized_account),
|
||||
let mut keyed_accounts = [
|
||||
(false, false, &stake_address, &stake_account),
|
||||
(false, false, &clock_address, &clock_account),
|
||||
(true, false, &authorized_address, &authorized_account),
|
||||
(true, false, &staker, &new_authorized_account),
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::AuthorizeChecked(StakeAuthorize::Staker)).unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&stake_address, false, &stake_account),
|
||||
KeyedAccount::new(&clock_address, false, &clock_account),
|
||||
KeyedAccount::new(&authorized_address, true, &authorized_account),
|
||||
KeyedAccount::new(&withdrawer, true, &new_authorized_account),
|
||||
];
|
||||
|
||||
keyed_accounts[3] = (true, false, &withdrawer, &new_authorized_account);
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::AuthorizeChecked(
|
||||
StakeAuthorize::Withdrawer
|
||||
))
|
||||
.unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
@ -926,7 +1000,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@ -941,7 +1015,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts[3] = AccountMeta::new_readonly(staker, false);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@ -953,15 +1027,14 @@ mod tests {
|
||||
&id(),
|
||||
)
|
||||
.unwrap();
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&address_with_seed, false, &stake_account),
|
||||
KeyedAccount::new(&authorized_owner, true, &authorized_account),
|
||||
KeyedAccount::new(&clock_address, false, &clock_account),
|
||||
KeyedAccount::new(&staker, true, &new_authorized_account),
|
||||
let mut keyed_accounts = [
|
||||
(false, false, &address_with_seed, &stake_account),
|
||||
(true, false, &authorized_owner, &authorized_account),
|
||||
(false, false, &clock_address, &clock_account),
|
||||
(true, false, &staker, &new_authorized_account),
|
||||
];
|
||||
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::AuthorizeCheckedWithSeed(
|
||||
AuthorizeCheckedWithSeedArgs {
|
||||
@ -971,20 +1044,14 @@ mod tests {
|
||||
}
|
||||
))
|
||||
.unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&address_with_seed, false, &stake_account),
|
||||
KeyedAccount::new(&authorized_owner, true, &authorized_account),
|
||||
KeyedAccount::new(&clock_address, false, &clock_account),
|
||||
KeyedAccount::new(&withdrawer, true, &new_authorized_account),
|
||||
];
|
||||
|
||||
keyed_accounts[3] = (true, false, &withdrawer, &new_authorized_account);
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
process_instruction(
|
||||
&Pubkey::default(),
|
||||
&serialize(&StakeInstruction::AuthorizeCheckedWithSeed(
|
||||
AuthorizeCheckedWithSeedArgs {
|
||||
@ -994,7 +1061,7 @@ mod tests {
|
||||
}
|
||||
))
|
||||
.unwrap(),
|
||||
&mut MockInvokeContext::new(keyed_accounts)
|
||||
&keyed_accounts,
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
@ -1012,7 +1079,7 @@ mod tests {
|
||||
);
|
||||
instruction.accounts[2] = AccountMeta::new_readonly(custodian, false);
|
||||
assert_eq!(
|
||||
process_instruction(&instruction),
|
||||
process_instruction_as_one_arg(&instruction),
|
||||
Err(InstructionError::MissingRequiredSignature),
|
||||
);
|
||||
|
||||
@ -1026,13 +1093,18 @@ mod tests {
|
||||
.unwrap();
|
||||
let custodian_account = create_default_account();
|
||||
|
||||
let keyed_accounts = vec![
|
||||
KeyedAccount::new(&stake_address, false, &stake_account),
|
||||
KeyedAccount::new(&withdrawer, true, &withdrawer_account),
|
||||
KeyedAccount::new(&custodian, true, &custodian_account),
|
||||
let processor_account = RefCell::new(AccountSharedData::from(Account {
|
||||
owner: solana_sdk::native_loader::id(),
|
||||
..Account::default()
|
||||
}));
|
||||
let keyed_accounts = [
|
||||
(false, false, &id(), &processor_account),
|
||||
(false, false, &stake_address, &stake_account),
|
||||
(true, false, &withdrawer, &withdrawer_account),
|
||||
(true, false, &custodian, &custodian_account),
|
||||
];
|
||||
|
||||
let mut invoke_context = MockInvokeContext::new(keyed_accounts);
|
||||
let mut invoke_context =
|
||||
MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
|
||||
let clock = Clock::default();
|
||||
let mut data = vec![];
|
||||
bincode::serialize_into(&mut data, &clock).unwrap();
|
||||
@ -1043,12 +1115,13 @@ mod tests {
|
||||
assert_eq!(
|
||||
super::process_instruction(
|
||||
&Pubkey::default(),
|
||||
1,
|
||||
&serialize(&StakeInstruction::SetLockupChecked(LockupCheckedArgs {
|
||||
unix_timestamp: None,
|
||||
epoch: Some(1),
|
||||
}))
|
||||
.unwrap(),
|
||||
&mut invoke_context
|
||||
&mut invoke_context,
|
||||
),
|
||||
Ok(()),
|
||||
);
|
||||
|
@ -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