Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ pub trait InvokeContext {
fn remove_first_keyed_account(&mut self) -> Result<(), InstructionError>;
/// Get the list of keyed accounts
fn get_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError>;
/// Get the list of keyed accounts skipping `first_instruction_account` many entries
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intent is to also include the instruction program keyed account in this list, can you update the comment to say so? The comment right now indicates that this will return a list of instruction account inputs (excluding the program account)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it includes the program_id (at index 0).
And good point, I will update the comment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It would be really helpful if the comment made it clearer what the breakdown of invoke context keyed accounts is and when it's appropriate to use get_instruction_keyed_accounts over get_keyed_accounts.

fn get_instruction_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError>;
/// Get this invocation's LogCollector
fn get_log_collector(&self) -> Option<Rc<RefCell<LogCollector>>>;
/// Get this invocation's ComputeMeter
Expand Down Expand Up @@ -621,7 +623,7 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
let message = Message::new(&[instruction.clone()], None);

// Gather keyed_accounts in the order of message.account_keys
let caller_keyed_accounts = self.get_keyed_accounts()?;
let caller_keyed_accounts = self.get_instruction_keyed_accounts()?;
let callee_keyed_accounts = message
.account_keys
.iter()
Expand Down Expand Up @@ -815,6 +817,17 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
.map(|frame| &frame.keyed_accounts[frame.keyed_accounts_range.clone()])
.ok_or(InstructionError::CallDepth)
}
fn get_instruction_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError> {
let frame = self
.invoke_stack
.last()
.ok_or(InstructionError::CallDepth)?;
let first_instruction_account = frame
.number_of_program_accounts
.checked_sub(1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lichtso why do you subtract 1 here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the number_of_program_accounts - 1 is first_instruction_account.
Or in other words number_of_program_accounts is first_instruction_account + 1.

first_instruction_account is actually the index of the program_id of that instruction, see:

.get(self.number_of_program_accounts.saturating_sub(1))

.ok_or(InstructionError::CallDepth)?;
Ok(&frame.keyed_accounts[first_instruction_account..])
}
fn get_log_collector(&self) -> Option<Rc<RefCell<LogCollector>>> {
self.log_collector.clone()
}
Expand Down
2 changes: 1 addition & 1 deletion programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2003,7 +2003,7 @@ where
let demote_program_write_locks =
invoke_context.is_feature_active(&demote_program_write_locks::id());
let keyed_accounts = invoke_context
.get_keyed_accounts()
.get_instruction_keyed_accounts()
.map_err(SyscallError::InstructionError)?;
let mut account_indices = Vec::with_capacity(message.account_keys.len());
let mut accounts = Vec::with_capacity(message.account_keys.len());
Expand Down