Line data Source code
1 : #include "fd_exec_instr_ctx.h"
2 : #include "../fd_runtime.h"
3 : #include "../fd_borrowed_account.h"
4 :
5 : int
6 : fd_exec_instr_ctx_find_idx_of_instr_account( fd_exec_instr_ctx_t const * ctx,
7 0 : fd_pubkey_t const * pubkey ) {
8 0 : for( int i=0; i<ctx->instr->acct_cnt; i++ ) {
9 0 : ushort idx_in_txn = ctx->instr->accounts[ i ].index_in_transaction;
10 0 : if( memcmp( pubkey->uc, ctx->txn_out->accounts.account_keys[ idx_in_txn ].uc, sizeof(fd_pubkey_t) )==0 ) {
11 0 : return i;
12 0 : }
13 0 : }
14 0 : return -1;
15 0 : }
16 :
17 : int
18 : fd_exec_instr_ctx_get_key_of_account_at_index( fd_exec_instr_ctx_t const * ctx,
19 : ushort idx_in_instr,
20 0 : fd_pubkey_t const * * key ) {
21 0 : ushort idx_in_txn;
22 0 : int err = fd_exec_instr_ctx_get_index_of_instr_account_in_transaction( ctx,
23 0 : idx_in_instr,
24 0 : &idx_in_txn );
25 0 : if( FD_UNLIKELY( err ) ) {
26 0 : return err;
27 0 : }
28 :
29 0 : return fd_runtime_get_key_of_account_at_index( ctx->txn_out,
30 0 : idx_in_txn,
31 0 : key );
32 0 : }
33 :
34 : int
35 : fd_exec_instr_ctx_get_last_program_key( fd_exec_instr_ctx_t const * ctx,
36 0 : fd_pubkey_t const * * key ) {
37 0 : return fd_runtime_get_key_of_account_at_index( ctx->txn_out,
38 0 : ctx->instr->program_id,
39 0 : key );
40 0 : }
41 :
42 : int
43 : fd_exec_instr_ctx_try_borrow_account( fd_exec_instr_ctx_t const * ctx,
44 : ushort idx_in_instr,
45 : ushort idx_in_txn,
46 0 : fd_borrowed_account_t * account ) {
47 : /* Get the account from the transaction context using idx_in_txn.
48 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L600-L602 */
49 0 : fd_txn_account_t * txn_account = NULL;
50 0 : int err = fd_runtime_get_account_at_index( ctx->txn_in,
51 0 : ctx->txn_out,
52 0 : idx_in_txn,
53 0 : &txn_account,
54 0 : NULL );
55 0 : if( FD_UNLIKELY( err ) ) {
56 : /* Return a MissingAccount error if the account is not found.
57 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L603 */
58 0 : FD_TXN_ERR_FOR_LOG_INSTR( ctx->txn_out, FD_EXECUTOR_INSTR_ERR_MISSING_ACC, ctx->txn_out->err.exec_err_idx );
59 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
60 0 : }
61 :
62 : /* Return an AccountBorrowFailed error if the write is not acquirable.
63 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L605 */
64 0 : int borrow_res = fd_txn_account_try_borrow_mut( txn_account );
65 0 : if( FD_UNLIKELY( !borrow_res ) ) {
66 0 : return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
67 0 : }
68 :
69 : /* Create a BorrowedAccount upon success.
70 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L606 */
71 0 : fd_borrowed_account_init( account,
72 0 : txn_account,
73 0 : ctx,
74 0 : idx_in_instr );
75 0 : return FD_EXECUTOR_INSTR_SUCCESS;
76 0 : }
77 :
78 : int
79 : fd_exec_instr_ctx_try_borrow_instr_account( fd_exec_instr_ctx_t const * ctx,
80 : ushort idx,
81 0 : fd_borrowed_account_t * account ) {
82 : /* Find the index of the account in the transaction context.
83 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L649-L650 */
84 0 : ushort idx_in_txn;
85 0 : int err = fd_exec_instr_ctx_get_index_of_instr_account_in_transaction( ctx,
86 0 : idx,
87 0 : &idx_in_txn );
88 0 : if( FD_UNLIKELY( err ) ) {
89 0 : return err;
90 0 : }
91 :
92 0 : return fd_exec_instr_ctx_try_borrow_account( ctx,
93 0 : idx,
94 0 : idx_in_txn,
95 0 : account );
96 0 : }
97 :
98 : int
99 : fd_exec_instr_ctx_try_borrow_instr_account_with_key( fd_exec_instr_ctx_t const * ctx,
100 : fd_pubkey_t const * pubkey,
101 0 : fd_borrowed_account_t * account ) {
102 0 : for( ushort i=0; i<ctx->instr->acct_cnt; i++ ) {
103 0 : ushort idx_in_txn = ctx->instr->accounts[ i ].index_in_transaction;
104 0 : if( memcmp( pubkey->uc, ctx->txn_out->accounts.account_keys[ idx_in_txn ].uc, sizeof(fd_pubkey_t) )==0 ) {
105 0 : return fd_exec_instr_ctx_try_borrow_instr_account( ctx, i, account );
106 0 : }
107 0 : }
108 :
109 : /* Return a NotEnoughAccountKeys error if the account is not found
110 : in the instruction context to match the error code returned by
111 : fd_exec_instr_ctx_try_borrow_instr_account. */
112 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
113 0 : }
114 :
115 : int
116 : fd_exec_instr_ctx_try_borrow_last_program_account( fd_exec_instr_ctx_t const * ctx,
117 0 : fd_borrowed_account_t * account ) {
118 : /* The index_in_instruction for a borrowed program account is invalid,
119 : so it is set to a sentinel value of USHORT_MAX. */
120 0 : return fd_exec_instr_ctx_try_borrow_account( ctx,
121 0 : USHORT_MAX,
122 0 : ctx->instr->program_id,
123 0 : account );
124 0 : }
125 :
126 : int
127 : fd_exec_instr_ctx_get_signers( fd_exec_instr_ctx_t const * ctx,
128 0 : fd_pubkey_t const * signers[static FD_TXN_SIG_MAX] ) {
129 0 : ulong j = 0UL;
130 0 : for( ushort i=0; i<ctx->instr->acct_cnt && j<FD_TXN_SIG_MAX; i++ )
131 0 : if( fd_instr_acc_is_signer_idx( ctx->instr, i, NULL ) ) {
132 0 : ushort idx_in_txn = ctx->instr->accounts[i].index_in_transaction;
133 0 : int err = fd_runtime_get_key_of_account_at_index( ctx->txn_out,
134 0 : idx_in_txn,
135 0 : &signers[j++] );
136 0 : if( FD_UNLIKELY( err ) ) {
137 0 : return err;
138 0 : }
139 0 : }
140 0 : return FD_EXECUTOR_INSTR_SUCCESS;
141 0 : }
142 :
143 : int
144 : fd_exec_instr_ctx_any_signed( fd_exec_instr_ctx_t const * ctx,
145 0 : fd_pubkey_t const * pubkey ) {
146 0 : int is_signer = 0;
147 0 : for( ushort j=0; j<ctx->instr->acct_cnt; j++ ) {
148 0 : ushort idx_in_txn = ctx->instr->accounts[ j ].index_in_transaction;
149 0 : is_signer |=
150 : ( ( !!fd_instr_acc_is_signer_idx( ctx->instr, j, NULL ) ) &
151 0 : ( 0==memcmp( pubkey->key, ctx->txn_out->accounts.account_keys[ idx_in_txn ].key, sizeof(fd_pubkey_t) ) ) );
152 0 : }
153 0 : return is_signer;
154 0 : }
|