Skip to content

Remove index_in_caller from InstructionAccount#7220

Merged
LucasSte merged 5 commits intoanza-xyz:masterfrom
LucasSte:caller-idx-1
Jul 30, 2025
Merged

Remove index_in_caller from InstructionAccount#7220
LucasSte merged 5 commits intoanza-xyz:masterfrom
LucasSte:caller-idx-1

Conversation

@LucasSte
Copy link
Copy Markdown

Problem

According to SIMD-0177, the struct InstructionAccount is supposed to be shared with programs. It should not contain the index_in_caller member, since it is irrelevant to programs and unused in ABIv2.

That parameter is only used in CPI.

Summary of Changes

  1. Unify index_in_transaction from BorrowedAccount to the meaning used in InstructionContext to avoid confusion.
  2. Calculate index_in_caller on the fly from index_in_transaction.
  3. Remove index_in_caller from InstructionAccount.

@mergify
Copy link
Copy Markdown

mergify Bot commented Jul 28, 2025

The Firedancer team maintains a line-for-line reimplementation of the
native programs, and until native programs are moved to BPF, those
implementations must exactly match their Agave counterparts.
If this PR represents a change to a native program implementation (not
tests), please include a reviewer from the Firedancer team. And please
keep refactors to a minimum.

}

/// Get the index of account in instruction from the index in transaction
pub fn get_index_of_account_in_instruction(
Copy link
Copy Markdown
Author

@LucasSte LucasSte Jul 28, 2025

Choose a reason for hiding this comment

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

This reverse search is O(n) on the number of instruction accounts. It is called at most twice during a CPI. Once in prepare_next_instruction to verify account permissions, and once in translate_and_update_accounts. The number of accounts is low, so the performance impact should not be representative. We are also avoiding the pub key comparisons, which was the previous method we used to find the index_in_transaction.

I still need to remove index_in_calle from InstructionAccounts, and that gave me an idea. When we are building the InstructionAccounts vector we create the index_in_transaction <-> index_in_callee map. Instead of discarding this map, we could store it in InstructionContext to detect if an account is duplicated and retrieve the index_in_instruction from index_in_transaction.

Although it may waste some space, since not all the 256 entries will be occupied, lookups in a vector are faster than those in a hash map. Also, consider that it has only 256 bytes, so impact in memory should be low.

PS: That is for a next PR to avoid cluttering this one.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jul 28, 2025

Codecov Report

❌ Patch coverage is 72.41379% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.1%. Comparing base (c5b2d3a) to head (16d1842).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7220   +/-   ##
=======================================
  Coverage    83.1%    83.1%           
=======================================
  Files         849      849           
  Lines      369954   369935   -19     
=======================================
- Hits       307735   307728    -7     
+ Misses      62219    62207   -12     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucasSte
Copy link
Copy Markdown
Author

I split the changes in three commits for easier review.

@LucasSte LucasSte requested a review from Lichtso July 28, 2025 21:34
@LucasSte LucasSte marked this pull request as ready for review July 28, 2025 21:34
@LucasSte LucasSte requested a review from a team as a code owner July 28, 2025 21:34
Comment thread transaction-context/src/lib.rs Outdated
}

/// Retrieves an instruction account using the index in transaction
pub fn try_borrow_instruction_account_with_transaction_index<'a, 'b: 'a>(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would say we inline this at its three callsites. That way it is clear where the expensive get_index_of_account_in_instruction() happens.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 16d1842

Comment thread transaction-context/src/lib.rs Outdated
instruction_context: &'a InstructionContext,
index_in_transaction: IndexOfAccount,
index_in_instruction: IndexOfAccount,
// Program accounts are not part of the instruction_accounts vector
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

and thus None

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in cbc0c0b

@LucasSte LucasSte requested a review from Lichtso July 29, 2025 17:56
@LucasSte LucasSte merged commit 2c8930d into anza-xyz:master Jul 30, 2025
41 checks passed
@LucasSte LucasSte deleted the caller-idx-1 branch July 30, 2025 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants