Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Unifies create_keyed_is_signer_accounts() and create_keyed_readonly_a…
Browse files Browse the repository at this point in the history
…ccounts() into create_keyed_accounts_unified().

Throws instead CallDepth of GenericError at invoke_stack.last().ok_or().
Renames program => executable.
  • Loading branch information
Lichtso committed Apr 16, 2021
1 parent cf55d21 commit 41aec06
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 98 deletions.
8 changes: 3 additions & 5 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ pub fn create_executor(
return Err(InstructionError::ProgramFailedToCompile);
}
}
Ok(Arc::new(BpfExecutor {
program: executable,
}))
Ok(Arc::new(BpfExecutor { executable }))
}

fn write_program_data(
Expand Down Expand Up @@ -725,7 +723,7 @@ impl InstructionMeter for ThisInstructionMeter {

/// BPF Loader's Executor implementation
pub struct BpfExecutor {
program: Box<dyn Executable<BpfError, ThisInstructionMeter>>,
executable: Box<dyn Executable<BpfError, ThisInstructionMeter>>,
}

// Well, implement Debug for solana_rbpf::vm::Executable in solana-rbpf...
Expand Down Expand Up @@ -760,7 +758,7 @@ impl Executor for BpfExecutor {
let compute_meter = invoke_context.get_compute_meter();
let mut vm = match create_vm(
loader_id,
self.program.as_ref(),
self.executable.as_ref(),
&mut parameter_bytes,
invoke_context,
) {
Expand Down
98 changes: 49 additions & 49 deletions programs/config/src/config_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod tests {
use serde_derive::{Deserialize, Serialize};
use solana_sdk::{
account::{Account, AccountSharedData},
keyed_account::create_keyed_is_signer_accounts,
keyed_account::create_keyed_accounts_unified,
process_instruction::MockInvokeContext,
signature::{Keypair, Signer},
system_instruction::SystemInstruction,
Expand Down Expand Up @@ -188,8 +188,8 @@ mod tests {
owner: id(),
..Account::default()
}));
let accounts = vec![(&config_pubkey, true, &config_account)];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let accounts = vec![(true, false, &config_pubkey, &config_account)];
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand Down Expand Up @@ -222,8 +222,8 @@ mod tests {
let my_config = MyConfig::new(42);

let instruction = config_instruction::store(&config_pubkey, true, keys, &my_config);
let accounts = vec![(&config_pubkey, true, &config_account)];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let accounts = vec![(true, false, &config_pubkey, &config_account)];
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -248,8 +248,8 @@ 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![(&config_pubkey, true, &config_account)];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let accounts = vec![(true, false, &config_pubkey, &config_account)];
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -270,8 +270,8 @@ 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![(&config_pubkey, false, &config_account)];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let accounts = vec![(false, false, &config_pubkey, &config_account)];
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand Down Expand Up @@ -301,11 +301,11 @@ mod tests {
let signer0_account = RefCell::new(AccountSharedData::default());
let signer1_account = RefCell::new(AccountSharedData::default());
let accounts = vec![
(&config_pubkey, true, &config_account),
(&signer0_pubkey, true, &signer0_account),
(&signer1_pubkey, true, &signer1_account),
(true, false, &config_pubkey, &config_account),
(true, false, &signer0_pubkey, &signer0_account),
(true, false, &signer1_pubkey, &signer1_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand Down Expand Up @@ -337,8 +337,8 @@ mod tests {
owner: id(),
..Account::default()
}));
let accounts = vec![(&signer0_pubkey, true, &signer0_account)];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let accounts = vec![(true, false, &signer0_pubkey, &signer0_account)];
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -365,10 +365,10 @@ mod tests {

// Config-data pubkey doesn't match signer
let accounts = vec![
(&config_pubkey, true, &config_account),
(&signer1_pubkey, true, &signer1_account),
(true, false, &config_pubkey, &config_account),
(true, false, &signer1_pubkey, &signer1_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -380,10 +380,10 @@ mod tests {

// Config-data pubkey not a signer
let accounts = vec![
(&config_pubkey, true, &config_account),
(&signer0_pubkey, false, &signer0_account),
(true, false, &config_pubkey, &config_account),
(false, false, &signer0_pubkey, &signer0_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand Down Expand Up @@ -415,11 +415,11 @@ mod tests {

let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
let accounts = vec![
(&config_pubkey, true, &config_account),
(&signer0_pubkey, true, &signer0_account),
(&signer1_pubkey, true, &signer1_account),
(true, false, &config_pubkey, &config_account),
(true, false, &signer0_pubkey, &signer0_account),
(true, false, &signer1_pubkey, &signer1_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -434,11 +434,11 @@ mod tests {
let instruction =
config_instruction::store(&config_pubkey, false, keys.clone(), &new_config);
let accounts = vec![
(&config_pubkey, false, &config_account),
(&signer0_pubkey, true, &signer0_account),
(&signer1_pubkey, true, &signer1_account),
(false, false, &config_pubkey, &config_account),
(true, false, &signer0_pubkey, &signer0_account),
(true, false, &signer1_pubkey, &signer1_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -459,11 +459,11 @@ mod tests {
let keys = vec![(pubkey, false), (signer0_pubkey, true)];
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
let accounts = vec![
(&config_pubkey, false, &config_account),
(&signer0_pubkey, true, &signer0_account),
(&signer1_pubkey, false, &signer1_account),
(false, false, &config_pubkey, &config_account),
(true, false, &signer0_pubkey, &signer0_account),
(false, false, &signer1_pubkey, &signer1_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -481,11 +481,11 @@ mod tests {
];
let instruction = config_instruction::store(&config_pubkey, false, keys, &my_config);
let accounts = vec![
(&config_pubkey, false, &config_account),
(&signer0_pubkey, true, &signer0_account),
(&signer2_pubkey, true, &signer2_account),
(false, false, &config_pubkey, &config_account),
(true, false, &signer0_pubkey, &signer0_account),
(true, false, &signer2_pubkey, &signer2_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand Down Expand Up @@ -519,10 +519,10 @@ mod tests {

let instruction = config_instruction::store(&config_pubkey, true, keys.clone(), &my_config);
let accounts = vec![
(&config_pubkey, true, &config_account),
(&signer0_pubkey, true, &signer0_account),
(true, false, &config_pubkey, &config_account),
(true, false, &signer0_pubkey, &signer0_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -537,10 +537,10 @@ mod tests {
let instruction =
config_instruction::store(&config_pubkey, true, keys.clone(), &new_config);
let accounts = vec![
(&config_pubkey, true, &config_account),
(&signer0_pubkey, true, &signer0_account),
(true, false, &config_pubkey, &config_account),
(true, false, &signer0_pubkey, &signer0_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -560,8 +560,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![(&config_pubkey, true, &config_account)];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let accounts = vec![(true, false, &config_pubkey, &config_account)];
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -579,7 +579,7 @@ mod tests {
let instructions =
config_instruction::create_account::<MyConfig>(&from_pubkey, &config_pubkey, 1, vec![]);
let accounts = vec![];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand All @@ -606,10 +606,10 @@ mod tests {

let instruction = config_instruction::store(&config_pubkey, true, keys, &new_config);
let accounts = vec![
(&config_pubkey, true, &config_account),
(&signer0_pubkey, true, &signer0_account),
(true, false, &config_pubkey, &config_account),
(true, false, &signer0_pubkey, &signer0_account),
];
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
let keyed_accounts = create_keyed_accounts_unified(&accounts);
assert_eq!(
process_instruction(
&id(),
Expand Down
31 changes: 17 additions & 14 deletions runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use solana_sdk::{
},
ic_msg,
instruction::{CompiledInstruction, Instruction, InstructionError},
keyed_account::{keyed_account_at_index, KeyedAccount},
keyed_account::{create_keyed_accounts_unified, keyed_account_at_index, KeyedAccount},
message::Message,
native_loader,
process_instruction::{
Expand Down Expand Up @@ -318,15 +318,18 @@ impl<'a> ThisInvokeContext<'a> {
};
invoke_context
.invoke_stack
.push(InvokeContextStackFrame::new(*program_id, &keyed_accounts));
.push(InvokeContextStackFrame::new(
*program_id,
create_keyed_accounts_unified(&keyed_accounts),
));
invoke_context
}
}
impl<'a> InvokeContext for ThisInvokeContext<'a> {
fn push(
&mut self,
key: &Pubkey,
keyed_accounts: &[(&Pubkey, bool, bool, &RefCell<AccountSharedData>)],
keyed_accounts: &[(bool, bool, &Pubkey, &RefCell<AccountSharedData>)],
) -> Result<(), InstructionError> {
if self.invoke_stack.len() > self.bpf_compute_budget.max_invoke_depth {
return Err(InstructionError::CallDepth);
Expand All @@ -340,7 +343,7 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
// with the ones already existing in self, so that the lifetime 'a matches.
let keyed_accounts = keyed_accounts
.iter()
.map(|(search_key, is_signer, is_writable, account)| {
.map(|(is_signer, is_writable, search_key, account)| {
self.account_deps
.iter()
.map(|(key, _account)| key)
Expand All @@ -349,17 +352,17 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
.map(|mut index| {
if index < self.account_deps.len() {
(
&self.account_deps[index].0,
*is_signer,
*is_writable,
&self.account_deps[index].0,
&self.account_deps[index].1 as &RefCell<AccountSharedData>,
)
} else {
index = index.saturating_sub(self.account_deps.len());
(
&self.message.account_keys[index],
*is_signer,
*is_writable,
&self.message.account_keys[index],
// TODO
// Currently we are constructing new accounts on the stack
// before calling MessageProcessor::process_cross_program_instruction
Expand All @@ -374,7 +377,7 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
.ok_or(InstructionError::InvalidArgument)?;
self.invoke_stack.push(InvokeContextStackFrame::new(
*key,
keyed_accounts.as_slice(),
create_keyed_accounts_unified(keyed_accounts.as_slice()),
));
Ok(())
}
Expand All @@ -394,7 +397,7 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
let stack_frame = self
.invoke_stack
.last()
.ok_or(InstructionError::GenericError)?;
.ok_or(InstructionError::CallDepth)?;
MessageProcessor::verify_and_update(
message,
instruction,
Expand All @@ -411,13 +414,13 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
self.invoke_stack
.last()
.map(|frame| &frame.key)
.ok_or(InstructionError::GenericError)
.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::GenericError)?;
.ok_or(InstructionError::CallDepth)?;
stack_frame.keyed_accounts_range.start =
stack_frame.keyed_accounts_range.start.saturating_add(1);
Ok(())
Expand All @@ -426,7 +429,7 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
self.invoke_stack
.last()
.map(|frame| &frame.keyed_accounts[frame.keyed_accounts_range.clone()])
.ok_or(InstructionError::GenericError)
.ok_or(InstructionError::CallDepth)
}
fn get_programs(&self) -> &[(Pubkey, ProcessInstructionWithContext)] {
self.programs
Expand Down Expand Up @@ -632,16 +635,16 @@ impl MessageProcessor {
executable_accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
accounts: &'a [Rc<RefCell<AccountSharedData>>],
demote_sysvar_write_locks: bool,
) -> Vec<(&'a Pubkey, bool, bool, &'a RefCell<AccountSharedData>)> {
) -> Vec<(bool, bool, &'a Pubkey, &'a RefCell<AccountSharedData>)> {
executable_accounts
.iter()
.map(|(key, account)| (key, false, false, account as &RefCell<AccountSharedData>))
.map(|(key, account)| (false, false, key, account as &RefCell<AccountSharedData>))
.chain(instruction.accounts.iter().map(|index| {
let index = *index as usize;
(
&message.account_keys[index],
message.is_signer(index),
message.is_writable(index, demote_sysvar_write_locks),
&message.account_keys[index],
&accounts[index] as &RefCell<AccountSharedData>,
)
}))
Expand Down
Loading

0 comments on commit 41aec06

Please sign in to comment.