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:
Alexander Meißner
2021-10-08 11:41:07 +02:00
committed by GitHub
parent a6a4cfda89
commit 4e65487d2f
17 changed files with 1364 additions and 1366 deletions

View File

@ -33,6 +33,7 @@ const NOOP_PROGRAM_ID: [u8; 32] = [
#[allow(clippy::unnecessary_wraps)]
fn process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {

View File

@ -6693,6 +6693,7 @@ pub(crate) mod tests {
fn mock_process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> result::Result<(), InstructionError> {
@ -6700,11 +6701,11 @@ pub(crate) mod tests {
if let Ok(instruction) = bincode::deserialize(data) {
match instruction {
MockInstruction::Deduction => {
keyed_accounts[1]
keyed_accounts[first_instruction_account + 1]
.account
.borrow_mut()
.checked_add_lamports(1)?;
keyed_accounts[2]
keyed_accounts[first_instruction_account + 2]
.account
.borrow_mut()
.checked_sub_lamports(1)?;
@ -10340,6 +10341,7 @@ pub(crate) mod tests {
}
fn mock_vote_processor(
program_id: &Pubkey,
_first_instruction_account: usize,
_instruction_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
@ -10397,6 +10399,7 @@ pub(crate) mod tests {
fn mock_vote_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
@ -10447,6 +10450,7 @@ pub(crate) mod tests {
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
@ -11288,21 +11292,24 @@ pub(crate) mod tests {
fn mock_process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> result::Result<(), InstructionError> {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let lamports = data[0] as u64;
{
let mut to_account = keyed_accounts[1].try_account_ref_mut()?;
let mut dup_account = keyed_accounts[2].try_account_ref_mut()?;
let mut to_account =
keyed_accounts[first_instruction_account + 1].try_account_ref_mut()?;
let mut dup_account =
keyed_accounts[first_instruction_account + 2].try_account_ref_mut()?;
dup_account.checked_sub_lamports(lamports)?;
to_account.checked_add_lamports(lamports)?;
}
keyed_accounts[0]
keyed_accounts[first_instruction_account]
.try_account_ref_mut()?
.checked_sub_lamports(lamports)?;
keyed_accounts[1]
keyed_accounts[first_instruction_account + 1]
.try_account_ref_mut()?
.checked_add_lamports(lamports)?;
Ok(())
@ -11346,6 +11353,7 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> result::Result<(), InstructionError> {
@ -11532,6 +11540,7 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_ok_vote_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
@ -11782,12 +11791,18 @@ pub(crate) mod tests {
fn test_same_program_id_uses_unqiue_executable_accounts() {
fn nested_processor(
_program_id: &Pubkey,
first_instruction_account: usize,
_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> result::Result<(), InstructionError> {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
assert_eq!(42, keyed_accounts[0].lamports().unwrap());
let mut account = keyed_accounts[0].try_account_ref_mut()?;
assert_eq!(
42,
keyed_accounts[first_instruction_account]
.lamports()
.unwrap()
);
let mut account = keyed_accounts[first_instruction_account].try_account_ref_mut()?;
account.checked_add_lamports(1)?;
Ok(())
}
@ -12148,6 +12163,7 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
@ -12197,6 +12213,7 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
@ -12583,8 +12600,8 @@ pub(crate) mod tests {
impl Executor for TestExecutor {
fn execute(
&self,
_loader_id: &Pubkey,
_program_id: &Pubkey,
_first_instruction_account: usize,
_instruction_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
_use_jit: bool,
@ -13088,6 +13105,7 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), solana_sdk::instruction::InstructionError> {
@ -14585,12 +14603,13 @@ pub(crate) mod tests {
fn mock_ix_processor(
_pubkey: &Pubkey,
first_instruction_account: usize,
_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
use solana_sdk::account::WritableAccount;
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let mut data = keyed_accounts[1].try_account_ref_mut()?;
let mut data = keyed_accounts[first_instruction_account + 1].try_account_ref_mut()?;
data.data_as_mut_slice()[0] = 5;
Ok(())
}
@ -14794,6 +14813,7 @@ pub(crate) mod tests {
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {

View File

@ -14,13 +14,21 @@ use solana_frozen_abi::abi_example::AbiExample;
fn process_instruction_with_program_logging(
process_instruction: ProcessInstructionWithContext,
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
debug_assert_eq!(first_instruction_account, 1);
let logger = invoke_context.get_logger();
stable_log::program_invoke(&logger, program_id, invoke_context.invoke_depth());
let result = process_instruction(program_id, instruction_data, invoke_context);
let result = process_instruction(
program_id,
first_instruction_account,
instruction_data,
invoke_context,
);
match &result {
Ok(()) => stable_log::program_success(&logger, program_id),
@ -31,10 +39,14 @@ fn process_instruction_with_program_logging(
macro_rules! with_program_logging {
($process_instruction:expr) => {
|program_id: &Pubkey, instruction_data: &[u8], invoke_context: &mut dyn InvokeContext| {
|program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext| {
process_instruction_with_program_logging(
$process_instruction,
program_id,
first_instruction_account,
instruction_data,
invoke_context,
)
@ -82,7 +94,7 @@ impl AbiExample for Builtin {
Self {
name: String::default(),
id: Pubkey::default(),
process_instruction_with_context: |_, _, _| Ok(()),
process_instruction_with_context: |_, _, _, _| Ok(()),
}
}
}
@ -130,6 +142,7 @@ fn genesis_builtins() -> Vec<Builtin> {
/// place holder for secp256k1, remove when the precompile program is deactivated via feature activation
fn dummy_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {

View File

@ -11,7 +11,8 @@ use solana_sdk::{
compute_budget::ComputeBudget,
feature_set::{
demote_program_write_locks, do_support_realloc, neon_evm_compute_budget,
prevent_calling_precompiles_as_programs, tx_wide_compute_cap, FeatureSet,
prevent_calling_precompiles_as_programs, remove_native_loader, tx_wide_compute_cap,
FeatureSet,
},
fee_calculator::FeeCalculator,
hash::Hash,
@ -329,12 +330,14 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
.ok_or(InstructionError::CallDepth)
}
fn remove_first_keyed_account(&mut self) -> Result<(), InstructionError> {
let stack_frame = &mut self
.invoke_stack
.last_mut()
.ok_or(InstructionError::CallDepth)?;
stack_frame.keyed_accounts_range.start =
stack_frame.keyed_accounts_range.start.saturating_add(1);
if !self.is_feature_active(&remove_native_loader::id()) {
let stack_frame = &mut self
.invoke_stack
.last_mut()
.ok_or(InstructionError::CallDepth)?;
stack_frame.keyed_accounts_range.start =
stack_frame.keyed_accounts_range.start.saturating_add(1);
}
Ok(())
}
fn get_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError> {
@ -596,14 +599,18 @@ mod tests {
fn mock_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()?;
assert_eq!(*program_id, keyed_accounts[0].owner()?);
assert_eq!(
*program_id,
keyed_accounts[first_instruction_account].owner()?
);
assert_ne!(
keyed_accounts[1].owner()?,
*keyed_accounts[0].unsigned_key()
keyed_accounts[first_instruction_account + 1].owner()?,
*keyed_accounts[first_instruction_account].unsigned_key()
);
if let Ok(instruction) = bincode::deserialize(data) {
@ -611,13 +618,19 @@ mod tests {
MockInstruction::NoopSuccess => (),
MockInstruction::NoopFail => return Err(InstructionError::GenericError),
MockInstruction::ModifyOwned => {
keyed_accounts[0].try_account_ref_mut()?.data_as_mut_slice()[0] = 1
keyed_accounts[first_instruction_account]
.try_account_ref_mut()?
.data_as_mut_slice()[0] = 1
}
MockInstruction::ModifyNotOwned => {
keyed_accounts[1].try_account_ref_mut()?.data_as_mut_slice()[0] = 1
keyed_accounts[first_instruction_account + 1]
.try_account_ref_mut()?
.data_as_mut_slice()[0] = 1
}
MockInstruction::ModifyReadonly => {
keyed_accounts[2].try_account_ref_mut()?.data_as_mut_slice()[0] = 1
keyed_accounts[first_instruction_account + 2]
.try_account_ref_mut()?
.data_as_mut_slice()[0] = 1
}
}
} else {
@ -809,6 +822,7 @@ mod tests {
fn mock_system_process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
@ -817,11 +831,11 @@ mod tests {
match instruction {
MockSystemInstruction::Correct => Ok(()),
MockSystemInstruction::AttemptCredit { lamports } => {
keyed_accounts[0]
keyed_accounts[first_instruction_account]
.account
.borrow_mut()
.checked_sub_lamports(lamports)?;
keyed_accounts[1]
keyed_accounts[first_instruction_account + 1]
.account
.borrow_mut()
.checked_add_lamports(lamports)?;
@ -829,7 +843,10 @@ mod tests {
}
// Change data in a read-only account
MockSystemInstruction::AttemptDataChange { data } => {
keyed_accounts[1].account.borrow_mut().set_data(vec![data]);
keyed_accounts[first_instruction_account + 1]
.account
.borrow_mut()
.set_data(vec![data]);
Ok(())
}
}
@ -979,6 +996,7 @@ mod tests {
fn mock_system_process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
@ -986,8 +1004,10 @@ mod tests {
if let Ok(instruction) = bincode::deserialize(data) {
match instruction {
MockSystemInstruction::BorrowFail => {
let from_account = keyed_accounts[0].try_account_ref_mut()?;
let dup_account = keyed_accounts[2].try_account_ref_mut()?;
let from_account =
keyed_accounts[first_instruction_account].try_account_ref_mut()?;
let dup_account =
keyed_accounts[first_instruction_account + 2].try_account_ref_mut()?;
if from_account.lamports() != dup_account.lamports() {
return Err(InstructionError::InvalidArgument);
}
@ -995,11 +1015,13 @@ mod tests {
}
MockSystemInstruction::MultiBorrowMut => {
let from_lamports = {
let from_account = keyed_accounts[0].try_account_ref_mut()?;
let from_account =
keyed_accounts[first_instruction_account].try_account_ref_mut()?;
from_account.lamports()
};
let dup_lamports = {
let dup_account = keyed_accounts[2].try_account_ref_mut()?;
let dup_account = keyed_accounts[first_instruction_account + 2]
.try_account_ref_mut()?;
dup_account.lamports()
};
if from_lamports != dup_lamports {
@ -1009,16 +1031,18 @@ mod tests {
}
MockSystemInstruction::DoWork { lamports, data } => {
{
let mut to_account = keyed_accounts[1].try_account_ref_mut()?;
let mut dup_account = keyed_accounts[2].try_account_ref_mut()?;
let mut to_account = keyed_accounts[first_instruction_account + 1]
.try_account_ref_mut()?;
let mut dup_account = keyed_accounts[first_instruction_account + 2]
.try_account_ref_mut()?;
dup_account.checked_sub_lamports(lamports)?;
to_account.checked_add_lamports(lamports)?;
dup_account.set_data(vec![data]);
}
keyed_accounts[0]
keyed_accounts[first_instruction_account]
.try_account_ref_mut()?
.checked_sub_lamports(lamports)?;
keyed_accounts[1]
keyed_accounts[first_instruction_account + 1]
.try_account_ref_mut()?
.checked_add_lamports(lamports)?;
Ok(())
@ -1500,6 +1524,7 @@ mod tests {
let mock_program_id = Pubkey::new_unique();
fn mock_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {

View File

@ -264,6 +264,7 @@ fn transfer_with_seed(
pub fn process_instruction(
_owner: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
@ -273,16 +274,16 @@ pub fn process_instruction(
trace!("process_instruction: {:?}", instruction);
trace!("keyed_accounts: {:?}", keyed_accounts);
let signers = get_signers(keyed_accounts);
let _ = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let signers = get_signers(&keyed_accounts[first_instruction_account..]);
match instruction {
SystemInstruction::CreateAccount {
lamports,
space,
owner,
} => {
let from = keyed_account_at_index(keyed_accounts, 0)?;
let to = keyed_account_at_index(keyed_accounts, 1)?;
let from = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let to = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let to_address = Address::create(to.unsigned_key(), None, invoke_context)?;
create_account(
from,
@ -302,8 +303,8 @@ pub fn process_instruction(
space,
owner,
} => {
let from = keyed_account_at_index(keyed_accounts, 0)?;
let to = keyed_account_at_index(keyed_accounts, 1)?;
let from = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let to = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let to_address = Address::create(
to.unsigned_key(),
Some((&base, &seed, &owner)),
@ -321,14 +322,14 @@ pub fn process_instruction(
)
}
SystemInstruction::Assign { owner } => {
let keyed_account = keyed_account_at_index(keyed_accounts, 0)?;
let keyed_account = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let mut account = keyed_account.try_account_ref_mut()?;
let address = Address::create(keyed_account.unsigned_key(), None, invoke_context)?;
assign(&mut account, &address, &owner, &signers, invoke_context)
}
SystemInstruction::Transfer { lamports } => {
let from = keyed_account_at_index(keyed_accounts, 0)?;
let to = keyed_account_at_index(keyed_accounts, 1)?;
let from = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let to = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
transfer(from, to, lamports, invoke_context)
}
SystemInstruction::TransferWithSeed {
@ -336,9 +337,9 @@ pub fn process_instruction(
from_seed,
from_owner,
} => {
let from = keyed_account_at_index(keyed_accounts, 0)?;
let base = keyed_account_at_index(keyed_accounts, 1)?;
let to = keyed_account_at_index(keyed_accounts, 2)?;
let from = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let base = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let to = keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
transfer_with_seed(
from,
base,
@ -350,10 +351,10 @@ pub fn process_instruction(
)
}
SystemInstruction::AdvanceNonceAccount => {
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
#[allow(deprecated)]
if from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>(
keyed_account_at_index(keyed_accounts, 1)?,
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
)?
.is_empty()
{
@ -366,25 +367,28 @@ pub fn process_instruction(
me.advance_nonce_account(&signers, invoke_context)
}
SystemInstruction::WithdrawNonceAccount(lamports) => {
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
let to = &mut keyed_account_at_index(keyed_accounts, 1)?;
let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let to = &mut keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
#[allow(deprecated)]
let _ = from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>(
keyed_account_at_index(keyed_accounts, 2)?,
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
)?;
me.withdraw_nonce_account(
lamports,
to,
&from_keyed_account::<Rent>(keyed_account_at_index(keyed_accounts, 3)?)?,
&from_keyed_account::<Rent>(keyed_account_at_index(
keyed_accounts,
first_instruction_account + 3,
)?)?,
&signers,
invoke_context,
)
}
SystemInstruction::InitializeNonceAccount(authorized) => {
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
#[allow(deprecated)]
if from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>(
keyed_account_at_index(keyed_accounts, 1)?,
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
)?
.is_empty()
{
@ -396,16 +400,19 @@ pub fn process_instruction(
}
me.initialize_nonce_account(
&authorized,
&from_keyed_account::<Rent>(keyed_account_at_index(keyed_accounts, 2)?)?,
&from_keyed_account::<Rent>(keyed_account_at_index(
keyed_accounts,
first_instruction_account + 2,
)?)?,
invoke_context,
)
}
SystemInstruction::AuthorizeNonceAccount(nonce_authority) => {
let me = &mut keyed_account_at_index(keyed_accounts, 0)?;
let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
me.authorize_nonce_account(&nonce_authority, &signers, invoke_context)
}
SystemInstruction::Allocate { space } => {
let keyed_account = keyed_account_at_index(keyed_accounts, 0)?;
let keyed_account = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let mut account = keyed_account.try_account_ref_mut()?;
let address = Address::create(keyed_account.unsigned_key(), None, invoke_context)?;
allocate(&mut account, &address, space, &signers, invoke_context)
@ -416,7 +423,7 @@ pub fn process_instruction(
space,
owner,
} => {
let keyed_account = keyed_account_at_index(keyed_accounts, 0)?;
let keyed_account = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let mut account = keyed_account.try_account_ref_mut()?;
let address = Address::create(
keyed_account.unsigned_key(),
@ -433,7 +440,7 @@ pub fn process_instruction(
)
}
SystemInstruction::AssignWithSeed { base, seed, owner } => {
let keyed_account = keyed_account_at_index(keyed_accounts, 0)?;
let keyed_account = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let mut account = keyed_account.try_account_ref_mut()?;
let address = Address::create(
keyed_account.unsigned_key(),
@ -484,6 +491,7 @@ mod tests {
genesis_config::create_genesis_config,
hash::{hash, Hash},
instruction::{AccountMeta, Instruction, InstructionError},
keyed_account::create_keyed_accounts_unified,
message::Message,
nonce, nonce_account,
process_instruction::MockInvokeContext,
@ -506,13 +514,21 @@ mod tests {
fn process_instruction(
owner: &Pubkey,
keyed_accounts: Vec<KeyedAccount>,
instruction_data: &[u8],
keyed_accounts: &[(bool, bool, &Pubkey, &RefCell<AccountSharedData>)],
) -> Result<(), InstructionError> {
let processor_account = RefCell::new(AccountSharedData::from(Account {
owner: solana_sdk::native_loader::id(),
..Account::default()
}));
let mut keyed_accounts = keyed_accounts.to_vec();
let processor_id = Pubkey::default();
keyed_accounts.insert(0, (false, false, &processor_id, &processor_account));
super::process_instruction(
owner,
1,
instruction_data,
&mut MockInvokeContext::new(keyed_accounts),
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts)),
)
}
@ -546,16 +562,16 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&from, true, &from_account),
KeyedAccount::new(&to, true, &to_account)
],
&bincode::serialize(&SystemInstruction::CreateAccount {
lamports: 50,
space: 2,
owner: new_owner
})
.unwrap()
.unwrap(),
&[
(true, false, &from, &from_account),
(true, false, &to, &to_account),
],
),
Ok(())
);
@ -571,17 +587,12 @@ mod tests {
let from = solana_sdk::pubkey::new_rand();
let seed = "shiny pepper";
let to = Pubkey::create_with_seed(&from, seed, &new_owner).unwrap();
let from_account = AccountSharedData::new_ref(100, 0, &system_program::id());
let to_account = AccountSharedData::new_ref(0, 0, &Pubkey::default());
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&from, true, &from_account),
KeyedAccount::new(&to, false, &to_account)
],
&bincode::serialize(&SystemInstruction::CreateAccountWithSeed {
base: from,
seed: seed.to_string(),
@ -589,7 +600,11 @@ mod tests {
space: 2,
owner: new_owner
})
.unwrap()
.unwrap(),
&[
(true, false, &from, &from_account),
(false, false, &to, &to_account),
],
),
Ok(())
);
@ -606,7 +621,6 @@ mod tests {
let base = solana_sdk::pubkey::new_rand();
let seed = "shiny pepper";
let to = Pubkey::create_with_seed(&base, seed, &new_owner).unwrap();
let from_account = AccountSharedData::new_ref(100, 0, &system_program::id());
let to_account = AccountSharedData::new_ref(0, 0, &Pubkey::default());
let base_account = AccountSharedData::new_ref(0, 0, &Pubkey::default());
@ -614,11 +628,6 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&from, true, &from_account),
KeyedAccount::new(&to, false, &to_account),
KeyedAccount::new(&base, true, &base_account)
],
&bincode::serialize(&SystemInstruction::CreateAccountWithSeed {
base,
seed: seed.to_string(),
@ -626,7 +635,12 @@ mod tests {
space: 2,
owner: new_owner
})
.unwrap()
.unwrap(),
&[
(true, false, &from, &from_account),
(false, false, &to, &to_account),
(true, false, &base, &base_account),
],
),
Ok(())
);
@ -1036,7 +1050,6 @@ mod tests {
#[test]
fn test_assign() {
let new_owner = Pubkey::new(&[9; 32]);
let pubkey = solana_sdk::pubkey::new_rand();
let mut account = AccountSharedData::new(100, 0, &system_program::id());
@ -1050,6 +1063,7 @@ mod tests {
),
Err(InstructionError::MissingRequiredSignature)
);
// no change, no signature needed
assert_eq!(
assign(
@ -1066,8 +1080,8 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![KeyedAccount::new(&pubkey, true, &account)],
&bincode::serialize(&SystemInstruction::Assign { owner: new_owner }).unwrap()
&bincode::serialize(&SystemInstruction::Assign { owner: new_owner }).unwrap(),
&[(true, false, &pubkey, &account)],
),
Ok(())
);
@ -1123,18 +1137,18 @@ mod tests {
owner: solana_sdk::pubkey::new_rand(),
};
let data = serialize(&instruction).unwrap();
let result = process_instruction(&system_program::id(), vec![], &data);
let result = process_instruction(&system_program::id(), &data, &[]);
assert_eq!(result, Err(InstructionError::NotEnoughAccountKeys));
// Attempt to transfer with no destination
let from = solana_sdk::pubkey::new_rand();
let from_account = AccountSharedData::new_ref(100, 0, &system_program::id());
// Attempt to transfer with no destination
let instruction = SystemInstruction::Transfer { lamports: 0 };
let data = serialize(&instruction).unwrap();
let result = process_instruction(
&system_program::id(),
vec![KeyedAccount::new(&from, true, &from_account)],
&data,
&[(true, false, &from, &from_account)],
);
assert_eq!(result, Err(InstructionError::NotEnoughAccountKeys));
}
@ -1178,7 +1192,7 @@ mod tests {
0,
&MockInvokeContext::new(vec![]),
)
.is_ok(),);
.is_ok());
assert_eq!(from_keyed_account.account.borrow().lamports(), 50);
assert_eq!(to_keyed_account.account.borrow().lamports(), 51);
@ -1252,7 +1266,7 @@ mod tests {
0,
&MockInvokeContext::new(vec![]),
)
.is_ok(),);
.is_ok());
assert_eq!(from_keyed_account.account.borrow().lamports(), 50);
assert_eq!(to_keyed_account.account.borrow().lamports(), 51);
}
@ -1487,9 +1501,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();
process_instruction(&Pubkey::default(), keyed_accounts, &instruction.data)
process_instruction(&Pubkey::default(), &instruction.data, &keyed_accounts)
}
}
@ -1509,8 +1523,8 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![],
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap()
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
&[],
),
Err(InstructionError::NotEnoughAccountKeys),
);
@ -1521,12 +1535,8 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![KeyedAccount::new(
&Pubkey::default(),
true,
&create_default_account(),
)],
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
&[(true, false, &Pubkey::default(), &create_default_account())],
),
Err(InstructionError::NotEnoughAccountKeys),
);
@ -1537,16 +1547,17 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &create_default_account()),
KeyedAccount::new(
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
&[
(true, false, &Pubkey::default(), &create_default_account()),
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_account(),
),
],
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
),
Err(InstructionError::InvalidArgument),
);
@ -1557,17 +1568,23 @@ mod tests {
let nonce_acc = nonce_account::create_account(1_000_000);
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &nonce_acc),
KeyedAccount::new(
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[
(true, false, &Pubkey::default(), &nonce_acc),
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_recent_blockhashes_account(),
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_rent_account()),
(
false,
false,
&sysvar::rent::id(),
&create_default_rent_account(),
),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
)
.unwrap();
let blockhash = &hash(&serialize(&0).unwrap());
@ -1584,14 +1601,22 @@ mod tests {
let owner = Pubkey::default();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
let mut invoke_context = &mut MockInvokeContext::new(vec![
KeyedAccount::new(&owner, true, &nonce_acc),
KeyedAccount::new(&blockhash_id, false, &new_recent_blockhashes_account),
]);
let processor_account = RefCell::new(AccountSharedData::from(Account {
owner: solana_sdk::native_loader::id(),
..Account::default()
}));
let keyed_accounts = [
(false, false, &Pubkey::default(), &processor_account),
(true, false, &owner, &nonce_acc),
(false, false, &blockhash_id, &new_recent_blockhashes_account),
];
let mut invoke_context =
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
invoke_context.blockhash = *blockhash;
assert_eq!(
super::process_instruction(
&Pubkey::default(),
1,
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
invoke_context,
),
@ -1617,8 +1642,8 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![],
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
&[],
),
Err(InstructionError::NotEnoughAccountKeys),
);
@ -1629,12 +1654,8 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![KeyedAccount::new(
&Pubkey::default(),
true,
&create_default_account()
)],
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
&[(true, false, &Pubkey::default(), &create_default_account())],
),
Err(InstructionError::NotEnoughAccountKeys),
);
@ -1645,17 +1666,18 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &create_default_account()),
KeyedAccount::new(&Pubkey::default(), false, &create_default_account()),
KeyedAccount::new(
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
&[
(true, false, &Pubkey::default(), &create_default_account()),
(false, false, &Pubkey::default(), &create_default_account()),
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_account()
),
],
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
),
Err(InstructionError::InvalidArgument),
);
@ -1666,22 +1688,24 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(
&Pubkey::default(),
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
&[
(
true,
false,
&Pubkey::default(),
&nonce_account::create_account(1_000_000),
),
KeyedAccount::new(&Pubkey::default(), true, &create_default_account()),
KeyedAccount::new(
(true, false, &Pubkey::default(), &create_default_account()),
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_recent_blockhashes_account(),
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_account()),
(false, false, &sysvar::rent::id(), &create_default_account()),
],
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
),
Err(InstructionError::InvalidArgument),
);
@ -1692,22 +1716,29 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(
&Pubkey::default(),
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
&[
(
true,
false,
&Pubkey::default(),
&nonce_account::create_account(1_000_000),
),
KeyedAccount::new(&Pubkey::default(), true, &create_default_account()),
KeyedAccount::new(
(true, false, &Pubkey::default(), &create_default_account()),
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_recent_blockhashes_account(),
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_rent_account()),
(
false,
false,
&sysvar::rent::id(),
&create_default_rent_account()
),
],
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
),
Ok(()),
);
@ -1718,8 +1749,8 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[],
),
Err(InstructionError::NotEnoughAccountKeys),
);
@ -1730,12 +1761,13 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![KeyedAccount::new(
&Pubkey::default(),
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[(
true,
false,
&Pubkey::default(),
&nonce_account::create_account(1_000_000),
)],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
),
Err(InstructionError::NotEnoughAccountKeys),
);
@ -1746,20 +1778,22 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(
&Pubkey::default(),
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[
(
true,
false,
&Pubkey::default(),
&nonce_account::create_account(1_000_000),
),
KeyedAccount::new(
(
true,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_account()
),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
),
Err(InstructionError::InvalidArgument),
);
@ -1770,21 +1804,23 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(
&Pubkey::default(),
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[
(
true,
false,
&Pubkey::default(),
&nonce_account::create_account(1_000_000),
),
KeyedAccount::new(
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_recent_blockhashes_account(),
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_account()),
(false, false, &sysvar::rent::id(), &create_default_account()),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
),
Err(InstructionError::InvalidArgument),
);
@ -1795,21 +1831,28 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(
&Pubkey::default(),
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[
(
true,
false,
&Pubkey::default(),
&nonce_account::create_account(1_000_000),
),
KeyedAccount::new(
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_recent_blockhashes_account(),
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_rent_account()),
(
false,
false,
&sysvar::rent::id(),
&create_default_rent_account()
),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
),
Ok(()),
);
@ -1820,24 +1863,30 @@ mod tests {
let nonce_acc = nonce_account::create_account(1_000_000);
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &nonce_acc),
KeyedAccount::new(
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[
(true, false, &Pubkey::default(), &nonce_acc),
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_recent_blockhashes_account(),
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_rent_account()),
(
false,
false,
&sysvar::rent::id(),
&create_default_rent_account(),
),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
)
.unwrap();
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![KeyedAccount::new(&Pubkey::default(), true, &nonce_acc,),],
&serialize(&SystemInstruction::AuthorizeNonceAccount(Pubkey::default(),)).unwrap(),
&serialize(&SystemInstruction::AuthorizeNonceAccount(Pubkey::default())).unwrap(),
&[(true, false, &Pubkey::default(), &nonce_acc)],
),
Ok(()),
);
@ -1920,17 +1969,23 @@ mod tests {
assert_eq!(
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &nonce_acc),
KeyedAccount::new(
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[
(true, false, &Pubkey::default(), &nonce_acc),
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&new_recent_blockhashes_account,
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_rent_account()),
(
false,
false,
&sysvar::rent::id(),
&create_default_rent_account()
),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
),
Err(NonceError::NoRecentBlockhashes.into())
);
@ -1941,17 +1996,23 @@ mod tests {
let nonce_acc = nonce_account::create_account(1_000_000);
process_instruction(
&Pubkey::default(),
vec![
KeyedAccount::new(&Pubkey::default(), true, &nonce_acc),
KeyedAccount::new(
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
&[
(true, false, &Pubkey::default(), &nonce_acc),
(
false,
false,
#[allow(deprecated)]
&sysvar::recent_blockhashes::id(),
false,
&create_default_recent_blockhashes_account(),
),
KeyedAccount::new(&sysvar::rent::id(), false, &create_default_rent_account()),
(
false,
false,
&sysvar::rent::id(),
&create_default_rent_account(),
),
],
&serialize(&SystemInstruction::InitializeNonceAccount(Pubkey::default())).unwrap(),
)
.unwrap();
let blockhash = &hash(&serialize(&0).unwrap());
@ -1964,14 +2025,22 @@ mod tests {
let owner = Pubkey::default();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
let mut invoke_context = &mut MockInvokeContext::new(vec![
KeyedAccount::new(&owner, true, &nonce_acc),
KeyedAccount::new(&blockhash_id, false, &new_recent_blockhashes_account),
]);
let processor_account = RefCell::new(AccountSharedData::from(Account {
owner: solana_sdk::native_loader::id(),
..Account::default()
}));
let keyed_accounts = [
(false, false, &Pubkey::default(), &processor_account),
(true, false, &owner, &nonce_acc),
(false, false, &blockhash_id, &new_recent_blockhashes_account),
];
let mut invoke_context =
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
invoke_context.blockhash = *blockhash;
assert_eq!(
super::process_instruction(
&Pubkey::default(),
1,
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
invoke_context,
),