Skip to content

Commit

Permalink
Cleanup: get_program_key() and get_loader_key() in TransactionContext (
Browse files Browse the repository at this point in the history
…#23191)

* Moves TransactionContext::get_program_key() to InstructionContext::get_program_key().

* Removes TransactionContext::get_loader_key().

* Test full program and loader executable account chain in BPF loader.
  • Loading branch information
Lichtso authored Feb 17, 2022
1 parent 619335d commit da00b39
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 361 deletions.
13 changes: 8 additions & 5 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,12 @@ impl<'a> InvokeContext<'a> {
) -> Result<(), InstructionError> {
let do_support_realloc = self.feature_set.is_active(&do_support_realloc::id());
let cap_accounts_data_len = self.feature_set.is_active(&cap_accounts_data_len::id());
let program_id = self
let instruction_context = self
.transaction_context
.get_program_key()
.get_current_instruction_context()
.map_err(|_| InstructionError::CallDepth)?;
let program_id = instruction_context
.get_program_key(self.transaction_context)
.map_err(|_| InstructionError::CallDepth)?;

// Verify all executable accounts have zero outstanding refs
Expand Down Expand Up @@ -530,8 +533,8 @@ impl<'a> InvokeContext<'a> {
let cap_accounts_data_len = self.feature_set.is_active(&cap_accounts_data_len::id());
let transaction_context = &self.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let program_id = transaction_context
.get_program_key()
let program_id = instruction_context
.get_program_key(transaction_context)
.map_err(|_| InstructionError::CallDepth)?;

// Verify the per-account instruction results
Expand Down Expand Up @@ -1268,7 +1271,7 @@ mod tests {
) -> Result<(), InstructionError> {
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let program_id = transaction_context.get_program_key()?;
let program_id = instruction_context.get_program_key(transaction_context)?;
assert_eq!(
program_id,
instruction_context
Expand Down
4 changes: 3 additions & 1 deletion program-runtime/src/native_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ impl NativeLoader {
invoke_context: &mut InvokeContext,
) -> Result<(), InstructionError> {
let (program_id, name_vec) = {
let program_id = invoke_context.transaction_context.get_program_key()?;
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let program_id = instruction_context.get_program_key(transaction_context)?;
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
if native_loader::id() != *program_id {
Expand Down
26 changes: 15 additions & 11 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn builtin_process_instruction(
..instruction_context.get_number_of_accounts();

let log_collector = invoke_context.get_log_collector();
let program_id = transaction_context.get_program_key()?;
let program_id = instruction_context.get_program_key(transaction_context)?;
stable_log::program_invoke(
&log_collector,
program_id,
Expand Down Expand Up @@ -250,10 +250,12 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
) -> ProgramResult {
let invoke_context = get_invoke_context();
let log_collector = invoke_context.get_log_collector();

let caller = *invoke_context
.transaction_context
.get_program_key()
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context
.get_current_instruction_context()
.unwrap();
let caller = instruction_context
.get_program_key(transaction_context)
.unwrap();

stable_log::program_invoke(
Expand All @@ -264,7 +266,7 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {

let signers = signers_seeds
.iter()
.map(|seeds| Pubkey::create_program_address(seeds, &caller).unwrap())
.map(|seeds| Pubkey::create_program_address(seeds, caller).unwrap())
.collect::<Vec<_>>();
let (instruction_accounts, program_indices) = invoke_context
.prepare_instruction(instruction, &signers)
Expand Down Expand Up @@ -374,12 +376,14 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {

fn sol_set_return_data(&self, data: &[u8]) {
let invoke_context = get_invoke_context();
let caller = *invoke_context
.transaction_context
.get_program_key()
let transaction_context = &mut invoke_context.transaction_context;
let instruction_context = transaction_context
.get_current_instruction_context()
.unwrap();
invoke_context
.transaction_context
let caller = *instruction_context
.get_program_key(transaction_context)
.unwrap();
transaction_context
.set_return_data(caller, data.to_vec())
.unwrap();
}
Expand Down
11 changes: 5 additions & 6 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,12 @@ fn run_program(name: &str) -> u64 {
let mut instruction_count = 0;
let mut tracer = None;
for i in 0..2 {
invoke_context
.transaction_context
let transaction_context = &mut invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context().unwrap();
let caller = *instruction_context.get_program_key(transaction_context).unwrap();
transaction_context
.set_return_data(
*invoke_context
.transaction_context
.get_program_key()
.unwrap(),
caller,
Vec::new(),
)
.unwrap();
Expand Down
32 changes: 20 additions & 12 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ fn process_instruction_common(
use_jit: bool,
) -> Result<(), InstructionError> {
let log_collector = invoke_context.get_log_collector();
let program_id = invoke_context.transaction_context.get_program_key()?;
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let program_id = instruction_context.get_program_key(transaction_context)?;

let keyed_accounts = invoke_context.get_keyed_accounts()?;
let first_account = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
Expand Down Expand Up @@ -350,7 +352,9 @@ fn process_instruction_common(
use_jit,
false,
)?;
let program_id = invoke_context.transaction_context.get_program_key()?;
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let program_id = instruction_context.get_program_key(transaction_context)?;
invoke_context.add_executor(program_id, executor.clone());
executor
}
Expand Down Expand Up @@ -397,7 +401,9 @@ fn process_loader_upgradeable_instruction(
use_jit: bool,
) -> Result<(), InstructionError> {
let log_collector = invoke_context.get_log_collector();
let program_id = invoke_context.transaction_context.get_program_key()?;
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let program_id = instruction_context.get_program_key(transaction_context)?;
let keyed_accounts = invoke_context.get_keyed_accounts()?;

match limited_deserialize(instruction_data)? {
Expand Down Expand Up @@ -550,7 +556,9 @@ fn process_loader_upgradeable_instruction(
.accounts
.push(AccountMeta::new(*buffer.unsigned_key(), false));

let caller_program_id = invoke_context.transaction_context.get_program_key()?;
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let caller_program_id = instruction_context.get_program_key(transaction_context)?;
let signers = [&[new_program_id.as_ref(), &[bump_seed]]]
.iter()
.map(|seeds| Pubkey::create_program_address(*seeds, caller_program_id))
Expand Down Expand Up @@ -947,7 +955,9 @@ fn process_loader_instruction(
invoke_context: &mut InvokeContext,
use_jit: bool,
) -> Result<(), InstructionError> {
let program_id = invoke_context.transaction_context.get_program_key()?;
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let program_id = instruction_context.get_program_key(transaction_context)?;
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
if program.owner()? != *program_id {
Expand Down Expand Up @@ -1036,15 +1046,13 @@ impl Executor for BpfExecutor {
let log_collector = invoke_context.get_log_collector();
let compute_meter = invoke_context.get_compute_meter();
let stack_height = invoke_context.get_stack_height();
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
let program_id = *instruction_context.get_program_key(transaction_context)?;

let mut serialize_time = Measure::start("serialize");
let program_id = *invoke_context.transaction_context.get_program_key()?;
let (mut parameter_bytes, account_lengths) = serialize_parameters(
invoke_context.transaction_context,
invoke_context
.transaction_context
.get_current_instruction_context()?,
)?;
let (mut parameter_bytes, account_lengths) =
serialize_parameters(invoke_context.transaction_context, instruction_context)?;
serialize_time.stop();
let mut create_vm_time = Measure::start("create_vm");
let mut execute_time;
Expand Down
Loading

0 comments on commit da00b39

Please sign in to comment.