@@ -81,54 +81,86 @@ fd_exec_instr_ctx_delete( void * mem ) {
81
81
return mem ;
82
82
}
83
83
84
+ int
85
+ fd_exec_instr_ctx_find_idx_of_instr_account ( fd_exec_instr_ctx_t const * ctx ,
86
+ fd_pubkey_t const * pubkey ) {
87
+ for ( int i = 0 ; i < ctx -> instr -> acct_cnt ; i ++ ) {
88
+ if ( memcmp ( pubkey -> uc , ctx -> instr -> acct_pubkeys [i ].uc , sizeof (fd_pubkey_t ) )== 0 ) {
89
+ return i ;
90
+ }
91
+ }
92
+ return -1 ;
93
+ }
94
+
84
95
int
85
96
fd_exec_instr_ctx_try_borrow_account ( fd_exec_instr_ctx_t const * ctx ,
86
97
ulong idx ,
98
+ fd_txn_account_t * txn_account ,
87
99
fd_borrowed_account_t * account ) {
88
100
/* TODO this is slightly wrong. Agave returns NotEnoughAccountKeys when the account index is
89
101
out of bounds in the transaction context and MissingAccount when the account index is out of
90
102
bounds in the instruction context. */
91
103
92
- /* Return a NotEnoughAccountKeys error if the idx is out of bounds.
93
- https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L603 */
94
- if ( FD_UNLIKELY ( idx >= ctx -> instr -> acct_cnt ) ) {
95
- return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS ;
96
- }
97
-
98
- fd_txn_account_t * instr_account = ctx -> instr -> accounts [idx ];
99
-
100
104
/* Return an AccountBorrowFailed error if the write is not acquirable.
101
105
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L605 */
102
- int acquire_result = fd_txn_account_acquire_write ( instr_account );
106
+ int acquire_result = fd_txn_account_acquire_write ( txn_account );
103
107
if ( FD_UNLIKELY ( !acquire_result ) ) {
104
108
return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED ;
105
109
}
106
110
107
111
/* Create a BorrowedAccount upon success.
108
112
https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L606 */
109
- fd_borrowed_account_init ( account , instr_account , ctx , (int )idx );
113
+ fd_borrowed_account_init ( account , txn_account , ctx , (ushort )idx );
110
114
return FD_EXECUTOR_INSTR_SUCCESS ;
111
115
}
112
116
113
117
int
114
- fd_exec_instr_ctx_try_borrow_account_with_key ( fd_exec_instr_ctx_t * ctx ,
115
- fd_pubkey_t const * pubkey ,
116
- fd_borrowed_account_t * account ) {
117
- for ( ulong i = 0 ; i < ctx -> instr -> acct_cnt ; i ++ ) {
118
- if ( memcmp ( pubkey -> uc , ctx -> instr -> acct_pubkeys [ i ]. uc , sizeof ( fd_pubkey_t ) ) == 0 ) {
119
- return fd_exec_instr_ctx_try_borrow_account ( ctx , i , account );
120
- }
118
+ fd_exec_instr_ctx_try_borrow_instr_account ( fd_exec_instr_ctx_t const * ctx ,
119
+ ulong idx ,
120
+ fd_borrowed_account_t * account ) {
121
+ /* Return a NotEnoughAccountKeys error if the idx is out of bounds.
122
+ https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L603 */
123
+ if ( FD_UNLIKELY ( idx >= ctx -> instr -> acct_cnt ) ) {
124
+ return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS ;
121
125
}
122
- return FD_EXECUTOR_INSTR_ERR_MISSING_ACC ;
126
+
127
+ fd_txn_account_t * instr_account = ctx -> instr -> accounts [idx ];
128
+
129
+ return fd_exec_instr_ctx_try_borrow_account ( ctx ,
130
+ idx ,
131
+ instr_account ,
132
+ account );
123
133
}
124
134
125
135
int
126
- fd_exec_instr_ctx_find_idx_of_instr_account ( fd_exec_instr_ctx_t const * ctx ,
127
- fd_pubkey_t const * pubkey ) {
128
- for ( int i = 0 ; i < ctx -> instr -> acct_cnt ; i ++ ) {
136
+ fd_exec_instr_ctx_try_borrow_instr_account_with_key ( fd_exec_instr_ctx_t const * ctx ,
137
+ fd_pubkey_t const * pubkey ,
138
+ fd_borrowed_account_t * account ) {
139
+ for ( ulong i = 0 ; i < ctx -> instr -> acct_cnt ; i ++ ) {
129
140
if ( memcmp ( pubkey -> uc , ctx -> instr -> acct_pubkeys [i ].uc , sizeof (fd_pubkey_t ) )== 0 ) {
130
- return i ;
141
+ return fd_exec_instr_ctx_try_borrow_instr_account ( ctx , i , account ) ;
131
142
}
132
143
}
133
- return -1 ;
144
+
145
+ /* Return a NotEnoughAccountKeys error if the account is not found
146
+ in the instruction context to match the error code returned by
147
+ fd_exec_instr_ctx_try_borrow_instr_account. */
148
+ return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS ;
149
+ }
150
+
151
+ int
152
+ fd_exec_instr_ctx_try_borrow_last_program_account ( fd_exec_instr_ctx_t const * ctx ,
153
+ fd_borrowed_account_t * account ) {
154
+ fd_txn_account_t * program_account = NULL ;
155
+ fd_exec_txn_ctx_get_account_at_index ( ctx -> txn_ctx ,
156
+ ctx -> instr -> program_id ,
157
+ & program_account ,
158
+ NULL );
159
+
160
+ /* The index_in_instruction for a borrowed program account is invalid,
161
+ so it is set to a sentinel value of USHORT_MAX. */
162
+ return fd_exec_instr_ctx_try_borrow_account ( ctx ,
163
+ USHORT_MAX ,
164
+ program_account ,
165
+ account );
134
166
}
0 commit comments