Skip to content

Commit

Permalink
Removes "executable_accounts",
Browse files Browse the repository at this point in the history
now referenced by transaction wide index into "accounts".
  • Loading branch information
Lichtso committed Sep 6, 2021
1 parent c8006c9 commit f115e3b
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 222 deletions.
31 changes: 9 additions & 22 deletions program-runtime/src/instruction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ impl InstructionProcessor {

let (
message,
executable_accounts,
program_indices,
accounts,
keyed_account_indices_reordered,
caller_write_privileges,
Expand Down Expand Up @@ -526,20 +526,16 @@ impl InstructionProcessor {
);
return Err(InstructionError::AccountNotExecutable);
}
let mut executable_accounts = vec![];
let mut program_indices = vec![];
if program_account.borrow().owner() == &bpf_loader_upgradeable::id() {
if let UpgradeableLoaderState::Program {
programdata_address,
} = program_account.borrow().state()?
{
if let Some((programdata_account_index, programdata_account)) =
if let Some((programdata_account_index, _programdata_account)) =
invoke_context.get_account(&programdata_address)
{
executable_accounts.push((
programdata_address,
programdata_account,
programdata_account_index,
));
program_indices.push(programdata_account_index);
} else {
ic_msg!(
invoke_context,
Expand All @@ -557,13 +553,10 @@ impl InstructionProcessor {
return Err(InstructionError::MissingAccount);
}
}
executable_accounts.insert(
0,
(callee_program_id, program_account, program_account_index),
);
program_indices.insert(0, program_account_index);
(
message,
executable_accounts,
program_indices,
accounts,
keyed_account_indices_reordered,
caller_write_privileges,
Expand All @@ -573,7 +566,7 @@ impl InstructionProcessor {
#[allow(clippy::deref_addrof)]
InstructionProcessor::process_cross_program_instruction(
&message,
&executable_accounts,
&program_indices,
&accounts,
&caller_write_privileges,
*(&mut *(invoke_context.borrow_mut())),
Expand Down Expand Up @@ -627,7 +620,7 @@ impl InstructionProcessor {
/// This method calls the instruction's program entrypoint function
pub fn process_cross_program_instruction(
message: &Message,
executable_accounts: &[(Pubkey, Rc<RefCell<AccountSharedData>>, usize)],
program_indices: &[usize],
accounts: &[(Pubkey, Rc<RefCell<AccountSharedData>>)],
caller_write_privileges: &[bool],
invoke_context: &mut dyn InvokeContext,
Expand All @@ -639,13 +632,7 @@ impl InstructionProcessor {
invoke_context.verify_and_update(instruction, accounts, caller_write_privileges)?;

// Invoke callee
invoke_context.push(
program_id,
message,
instruction,
executable_accounts,
accounts,
)?;
invoke_context.push(program_id, message, instruction, program_indices, accounts)?;

let mut instruction_processor = InstructionProcessor::default();
for (program_id, process_instruction) in invoke_context.get_programs().iter() {
Expand Down
8 changes: 2 additions & 6 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,7 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
let (program_account_index, found_program_account) =
invoke_context.get_account(&program_id).unwrap();
assert_eq!(program_account, *(found_program_account.borrow()));
let executables = vec![(
program_id,
Rc::new(RefCell::new(program_account)),
program_account_index,
)];
let program_indices = vec![program_account_index];

// Check Signers
for account_info in account_infos {
Expand Down Expand Up @@ -335,7 +331,7 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {

InstructionProcessor::process_cross_program_instruction(
&message,
&executables,
&program_indices,
&accounts,
&caller_privileges,
invoke_context,
Expand Down
26 changes: 11 additions & 15 deletions programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2253,20 +2253,16 @@ fn get_upgradeable_executable(
callee_program_id: &Pubkey,
program_account: &Rc<RefCell<AccountSharedData>>,
invoke_context: &Ref<&mut dyn InvokeContext>,
) -> Result<Option<(Pubkey, Rc<RefCell<AccountSharedData>>, usize)>, EbpfError<BpfError>> {
) -> Result<Option<usize>, EbpfError<BpfError>> {
if program_account.borrow().owner() == &bpf_loader_upgradeable::id() {
match program_account.borrow().state() {
Ok(UpgradeableLoaderState::Program {
programdata_address,
}) => {
if let Some((programdata_account_index, account)) =
if let Some((programdata_account_index, _programdata_account)) =
invoke_context.get_account(&programdata_address)
{
Ok(Some((
programdata_address,
account,
programdata_account_index,
)))
Ok(Some(programdata_account_index))
} else {
ic_msg!(
invoke_context,
Expand Down Expand Up @@ -2302,7 +2298,7 @@ fn call<'a>(
) -> Result<u64, EbpfError<BpfError>> {
let (
message,
executables,
program_indices,
accounts,
account_refs,
caller_write_privileges,
Expand Down Expand Up @@ -2391,11 +2387,11 @@ fn call<'a>(
)?;
assert_eq!(program_account, found_program_account);

let programdata_executable =
get_upgradeable_executable(&callee_program_id, &program_account, &invoke_context)?;
let mut executables = vec![(callee_program_id, program_account, program_account_index)];
if let Some(programdata_executable) = programdata_executable {
executables.push(programdata_executable);
let mut program_indices = vec![program_account_index];
if let Some(programdata_account_index) =
get_upgradeable_executable(&callee_program_id, &program_account, &invoke_context)?
{
program_indices.push(programdata_account_index);
}

// Record the instruction
Expand All @@ -2404,7 +2400,7 @@ fn call<'a>(

(
message,
executables,
program_indices,
accounts,
account_refs,
caller_write_privileges,
Expand All @@ -2417,7 +2413,7 @@ fn call<'a>(
#[allow(clippy::deref_addrof)]
match InstructionProcessor::process_cross_program_instruction(
&message,
&executables,
&program_indices,
&accounts,
&caller_write_privileges,
*(&mut *(syscall.get_context_mut()?)),
Expand Down
110 changes: 51 additions & 59 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ pub struct Accounts {
// for the load instructions
pub type TransactionAccounts = Vec<(Pubkey, AccountSharedData)>;
pub type TransactionRent = u64;
pub type TransactionLoaders = Vec<Vec<(Pubkey, AccountSharedData, usize)>>;
pub type TransactionProgramIndices = Vec<Vec<usize>>;
#[derive(PartialEq, Debug, Clone)]
pub struct LoadedTransaction {
pub accounts: TransactionAccounts,
pub loaders: TransactionLoaders,
pub program_indices: TransactionProgramIndices,
pub rent: TransactionRent,
pub rent_debits: RentDebits,
}
Expand Down Expand Up @@ -351,26 +351,21 @@ impl Accounts {
.map_err(|_| TransactionError::InsufficientFundsForFee)?;

let message = tx.message();
let loaders = message
let program_indices = message
.program_instructions_iter()
.map(|(program_id, _ix)| {
self.load_executable_accounts(ancestors, program_id, error_counters)
.map(|loaders| {
.map(|programs| {
let base_index = accounts.len();
accounts.append(&mut loaders.clone());
loaders
.into_iter()
.enumerate()
.map(|(index, (key, account))| {
(key, account, base_index + index)
})
accounts.append(&mut programs.clone());
(base_index..base_index + programs.len())
.collect::<Vec<_>>()
})
})
.collect::<Result<TransactionLoaders>>()?;
.collect::<Result<Vec<Vec<usize>>>>()?;
Ok(LoadedTransaction {
accounts,
loaders,
program_indices,
rent: tx_rent,
rent_debits,
})
Expand Down Expand Up @@ -1462,8 +1457,8 @@ mod tests {
(Ok(loaded_transaction), _nonce_rollback) => {
assert_eq!(loaded_transaction.accounts.len(), 3);
assert_eq!(loaded_transaction.accounts[0].1, accounts[0].1);
assert_eq!(loaded_transaction.loaders.len(), 1);
assert_eq!(loaded_transaction.loaders[0].len(), 0);
assert_eq!(loaded_transaction.program_indices.len(), 1);
assert_eq!(loaded_transaction.program_indices[0].len(), 0);
}
(Err(e), _nonce_rollback) => Err(e).unwrap(),
}
Expand Down Expand Up @@ -1650,14 +1645,20 @@ mod tests {
(Ok(loaded_transaction), _nonce_rollback) => {
assert_eq!(loaded_transaction.accounts.len(), 6);
assert_eq!(loaded_transaction.accounts[0].1, accounts[0].1);
assert_eq!(loaded_transaction.loaders.len(), 2);
assert_eq!(loaded_transaction.loaders[0].len(), 1);
assert_eq!(loaded_transaction.loaders[1].len(), 2);
for loaders in loaded_transaction.loaders.iter() {
for (i, accounts_subset) in loaders.iter().enumerate() {
assert_eq!(loaded_transaction.program_indices.len(), 2);
assert_eq!(loaded_transaction.program_indices[0].len(), 1);
assert_eq!(loaded_transaction.program_indices[1].len(), 2);
for program_indices in loaded_transaction.program_indices.iter() {
for (i, program_index) in program_indices.iter().enumerate() {
// +1 to skip first not loader account
assert_eq!(accounts_subset.0, accounts[i + 1].0);
assert_eq!(accounts_subset.1, accounts[i + 1].1);
assert_eq!(
loaded_transaction.accounts[*program_index].0,
accounts[i + 1].0
);
assert_eq!(
loaded_transaction.accounts[*program_index].1,
accounts[i + 1].1
);
}
}
}
Expand Down Expand Up @@ -2036,27 +2037,21 @@ mod tests {
];
let tx1 = new_sanitized_tx(&[&keypair1], message, Hash::default());

let loaders = vec![(Ok(()), None), (Ok(()), None)];

let transaction_loaders0 = vec![];
let transaction_rent0 = 0;
let loaded0 = (
Ok(LoadedTransaction {
accounts: transaction_accounts0,
loaders: transaction_loaders0,
rent: transaction_rent0,
program_indices: vec![],
rent: 0,
rent_debits: RentDebits::default(),
}),
None,
);

let transaction_loaders1 = vec![];
let transaction_rent1 = 0;
let loaded1 = (
Ok(LoadedTransaction {
accounts: transaction_accounts1,
loaders: transaction_loaders1,
rent: transaction_rent1,
program_indices: vec![],
rent: 0,
rent_debits: RentDebits::default(),
}),
None,
Expand All @@ -2079,9 +2074,10 @@ mod tests {
.insert_new_readonly(&pubkey);
}
let txs = vec![tx0, tx1];
let programs = vec![(Ok(()), None), (Ok(()), None)];
let collected_accounts = accounts.collect_accounts_to_store(
&txs,
&loaders,
&programs,
loaded.as_mut_slice(),
&rent_collector,
&(Hash::default(), FeeCalculator::default()),
Expand Down Expand Up @@ -2428,24 +2424,15 @@ mod tests {
nonce_account_pre.clone(),
Some(from_account_pre.clone()),
));
let loaders = vec![(
Err(TransactionError::InstructionError(
1,
InstructionError::InvalidArgument,
)),
nonce_rollback.clone(),
)];

let transaction_loaders = vec![];
let transaction_rent = 0;
let loaded = (
Ok(LoadedTransaction {
accounts: transaction_accounts,
loaders: transaction_loaders,
rent: transaction_rent,
program_indices: vec![],
rent: 0,
rent_debits: RentDebits::default(),
}),
nonce_rollback,
nonce_rollback.clone(),
);

let mut loaded = vec![loaded];
Expand All @@ -2459,9 +2446,16 @@ mod tests {
AccountShrinkThreshold::default(),
);
let txs = vec![tx];
let programs = vec![(
Err(TransactionError::InstructionError(
1,
InstructionError::InvalidArgument,
)),
nonce_rollback,
)];
let collected_accounts = accounts.collect_accounts_to_store(
&txs,
&loaders,
&programs,
loaded.as_mut_slice(),
&rent_collector,
&(next_blockhash, FeeCalculator::default()),
Expand Down Expand Up @@ -2546,24 +2540,15 @@ mod tests {
nonce_account_pre.clone(),
None,
));
let loaders = vec![(
Err(TransactionError::InstructionError(
1,
InstructionError::InvalidArgument,
)),
nonce_rollback.clone(),
)];

let transaction_loaders = vec![];
let transaction_rent = 0;
let loaded = (
Ok(LoadedTransaction {
accounts: transaction_accounts,
loaders: transaction_loaders,
rent: transaction_rent,
program_indices: vec![],
rent: 0,
rent_debits: RentDebits::default(),
}),
nonce_rollback,
nonce_rollback.clone(),
);

let mut loaded = vec![loaded];
Expand All @@ -2577,9 +2562,16 @@ mod tests {
AccountShrinkThreshold::default(),
);
let txs = vec![tx];
let programs = vec![(
Err(TransactionError::InstructionError(
1,
InstructionError::InvalidArgument,
)),
nonce_rollback,
)];
let collected_accounts = accounts.collect_accounts_to_store(
&txs,
&loaders,
&programs,
loaded.as_mut_slice(),
&rent_collector,
&(next_blockhash, FeeCalculator::default()),
Expand Down
Loading

0 comments on commit f115e3b

Please sign in to comment.