Skip to content

Commit 86a80a7

Browse files
remove 1-indexed instruction accounts
1 parent caca5c3 commit 86a80a7

File tree

3 files changed

+13
-42
lines changed

3 files changed

+13
-42
lines changed

src/flamenco/runtime/context/fd_exec_instr_ctx.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,8 @@ fd_exec_instr_ctx_try_borrow_instr_account( fd_exec_instr_ctx_t const * ctx,
126126

127127
fd_txn_account_t * instr_account = ctx->instr->accounts[idx];
128128

129-
/* Calculate the index_in_instruction by adding the number of program accounts to the instruction account index.
130-
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L654-L655 */
131-
ulong idx_in_instr = fd_ulong_sat_add(idx, fd_exec_instr_ctx_get_number_of_program_accounts( ctx ) );
132-
133129
return fd_exec_instr_ctx_try_borrow_account( ctx,
134-
idx_in_instr,
130+
idx,
135131
instr_account,
136132
account );
137133
}
@@ -161,8 +157,10 @@ fd_exec_instr_ctx_try_borrow_last_program_account( fd_exec_instr_ctx_t const * c
161157
&program_account,
162158
NULL );
163159

160+
/* The index_in_instruction for a borrowed program account is invalid,
161+
so it is set to a sentinel value of USHORT_MAX. */
164162
return fd_exec_instr_ctx_try_borrow_account( ctx,
165-
0UL,
163+
USHORT_MAX,
166164
program_account,
167165
account );
168166
}

src/flamenco/runtime/context/fd_exec_instr_ctx.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fd_exec_instr_ctx_find_idx_of_instr_account( fd_exec_instr_ctx_t const * ctx,
9393

9494
/* Mirrors Agave function solana_sdk::transaction_context::InstructionContext::get_number_of_program_accounts.
9595
96-
Strictly returns 1, as we only support one program account per instruction for now.
96+
Strictly returns 1, as we only support one program account per instruction.
9797
9898
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L480-L482 */
9999

src/flamenco/runtime/fd_borrowed_account.h

+8-35
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ struct fd_borrowed_account {
2020
fd_txn_account_t * acct;
2121
fd_exec_instr_ctx_t const * instr_ctx;
2222

23-
/* Agave's index_in_instruction includes program and instruction accounts,
24-
with the program account occupying index 0,
25-
so the value for instruction accounts will be off by one. */
23+
/* index_in_instruction will be USHORT_MAX for borrowed program accounts because
24+
they are not stored in the list of instruction accounts in the instruction context */
2625
ushort index_in_instruction;
2726
};
2827
typedef struct fd_borrowed_account fd_borrowed_account_t;
@@ -294,7 +293,7 @@ fd_borrowed_account_is_executable_internal( fd_borrowed_account_t const * borrow
294293
fd_borrowed_account_is_executable( borrowed_acct );
295294
}
296295

297-
/* fd_borrowed_account_is_signer mirror the Agave functions
296+
/* fd_borrowed_account_is_signer mirrors the Agave function
298297
solana_sdk::transaction_context::BorrowedAccount::is_signer.
299298
Returns 1 if the account is a signer or is writable and 0 otherwise.
300299
@@ -305,27 +304,14 @@ fd_borrowed_account_is_signer( fd_borrowed_account_t const * borrowed_acct ) {
305304
fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx;
306305
fd_instr_info_t const * instr = instr_ctx->instr;
307306

308-
/* The index in instruction is 1-indexed for instruction accounts, with the program account
309-
occupying index 0. */
310-
if( FD_UNLIKELY( borrowed_acct->index_in_instruction > instr_ctx->instr->acct_cnt ) ) {
307+
if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) {
311308
return 0;
312309
}
313310

314-
/* Check if account is a program account
315-
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1053 */
316-
if( FD_UNLIKELY( borrowed_acct->index_in_instruction <
317-
fd_exec_instr_ctx_get_number_of_program_accounts( instr_ctx ) ) ) {
318-
return 0;
319-
}
320-
321-
/* Converts the index_in_instruction
322-
into an instruction account index by subtracting the number of program accounts.
323-
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1045-L1046 */
324-
ulong idx = fd_ulong_sat_sub( (ulong)borrowed_acct->index_in_instruction, fd_exec_instr_ctx_get_number_of_program_accounts( instr_ctx ) );
325-
return fd_instr_acc_is_signer_idx( instr, idx );
311+
return fd_instr_acc_is_signer_idx( instr, borrowed_acct->index_in_instruction );
326312
}
327313

328-
/* fd_borrowed_account_is_writer mirror the Agave functions
314+
/* fd_borrowed_account_is_writer mirrors the Agave function
329315
solana_sdk::transaction_context::BorrowedAccount::is_writer.
330316
Returns 1 if the account is a signer or is writable and 0 otherwise.
331317
@@ -336,24 +322,11 @@ fd_borrowed_account_is_writable( fd_borrowed_account_t const * borrowed_acct ) {
336322
fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx;
337323
fd_instr_info_t const * instr = instr_ctx->instr;
338324

339-
/* The index in instruction is 1-indexed for instruction accounts, with the program account
340-
occupying index 0. */
341-
if( FD_UNLIKELY( borrowed_acct->index_in_instruction > instr_ctx->instr->acct_cnt ) ) {
342-
return 0;
343-
}
344-
345-
/* Check if account is a program account
346-
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1053 */
347-
if( FD_UNLIKELY( borrowed_acct->index_in_instruction <
348-
fd_exec_instr_ctx_get_number_of_program_accounts( instr_ctx ) ) ) {
325+
if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) {
349326
return 0;
350327
}
351328

352-
/* Converts the index_in_instruction
353-
into an instruction account index by subtracting the number of program accounts.
354-
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1058-L1059 */
355-
ulong idx = fd_ulong_sat_sub( (ulong)borrowed_acct->index_in_instruction, fd_exec_instr_ctx_get_number_of_program_accounts( instr_ctx ) );
356-
return fd_instr_acc_is_writable_idx( instr, idx );
329+
return fd_instr_acc_is_writable_idx( instr, borrowed_acct->index_in_instruction );
357330
}
358331

359332
/* fd_borrowed_account_is_owned_by_current_program mirrors Agave's

0 commit comments

Comments
 (0)