Skip to content

Commit

Permalink
Adds IndexOfAccount type (solana-labs#27599)
Browse files Browse the repository at this point in the history
Adds the type `IndexOfAccount`.
  • Loading branch information
Lichtso authored Sep 6, 2022
1 parent 6f2e556 commit 12d2147
Show file tree
Hide file tree
Showing 26 changed files with 295 additions and 227 deletions.
62 changes: 35 additions & 27 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use {
pubkey::Pubkey,
rent::Rent,
saturating_add_assign,
transaction_context::{InstructionAccount, TransactionAccount, TransactionContext},
transaction_context::{
IndexOfAccount, InstructionAccount, TransactionAccount, TransactionContext,
},
},
std::{
alloc::Layout,
Expand All @@ -34,7 +36,7 @@ use {
};

pub type ProcessInstructionWithContext =
fn(usize, &mut InvokeContext) -> Result<(), InstructionError>;
fn(IndexOfAccount, &mut InvokeContext) -> Result<(), InstructionError>;

#[derive(Clone)]
pub struct BuiltinProgram {
Expand All @@ -46,7 +48,7 @@ impl std::fmt::Debug for BuiltinProgram {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// These are just type aliases for work around of Debug-ing above pointers
type ErasedProcessInstructionWithContext =
fn(usize, &'static mut InvokeContext<'static>) -> Result<(), InstructionError>;
fn(IndexOfAccount, &'static mut InvokeContext<'static>) -> Result<(), InstructionError>;

// rustc doesn't compile due to bug without this work around
// https://github.com/rust-lang/rust/issues/50280
Expand All @@ -61,7 +63,7 @@ pub trait Executor: Debug + Send + Sync {
/// Execute the program
fn execute(
&self,
first_instruction_account: usize,
first_instruction_account: IndexOfAccount,
invoke_context: &mut InvokeContext,
) -> Result<(), InstructionError>;
}
Expand Down Expand Up @@ -292,8 +294,9 @@ impl<'a> InvokeContext<'a> {
.feature_set
.is_active(&enable_early_verification_of_account_modifications::id())
{
self.pre_accounts =
Vec::with_capacity(instruction_context.get_number_of_instruction_accounts());
self.pre_accounts = Vec::with_capacity(
instruction_context.get_number_of_instruction_accounts() as usize,
);
for instruction_account_index in
0..instruction_context.get_number_of_instruction_accounts()
{
Expand Down Expand Up @@ -374,7 +377,7 @@ impl<'a> InvokeContext<'a> {
fn verify(
&mut self,
instruction_accounts: &[InstructionAccount],
program_indices: &[usize],
program_indices: &[IndexOfAccount],
) -> Result<(), InstructionError> {
let instruction_context = self
.transaction_context
Expand All @@ -398,7 +401,7 @@ impl<'a> InvokeContext<'a> {
for (instruction_account_index, instruction_account) in
instruction_accounts.iter().enumerate()
{
if instruction_account_index != instruction_account.index_in_callee {
if instruction_account_index as IndexOfAccount != instruction_account.index_in_callee {
continue; // Skip duplicate account
}
{
Expand Down Expand Up @@ -477,7 +480,7 @@ impl<'a> InvokeContext<'a> {
for (instruction_account_index, instruction_account) in
instruction_accounts.iter().enumerate()
{
if instruction_account_index != instruction_account.index_in_callee {
if instruction_account_index as IndexOfAccount != instruction_account.index_in_callee {
continue; // Skip duplicate account
}
if instruction_account.index_in_transaction
Expand Down Expand Up @@ -575,7 +578,7 @@ impl<'a> InvokeContext<'a> {
&mut self,
instruction: &Instruction,
signers: &[Pubkey],
) -> Result<(Vec<InstructionAccount>, Vec<usize>), InstructionError> {
) -> Result<(Vec<InstructionAccount>, Vec<IndexOfAccount>), InstructionError> {
// Finds the index of each account in the instruction by its pubkey.
// Then normalizes / unifies the privileges of duplicate accounts.
// Note: This is an O(n^2) algorithm,
Expand Down Expand Up @@ -626,7 +629,7 @@ impl<'a> InvokeContext<'a> {
deduplicated_instruction_accounts.push(InstructionAccount {
index_in_transaction,
index_in_caller,
index_in_callee: instruction_account_index,
index_in_callee: instruction_account_index as IndexOfAccount,
is_signer: account_meta.is_signer,
is_writable: account_meta.is_writable,
});
Expand Down Expand Up @@ -723,7 +726,7 @@ impl<'a> InvokeContext<'a> {
&mut self,
instruction_data: &[u8],
instruction_accounts: &[InstructionAccount],
program_indices: &[usize],
program_indices: &[IndexOfAccount],
compute_units_consumed: &mut u64,
timings: &mut ExecuteTimings,
) -> Result<(), InstructionError> {
Expand Down Expand Up @@ -960,23 +963,24 @@ pub struct MockInvokeContextPreparation {
pub fn prepare_mock_invoke_context(
transaction_accounts: Vec<TransactionAccount>,
instruction_account_metas: Vec<AccountMeta>,
_program_indices: &[usize],
_program_indices: &[IndexOfAccount],
) -> MockInvokeContextPreparation {
let mut instruction_accounts: Vec<InstructionAccount> =
Vec::with_capacity(instruction_account_metas.len());
for (instruction_account_index, account_meta) in instruction_account_metas.iter().enumerate() {
let index_in_transaction = transaction_accounts
.iter()
.position(|(key, _account)| *key == account_meta.pubkey)
.unwrap_or(transaction_accounts.len());
.unwrap_or(transaction_accounts.len())
as IndexOfAccount;
let index_in_callee = instruction_accounts
.get(0..instruction_account_index)
.unwrap()
.iter()
.position(|instruction_account| {
instruction_account.index_in_transaction == index_in_transaction
})
.unwrap_or(instruction_account_index);
.unwrap_or(instruction_account_index) as IndexOfAccount;
instruction_accounts.push(InstructionAccount {
index_in_transaction,
index_in_caller: index_in_transaction,
Expand Down Expand Up @@ -1037,7 +1041,7 @@ pub fn with_mock_invoke_context<R, F: FnMut(&mut InvokeContext) -> R>(

pub fn mock_process_instruction(
loader_id: &Pubkey,
mut program_indices: Vec<usize>,
mut program_indices: Vec<IndexOfAccount>,
instruction_data: &[u8],
transaction_accounts: Vec<TransactionAccount>,
instruction_accounts: Vec<AccountMeta>,
Expand All @@ -1046,7 +1050,7 @@ pub fn mock_process_instruction(
expected_result: Result<(), InstructionError>,
process_instruction: ProcessInstructionWithContext,
) -> Vec<AccountSharedData> {
program_indices.insert(0, transaction_accounts.len());
program_indices.insert(0, transaction_accounts.len() as IndexOfAccount);
let mut preparation =
prepare_mock_invoke_context(transaction_accounts, instruction_accounts, &program_indices);
let processor_account = AccountSharedData::new(0, 0, &native_loader::id());
Expand Down Expand Up @@ -1117,14 +1121,14 @@ mod tests {
fn test_program_entry_debug() {
#[allow(clippy::unnecessary_wraps)]
fn mock_process_instruction(
_first_instruction_account: usize,
_first_instruction_account: IndexOfAccount,
_invoke_context: &mut InvokeContext,
) -> Result<(), InstructionError> {
Ok(())
}
#[allow(clippy::unnecessary_wraps)]
fn mock_ix_processor(
_first_instruction_account: usize,
_first_instruction_account: IndexOfAccount,
_invoke_context: &mut InvokeContext,
) -> Result<(), InstructionError> {
Ok(())
Expand All @@ -1144,7 +1148,7 @@ mod tests {

#[allow(clippy::integer_arithmetic)]
fn mock_process_instruction(
_first_instruction_account: usize,
_first_instruction_account: IndexOfAccount,
invoke_context: &mut InvokeContext,
) -> Result<(), InstructionError> {
let transaction_context = &invoke_context.transaction_context;
Expand Down Expand Up @@ -1256,9 +1260,9 @@ mod tests {
AccountSharedData::new(index as u64, 1, invoke_stack.get(index).unwrap()),
));
instruction_accounts.push(InstructionAccount {
index_in_transaction: index,
index_in_caller: index,
index_in_callee: instruction_accounts.len(),
index_in_transaction: index as IndexOfAccount,
index_in_caller: index as IndexOfAccount,
index_in_callee: instruction_accounts.len() as IndexOfAccount,
is_signer: false,
is_writable: true,
});
Expand All @@ -1269,9 +1273,9 @@ mod tests {
AccountSharedData::new(1, 1, &solana_sdk::pubkey::Pubkey::default()),
));
instruction_accounts.push(InstructionAccount {
index_in_transaction: index,
index_in_caller: index,
index_in_callee: index,
index_in_transaction: index as IndexOfAccount,
index_in_caller: index as IndexOfAccount,
index_in_callee: index as IndexOfAccount,
is_signer: false,
is_writable: false,
});
Expand All @@ -1291,7 +1295,11 @@ mod tests {
.transaction_context
.get_next_instruction_context()
.unwrap()
.configure(&[MAX_DEPTH + depth_reached], &instruction_accounts, &[]);
.configure(
&[(MAX_DEPTH + depth_reached) as IndexOfAccount],
&instruction_accounts,
&[],
);
if Err(InstructionError::CallDepth) == invoke_context.push() {
break;
}
Expand Down
14 changes: 7 additions & 7 deletions program-runtime/src/sysvar_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
clock::Clock, epoch_schedule::EpochSchedule, rent::Rent, slot_hashes::SlotHashes,
stake_history::StakeHistory, Sysvar, SysvarId,
},
transaction_context::{InstructionContext, TransactionContext},
transaction_context::{IndexOfAccount, InstructionContext, TransactionContext},
},
std::sync::Arc,
};
Expand Down Expand Up @@ -183,7 +183,7 @@ pub mod get_sysvar_with_account_check {
fn check_sysvar_account<S: Sysvar>(
transaction_context: &TransactionContext,
instruction_context: &InstructionContext,
instruction_account_index: usize,
instruction_account_index: IndexOfAccount,
) -> Result<(), InstructionError> {
let index_in_transaction = instruction_context
.get_index_of_instruction_account_in_transaction(instruction_account_index)?;
Expand All @@ -196,7 +196,7 @@ pub mod get_sysvar_with_account_check {
pub fn clock(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
instruction_account_index: usize,
instruction_account_index: IndexOfAccount,
) -> Result<Arc<Clock>, InstructionError> {
check_sysvar_account::<Clock>(
invoke_context.transaction_context,
Expand All @@ -209,7 +209,7 @@ pub mod get_sysvar_with_account_check {
pub fn rent(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
instruction_account_index: usize,
instruction_account_index: IndexOfAccount,
) -> Result<Arc<Rent>, InstructionError> {
check_sysvar_account::<Rent>(
invoke_context.transaction_context,
Expand All @@ -222,7 +222,7 @@ pub mod get_sysvar_with_account_check {
pub fn slot_hashes(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
instruction_account_index: usize,
instruction_account_index: IndexOfAccount,
) -> Result<Arc<SlotHashes>, InstructionError> {
check_sysvar_account::<SlotHashes>(
invoke_context.transaction_context,
Expand All @@ -236,7 +236,7 @@ pub mod get_sysvar_with_account_check {
pub fn recent_blockhashes(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
instruction_account_index: usize,
instruction_account_index: IndexOfAccount,
) -> Result<Arc<RecentBlockhashes>, InstructionError> {
check_sysvar_account::<RecentBlockhashes>(
invoke_context.transaction_context,
Expand All @@ -249,7 +249,7 @@ pub mod get_sysvar_with_account_check {
pub fn stake_history(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
instruction_account_index: usize,
instruction_account_index: IndexOfAccount,
) -> Result<Arc<StakeHistory>, InstructionError> {
check_sysvar_account::<StakeHistory>(
invoke_context.transaction_context,
Expand Down
7 changes: 4 additions & 3 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use {
pub use {
solana_banks_client::{BanksClient, BanksClientError},
solana_program_runtime::invoke_context::InvokeContext,
solana_sdk::transaction_context::IndexOfAccount,
};

pub mod programs;
Expand Down Expand Up @@ -94,7 +95,7 @@ fn get_invoke_context<'a, 'b>() -> &'a mut InvokeContext<'b> {

pub fn builtin_process_instruction(
process_instruction: solana_sdk::entrypoint::ProcessInstruction,
_first_instruction_account: usize,
_first_instruction_account: IndexOfAccount,
invoke_context: &mut InvokeContext,
) -> Result<(), InstructionError> {
set_invoke_context(invoke_context);
Expand All @@ -113,7 +114,7 @@ pub fn builtin_process_instruction(
);

// Copy indices_in_instruction into a HashSet to ensure there are no duplicates
let deduplicated_indices: HashSet<usize> = instruction_account_indices.collect();
let deduplicated_indices: HashSet<IndexOfAccount> = instruction_account_indices.collect();

// Serialize entrypoint parameters with BPF ABI
let (mut parameter_bytes, _account_lengths) = serialize_parameters(
Expand Down Expand Up @@ -177,7 +178,7 @@ pub fn builtin_process_instruction(
macro_rules! processor {
($process_instruction:expr) => {
Some(
|first_instruction_account: usize,
|first_instruction_account: $crate::IndexOfAccount,
invoke_context: &mut solana_program_test::InvokeContext| {
$crate::builtin_process_instruction(
$process_instruction,
Expand Down
3 changes: 2 additions & 1 deletion programs/address-lookup-table/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use {
program_utils::limited_deserialize,
pubkey::{Pubkey, PUBKEY_BYTES},
system_instruction,
transaction_context::IndexOfAccount,
},
std::convert::TryFrom,
};

pub fn process_instruction(
_first_instruction_account: usize,
_first_instruction_account: IndexOfAccount,
invoke_context: &mut InvokeContext,
) -> Result<(), InstructionError> {
let transaction_context = &invoke_context.transaction_context;
Expand Down
6 changes: 3 additions & 3 deletions programs/bpf_loader/benches/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
account::{Account, AccountSharedData},
bpf_loader,
sysvar::rent::Rent,
transaction_context::{InstructionAccount, TransactionContext},
transaction_context::{IndexOfAccount, InstructionAccount, TransactionContext},
},
test::Bencher,
};
Expand Down Expand Up @@ -94,9 +94,9 @@ fn create_inputs() -> TransactionContext {
.enumerate()
.map(
|(instruction_account_index, index_in_transaction)| InstructionAccount {
index_in_caller: instruction_account_index,
index_in_caller: instruction_account_index as IndexOfAccount,
index_in_transaction,
index_in_callee: instruction_account_index,
index_in_callee: instruction_account_index as IndexOfAccount,
is_signer: false,
is_writable: instruction_account_index >= 4,
},
Expand Down
Loading

0 comments on commit 12d2147

Please sign in to comment.